-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Select additional submission options page #988
Merged
Demwunz
merged 23 commits into
main
from
WATER-4306-select-additional-submission-options-page-layout-and-controls
May 17, 2024
Merged
Changes from 14 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
82c696f
Select additional submission options page
Demwunz 2900fe0
feat(app): add additional options
Demwunz 4254b22
feat(app): amended options
Demwunz 55b9d35
feat(app): updated tests
Demwunz c06f337
Merge branch 'main' into WATER-4306-select-additional-submission-opti…
Demwunz 10791ed
feat(app): remove redundant comments
Demwunz 9f37830
feat(app): fixing tests
Demwunz 8008ecb
feat(app): fixing tests
Demwunz 35559d8
Merge branch 'main' into WATER-4306-select-additional-submission-opti…
Demwunz c875061
feat(app): normalise arrays
Demwunz f443bea
fix(app): changing sonarcloud issues
Demwunz 79ad8f2
Merge branch 'main' into WATER-4306-select-additional-submission-opti…
Demwunz 0d5fa03
fix(app): revert validation
Demwunz 355386a
fix(app): reduce func by 1 line for sonarcloud
Demwunz 10ad08d
Merge branch 'main' into WATER-4306-select-additional-submission-opti…
Demwunz f3a5fe8
fix(app): reduce test complexity
Demwunz a60244f
fix(app): feedback amends
Demwunz 80776c6
Merge branch 'main' into WATER-4306-select-additional-submission-opti…
Demwunz 258e1cd
fix(app): amended var name
Demwunz 747d4a1
Merge branch 'main' into WATER-4306-select-additional-submission-opti…
Demwunz 64b40b2
fix(app): fix error from pasting in code
Demwunz 88d1c15
fix(app): minor amends
Demwunz 3933dc2
Merge branch 'main' into WATER-4306-select-additional-submission-opti…
Demwunz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||
} |
100 changes: 100 additions & 0 deletions
100
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,100 @@ | ||
'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) | ||
// Ensure 'additional-submission-options' is an array and assign it to options | ||
const options = payload['additional-submission-options'] = _ensureIsArray(payload['additional-submission-options']) | ||
Cruikshanks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const validationResult = _validate(payload) | ||
|
||
if (!validationResult) { | ||
const notification = _notification(session, options) | ||
await _save(session, options) | ||
|
||
if (notification) { | ||
yar.flash('notification', notification) | ||
} | ||
|
||
return {} | ||
} | ||
|
||
const submittedSessionData = _submittedSessionData(session, options) | ||
|
||
return { | ||
activeNavBar: 'search', | ||
error: validationResult, | ||
pageTitle: 'Select any additional submission options for the return requirements', | ||
...submittedSessionData | ||
} | ||
} | ||
|
||
function _ensureIsArray (options) { | ||
return Array.isArray(options) ? options : [options] | ||
} | ||
|
||
function _notification (session, options) { | ||
const additionalSubmissionOptions = session?.additionalSubmissionOptions | ||
|
||
if (additionalSubmissionOptions !== options) { | ||
return { | ||
text: 'Changes updated', | ||
title: 'Updated' | ||
} | ||
} | ||
|
||
return null | ||
} | ||
|
||
async function _save (session, options) { | ||
session.additionalSubmissionOptions = options | ||
|
||
return session.$update() | ||
} | ||
|
||
function _submittedSessionData (session, options) { | ||
session.additionalSubmissionOptions = options ?? [] | ||
|
||
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 | ||
} |
47 changes: 47 additions & 0 deletions
47
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,47 @@ | ||
'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) { | ||
/** | ||
* NOTE: When a single point is checked by a user, it returns as a string. | ||
* When multiple additionalSubmissionOptions are checked, the 'payload' is returned as an array. | ||
* To make Joi validation straightforward, if the "payload['additional-submission-options']" is a string, | ||
* it is turned into an array and validated as such. | ||
*/ | ||
const additionalSubmissionOptions = payload['additional-submission-options'] | ||
|
||
const errorMessage = 'Select additional submission options for the requirements for returns' | ||
|
||
const schema = Joi.object({ | ||
'additional-submission-options': Joi.array() | ||
.items(Joi.string()) | ||
.required() | ||
}).messages({ | ||
'any.required': errorMessage, | ||
'array.sparse': errorMessage | ||
}) | ||
|
||
return schema.validate({ 'additional-submission-options': additionalSubmissionOptions }, { abortEarly: false }) | ||
} | ||
|
||
module.exports = { | ||
go | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT - think this just needs to go up a few lines to be under
AbstractionPeriodService
.