Skip to content

Commit 82201b1

Browse files
committed
Sort out "Check details" functionality for received service
1 parent 79dfa8b commit 82201b1

File tree

7 files changed

+124
-30
lines changed

7 files changed

+124
-30
lines changed

app/controllers/return-logs-setup.controller.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,20 @@ async function submitNote(request, h) {
151151
async function submitReceived(request, h) {
152152
const {
153153
params: { sessionId },
154-
payload
154+
payload,
155+
yar
155156
} = request
156157

157-
const pageData = await SubmitReceivedService.go(sessionId, payload)
158+
const pageData = await SubmitReceivedService.go(sessionId, payload, yar)
158159

159160
if (pageData.error) {
160161
return h.view('return-logs/setup/received.njk', pageData)
161162
}
162163

164+
if (pageData.checkPageVisited) {
165+
return h.redirect(`/system/return-logs/setup/${sessionId}/check`)
166+
}
167+
163168
return h.redirect(`/system/return-logs/setup/${sessionId}/submission`)
164169
}
165170

app/presenters/return-logs/setup/received.presenter.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
function go(session) {
1616
const {
1717
id: sessionId,
18-
licenceId,
1918
returnReference,
2019
receivedDateOptions,
2120
receivedDateDay,
@@ -24,7 +23,7 @@ function go(session) {
2423
} = session
2524

2625
return {
27-
backLink: `/system/licences/${licenceId}/returns`,
26+
backLink: _backLink(session),
2827
pageTitle: 'When was the return received?',
2928
receivedDateDay: receivedDateDay ?? null,
3029
receivedDateMonth: receivedDateMonth ?? null,
@@ -35,6 +34,16 @@ function go(session) {
3534
}
3635
}
3736

37+
function _backLink(session) {
38+
const { checkPageVisited, id, licenceId } = session
39+
40+
if (checkPageVisited) {
41+
return `/system/return-logs/setup/${id}/check`
42+
}
43+
44+
return `/system/licences/${licenceId}/returns`
45+
}
46+
3847
module.exports = {
3948
go
4049
}

app/services/return-logs/setup/submit-received.service.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* @module SubmitReceivedService
66
*/
77

8+
const GeneralLib = require('../../../lib/general.lib.js')
89
const ReceivedDateValidator = require('../../../validators/return-logs/setup/received-date.validator.js')
910
const ReceivedPresenter = require('../../../presenters/return-logs/setup/received.presenter.js')
1011
const SessionModel = require('../../../models/session.model.js')
@@ -21,10 +22,11 @@ const SessionModel = require('../../../models/session.model.js')
2122
*
2223
* @param {string} sessionId - The UUID of the current session
2324
* @param {object} payload - The submitted form data
25+
* @param {object} yar - The Hapi `request.yar` session manager passed on by the controller
2426
*
2527
* @returns {Promise<object>} If no errors the page data for the received page else the validation error details
2628
*/
27-
async function go(sessionId, payload) {
29+
async function go(sessionId, payload, yar) {
2830
const session = await SessionModel.query().findById(sessionId)
2931

3032
const { startDate } = session
@@ -33,7 +35,13 @@ async function go(sessionId, payload) {
3335
if (!validationResult) {
3436
await _save(session, payload)
3537

36-
return {}
38+
if (session.checkPageVisited) {
39+
GeneralLib.flashNotification(yar)
40+
}
41+
42+
return {
43+
checkPageVisited: session.checkPageVisited
44+
}
3745
}
3846

3947
const formattedData = _submittedSessionData(session, payload)

app/views/return-logs/setup/check.njk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
actions: {
101101
items: [
102102
{
103-
href: "#",
103+
href: "received",
104104
text: "Change",
105105
visuallyHiddenText: "return received date"
106106
}

test/controllers/return-logs-setup.controller.test.js

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,8 @@ describe('Return Logs Setup controller', () => {
238238
})
239239

240240
describe('POST', () => {
241-
describe('when a request is valid', () => {
242-
describe('and the received date is entered', () => {
241+
describe('when the request succeeds', () => {
242+
describe('and the page has not been visited previously', () => {
243243
beforeEach(() => {
244244
Sinon.stub(SubmitReceivedService, 'go').resolves({})
245245
})
@@ -251,23 +251,36 @@ describe('Return Logs Setup controller', () => {
251251
expect(response.headers.location).to.equal(`/system/return-logs/setup/${sessionId}/submission`)
252252
})
253253
})
254-
})
255254

256-
describe('when a request is invalid', () => {
257-
beforeEach(() => {
258-
Sinon.stub(SubmitReceivedService, 'go').resolves({
259-
error: { message: 'Enter a real received date' },
260-
pageTitle: 'When was the return received?',
261-
sessionId
255+
describe('and the page has been visited previously', () => {
256+
beforeEach(() => {
257+
Sinon.stub(SubmitReceivedService, 'go').resolves({ checkPageVisited: true })
258+
})
259+
260+
it('redirects to the "submission" page', async () => {
261+
const response = await server.inject(_postOptions(path, {}))
262+
263+
expect(response.statusCode).to.equal(302)
264+
expect(response.headers.location).to.equal(`/system/return-logs/setup/${sessionId}/check`)
262265
})
263266
})
264267

265-
it('re-renders the page with an error message', async () => {
266-
const response = await server.inject(_postOptions(path))
268+
describe('and the validation fails', () => {
269+
beforeEach(() => {
270+
Sinon.stub(SubmitReceivedService, 'go').resolves({
271+
error: { message: 'Enter a real received date' },
272+
pageTitle: 'When was the return received?',
273+
sessionId
274+
})
275+
})
267276

268-
expect(response.statusCode).to.equal(200)
269-
expect(response.payload).to.contain('Enter a real received date')
270-
expect(response.payload).to.contain('There is a problem')
277+
it('returns the page successfully with the error summary banner', async () => {
278+
const response = await server.inject(_postOptions(path))
279+
280+
expect(response.statusCode).to.equal(200)
281+
expect(response.payload).to.contain('Enter a real received date')
282+
expect(response.payload).to.contain('There is a problem')
283+
})
271284
})
272285
})
273286
})

test/presenters/return-logs/setup/received.presenter.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,28 @@ describe('Return Logs Setup - Received presenter', () => {
3838
})
3939
})
4040

41+
describe('the "backLink" property', () => {
42+
describe('when the user has come from the "check" page', () => {
43+
beforeEach(() => {
44+
session.checkPageVisited = true
45+
})
46+
47+
it('returns a link back to the "check" page', () => {
48+
const result = ReceivedPresenter.go(session)
49+
50+
expect(result.backLink).to.equal('/system/return-logs/setup/61e07498-f309-4829-96a9-72084a54996d/check')
51+
})
52+
})
53+
54+
describe('when the user has come from somewhere else', () => {
55+
it('returns a link back to the "Licence" page on the "Returns" tab', () => {
56+
const result = ReceivedPresenter.go(session)
57+
58+
expect(result.backLink).to.equal('/system/licences/a96ce5c6-2c42-4b3f-946d-0428b5f07ce6/returns')
59+
})
60+
})
61+
})
62+
4163
describe('the "receivedDate" properties', () => {
4264
describe('when the user has previously selected todays date as the received date', () => {
4365
beforeEach(() => {

test/services/return-logs/setup/submit-received.service.test.js

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Test framework dependencies
44
const Lab = require('@hapi/lab')
55
const Code = require('@hapi/code')
6+
const Sinon = require('sinon')
67

78
const { describe, it, beforeEach } = (exports.lab = Lab.script())
89
const { expect } = Code
@@ -18,6 +19,7 @@ describe('Return Logs Setup - Submit Received service', () => {
1819
let session
1920
let sessionData
2021
let testDate
22+
let yarStub
2123

2224
beforeEach(async () => {
2325
sessionData = {
@@ -29,6 +31,8 @@ describe('Return Logs Setup - Submit Received service', () => {
2931
}
3032

3133
session = await SessionHelper.add(sessionData)
34+
35+
yarStub = { flash: Sinon.stub() }
3236
})
3337

3438
describe('when called', () => {
@@ -42,13 +46,46 @@ describe('Return Logs Setup - Submit Received service', () => {
4246
})
4347

4448
it('saves the submitted option', async () => {
45-
await SubmitReceivedService.go(session.id, payload)
49+
await SubmitReceivedService.go(session.id, payload, yarStub)
4650

4751
const refreshedSession = await session.$query()
4852

4953
expect(refreshedSession.receivedDateOptions).to.equal('today')
5054
expect(new Date(refreshedSession.receivedDate)).to.equal(testDate)
5155
})
56+
57+
describe('and the page has been not been visited', () => {
58+
it('returns the correct details the controller needs to redirect the journey', async () => {
59+
const result = await SubmitReceivedService.go(session.id, payload, yarStub)
60+
61+
expect(result).to.equal({
62+
checkPageVisited: undefined
63+
})
64+
})
65+
})
66+
67+
describe('and the page has been visited', () => {
68+
beforeEach(async () => {
69+
session = await SessionHelper.add({ data: { ...sessionData.data, checkPageVisited: true } })
70+
})
71+
72+
it('returns the correct details the controller needs to redirect the journey to the check page', async () => {
73+
const result = await SubmitReceivedService.go(session.id, payload, yarStub)
74+
75+
expect(result).to.equal({
76+
checkPageVisited: true
77+
})
78+
})
79+
80+
it('sets the notification message title to "Updated" and the text to "Changes made" ', async () => {
81+
await SubmitReceivedService.go(session.id, payload, yarStub)
82+
83+
const [flashType, notification] = yarStub.flash.args[0]
84+
85+
expect(flashType).to.equal('notification')
86+
expect(notification).to.equal({ title: 'Updated', text: 'Changes made' })
87+
})
88+
})
5289
})
5390

5491
describe('with a valid payload (yesterdays date)', () => {
@@ -62,7 +99,7 @@ describe('Return Logs Setup - Submit Received service', () => {
6299
})
63100

64101
it('saves the submitted option', async () => {
65-
await SubmitReceivedService.go(session.id, payload)
102+
await SubmitReceivedService.go(session.id, payload, yarStub)
66103

67104
const refreshedSession = await session.$query()
68105

@@ -82,7 +119,7 @@ describe('Return Logs Setup - Submit Received service', () => {
82119
})
83120

84121
it('saves the submitted values', async () => {
85-
await SubmitReceivedService.go(session.id, payload)
122+
await SubmitReceivedService.go(session.id, payload, yarStub)
86123

87124
const refreshedSession = await session.$query()
88125

@@ -94,9 +131,9 @@ describe('Return Logs Setup - Submit Received service', () => {
94131
})
95132

96133
it('returns the correct details the controller needs to redirect the journey', async () => {
97-
const result = await SubmitReceivedService.go(session.id, payload)
134+
const result = await SubmitReceivedService.go(session.id, payload, yarStub)
98135

99-
expect(result).to.equal({})
136+
expect(result).to.equal({ checkPageVisited: undefined })
100137
})
101138
})
102139

@@ -106,7 +143,7 @@ describe('Return Logs Setup - Submit Received service', () => {
106143
})
107144

108145
it('returns the page data for the view', async () => {
109-
const result = await SubmitReceivedService.go(session.id, payload)
146+
const result = await SubmitReceivedService.go(session.id, payload, yarStub)
110147

111148
expect(result).to.equal(
112149
{
@@ -125,7 +162,7 @@ describe('Return Logs Setup - Submit Received service', () => {
125162

126163
describe('because the user has not selected anything', () => {
127164
it('includes an error for the radio form element', async () => {
128-
const result = await SubmitReceivedService.go(session.id, payload)
165+
const result = await SubmitReceivedService.go(session.id, payload, yarStub)
129166

130167
expect(result.error).to.equal({
131168
message: 'Select the return received date',
@@ -146,7 +183,7 @@ describe('Return Logs Setup - Submit Received service', () => {
146183
})
147184

148185
it('includes an error for the date input element', async () => {
149-
const result = await SubmitReceivedService.go(session.id, payload)
186+
const result = await SubmitReceivedService.go(session.id, payload, yarStub)
150187

151188
expect(result.error).to.equal({
152189
message: 'Enter a real received date',
@@ -156,7 +193,7 @@ describe('Return Logs Setup - Submit Received service', () => {
156193
})
157194

158195
it('includes what was submitted', async () => {
159-
const result = await SubmitReceivedService.go(session.id, payload)
196+
const result = await SubmitReceivedService.go(session.id, payload, yarStub)
160197

161198
expect(result.receivedDateDay).to.equal('a')
162199
expect(result.receivedDateMonth).to.equal('b')

0 commit comments

Comments
 (0)