Skip to content

Commit

Permalink
Sort out "Check details" functionality for received service
Browse files Browse the repository at this point in the history
  • Loading branch information
Jozzey committed Feb 4, 2025
1 parent 79dfa8b commit 82201b1
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 30 deletions.
9 changes: 7 additions & 2 deletions app/controllers/return-logs-setup.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,20 @@ async function submitNote(request, h) {
async function submitReceived(request, h) {
const {
params: { sessionId },
payload
payload,
yar
} = request

const pageData = await SubmitReceivedService.go(sessionId, payload)
const pageData = await SubmitReceivedService.go(sessionId, payload, yar)

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

if (pageData.checkPageVisited) {
return h.redirect(`/system/return-logs/setup/${sessionId}/check`)
}

return h.redirect(`/system/return-logs/setup/${sessionId}/submission`)
}

Expand Down
13 changes: 11 additions & 2 deletions app/presenters/return-logs/setup/received.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
function go(session) {
const {
id: sessionId,
licenceId,
returnReference,
receivedDateOptions,
receivedDateDay,
Expand All @@ -24,7 +23,7 @@ function go(session) {
} = session

return {
backLink: `/system/licences/${licenceId}/returns`,
backLink: _backLink(session),
pageTitle: 'When was the return received?',
receivedDateDay: receivedDateDay ?? null,
receivedDateMonth: receivedDateMonth ?? null,
Expand All @@ -35,6 +34,16 @@ function go(session) {
}
}

function _backLink(session) {
const { checkPageVisited, id, licenceId } = session

if (checkPageVisited) {
return `/system/return-logs/setup/${id}/check`
}

return `/system/licences/${licenceId}/returns`
}

module.exports = {
go
}
12 changes: 10 additions & 2 deletions app/services/return-logs/setup/submit-received.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* @module SubmitReceivedService
*/

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

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

return {}
if (session.checkPageVisited) {
GeneralLib.flashNotification(yar)
}

return {
checkPageVisited: session.checkPageVisited
}
}

const formattedData = _submittedSessionData(session, payload)
Expand Down
2 changes: 1 addition & 1 deletion app/views/return-logs/setup/check.njk
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
actions: {
items: [
{
href: "#",
href: "received",
text: "Change",
visuallyHiddenText: "return received date"
}
Expand Down
41 changes: 27 additions & 14 deletions test/controllers/return-logs-setup.controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ describe('Return Logs Setup controller', () => {
})

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

describe('when a request is invalid', () => {
beforeEach(() => {
Sinon.stub(SubmitReceivedService, 'go').resolves({
error: { message: 'Enter a real received date' },
pageTitle: 'When was the return received?',
sessionId
describe('and the page has been visited previously', () => {
beforeEach(() => {
Sinon.stub(SubmitReceivedService, 'go').resolves({ checkPageVisited: true })
})

it('redirects to the "submission" page', async () => {
const response = await server.inject(_postOptions(path, {}))

expect(response.statusCode).to.equal(302)
expect(response.headers.location).to.equal(`/system/return-logs/setup/${sessionId}/check`)
})
})

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

expect(response.statusCode).to.equal(200)
expect(response.payload).to.contain('Enter a real received date')
expect(response.payload).to.contain('There is a problem')
it('returns the page successfully with the error summary banner', async () => {
const response = await server.inject(_postOptions(path))

expect(response.statusCode).to.equal(200)
expect(response.payload).to.contain('Enter a real received date')
expect(response.payload).to.contain('There is a problem')
})
})
})
})
Expand Down
22 changes: 22 additions & 0 deletions test/presenters/return-logs/setup/received.presenter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,28 @@ describe('Return Logs Setup - Received presenter', () => {
})
})

describe('the "backLink" property', () => {
describe('when the user has come from the "check" page', () => {
beforeEach(() => {
session.checkPageVisited = true
})

it('returns a link back to the "check" page', () => {
const result = ReceivedPresenter.go(session)

expect(result.backLink).to.equal('/system/return-logs/setup/61e07498-f309-4829-96a9-72084a54996d/check')
})
})

describe('when the user has come from somewhere else', () => {
it('returns a link back to the "Licence" page on the "Returns" tab', () => {
const result = ReceivedPresenter.go(session)

expect(result.backLink).to.equal('/system/licences/a96ce5c6-2c42-4b3f-946d-0428b5f07ce6/returns')
})
})
})

describe('the "receivedDate" properties', () => {
describe('when the user has previously selected todays date as the received date', () => {
beforeEach(() => {
Expand Down
55 changes: 46 additions & 9 deletions test/services/return-logs/setup/submit-received.service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')
const Sinon = require('sinon')

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

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

session = await SessionHelper.add(sessionData)

yarStub = { flash: Sinon.stub() }
})

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

it('saves the submitted option', async () => {
await SubmitReceivedService.go(session.id, payload)
await SubmitReceivedService.go(session.id, payload, yarStub)

const refreshedSession = await session.$query()

expect(refreshedSession.receivedDateOptions).to.equal('today')
expect(new Date(refreshedSession.receivedDate)).to.equal(testDate)
})

describe('and the page has been not been visited', () => {
it('returns the correct details the controller needs to redirect the journey', async () => {
const result = await SubmitReceivedService.go(session.id, payload, yarStub)

expect(result).to.equal({
checkPageVisited: undefined
})
})
})

describe('and the page has been visited', () => {
beforeEach(async () => {
session = await SessionHelper.add({ data: { ...sessionData.data, checkPageVisited: true } })
})

it('returns the correct details the controller needs to redirect the journey to the check page', async () => {
const result = await SubmitReceivedService.go(session.id, payload, yarStub)

expect(result).to.equal({
checkPageVisited: true
})
})

it('sets the notification message title to "Updated" and the text to "Changes made" ', async () => {
await SubmitReceivedService.go(session.id, payload, yarStub)

const [flashType, notification] = yarStub.flash.args[0]

expect(flashType).to.equal('notification')
expect(notification).to.equal({ title: 'Updated', text: 'Changes made' })
})
})
})

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

it('saves the submitted option', async () => {
await SubmitReceivedService.go(session.id, payload)
await SubmitReceivedService.go(session.id, payload, yarStub)

const refreshedSession = await session.$query()

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

it('saves the submitted values', async () => {
await SubmitReceivedService.go(session.id, payload)
await SubmitReceivedService.go(session.id, payload, yarStub)

const refreshedSession = await session.$query()

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

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

expect(result).to.equal({})
expect(result).to.equal({ checkPageVisited: undefined })
})
})

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

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

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

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

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

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

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

it('includes what was submitted', async () => {
const result = await SubmitReceivedService.go(session.id, payload)
const result = await SubmitReceivedService.go(session.id, payload, yarStub)

expect(result.receivedDateDay).to.equal('a')
expect(result.receivedDateMonth).to.equal('b')
Expand Down

0 comments on commit 82201b1

Please sign in to comment.