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

Submit Check your answers page #811

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
b0f9c5e
Submit Check your answers page
Demwunz Mar 12, 2024
62d0835
feat(app): check your answers service
Demwunz Mar 12, 2024
4f50823
fix(app): fixed broken tests
Demwunz Mar 12, 2024
2acfd29
fix:(app) - updated service test
Demwunz Mar 13, 2024
9bd3300
fix:(app) - added fetch licence test
Demwunz Mar 13, 2024
fc05b7a
fix:(app) - removed console log
Demwunz Mar 13, 2024
0f53a72
Fix failing fetch-licence.service.test.js
Cruikshanks Mar 13, 2024
1a3e7a2
Housekeeping in FetchLicenceService
Cruikshanks Mar 13, 2024
ba1112a
Clean up FetchLicenceService test
Cruikshanks Mar 13, 2024
9af8930
fix:(app) - removed redundant tests
Demwunz Mar 13, 2024
6c2e52b
feat(app): moved check licence expired to new service
Demwunz Mar 19, 2024
33ff4c0
fix(app): simplified check licence
Demwunz Mar 19, 2024
4fbdec2
Update app/services/return-requirements/check-licence-ended.service.js
Demwunz Mar 20, 2024
78d7aba
Update app/services/return-requirements/check-licence-ended.service.js
Demwunz Mar 20, 2024
c0906cd
Update app/services/return-requirements/check-licence-ended.service.js
Demwunz Mar 20, 2024
c43048f
Update app/services/return-requirements/check-licence-ended.service.js
Demwunz Mar 20, 2024
53db271
Update app/services/return-requirements/check-licence-ended.service.js
Demwunz Mar 20, 2024
9ae6d57
Update app/services/return-requirements/check-licence-ended.service.js
Demwunz Mar 20, 2024
46e4b8c
Update app/services/return-requirements/submit-check-your-answers.ser…
Demwunz Mar 20, 2024
c73673a
Update app/services/return-requirements/check-your-answers.service.js
Demwunz Mar 20, 2024
85b45b9
Merge branch 'main' into WATER-4264-returns-not-required-journey-chec…
Demwunz Mar 20, 2024
bf77e18
fix(app): cleanup logic after changes
Demwunz Mar 20, 2024
195a614
Merge branch 'main' into WATER-4264-returns-not-required-journey-chec…
Demwunz Mar 20, 2024
98b6e2a
fix(app): restored licenceId for redirect
Demwunz Mar 20, 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
13 changes: 5 additions & 8 deletions app/controllers/return-requirements.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
* @module ReturnRequirementsController
*/

