-
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 additional submission options page (#988)
Select additional submission options page https://eaflood.atlassian.net/browse/WATER-4306 Additional options for submission page. Currently the options are "multiple upload", or "none", there is scope to add more later. Appropriate notifications will be shown on the check page to the user based on selection.
- Loading branch information
Showing
16 changed files
with
725 additions
and
1 deletion.
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
30 changes: 30 additions & 0 deletions
30
app/presenters/return-requirements/additional-submission-options.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,30 @@ | ||
'use strict' | ||
|
||
/** | ||
* Formats data for the `/return-requirements/{sessionId}/additional-submission-options` page | ||
* @module AdditionalSubmissionOptionsPresenter | ||
*/ | ||
|
||
/** | ||
* Formats data for the `/return-requirements/{sessionId}/additional-submission-options` page | ||
* | ||
* @param {module:SessionModel} session - The returns requirements session instance | ||
* | ||
* @returns {Object} - The data formatted for the view template | ||
*/ | ||
function go (session) { | ||
const { id: sessionId, licence: { id: licenceId, licenceRef }, additionalSubmissionOptions } = session | ||
const data = { | ||
additionalSubmissionOptions: additionalSubmissionOptions ?? [], | ||
backLink: `/system/return-requirements/${sessionId}/check`, | ||
licenceId, | ||
licenceRef, | ||
sessionId | ||
} | ||
|
||
return data | ||
} | ||
|
||
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
37 changes: 37 additions & 0 deletions
37
app/services/return-requirements/additional-submission-options.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,37 @@ | ||
'use strict' | ||
|
||
/** | ||
* Orchestrates fetching and presenting the data for | ||
* `/return-requirements/{sessionId}/additional-submission-options` page | ||
* @module AdditionalSubmissionOptionsService | ||
*/ | ||
|
||
const AdditionalSubmissionOptionsPresenter = require('../../presenters/return-requirements/additional-submission-options.presenter.js') | ||
const SessionModel = require('../../models/session.model.js') | ||
|
||
/** | ||
* Orchestrates fetching and presenting the data for | ||
* `/return-requirements/{sessionId}/additional-submission-options` page | ||
* | ||
* Supports generating the data needed for the points page in the return requirements setup journey. It fetches the | ||
* current session record and combines it with the checkboxes and other information needed for the form. | ||
* | ||
* @param {string} sessionId - The UUID of the current session | ||
* | ||
* @returns {Promise<Object>} The view data for the points page | ||
*/ | ||
async function go (sessionId) { | ||
const session = await SessionModel.query().findById(sessionId) | ||
|
||
const formattedData = AdditionalSubmissionOptionsPresenter.go(session) | ||
|
||
return { | ||
activeNavBar: 'search', | ||
pageTitle: 'Select any additional submission options for the return requirements', | ||
...formattedData | ||
} | ||
} | ||
|
||
module.exports = { | ||
go | ||
} |
108 changes: 108 additions & 0 deletions
108
app/services/return-requirements/submit-additional-submission-options.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,108 @@ | ||
'use strict' | ||
|
||
/** | ||
* Orchestrates validating the data for `/return-requirements/{sessionId}/additional-submission-options` page | ||
* @module AdditionalSubmissionOptionsService | ||
*/ | ||
|
||
const AdditionalSubmissionOptionsPresenter = require('../../presenters/return-requirements/additional-submission-options.presenter.js') | ||
const AdditionalSubmissionOptionsValidator = require('../../validators/return-requirements/additional-submission-options.validator.js') | ||
const SessionModel = require('../../models/session.model.js') | ||
|
||
/** | ||
* Orchestrates validating the data for `/return-requirements/{sessionId}/additional-submission-options` page | ||
* | ||
* It first retrieves the session instance for the returns requirements journey in progress. | ||
* | ||
* The validation 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 id of the current session | ||
* @param {Object} payload - The submitted form data | ||
* @param {Object} yar - The Hapi `request.yar` session manager passed on by the controller | ||
* | ||
* @returns {Promise<Object>} If no errors it returns an empty object else the page data for the note page including the | ||
* validation error details | ||
*/ | ||
async function go (sessionId, payload, yar) { | ||
const session = await SessionModel.query().findById(sessionId) | ||
|
||
_handleOneOptionSelected(payload) | ||
|
||
const validationResult = _validate(payload) | ||
|
||
if (!validationResult) { | ||
const notification = _notification(session, payload) | ||
await _save(session, payload) | ||
|
||
if (notification) { | ||
yar.flash('notification', notification) | ||
} | ||
|
||
return {} | ||
} | ||
|
||
const submittedSessionData = _submittedSessionData(session, payload) | ||
|
||
return { | ||
activeNavBar: 'search', | ||
error: validationResult, | ||
pageTitle: 'Select any additional submission options for the return requirements', | ||
...submittedSessionData | ||
} | ||
} | ||
|
||
/** | ||
* When a single additional submission option is checked by the user, it returns as a string. When multiple options are | ||
* checked, the 'additionalSubmissionOptions' is returned as an array. | ||
* This function works to make those single selected string 'additionalSubmissionOptions' into an array for uniformity. | ||
*/ | ||
function _handleOneOptionSelected (payload) { | ||
if (!Array.isArray(payload.additionalSubmissionOptions)) { | ||
payload.additionalSubmissionOptions = [payload.additionalSubmissionOptions] | ||
} | ||
} | ||
|
||
function _notification (session, payload) { | ||
const { additionalSubmissionOptions } = session ?? {} | ||
|
||
if (additionalSubmissionOptions !== payload.additionalSubmissionOptions) { | ||
return { | ||
text: 'Changes updated', | ||
title: 'Updated' | ||
} | ||
} | ||
|
||
return null | ||
} | ||
|
||
async function _save (session, payload) { | ||
session.additionalSubmissionOptions = payload.additionalSubmissionOptions | ||
|
||
return session.$update() | ||
} | ||
|
||
function _submittedSessionData (session, payload) { | ||
session.additionalSubmissionOptions = payload.additionalSubmissionOptions ?? [] | ||
|
||
return AdditionalSubmissionOptionsPresenter.go(session) | ||
} | ||
|
||
function _validate (payload) { | ||
const validation = AdditionalSubmissionOptionsValidator.go(payload) | ||
|
||
if (!validation.error) { | ||
return null | ||
} | ||
|
||
const { message } = validation.error.details[0] | ||
|
||
return { | ||
text: message | ||
} | ||
} | ||
|
||
module.exports = { | ||
go | ||
} |
41 changes: 41 additions & 0 deletions
41
app/validators/return-requirements/additional-submission-options.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,41 @@ | ||
'use strict' | ||
|
||
/** | ||
* Validates data submitted for the `/return-requirements/{sessionId}/additional-submission-options` page | ||
* @module AdditionalSubmissionOptionsValidator | ||
*/ | ||
|
||
const Joi = require('joi') | ||
|
||
/** | ||
* Validates data submitted for the `/return-requirements/{sessionId}/additional-submission-options` page | ||
* | ||
* When setting up a requirement users must specify additional-submission-options for the return requirement. | ||
* Users must select one or more points linked to the licence. | ||
* If these requirements are not met the validation will return an error. | ||
* | ||
* @param {Object} options - The options extracted from payload taken 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 additionalSubmissionOptions = payload.additionalSubmissionOptions | ||
|
||
const errorMessage = 'Select additional submission options for the requirements for returns' | ||
|
||
const schema = Joi.object({ | ||
additionalSubmissionOptions: Joi.array() | ||
.items(Joi.string()) | ||
.required() | ||
}).messages({ | ||
'any.required': errorMessage, | ||
'array.sparse': errorMessage | ||
}) | ||
|
||
return schema.validate({ additionalSubmissionOptions }, { abortEarly: false }) | ||
} | ||
|
||
module.exports = { | ||
go | ||
} |
Oops, something went wrong.