-
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.
Feature returns required purposes (#1064)
* Check your requirements - purposes 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
1 parent
8c7c791
commit 5f97578
Showing
9 changed files
with
400 additions
and
128 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
72 changes: 72 additions & 0 deletions
72
app/presenters/return-requirements/check/returns-requirements.presenter.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,72 @@ | ||
'use strict' | ||
|
||
const { formatAbstractionDate } = require('../../base.presenter.js') | ||
|
||
/** | ||
* Formats return requirements data for the `/return-requirements/{sessionId}/check` page | ||
* @module ReturnRequirementsPresenter | ||
*/ | ||
|
||
/** | ||
* Formats return requirements data for the `/return-requirements/{sessionId}/check` page | ||
* | ||
* @param {Object[]} requirements - The existing return requirements in the current session | ||
* @param {module:PurposeModel[]} purposes - All purposes that match those selected across the return requirements | ||
* @param {string} journey - Whether the setup journey is 'no-returns-required' or 'returns-required' | ||
* | ||
* @returns {Object} returns requirement data needed by the view template | ||
*/ | ||
function go (requirements, purposes, journey) { | ||
return { | ||
returnsRequired: journey === 'returns-required', | ||
requirements: _requirements(requirements, purposes) | ||
} | ||
} | ||
|
||
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, purposes) { | ||
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, purposes)) | ||
} | ||
} | ||
|
||
return completedRequirements | ||
} | ||
|
||
function _mapPurposes (requirementPurposes, purposes) { | ||
return requirementPurposes.map((requirementPurpose) => { | ||
const matchedPurpose = purposes.find((purpose) => { | ||
return purpose.id === requirementPurpose | ||
}) | ||
|
||
return matchedPurpose.description | ||
}) | ||
} | ||
|
||
function _mapRequirement (requirement, index, purposes) { | ||
return { | ||
abstractionPeriod: _abstractionPeriod(requirement.abstractionPeriod), | ||
frequencyCollected: requirement.frequencyCollected, | ||
frequencyReported: requirement.frequencyReported, | ||
index, | ||
purposes: _mapPurposes(requirement.purposes, purposes), | ||
siteDescription: requirement.siteDescription | ||
} | ||
} | ||
|
||
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
50 changes: 50 additions & 0 deletions
50
app/services/return-requirements/check/returns-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,50 @@ | ||
'use strict' | ||
|
||
/** | ||
* Orchestrates fetching and presenting the return requirements for the check page | ||
* @module ReturnRequirementsService | ||
*/ | ||
|
||
const PurposeModel = require('../../../models/purpose.model.js') | ||
const ReturnRequirementsPresenter = require('../../../presenters/return-requirements/check/returns-requirements.presenter.js') | ||
|
||
/** | ||
* Orchestrates fetching and presenting the return requirements for `/return-requirements/{sessionId}/check` page | ||
* | ||
* @param {module:SessionModel} session - The session for the return requirement journey | ||
* | ||
* @returns {Promise<Object>} page data needed by the view template | ||
*/ | ||
async function go (session) { | ||
const { requirements, journey } = session.data | ||
|
||
const purposeIds = _purposeIds(requirements) | ||
const purposes = await _fetchPurposes(purposeIds) | ||
|
||
return ReturnRequirementsPresenter.go(requirements, purposes, journey) | ||
} | ||
|
||
async function _fetchPurposes (purposeIds) { | ||
return PurposeModel.query() | ||
.select([ | ||
'purposes.id', | ||
'purposes.description' | ||
]) | ||
.findByIds(purposeIds) | ||
} | ||
|
||
function _purposeIds (requirements) { | ||
const requirementPurposes = requirements.flatMap((requirement) => { | ||
if (requirement.purposes) { | ||
return requirement.purposes | ||
} | ||
|
||
return [] | ||
}) | ||
|
||
return [...new Set(requirementPurposes)] | ||
} | ||
|
||
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
Oops, something went wrong.