Skip to content
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

Saving input from return requirements in session #939

Merged
merged 39 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
b4b5380
Saving input from return requirements in session
rvsiyad Apr 25, 2024
7ac1919
Added selectedPoints to check checkboxes users have previously select…
rvsiyad Apr 25, 2024
999517b
Removed unused payload from PointsPresenter as points are now saved t…
rvsiyad Apr 25, 2024
36249db
Changed import from SelectedPointsPresenter to PointsPresenter
rvsiyad Apr 25, 2024
6e9deb3
Added saving points to session to SubmitPointsService
rvsiyad Apr 25, 2024
a744453
Added check functionality to Points view for user previously selected…
rvsiyad Apr 25, 2024
1224262
Updated PointsPresenter test
rvsiyad Apr 25, 2024
2804626
Updated Points service test
rvsiyad Apr 25, 2024
5d4397a
Updated SubmitPoints service to check for points saved in session
rvsiyad Apr 25, 2024
535ea96
Merge branch 'main' into return-requirements-saving-to-session
rvsiyad Apr 25, 2024
01a695e
Reformatted SiteDescription presenter to use siteDescription saved in…
rvsiyad Apr 25, 2024
67aa18a
Added inputtedSiteDescription to keep track of user invalid inputs
rvsiyad Apr 25, 2024
ea69bcc
Changed import from SelectPurpose to Purpose validator and reformatte…
rvsiyad Apr 25, 2024
0e69c2a
Added saving to session and reformatted comments for SubmitSiteDescri…
rvsiyad Apr 25, 2024
0d631e8
Added errorSummary and value attribute for user inputted values
rvsiyad Apr 25, 2024
83c9d0b
Removed extra session data from Points presenter test
rvsiyad Apr 25, 2024
5b274f9
Fixed siteDescription tests
rvsiyad Apr 25, 2024
8b3fbdd
Merge branch 'return-requirements-saving-to-session' of https://githu…
rvsiyad Apr 25, 2024
db21604
Merge branch 'main' into return-requirements-saving-to-session
rvsiyad Apr 29, 2024
f176287
Taking AbstractionPeriod from session instead of payload
rvsiyad Apr 29, 2024
98c3584
Fix AbstractionPeriod view
rvsiyad Apr 29, 2024
67fef6e
Fixed AbstractionPeriod service to combine existing session data with…
rvsiyad Apr 29, 2024
370a3f0
Fixing AbstractionPeriod tests
rvsiyad Apr 29, 2024
cbdfc55
Altering Reason presenter to return reasons stored in session
rvsiyad Apr 29, 2024
372c2f0
Fixed Reason view to show previously checked reasons
rvsiyad Apr 29, 2024
d342013
Fixed Reason tests
rvsiyad Apr 29, 2024
0e239d7
Altered Setup presenter to return setup session data
rvsiyad Apr 29, 2024
9f26f0b
Updated payload to use kebab case instead of snake case
rvsiyad Apr 29, 2024
318844c
Updated view to use kebab case and check previously selected setup st…
rvsiyad Apr 29, 2024
4791c13
Fixed Setup tests
rvsiyad Apr 29, 2024
9d2bcf8
Remove payload from JSDocs
rvsiyad Apr 30, 2024
c396c52
Remove inputtedSiteDescription and payload from presenter
rvsiyad Apr 30, 2024
73bb3f1
Added separate function for replaying user inputted data from session
rvsiyad Apr 30, 2024
490074f
Updated view to show and replay site descriptions from users
rvsiyad Apr 30, 2024
15163c2
Updated SiteDescription tests
rvsiyad Apr 30, 2024
475d865
Merge branch 'main' into return-requirements-saving-to-session
rvsiyad Apr 30, 2024
01c57e5
Altered _submitSessionData() to use payload.siteDescription as a chec…
rvsiyad Apr 30, 2024
884976e
Merge branch 'return-requirements-saving-to-session' of https://githu…
rvsiyad Apr 30, 2024
9ebe76b
Merge branch 'main' into return-requirements-saving-to-session
rvsiyad Apr 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 3 additions & 19 deletions app/presenters/return-requirements/abstraction-period.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,20 @@
* Formats data for the `/return-requirements/{sessionId}/abstraction-period` 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, payload = {}) {
function go (session) {
const data = {
abstractionPeriod: session.data.abstractionPeriod ? session.data.abstractionPeriod : null,
id: session.id,
licenceId: session.data.licence.id,
licenceRef: session.data.licence.licenceRef,
abstractionPeriod: _licenceAbstractionPeriod(payload)
licenceRef: session.data.licence.licenceRef
}

return data
}

function _licenceAbstractionPeriod (payload) {
// NOTE: The following checks whether a user has inputted any values for the abstraction period for the returns
// requirements. If the values have not been set, then it is because the presenter has been called from
// 'AbstractionPeriodService' and it's the first load. Else it has been called by the 'SubmitAbstractionPeriod' and
// the user has not inputted any values for the abstraction period. Either way, we use it to tell us wether there is
// anything in the payload worth transforming.

return {
startDay: payload['start-abstraction-period-day'] ? payload['start-abstraction-period-day'] : null,
startMonth: payload['start-abstraction-period-month'] ? payload['start-abstraction-period-month'] : null,
endDay: payload['end-abstraction-period-day'] ? payload['end-abstraction-period-day'] : null,
endMonth: payload['end-abstraction-period-month'] ? payload['end-abstraction-period-month'] : null
}
}

module.exports = {
go
}
18 changes: 4 additions & 14 deletions app/presenters/return-requirements/points.presenter.js
rvsiyad marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,19 @@
*
* @returns {Object} - The data formatted for the view template
*/
function go (session, pointsData, payload = {}) {
function go (session, pointsData) {
const data = {
id: session.id,
licenceId: session.data.licence.id,
licenceRef: session.data.licence.licenceRef,
licencePoints: _licencePoints(pointsData, payload)
licencePoints: _licencePoints(pointsData),
selectedPoints: session.data.points ? session.data.points.join(',') : ''
}

return data
}

function _licencePoints (pointsData, payload) {
// NOTE: 'points' is the payload value that tells us whether the user selected any purposes
// for the return requirement.
// If it is not set then it is because the presenter has been called from 'PointsService' and it's the first
// load. Else it has been called by the 'SubmitPointsService' and the user has not checked a point from the list.
// Either way, we use it to tell us whether there is anything in the payload worth transforming.
const points = payload.points

if (points) {
return points
}

function _licencePoints (pointsData) {
const abstractionPoints = []

if (!pointsData) {
Expand Down
3 changes: 2 additions & 1 deletion app/presenters/return-requirements/reason.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
function go (session) {
const data = {
id: session.id,
licenceRef: session.data.licence.licenceRef
licenceRef: session.data.licence.licenceRef,
reason: session.data.reason ? session.data.reason : null
}

return data
Expand Down
3 changes: 2 additions & 1 deletion app/presenters/return-requirements/setup.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
function go (session) {
const data = {
id: session.id,
licenceRef: session.data.licence.licenceRef
licenceRef: session.data.licence.licenceRef,
setup: session.data.setup ? session.data.setup : null
}

return data
Expand Down
18 changes: 2 additions & 16 deletions app/presenters/return-requirements/site-description.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,13 @@ function go (session, payload = {}) {
id: session.id,
licenceId: session.data.licence.id,
licenceRef: session.data.licence.licenceRef,
licenceSiteDescription: _licenceSiteDescription(payload)
siteDescription: session.data.siteDescription ? session.data.siteDescription : null,
inputtedSiteDescription: payload.siteDescription ? payload.siteDescription : null
}

return data
}

function _licenceSiteDescription (payload) {
// NOTE: 'siteDescription' is the payload value that tells us whether the user inputted a siteDescription
// for the return requirement site.
// If it is not set then it is because the presenter has been called from 'SiteDescriptionService' and it's the first
// load. Else it has been called by the 'SubmitSiteDescriptionService' and the user has not inputted a site description.
// Either way, we use it to tell us wether there is anything in the payload worth transforming.
const siteDescription = payload.siteDescription

if (!siteDescription) {
return null
}

return siteDescription
}

module.exports = {
go
}
4 changes: 2 additions & 2 deletions app/services/return-requirements/points.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

const FetchPointsService = require('../../services/return-requirements/fetch-points.service.js')
const SelectPointsPresenter = require('../../presenters/return-requirements/points.presenter.js')
const PointsPresenter = require('../../presenters/return-requirements/points.presenter.js')
const SessionModel = require('../../models/session.model.js')

/**
Expand All @@ -23,7 +23,7 @@ async function go (sessionId) {
const session = await SessionModel.query().findById(sessionId)
const pointsData = await FetchPointsService.go(session.data.licence.id)

const formattedData = SelectPointsPresenter.go(session, pointsData)
const formattedData = PointsPresenter.go(session, pointsData)

return {
activeNavBar: 'search',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ async function go (sessionId, payload) {
return {}
}

const formattedData = AbstractionPeriodPresenter.go(session, payload)
const submittedSessionData = _submittedSessionData(session, payload)

return {
activeNavBar: 'search',
error: validationResult,
pageTitle: 'Enter the abstraction period for the requirements for returns',
...formattedData
...submittedSessionData
}
}

Expand All @@ -52,6 +52,16 @@ async function _save (session, payload) {
return session.$query().patch({ data: currentData })
}

/**
* Combines the existing session data with the submitted payload formatted by the presenter. If nothing is submitted by
* the user, payload will be an empty object.
*/
function _submittedSessionData (session, payload) {
session.data.abstractionPeriod = Object.keys(payload).length > 0 ? payload : null

return AbstractionPeriodPresenter.go(session)
}

function _validate (payload) {
const validation = AbstractionPeriodValidator.go(payload)

Expand Down
34 changes: 31 additions & 3 deletions app/services/return-requirements/submit-points.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

const FetchPointsService = require('../../services/return-requirements/fetch-points.service.js')
const PointsValidator = require('../../validators/return-requirements/points.validator.js')
const SelectPointsPresenter = require('../../presenters/return-requirements/points.presenter.js')
const PointsPresenter = require('../../presenters/return-requirements/points.presenter.js')
const SessionModel = require('../../models/session.model.js')

/**
Expand All @@ -27,9 +27,18 @@ const SessionModel = require('../../models/session.model.js')
async function go (sessionId, payload) {
const session = await SessionModel.query().findById(sessionId)

const pointsData = await FetchPointsService.go(session.data.licence.id)
_handleOneOptionSelected(payload)

const validationResult = _validate(payload)
const formattedData = SelectPointsPresenter.go(session, pointsData, payload)

if (!validationResult) {
await _save(session, payload)

return {}
}

const pointsData = await FetchPointsService.go(session.data.licence.id)
const formattedData = PointsPresenter.go(session, pointsData)

return {
activeNavBar: 'search',
Expand All @@ -39,6 +48,25 @@ async function go (sessionId, payload) {
}
}

/**
* When a single point is checked by the user, it returns as a string. When multiple points are checked, the
* 'points' is returned as an array. This function works to make those single selected string 'points' into an array
* for uniformity.
*/
function _handleOneOptionSelected (payload) {
if (!Array.isArray(payload.points)) {
payload.points = [payload.points]
}
}

async function _save (session, payload) {
const currentData = session.data

currentData.points = payload.points

return session.$query().patch({ data: currentData })
}

function _validate (payload) {
const validation = PointsValidator.go(payload)

Expand Down
10 changes: 5 additions & 5 deletions app/services/return-requirements/submit-purpose.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@

const FetchPurposesService = require('../../services/return-requirements/fetch-purposes.service.js')
const PurposeValidation = require('../../validators/return-requirements/purpose.validator.js')
const SelectPurposePresenter = require('../../presenters/return-requirements/purpose.presenter.js')
const PurposePresenter = require('../../presenters/return-requirements/purpose.presenter.js')
const SessionModel = require('../../models/session.model.js')

/**
* Orchestrates validating the data for `/return-requirements/{sessionId}/purpose` 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.
* 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 id of the current session
* @param {Object} payload - The submitted form data
Expand All @@ -38,7 +38,7 @@ async function go (sessionId, payload) {
}

const purposesData = await FetchPurposesService.go(session.data.licence.id)
const formattedData = SelectPurposePresenter.go(session, purposesData)
const formattedData = PurposePresenter.go(session, purposesData)

return {
activeNavBar: 'search',
Expand Down
4 changes: 2 additions & 2 deletions app/services/return-requirements/submit-setup.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ async function go (sessionId, payload) {
function _redirect (setup) {
let endpoint

if (setup === 'use_abstraction_data') {
if (setup === 'use-abstraction-data') {
endpoint = 'check-your-answers'
}

if (setup === 'set_up_manually') {
if (setup === 'set-up-manually') {
endpoint = 'purpose'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ const SiteDescriptionValidator = require('../../validators/return-requirements/s
*
* 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.
* The user input is then validated and the site description in the payload is saved in the session. If there is a
* validation error the controller will re-render the page. 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 {string} sessionId - The UUID of the current session
* @param {Object} payload - The submitted form data
*
* @returns {Promise<Object>} The page data for the start date page
Expand All @@ -28,6 +28,12 @@ async function go (sessionId, payload) {

const validationResult = _validate(payload)

if (!validationResult) {
await _save(session, payload)

return {}
}

const formattedData = SiteDescriptionPresenter.go(session, payload)

return {
Expand All @@ -39,6 +45,14 @@ async function go (sessionId, payload) {
}
}

async function _save (session, payload) {
const currentData = session.data

currentData.siteDescription = payload.siteDescription

return session.$query().patch({ data: currentData })
}

function _validate (payload) {
const validation = SiteDescriptionValidator.go(payload)

Expand Down
4 changes: 2 additions & 2 deletions app/validators/return-requirements/setup.validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
const Joi = require('joi')

const VALID_VALUES = [
'use_abstraction_data',
'set_up_manually'
'use-abstraction-data',
'set-up-manually'
]

/**
Expand Down
12 changes: 8 additions & 4 deletions app/views/return-requirements/abstraction-period.njk
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@
items: [
{
name: "day",
classes: "govuk-input--width-2" + startResultErrorClass
classes: "govuk-input--width-2" + startResultErrorClass,
value: abstractionPeriod['start-abstraction-period-day']
},
{
name: "month",
classes: "govuk-input--width-2" + startResultErrorClass
classes: "govuk-input--width-2" + startResultErrorClass,
value: abstractionPeriod['start-abstraction-period-month']
}
]
}) }}
Expand All @@ -96,11 +98,13 @@
items: [
{
name: "day",
classes: "govuk-input--width-2" + endResultErrorClass
classes: "govuk-input--width-2" + endResultErrorClass,
value: abstractionPeriod['end-abstraction-period-day']
},
{
name: "month",
classes: "govuk-input--width-2" + endResultErrorClass
classes: "govuk-input--width-2" + endResultErrorClass,
value: abstractionPeriod['end-abstraction-period-month']
}
]
}) }}
Expand Down
2 changes: 1 addition & 1 deletion app/views/return-requirements/points.njk
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
{% set checkBoxItem = {
value: point,
text: point,
checked: false
checked: point in selectedPoints
} %}

{% set checkBoxItems = (checkBoxItems.push(checkBoxItem), checkBoxItems) %}
Expand Down
Loading
Loading