Skip to content

Commit

Permalink
feat(app): added persistent data to session
Browse files Browse the repository at this point in the history
  • Loading branch information
Demwunz committed Mar 22, 2024
1 parent e44468c commit ced8c2f
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 19 deletions.
1 change: 0 additions & 1 deletion app/controllers/return-requirements.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down
35 changes: 33 additions & 2 deletions app/presenters/return-requirements/check-your-answers.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,47 @@
* 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
journey: session.data.journey,
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)

Expand Down
14 changes: 14 additions & 0 deletions app/services/return-requirements/submit-purpose.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)

Expand Down
13 changes: 13 additions & 0 deletions app/services/return-requirements/submit-reason.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)

Expand Down
16 changes: 15 additions & 1 deletion app/services/return-requirements/submit-setup.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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)

Expand Down
22 changes: 22 additions & 0 deletions app/services/return-requirements/submit-start-date.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)

Expand Down
38 changes: 25 additions & 13 deletions app/views/return-requirements/check-your-answers.njk
Original file line number Diff line number Diff line change
Expand Up @@ -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 #}
<div class="govuk-body">
Expand All @@ -25,7 +35,7 @@
text: "Start date"
},
value: {
text: "start_date_from_session"
text: startDate
},
actions: {
items: [
Expand All @@ -43,7 +53,7 @@
text: "Reason"
},
value: {
text: "reason_from_session"
text: reasonText
},
actions: {
items: [
Expand Down Expand Up @@ -90,18 +100,20 @@
</div>

<div class="govuk-body">
<h3 class="govuk-heading-m">Returns are not required for this licence</h3>
<hr class="govuk-!-margin-bottom-5 govuk-section-break--s govuk-section-break--visible">
{{ govukButton({
text: "Approve returns requirements",
classes: "govuk-!-margin-right-6"
}) }}
{% if journey == 'no-returns-required' %}
<h3 class="govuk-heading-m">Returns are not required for this licence</h3>
<hr class="govuk-!-margin-bottom-5 govuk-section-break--s govuk-section-break--visible">
{% endif %}
{{ govukButton({
text: "Approve returns requirements",
classes: "govuk-!-margin-right-6"
}) }}

{{ govukButton({
text: "Cancel return requirements",
classes: "govuk-button--secondary",
href: "#"
}) }}
{{ govukButton({
text: "Cancel return requirements",
classes: "govuk-button--secondary",
href: "#"
}) }}
</div>
</form>
{% endblock %}

0 comments on commit ced8c2f

Please sign in to comment.