diff --git a/app/services/return-requirements/fetch-existing-requirements.service.js b/app/services/return-requirements/fetch-existing-requirements.service.js new file mode 100644 index 0000000000..c2a4a28db8 --- /dev/null +++ b/app/services/return-requirements/fetch-existing-requirements.service.js @@ -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} 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 +} diff --git a/app/services/return-requirements/submit-existing.service.js b/app/services/return-requirements/submit-existing.service.js index 2d4c4be65f..66c63f4801 100644 --- a/app/services/return-requirements/submit-existing.service.js +++ b/app/services/return-requirements/submit-existing.service.js @@ -7,6 +7,7 @@ const ExistingPresenter = require('../../presenters/return-requirements/existing.presenter.js') const ExistingValidator = require('../../validators/return-requirements/existing.validator.js') +const FetchExistingRequirementsService = require('./fetch-existing-requirements.service.js') const SessionModel = require('../../models/session.model.js') /** @@ -30,6 +31,10 @@ async function go (sessionId, payload) { const validationResult = _validate(payload, session) if (!validationResult) { + const existingReturnRequirements = await FetchExistingRequirementsService.go(payload.existing) + console.log('🚀 ~ go ~ existingReturnVersion:', existingReturnRequirements) + await _save(session, existingReturnRequirements) + return {} } @@ -43,6 +48,12 @@ async function go (sessionId, payload) { } } +async function _save (session, existingReturnRequirements) { + session.requirements = existingReturnRequirements + + return session.$update() +} + function _validate (payload, session) { const { licence: { returnVersions } } = session