-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP - spotted that the frequency columns are wrong
- Loading branch information
1 parent
29ceb85
commit 22433c2
Showing
2 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
151 changes: 151 additions & 0 deletions
151
app/services/return-requirements/fetch-existing-requirements.service.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
'use strict' | ||
|
||
/** | ||
* Fetches existing return requirements to be copied from | ||
* @module FetchExistingRequirementsService | ||
*/ | ||
|
||
const ReturnVersionModel = require('../../models/return-version.model.js') | ||
|
||
const FREQUENCIES = { | ||
day: 'daily', | ||
week: 'weekly', | ||
fortnight: 'fortnightly', | ||
month: 'monthly', | ||
quarter: 'quarterly', | ||
year: 'yearly' | ||
} | ||
|
||
/** | ||
* Fetches existing return requirements to be copied from | ||
* | ||
* In the returns setup journey we allow users to select the option to create new requirements by copying from them | ||
* from an existing return version. This service fetches the selected return version and its requirements | ||
* | ||
* @param {string} returnVersionId - The UUID of the selected return version to copy requirements from | ||
* | ||
* @returns {Promise<Object>} The return version and its linked return requirements, plus their points and purposes | ||
*/ | ||
async function go (returnVersionId) { | ||
const returnVersion = await _fetch(returnVersionId) | ||
|
||
return _transformForSetup(returnVersion) | ||
} | ||
|
||
async function _fetch (returnVersionId) { | ||
return ReturnVersionModel.query() | ||
.findById(returnVersionId) | ||
.select([ | ||
'id' | ||
]) | ||
.withGraphFetched('returnRequirements') | ||
.modifyGraph('returnRequirements', (builder) => { | ||
builder.select([ | ||
'id', | ||
'abstractionPeriodEndDay', | ||
'abstractionPeriodEndMonth', | ||
'abstractionPeriodStartDay', | ||
'abstractionPeriodStartMonth', | ||
'collectionFrequency', | ||
'fiftySixException', | ||
'gravityFill', | ||
'reabstraction', | ||
'returnsFrequency', | ||
'siteDescription', | ||
'summer', | ||
'twoPartTariff' | ||
]) | ||
}) | ||
.withGraphFetched('returnRequirements.returnRequirementPoints') | ||
.modifyGraph('returnRequirements.returnRequirementPoints', (builder) => { | ||
builder.select([ | ||
'id', | ||
'naldPointId' | ||
]) | ||
}) | ||
.withGraphFetched('returnRequirements.returnRequirementPurposes') | ||
.modifyGraph('returnRequirements.returnRequirementPurposes', (builder) => { | ||
builder.select([ | ||
'id', | ||
'purposeId' | ||
]) | ||
}) | ||
} | ||
|
||
function _transformForSetup (returnVersion) { | ||
const { returnRequirements } = returnVersion | ||
|
||
return returnRequirements.map((returnRequirement) => { | ||
const { | ||
abstractionPeriodEndDay, | ||
abstractionPeriodEndMonth, | ||
abstractionPeriodStartDay, | ||
abstractionPeriodStartMonth, | ||
collectionFrequency, | ||
returnsFrequency, | ||
returnRequirementPoints, | ||
returnRequirementPurposes, | ||
siteDescription, | ||
summer | ||
} = returnRequirement | ||
|
||
return { | ||
points: _points(returnRequirementPoints), | ||
purposes: _purposes(returnRequirementPurposes), | ||
returnsCycle: summer ? 'summer' : 'winter-and-all-year', | ||
siteDescription, | ||
abstractionPeriod: { | ||
'end-abstraction-period-day': abstractionPeriodEndDay, | ||
'end-abstraction-period-month': abstractionPeriodEndMonth, | ||
'start-abstraction-period-day': abstractionPeriodStartDay, | ||
'start-abstraction-period-month': abstractionPeriodStartMonth | ||
}, | ||
frequencyReported: FREQUENCIES[returnsFrequency], | ||
frequencyCollected: FREQUENCIES[collectionFrequency], | ||
agreementsExceptions: _agreementExceptions(returnRequirement) | ||
} | ||
}) | ||
} | ||
|
||
function _agreementExceptions (returnRequirement) { | ||
const { fiftySixException, gravityFill, reabstraction, twoPartTariff } = returnRequirement | ||
const agreementsExceptions = [] | ||
|
||
if (fiftySixException) { | ||
agreementsExceptions.push('56-returns-exception') | ||
} | ||
|
||
if (gravityFill) { | ||
agreementsExceptions.push('gravity-fill') | ||
} | ||
|
||
if (reabstraction) { | ||
agreementsExceptions.push('transfer-re-abstraction-scheme') | ||
} | ||
|
||
if (twoPartTariff) { | ||
agreementsExceptions.push('two-part-tariff') | ||
} | ||
|
||
if (agreementsExceptions.length === 0) { | ||
agreementsExceptions.push('none') | ||
} | ||
|
||
return agreementsExceptions | ||
} | ||
|
||
function _points (returnRequirementPoints) { | ||
return returnRequirementPoints.map((returnRequirementPoint) => { | ||
return returnRequirementPoint.naldPointId.toString() | ||
}) | ||
} | ||
|
||
function _purposes (returnRequirementPurposes) { | ||
return returnRequirementPurposes.map((returnRequirementPurpose) => { | ||
return returnRequirementPurpose.purposeId | ||
}) | ||
} | ||
|
||
module.exports = { | ||
go | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters