Skip to content

Commit

Permalink
Check your requirements - purposes
Browse files Browse the repository at this point in the history
Due to the estimated complexity of adding fetch services for purposes, points and other display items. We have broken down the complex display items into their own branches of work.

https://eaflood.atlassian.net/browse/WATER-4386

This branch will add purposes to the return requirement summary card

Previous work done here - #1019
  • Loading branch information
jonathangoulding committed May 31, 2024
1 parent 871cf63 commit d27c048
Show file tree
Hide file tree
Showing 8 changed files with 462 additions and 305 deletions.
48 changes: 6 additions & 42 deletions app/presenters/return-requirements/check.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,71 +5,35 @@
* @module CheckPresenter
*/

const { formatAbstractionDate, formatLongDate } = require('../base.presenter.js')
const { formatLongDate } = require('../base.presenter.js')
const { returnRequirementReasons } = require('../../lib/static-lookups.lib.js')

function go (session) {
const { additionalSubmissionOptions, id: sessionId, journey, licence, note, reason } = session

const returnsRequired = journey === 'returns-required'

return {
additionalSubmissionOptions: additionalSubmissionOptions ?? [],
journey,
licenceRef: licence.licenceRef,
note: note ? note.content : null,
pageTitle: `Check the return requirements for ${licence.licenceHolder}`,
reason: returnRequirementReasons[reason],
reasonLink: _reasonLink(sessionId, journey),
requirements: _requirements(session),
reasonLink: _reasonLink(sessionId, returnsRequired),
sessionId,
startDate: _startDate(session),
userEmail: note ? note.userEmail : 'No notes added'
}
}

function _abstractionPeriod (abstractionPeriod) {
const { 'start-abstraction-period-day': startDay, 'start-abstraction-period-month': startMonth, 'end-abstraction-period-day': endDay, 'end-abstraction-period-month': endMonth } = abstractionPeriod
const startDate = formatAbstractionDate(startDay, startMonth)
const endDate = formatAbstractionDate(endDay, endMonth)

return `From ${startDate} to ${endDate}`
}

function _reasonLink (sessionId, journey) {
if (journey === 'returns-required') {
function _reasonLink (sessionId, returnsRequired) {
if (returnsRequired) {
return `/system/return-requirements/${sessionId}/reason`
}

return `/system/return-requirements/${sessionId}/no-returns-required`
}

function _requirements (session) {
const { requirements } = session

const completedRequirements = []

for (const [index, requirement] of requirements.entries()) {
const { agreementsExceptions } = requirement
// NOTE: We determine a requirement is complete because agreement exceptions is populated and it is the last step in
// the journey
if (agreementsExceptions) {
completedRequirements.push(_mapRequirement(requirement, index))
}
}

return completedRequirements
}

function _mapRequirement (requirement, index) {
return {
abstractionPeriod: _abstractionPeriod(requirement.abstractionPeriod),
frequencyCollected: requirement.frequencyCollected,
frequencyReported: requirement.frequencyReported,
index,
purposes: 'purpose',
siteDescription: requirement.siteDescription
}
}

function _startDate (session) {
const { licence, startDateOptions, startDateDay, startDateMonth, startDateYear } = session

Expand Down
5 changes: 5 additions & 0 deletions app/services/return-requirements/check.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
const CheckPresenter = require('../../presenters/return-requirements/check.presenter.js')
const SessionModel = require('../../models/session.model.js')

const RequirementsService = require('./check/returns-requirements.service.js')

/**
* Orchestrates fetching and presenting the data for `/return-requirements/{sessionId}/check` page
*
Expand All @@ -21,13 +23,16 @@ async function go (sessionId, yar) {

await _markCheckPageVisited(session)

const requirements = await RequirementsService.go(sessionId)

const formattedData = CheckPresenter.go(session)

const notification = yar.flash('notification')[0]

return {
activeNavBar: 'search',
notification,
...requirements,
...formattedData
}
}
Expand Down
32 changes: 32 additions & 0 deletions app/services/return-requirements/check/fetch-purposes.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict'

/**
* Fetches a licence's purposes needed for `/return-requirements/{sessionId}/purpose` page
* @module FetchPurposesByIdsService
*/

const PurposeModel = require('../../../models/purpose.model.js')

/**
* Fetches a licence's purposes needed for `/return-requirements/{sessionId}/purpose` page
*
* @param {[string]} purposeIds - An array of ids to fetch
*
* @returns {Promise<Object>} The purposes matching the array of id's
*/
async function go (purposeIds) {
return _fetch(purposeIds)
}

async function _fetch (purposeIds) {
return PurposeModel.query()
.select([
'purposes.id',
'purposes.description'
])
.findByIds(purposeIds)
}

module.exports = {
go
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
'use strict'

const { formatAbstractionDate } = require('../../../presenters/base.presenter.js')

/**
* Formats data for the `/return-requirements/{sessionId}/check` page
* @module ReturnRequirementsPresenter
*/

function go (requirements, purposeIds, journey) {
// Not clear of this can be an if else
const returnsRequired = journey === 'returns-required'
const noReturnsRequired = journey === 'no-returns-required'

return {
returnsRequired,
noReturnsRequired,
requirements: _requirements(requirements, purposeIds)
}
}

function _abstractionPeriod (abstractionPeriod) {
const { 'start-abstraction-period-day': startDay, 'start-abstraction-period-month': startMonth, 'end-abstraction-period-day': endDay, 'end-abstraction-period-month': endMonth } = abstractionPeriod
const startDate = formatAbstractionDate(startDay, startMonth)
const endDate = formatAbstractionDate(endDay, endMonth)

return `From ${startDate} to ${endDate}`
}
function _requirements (requirements, purposeIds) {
const completedRequirements = []

for (const [index, requirement] of requirements.entries()) {
const { agreementsExceptions } = requirement
// NOTE: We determine a requirement is complete because agreement exceptions is populated and it is the last step in
// the journey
if (agreementsExceptions) {
completedRequirements.push(_mapRequirement(requirement, index, purposeIds))
}
}

return completedRequirements
}

function _mapRequirement (requirement, index, purposeIds) {
return {
abstractionPeriod: _abstractionPeriod(requirement.abstractionPeriod),
frequencyCollected: requirement.frequencyCollected,
frequencyReported: requirement.frequencyReported,
index,
purposes: _mapPurpoes(requirement.purposes, purposeIds),
siteDescription: requirement.siteDescription
}
}

function _mapPurpoes (purposes, purposeIds) {
const uniquePurposes = [...new Set(purposes)]

return uniquePurposes.map((purpose) => {
return purposeIds.find((pid) => { return pid.id === purpose }).description
})
}

module.exports = {
go
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict'

/**
* Orchestrates fetching and presenting the requirements for the check page
* @module RequirementsService
*/

const FetchPurposesByIdsService = require('./fetch-purposes.service.js')
const ReturnRequirementsPresenter = require('./returns-requirements.presenter.js')
const SessionModel = require('../../../models/session.model.js')

/**
* Orchestrates fetching and presenting the requirements for `/return-requirements/{sessionId}/check` page
*
* @param {string} sessionId - The UUID for return requirement setup session record
*
* @returns {Promise<Object>} page data needed by the view template
*/
async function go (sessionId) {
const session = await SessionModel.query().findById(sessionId)

const { requirements, journey } = session
const purposeIds = _purposeIds(requirements)
const purposes = await FetchPurposesByIdsService.go(purposeIds)

return ReturnRequirementsPresenter.go(requirements, purposes, journey)
}

function _purposeIds (requirements) {
const requirementPurposes = requirements.flatMap((requirement) => {
if (requirement.purposes) {
return requirement.purposes
}

return []
})

// Duplicate is a bug this is temp
return [...new Set(requirementPurposes)]
}

module.exports = {
go
}
Loading

0 comments on commit d27c048

Please sign in to comment.