diff --git a/app/controllers/return-requirements.controller.js b/app/controllers/return-requirements.controller.js index ec8341c88c..bbada3204f 100644 --- a/app/controllers/return-requirements.controller.js +++ b/app/controllers/return-requirements.controller.js @@ -69,7 +69,6 @@ async function approved (request, h) { async function checkYourAnswers (request, h) { const { sessionId } = request.params - const pageData = await CheckYourAnswersService.go(sessionId) return h.view('return-requirements/check-your-answers.njk', { diff --git a/app/presenters/return-requirements/check-your-answers.presenter.js b/app/presenters/return-requirements/check-your-answers.presenter.js index a910c388d4..0caf4f963c 100644 --- a/app/presenters/return-requirements/check-your-answers.presenter.js +++ b/app/presenters/return-requirements/check-your-answers.presenter.js @@ -4,16 +4,45 @@ * Formats data for the `/return-requirements/{sessionId}/check-your-answers` page * @module CheckYourAnswersPresenter */ - +const { formatLongDate } = require('../base.presenter.js') function go (session) { const data = { id: session.id, - licenceRef: session.data.licence.licenceRef + licenceRef: session.data.licence.licenceRef, + reason: _reason(session.data), + startDate: _startDate(session.data) } return data } +function _reason (data) { + let reason = data.returnsRequired + if (data.journey === 'no-returns-required') { + reason = data.noReturnsRequired + } + + return reason +} +function _startDate (data) { + const selectedOption = data.startDateOptions + let date + + if (selectedOption === 'licenceStartDate') { + date = new Date(data.licence.currentVersionStartDate) + } + + if (selectedOption === 'anotherStartDate') { + const day = data.startDateDay + const month = data.startDateMonth + const year = data.startDateYear + + date = new Date(`${year}-${month}-${day}`) + } + + return formatLongDate(date) +} + module.exports = { go } diff --git a/app/services/return-requirements/check-your-answers.service.js b/app/services/return-requirements/check-your-answers.service.js index 6cbb929169..975c6ec36f 100644 --- a/app/services/return-requirements/check-your-answers.service.js +++ b/app/services/return-requirements/check-your-answers.service.js @@ -17,8 +17,8 @@ const SessionModel = require('../../models/session.model.js') */ async function go (sessionId) { const session = await SessionModel.query().findById(sessionId) - const formattedData = CheckYourAnswersPresenter.go(session) + const formattedData = CheckYourAnswersPresenter.go(session) return { activeNavBar: 'search', licenceRef: session.data.licence.licenceRef, diff --git a/app/services/return-requirements/submit-no-returns-required.service.js b/app/services/return-requirements/submit-no-returns-required.service.js index f496af1a34..bb3c2192ce 100644 --- a/app/services/return-requirements/submit-no-returns-required.service.js +++ b/app/services/return-requirements/submit-no-returns-required.service.js @@ -25,9 +25,15 @@ const SessionModel = require('../../models/session.model.js') */ async function go (sessionId, payload) { const session = await SessionModel.query().findById(sessionId) - const validationResult = _validate(payload) + if (!validationResult) { + await _save(session, payload) + return { + journey: session.data.journey + } + } + const formattedData = NoReturnsRequiredPresenter.go(session, payload) return { @@ -38,6 +44,14 @@ async function go (sessionId, payload) { } } +async function _save (session, payload) { + const currentData = session.data + + currentData.noReturnsRequired = payload['no-returns-required'] + + return session.$query().patch({ data: currentData }) +} + function _validate (payload) { const validation = NoReturnsRequiredValidator.go(payload) diff --git a/app/services/return-requirements/submit-purpose.service.js b/app/services/return-requirements/submit-purpose.service.js index 245909b7dd..ceb6d72cd4 100644 --- a/app/services/return-requirements/submit-purpose.service.js +++ b/app/services/return-requirements/submit-purpose.service.js @@ -29,6 +29,12 @@ async function go (sessionId, payload) { const purposesData = await FetchPurposesService.go(session.data.licence.id) const validationResult = _validate(payload) + + if (!validationResult) { + await _save(session, payload) + return {} + } + const formattedData = SelectPurposePresenter.go(session, purposesData, payload) return { @@ -39,6 +45,14 @@ async function go (sessionId, payload) { } } +async function _save (session, payload) { + const currentData = session.data + + currentData.purpose = payload.purpose + + return session.$query().patch({ data: currentData }) +} + function _validate (payload) { const validation = PurposeValidation.go(payload) diff --git a/app/services/return-requirements/submit-reason.service.js b/app/services/return-requirements/submit-reason.service.js index 8afc0a5aca..998e31168f 100644 --- a/app/services/return-requirements/submit-reason.service.js +++ b/app/services/return-requirements/submit-reason.service.js @@ -28,6 +28,11 @@ async function go (sessionId, payload) { const validationResult = _validate(payload) + if (!validationResult) { + await _save(session, payload) + return {} + } + const formattedData = ReasonPresenter.go(session, payload) return { @@ -38,6 +43,14 @@ async function go (sessionId, payload) { } } +async function _save (session, payload) { + const currentData = session.data + + currentData.reason = payload.reason + + return session.$query().patch({ data: currentData }) +} + function _validate (payload) { const validation = ReasonValidator.go(payload) diff --git a/app/services/return-requirements/submit-setup.service.js b/app/services/return-requirements/submit-setup.service.js index 7ba4a95d45..c8805fb488 100644 --- a/app/services/return-requirements/submit-setup.service.js +++ b/app/services/return-requirements/submit-setup.service.js @@ -28,13 +28,19 @@ async function go (sessionId, payload) { const validationResult = _validate(payload) + if (!validationResult) { + await _save(session, payload) + return { + redirect: _redirect(payload.setup) + } + } + const formattedData = SetupPresenter.go(session, payload) return { activeNavBar: 'search', error: validationResult, pageTitle: 'How do you want to set up the return requirement?', - redirect: _redirect(payload.setup), ...formattedData } } @@ -53,6 +59,14 @@ function _redirect (setup) { return endpoint } +async function _save (session, payload) { + const currentData = session.data + + currentData.setup = payload.setup + + return session.$query().patch({ data: currentData }) +} + function _validate (payload) { const validation = SetupValidator.go(payload) diff --git a/app/services/return-requirements/submit-start-date.service.js b/app/services/return-requirements/submit-start-date.service.js index df4c6b4e24..71945fdaee 100644 --- a/app/services/return-requirements/submit-start-date.service.js +++ b/app/services/return-requirements/submit-start-date.service.js @@ -30,6 +30,13 @@ async function go (sessionId, payload) { const { endDate, startDate } = session.data.licence const validationResult = _validate(payload, startDate, endDate) + if (!validationResult) { + await _save(session, payload) + return { + journey: session.data.journey + } + } + const formattedData = StartDatePresenter.go(session, payload) return { @@ -41,6 +48,21 @@ async function go (sessionId, payload) { } } +async function _save (session, payload) { + const currentData = session.data + const selectedOption = payload['start-date-options'] + + currentData.startDateOptions = selectedOption + + if (selectedOption === 'anotherStartDate') { + currentData.startDateDay = payload['start-date-day'] + currentData.startDateMonth = payload['start-date-month'] + currentData.startDateYear = payload['start-date-year'] + } + + return session.$query().patch({ data: currentData }) +} + function _validate (payload, licenceStartDate, licenceEndDate) { const validation = StartDateValidator.go(payload, licenceStartDate, licenceEndDate) diff --git a/app/views/return-requirements/check-your-answers.njk b/app/views/return-requirements/check-your-answers.njk index 08a1c0271f..9b43274253 100644 --- a/app/views/return-requirements/check-your-answers.njk +++ b/app/views/return-requirements/check-your-answers.njk @@ -4,6 +4,16 @@ {% set baseURL = "/system/return-requirements/" + id %} +{% set reasonText %} + {% if reason == 'abstraction_below_100_cubic_metres_per_day' %} + Abstraction amount below 100 cubic metres per day + {% elif reason == 'returns_exception' %} + Returns exception + {% elif reason == 'transfer_licence' %} + Transfer licence + {% endif %} +{% endset %} + {% block content %} {# Main heading #}
@@ -25,7 +35,7 @@ text: "Start date" }, value: { - text: "start_date_from_session" + text: startDate }, actions: { items: [ @@ -43,7 +53,7 @@ text: "Reason" }, value: { - text: "reason_from_session" + text: reasonText }, actions: { items: [