Skip to content

Commit

Permalink
WIP - spotted that the frequency columns are wrong
Browse files Browse the repository at this point in the history
  • Loading branch information
Cruikshanks committed Jun 10, 2024
1 parent 29ceb85 commit 22433c2
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 0 deletions.
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
}
11 changes: 11 additions & 0 deletions app/services/return-requirements/submit-existing.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

/**
Expand All @@ -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 {}
}

Expand All @@ -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

Expand Down

0 comments on commit 22433c2

Please sign in to comment.