-
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.
Select agreements and exceptions for requirements (#912)
* Select agreements and exceptions for requirements https://eaflood.atlassian.net/browse/WATER-4301 This is PR is focused on adding functionality to page 7 of 11 in the return requirement pages. The pages were previously setup as part of a separate PR to create the user journey.
- Loading branch information
1 parent
7f3cd4b
commit 3236030
Showing
15 changed files
with
597 additions
and
14 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
29 changes: 29 additions & 0 deletions
29
app/presenters/return-requirements/agreements-exceptions.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,29 @@ | ||
'use strict' | ||
|
||
/** | ||
* Formats data for the `/return-requirements/{sessionId}/agreements-exceptions` page | ||
* @module AgreementsExceptionsPresenter | ||
*/ | ||
|
||
/** | ||
* Formats data for the `/return-requirements/{sessionId}/agreements-exceptions` page | ||
* | ||
* @param {module:SessionModel} session - The returns requirements session instance | ||
* @param {Object} [payload] - The payload from the request | ||
* | ||
* @returns {Object} - The data formatted for the view template | ||
*/ | ||
function go (session) { | ||
const data = { | ||
id: session.id, | ||
licenceId: session.data.licence.id, | ||
licenceRef: session.data.licence.licenceRef, | ||
agreementsExceptions: session.data.agreementsExceptions ? session.data.agreementsExceptions : '' | ||
} | ||
|
||
return data | ||
} | ||
|
||
module.exports = { | ||
go | ||
} |
34 changes: 34 additions & 0 deletions
34
app/services/return-requirements/agreements-exceptions.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,34 @@ | ||
'use strict' | ||
|
||
/** | ||
* Orchestrates fetching and presenting the data for `/return-requirements/{sessionId}/agreements-exceptions` page | ||
* @module AgreementExceptionService | ||
*/ | ||
|
||
const AgreementsExceptionsPresenter = require('../../presenters/return-requirements/agreements-exceptions.presenter.js') | ||
const SessionModel = require('../../models/session.model.js') | ||
|
||
/** | ||
* Orchestrates fetching and presenting the data for `/return-requirements/{sessionId}/agreements-exceptions` page | ||
* | ||
* Supports generating the data needed for the agreements and exceptions page in the return requirements setup journey. | ||
* It fetches the current session record and combines it with the date fields and other information needed for the form. | ||
* | ||
* @param {string} sessionId - The UUID of the current session | ||
* | ||
* @returns {Promise<Object>} The view data for the agreements and exceptions page | ||
*/ | ||
async function go (sessionId) { | ||
const session = await SessionModel.query().findById(sessionId) | ||
const formattedData = AgreementsExceptionsPresenter.go(session) | ||
|
||
return { | ||
activeNavBar: 'search', | ||
pageTitle: 'Select agreements and exceptions for the requirements for returns', | ||
...formattedData | ||
} | ||
} | ||
|
||
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
84 changes: 84 additions & 0 deletions
84
app/services/return-requirements/submit-agreements-exceptions.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,84 @@ | ||
'use strict' | ||
|
||
/** | ||
* Orchestrates validating the data for `/return-requirements/{sessionId}/agreements-exceptions` page | ||
* @module SubmitAgreementsExceptions | ||
*/ | ||
|
||
const AgreementsExceptionsPresenter = require('../../presenters/return-requirements/agreements-exceptions.presenter.js') | ||
const AgreementsExceptionsValidator = require('../../validators/return-requirements/agreements-exceptions.validator.js') | ||
const SessionModel = require('../../models/session.model.js') | ||
|
||
/** | ||
* Orchestrates validating the data for `/return-requirements/{sessionId}/agreements-exceptions` page | ||
* | ||
* It first retrieves the session instance for the returns requirements journey in progress. | ||
* | ||
* The user input is then validated and the result is then combined with the output of the presenter to generate the | ||
* page data needed by the view. If there was a validation error the controller will re-render the page so needs this | ||
* information. If all is well the controller will redirect to the next page in the journey. | ||
* | ||
* @param {string} sessionId - The UUID of the current session | ||
* @param {Object} payload - The submitted form data | ||
* | ||
* @returns {Promise<Object>} The page data for the agreements and exceptions page | ||
*/ | ||
async function go (sessionId, payload) { | ||
const session = await SessionModel.query().findById(sessionId) | ||
|
||
_handleOneOptionSelected(payload) | ||
|
||
const validationResult = _validate(payload) | ||
|
||
if (!validationResult) { | ||
await _save(session, payload) | ||
|
||
return {} | ||
} | ||
|
||
const formattedData = AgreementsExceptionsPresenter.go(session, payload) | ||
|
||
return { | ||
activeNavBar: 'search', | ||
error: validationResult, | ||
pageTitle: 'Select agreements and exceptions for the requirements for returns', | ||
...formattedData | ||
} | ||
} | ||
|
||
/** | ||
* When a single agreement and exception is checked by the user, it returns as a string. When multiple agreements and | ||
* exceptions are checked, the 'agreementsExceptions' is returned as an array. This function works to make those single | ||
* selected string 'agreementsExceptions' into an array for uniformity. | ||
*/ | ||
function _handleOneOptionSelected (payload) { | ||
if (!Array.isArray(payload.agreementsExceptions)) { | ||
payload.agreementsExceptions = [payload.agreementsExceptions] | ||
} | ||
} | ||
|
||
async function _save (session, payload) { | ||
const currentData = session.data | ||
|
||
currentData.agreementsExceptions = payload.agreementsExceptions | ||
|
||
return session.$query().patch({ data: currentData }) | ||
} | ||
|
||
function _validate (payload) { | ||
const validation = AgreementsExceptionsValidator.go(payload) | ||
|
||
if (!validation.error) { | ||
return null | ||
} | ||
|
||
const { message } = validation.error.details[0] | ||
|
||
return { | ||
text: message | ||
} | ||
} | ||
|
||
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
53 changes: 53 additions & 0 deletions
53
app/validators/return-requirements/agreements-exceptions.validator.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,53 @@ | ||
'use strict' | ||
|
||
/** | ||
* Validates data submitted for the `/return-requirements/{sessionId}/agreements-exceptions` page | ||
* @module AgreementsExceptionsValidator | ||
*/ | ||
|
||
const Joi = require('joi') | ||
|
||
/** | ||
* Validates data submitted for the `/return-requirements/{sessionId}/agreements-exceptions` page | ||
* | ||
* When setting up a requirement users must specify an agreement and exception for the return requirement. | ||
* Users must select one or more agreements and exceptions linked to the licence. If these requirements are not met | ||
* the validation will return an error. | ||
* | ||
* @param {Object} payload - The payload from the request to be validated | ||
* | ||
* @returns {Object} The result from calling Joi's schema.validate(). If any errors are found the | ||
* `error:` property will also exist detailing what the issue is. | ||
*/ | ||
function go (payload) { | ||
const agreementsExceptions = payload.agreementsExceptions | ||
|
||
const errorMessage = 'Select if there are any agreements and exceptions needed for the return requirements' | ||
|
||
const schema = Joi.object({ | ||
agreementsExceptions: Joi.array() | ||
.items(Joi.string().valid(...VALID_VALUES)) | ||
.required() | ||
.messages({ | ||
'any.required': errorMessage, | ||
'any.only': errorMessage, | ||
'array.includesOne': errorMessage, | ||
'array.includes': errorMessage, | ||
'array.sparse': errorMessage | ||
}) | ||
}) | ||
|
||
return schema.validate({ agreementsExceptions }, { abortEarly: false }) | ||
} | ||
|
||
const VALID_VALUES = [ | ||
'gravity-fill', | ||
'transfer-re-abstraction-scheme', | ||
'two-part-tariff', | ||
'56-returns-exception', | ||
'none' | ||
] | ||
|
||
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
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
Oops, something went wrong.