-
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.
Update no-returns-required to use submit service (#708)
https://eaflood.atlassian.net/browse/WATER-4262 When we added validation and controls to the 'no returns required' page of the returns requirements setup journey it was the first page that we did so. It just has 3 radio buttons and no business logic so validation of the submitted form was quite simple. Therefore, so was our solution. The [Select a start date page](#646) on the overhand was a different story. Not only did we have to cover date validation for the first time, but we also had to combine the date entry with radio buttons and business logic. It was at this point we decided a whole new `Submit[Page]Service` was needed to manage handling the validation. In both cases, we will also need to handle persisting valid results into the session so a service separate from what we use during the `GET` request starts to make a lot of sense. This change revisits the 'no returns required' page to update it to use a `SubmitNoReturnsRequiredService`. Hopefully, this will make clear our patterns and conventions going forward.
- Loading branch information
1 parent
abbdfe8
commit 1f06915
Showing
11 changed files
with
230 additions
and
142 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
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
57 changes: 57 additions & 0 deletions
57
app/services/return-requirements/submit-no-returns-required.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,57 @@ | ||
'use strict' | ||
|
||
/** | ||
* Orchestrates validating the data for `/return-requirements/{sessionId}/no-returns-required` page | ||
* @module StartDateService | ||
*/ | ||
|
||
const NoReturnsRequiredPresenter = require('../../presenters/return-requirements/no-returns-required.presenter.js') | ||
const NoReturnsRequiredValidator = require('../../validators/return-requirements/no-returns-required.validator.js') | ||
const SessionModel = require('../../models/session.model.js') | ||
|
||
/** | ||
* Orchestrates validating the data for `/return-requirements/{sessionId}/no-returns-required` 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 | ||
* | ||
* @returns {Promise<Object>} The page data for the start date page | ||
*/ | ||
async function go (sessionId, payload) { | ||
const session = await SessionModel.query().findById(sessionId) | ||
|
||
const validationResult = _validate(payload) | ||
|
||
const formattedData = NoReturnsRequiredPresenter.go(session, payload) | ||
|
||
return { | ||
activeNavBar: 'search', | ||
error: validationResult, | ||
pageTitle: 'Why are no returns required?', | ||
...formattedData | ||
} | ||
} | ||
|
||
function _validate (payload) { | ||
const validation = NoReturnsRequiredValidator.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
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.