-
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.
Persist a new return version and its requirements (#1137)
https://eaflood.atlassian.net/browse/WATER-4519 Having completed the return requirements setup journey (returns-required), we need to persist both the return version (start date, reason and notes) along with its return requirements to the database. When "Approve" is clicked in the `/check` page the data in the session needs to be persisted to: - water.return_versions - water.return_requirements - water.return_requirement_points - water.return_requirement_purposes
- Loading branch information
Showing
35 changed files
with
1,040 additions
and
100 deletions.
There are no files selected for viewing
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
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
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
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
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
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
135 changes: 135 additions & 0 deletions
135
app/services/return-requirements/generate-return-version-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,135 @@ | ||
'use strict' | ||
|
||
/** | ||
* Uses the session data to generate the data sets required to create the return version requirements for a licence | ||
* @module GenerateReturnVersionRequirementsService | ||
*/ | ||
|
||
const FetchPointsService = require('./fetch-points.service.js') | ||
const LicenceModel = require('../../models/licence.model.js') | ||
const LicenceVersionModel = require('../../models/licence-version.model.js') | ||
const ReturnRequirementModel = require('../../models/return-requirement.model.js') | ||
|
||
/** | ||
* Uses the session data to generate the data sets required to create the return version requirements for a licence | ||
* | ||
* Creates the data needed to populate the `return_requirements`, `return_requirement_points` and | ||
* `return_requirement_purposes` tables. | ||
* | ||
* @param {String} licenceId - The UUID of the licence the requirements are for | ||
* @param {Object[]} requirements - The return requirements data from the session | ||
* | ||
* @returns {Promise<Object>} The new return version requirements data for a licence | ||
*/ | ||
async function go (licenceId, requirements) { | ||
const naldRegionId = await _fetchNaldRegionId(licenceId) | ||
const licencePoints = await FetchPointsService.go(licenceId) | ||
const returnRequirements = [] | ||
|
||
let legacyId = await _nextLegacyId(naldRegionId) | ||
|
||
for (const requirement of requirements) { | ||
const externalId = `${naldRegionId}:${legacyId}` | ||
|
||
const returnRequirement = { | ||
abstractionPeriodStartDay: requirement.abstractionPeriod['start-abstraction-period-day'], | ||
abstractionPeriodStartMonth: requirement.abstractionPeriod['start-abstraction-period-month'], | ||
abstractionPeriodEndDay: requirement.abstractionPeriod['end-abstraction-period-day'], | ||
abstractionPeriodEndMonth: requirement.abstractionPeriod['end-abstraction-period-month'], | ||
collectionFrequency: requirement.frequencyCollected, | ||
externalId, | ||
fiftySixException: requirement.agreementsExceptions.includes('56-returns-exception'), | ||
gravityFill: requirement.agreementsExceptions.includes('gravity-fill'), | ||
legacyId, | ||
reabstraction: requirement.agreementsExceptions.includes('transfer-re-abstraction-scheme'), | ||
reportingFrequency: requirement.frequencyReported, | ||
returnsFrequency: 'year', | ||
returnRequirementPoints: _generateReturnRequirementPoints(licencePoints, externalId, requirement.points), | ||
returnRequirementPurposes: await _generateReturnRequirementPurposes(licenceId, requirement.purposes), | ||
siteDescription: requirement.siteDescription, | ||
summer: requirement.returnsCycle === 'summer', | ||
twoPartTariff: requirement.agreementsExceptions.includes('two-part-tariff') | ||
} | ||
|
||
legacyId++ | ||
|
||
returnRequirements.push(returnRequirement) | ||
} | ||
|
||
return returnRequirements | ||
} | ||
|
||
async function _fetchNaldRegionId (licenceId) { | ||
const { naldRegionId } = await LicenceModel.query() | ||
.findById(licenceId) | ||
.select('region.naldRegionId') | ||
.innerJoinRelated('region') | ||
|
||
return naldRegionId | ||
} | ||
|
||
function _generateReturnRequirementPoints (licencePoints, requirementExternalId, requirementPoints) { | ||
const returnRequirementPoints = [] | ||
|
||
requirementPoints.forEach((requirementPoint) => { | ||
const point = licencePoints.find((licencePoint) => { | ||
return licencePoint.ID === requirementPoint | ||
}) | ||
|
||
const returnRequirementPoint = { | ||
description: point.LOCAL_NAME, | ||
externalId: `${requirementExternalId}:${point.ID}`, | ||
naldPointId: point.ID, | ||
ngr1: point.NGR1_SHEET !== 'null' ? `${point.NGR1_SHEET} ${point.NGR1_EAST} ${point.NGR1_NORTH}` : null, | ||
ngr2: point.NGR2_SHEET !== 'null' ? `${point.NGR2_SHEET} ${point.NGR2_EAST} ${point.NGR2_NORTH}` : null, | ||
ngr3: point.NGR3_SHEET !== 'null' ? `${point.NGR3_SHEET} ${point.NGR3_EAST} ${point.NGR3_NORTH}` : null, | ||
ngr4: point.NGR4_SHEET !== 'null' ? `${point.NGR4_SHEET} ${point.NGR4_EAST} ${point.NGR4_NORTH}` : null | ||
} | ||
|
||
returnRequirementPoints.push(returnRequirementPoint) | ||
}) | ||
|
||
return returnRequirementPoints | ||
} | ||
|
||
async function _generateReturnRequirementPurposes (licenceId, purposes) { | ||
const returnRequirementPurposes = [] | ||
|
||
for (const purpose of purposes) { | ||
const { primaryPurposeId, secondaryPurposeId } = await LicenceVersionModel.query() | ||
.select('primaryPurposeId', 'secondaryPurposeId') | ||
.innerJoinRelated('licenceVersionPurposes') | ||
.where('licenceId', licenceId) | ||
.andWhere('status', 'current') | ||
.andWhere('purposeId', purpose.id) | ||
.first() | ||
|
||
const returnRequirementPurpose = { | ||
alias: purpose.alias !== '' ? purpose.alias : null, | ||
primaryPurposeId, | ||
purposeId: purpose.id, | ||
secondaryPurposeId | ||
} | ||
|
||
returnRequirementPurposes.push(returnRequirementPurpose) | ||
} | ||
|
||
return returnRequirementPurposes | ||
} | ||
|
||
async function _nextLegacyId (naldRegionId) { | ||
const { lastLegacyId } = await ReturnRequirementModel.query() | ||
.max('legacyId as lastLegacyId') | ||
.whereLike('externalId', `${naldRegionId}%`) | ||
.first() | ||
|
||
if (lastLegacyId) { | ||
return lastLegacyId + 1 | ||
} | ||
|
||
return 1 | ||
} | ||
|
||
module.exports = { | ||
go | ||
} |
75 changes: 75 additions & 0 deletions
75
app/services/return-requirements/generate-return-version.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,75 @@ | ||
'use strict' | ||
|
||
/** | ||
* Uses the session data to generate the data sets required to create a new return version for a licence | ||
* @module GenerateReturnVersionService | ||
*/ | ||
|
||
const GenerateReturnVersionRequirementsService = require('./generate-return-version-requirements.service.js') | ||
const ReturnVersionModel = require('../../models/return-version.model.js') | ||
|
||
/** | ||
* Uses the session data to generate the data sets required to create a new return version for a licence | ||
* | ||
* Creates the data needed to populate the `return_versions`, `return_requirements`, `return_requirement_points` and | ||
* `return_requirement_purposes` tables. | ||
* | ||
* @param {string} sessionData - The session data required to set up a new return version for a licence | ||
* @param {number} userId - The id of the logged in user | ||
* | ||
* @returns {Promise<Object>} The new return version and requirement data for a licence | ||
*/ | ||
async function go (sessionData, userId) { | ||
const returnVersion = await _generateReturnVersion(sessionData, userId) | ||
const returnRequirements = await GenerateReturnVersionRequirementsService.go(sessionData.licence.id, sessionData.requirements) | ||
|
||
return { | ||
returnRequirements, | ||
returnVersion | ||
} | ||
} | ||
|
||
function _calculateStartDate (sessionData) { | ||
if (sessionData.startDateOptions === 'anotherStartDate') { | ||
// Reminder! Because of the unique qualities of Javascript, Year and Day are literal values, month is an index! So, | ||
// January is actually 0, February is 1 etc. This is why we deduct 1 from the month. | ||
return new Date(sessionData.startDateYear, sessionData.startDateMonth - 1, sessionData.startDateDay) | ||
} | ||
|
||
return sessionData.licence.currentVersionStartDate | ||
} | ||
|
||
async function _generateReturnVersion (sessionData, userId) { | ||
return { | ||
createdBy: userId, | ||
endDate: null, | ||
licenceId: sessionData.licence.id, | ||
multipleUpload: _multipleUpload(sessionData?.additionalSubmissionOptions), | ||
notes: sessionData?.note?.content, | ||
reason: sessionData.reason, | ||
startDate: _calculateStartDate(sessionData), | ||
status: 'current', | ||
version: await _nextVersionNumber(sessionData.licence.id) | ||
} | ||
} | ||
|
||
async function _nextVersionNumber (licenceId) { | ||
const { lastVersionNumber } = await ReturnVersionModel.query() | ||
.max('version as lastVersionNumber') | ||
.where({ licenceId }) | ||
.first() | ||
|
||
if (lastVersionNumber) { | ||
return lastVersionNumber + 1 | ||
} | ||
|
||
return 1 | ||
} | ||
|
||
function _multipleUpload (additionalSubmissionOptions) { | ||
return additionalSubmissionOptions ? additionalSubmissionOptions.includes('multiple-upload') : false | ||
} | ||
|
||
module.exports = { | ||
go | ||
} |
Oops, something went wrong.