const CheckYourAnswersService = require('../services/return-requirements/check-your-answers.service.js')
const NoReturnsRequiredService = require('../services/return-requirements/no-returns-required.service.js')
const SelectPurposeService = require('../services/return-requirements/purpose.service.js')
const SelectReasonService = require('../services/return-requirements/reason.service.js')
const SessionModel = require('../models/session.model.js')
const SetupService = require('../services/return-requirements/setup.service.js')
const SiteDescriptionService = require('../services/return-requirements/site-description.service.js')
const StartDateService = require('../services/return-requirements/start-date.service.js')
const SubmitCheckYourAnswersService = require('../services/return-requirements/submit-check-your-answers.service.js')
const SubmitNoReturnsRequiredService = require('../services/return-requirements/submit-no-returns-required.service.js')
const SubmitPurposeService = require('../services/return-requirements/submit-purpose.service.js')
const SubmitReasonService = require('../services/return-requirements/submit-reason.service.js')
Expand Down Expand Up @@ -68,12 +70,10 @@ async function approved (request, h) {
async function checkYourAnswers (request, h) {
const { sessionId } = request.params

const session = await SessionModel.query().findById(sessionId)
const pageData = await CheckYourAnswersService.go(sessionId)

return h.view('return-requirements/check-your-answers.njk', {
activeNavBar: 'search',
pageTitle: `Check the return requirements for ${session?.data?.licence?.licenceHolder}`,
...session
...pageData
})
}

Expand Down Expand Up @@ -217,10 +217,7 @@ async function submitAgreementsExceptions (request, h) {

async function submitCheckYourAnswers (request, h) {
const { sessionId } = request.params

const session = await SessionModel.query().findById(sessionId)

const { id: licenceId } = session.data.licence
const licenceId = await SubmitCheckYourAnswersService.go(sessionId)

return h.redirect(`/system/return-requirements/${licenceId}/approved`)
}
Expand Down
19 changes: 19 additions & 0 deletions app/presenters/return-requirements/check-your-answers.presenter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict'

/**
* Formats data for the `/return-requirements/{sessionId}/check-your-answers` page
* @module CheckYourAnswersPresenter
*/

function go (session) {
const data = {
id: session.id,
licenceRef: session.data.licence.licenceRef
}

return data
}

module.exports = {
go
}
57 changes: 57 additions & 0 deletions app/services/return-requirements/check-licence-ended.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict'

/**
* Checks if a licence is 'ended' (expired, lapsed or revoked)
* @module CheckLicenceEndedService
*/

const LicenceModel = require('../../models/licence.model.js')

/**
* Checks if a licence is 'ended' (expired, lapsed or revoked)
*
* A licence ended if it has expired, lapsed or revoked on or before the current date. Because we need to check this
* in a number of places we have the `LicenceModel` instance method `$ends()` to do this for us.
*
* But for that to work you have to fetch the licence and the 3 dates from the licence record. This service combines
* fetching the licence and the dates then returning the result of `$ends()` to whatever service calls it.
*
* @param {string} id - The UUID for the licence to fetch
*
* @returns {Promise<boolean>} true if the licence has ended else false
*/
async function go (id) {
const licence = await _fetchLicence(id)
const licenceEnded = _licenceEnded(licence)
return licenceEnded
}

async function _fetchLicence (id) {
return LicenceModel.query()
.findById(id)
.select([
'expiredDate',
'id',
'lapsedDate',
'licenceRef',
'revokedDate',
'startDate'
])
}

function _licenceEnded (licence) {
const ends = licence.$ends()

if (!ends) {
return false
}

const today = new Date()
today.setHours(0, 0, 0, 0)

return ends.date <= today
}

module.exports = {
go
}
32 changes: 32 additions & 0 deletions app/services/return-requirements/check-your-answers.service.js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd avoid using the Optional Chaining (sometimes known as the safe navigation operator) unless we have a situation where we know a property may not be populated.

In the case of this service, the session should be correctly populated. If it isn't it's because someone has purposely tried to access the page with a valid session ID but an invalid session. So, it's own them!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Cruikshanks ok, that makes sense. I'll remove it and replace with a direct reference.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict'

/**
* Orchestrates fetching and presenting the data for `/return-requirements/{sessionId}/check-your-answers` page
* @module CheckYourAnswersService
*/

const CheckYourAnswersPresenter = require('../../presenters/return-requirements/check-your-answers.presenter.js')
const SessionModel = require('../../models/session.model.js')

/**
* Orchestrates fetching and presenting the data for `/return-requirements/{sessionId}/check-your-answers` page
*
* @param {string} sessionId - The UUID for return requirement setup session record
*
* @returns {Promise<Object>} page data needed by the view template
*/
async function go (sessionId) {
const session = await SessionModel.query().findById(sessionId)
const formattedData = CheckYourAnswersPresenter.go(session)

return {
activeNavBar: 'search',
licenceRef: session.data.licence.licenceRef,
pageTitle: `Check the return requirements for ${session.data.licence.licenceHolder}`,
...formattedData
}
}

module.exports = {
go
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict'

/**
* Manages converting the session data to return requirement records when check your answers is confirmed
* @module SubmitCheckYoursAnswersService
*/

const CheckLicenceEndedService = require('./check-licence-ended.service.js')
const ExpandedError = require('../../errors/expanded.error.js')
const SessionModel = require('../../models/session.model.js')

/**
* Manages converting the session data to return requirement records when check your answers is confirmed
*
* > This service is work in progress. Some of the functionality described is yet to be implemented
*
* After fetching the session instance for the returns requirements journey in progress it validates that what the user
* has setup can be persisted for the licence.
*
* If valid it converts the session data to return requirements records then deletes the session record.
*
* @param {string} sessionId - The UUID for return requirement setup session record
*
* @returns {string} The licence ID
*/
async function go (sessionId) {
const session = await SessionModel.query().findById(sessionId)

await _validateLicence(session.data.licence.id)

return session.data.licence.id
}

async function _validateLicence (licenceId) {
const licenceEnded = await CheckLicenceEndedService.go(licenceId)

if (!licenceEnded) {
return
}

throw new ExpandedError('Invalid licence for return requirements', { licenceId, licenceEnded })
}

module.exports = {
go
}
93 changes: 91 additions & 2 deletions app/views/return-requirements/check-your-answers.njk
Original file line number Diff line number Diff line change
@@ -1,15 +1,104 @@
{% extends 'layout.njk' %}
{% from "govuk/components/button/macro.njk" import govukButton %}
{% from "govuk/components/summary-list/macro.njk" import govukSummaryList %}

{% set baseURL = "/system/return-requirements/" + id %}

{% block content %}
{# Main heading #}
<div class="govuk-body">
<h1 class="govuk-heading-xl govuk-!-margin-bottom-3">{{ pageTitle }}</h1>
<h1 class="govuk-heading-xl govuk-!-margin-bottom-3">
<span class="govuk-caption-l">Licence {{ licenceRef }}</span>
{{ pageTitle }}
</h1>
</div>

<form method="post">
<div class="govuk-!-margin-bottom-9">
<hr class="govuk-section-break govuk-!-margin-bottom-2 govuk-section-break--visible">
{{ govukSummaryList({
classes: 'govuk-!-margin-bottom-2',
rows: [
{
classes: 'govuk-summary-list govuk-summary-list__row--no-border',
key: {
text: "Start date"
},
value: {
text: "start_date_from_session"
},
actions: {
items: [
{
href: baseURL + "/start-date",
text: "Change",
visuallyHiddenText: "the start date for the return requirement"
}
]
}
},
{
classes: 'govuk-summary-list govuk-summary-list__row--no-border',
key: {
text: "Reason"
},
value: {
text: "reason_from_session"
},
actions: {
items: [
{
href: baseURL + "/reason",
text: "Change",
visuallyHiddenText: "the reason for the return requirement"
}
]
}
}
]
}) }}
<hr class="govuk-section-break govuk-!-margin-bottom-2 govuk-section-break--visible">
</div>

<div class="govuk-!-margin-bottom-9">
<h2 class="govuk-heading-l govuk-!-margin-bottom-4" >Notes</h2>
<hr class="govuk-section-break govuk-!-margin-bottom-2 govuk-section-break--visible">
{{ govukSummaryList({
classes: 'govuk-!-margin-bottom-2',
rows: [
{
classes: 'govuk-summary-list govuk-summary-list__row--no-border',
key: {
text: "No notes added"
},
value: {
text: ""
},
actions: {
items: [
{
href: "#",
text: "Change",
visuallyHiddenText: "the note"
}
]
}
}
]
}) }}
<hr class="govuk-section-break govuk-!-margin-bottom-2 govuk-section-break--visible">
</div>
<div class="govuk-body">
{{ govukButton({ text: "Approve returns requirements" }) }}
{{ govukButton({
text: "Approve returns requirements",
classes: "govuk-!-margin-right-6"
}) }}

{{ govukButton({
text: "Cancel return requirements",
classes: "govuk-button--secondary",
href: ""
}) }}
</div>
</form>
{% endblock %}
7 changes: 7 additions & 0 deletions test/controllers/return-requirements.controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const { describe, it, beforeEach, afterEach } = exports.lab = Lab.script()
const { expect } = Code

// Things we need to stub
const CheckYourAnswersService = require('../../app/services/return-requirements/check-your-answers.service.js')
const NoReturnsRequiredService = require('../../app/services/return-requirements/no-returns-required.service.js')
const SelectPurposeService = require('../../app/services/return-requirements/purpose.service.js')
const SelectReasonService = require('../../app/services/return-requirements/reason.service.js')
Expand Down Expand Up @@ -83,6 +84,12 @@ describe('Return requirements controller', () => {
})

describe('GET /return-requirements/{sessionId}/check-your-answers', () => {
beforeEach(async () => {
Sinon.stub(CheckYourAnswersService, 'go').resolves({
id: '8702b98f-ae51-475d-8fcc-e049af8b8d38', pageTitle: 'Check the return requirements for Acme Corp.'
})
})

describe('when the request succeeds', () => {
it('returns the page successfully', async () => {
const response = await server.inject(_options('check-your-answers'))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')

const { describe, it, beforeEach } = exports.lab = Lab.script()
const { expect } = Code

// Thing under test
const CheckYourAnswersPresenter = require('../../../app/presenters/return-requirements/check-your-answers.presenter.js')

describe('Check Your Answers presenter', () => {
let session

beforeEach(() => {
session = {
id: 'f1288f6c-8503-4dc1-b114-75c408a14bd0',
data: {
licence: {
id: 'ea53bfc6-740d-46c5-9558-fc8cabfc6c1f',
licenceRef: '01/123',
licenceHolder: 'Astro Boy'
}
}
}
})

describe('when provided with a populated session', () => {
it('correctly presents the data', () => {
const result = CheckYourAnswersPresenter.go(session)

expect(result).to.equal({
id: 'f1288f6c-8503-4dc1-b114-75c408a14bd0',
licenceRef: '01/123'
})
})
})
})
Loading
Loading