Skip to content

Commit

Permalink
fix(app): restored licenceId for redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
Demwunz committed Mar 20, 2024
1 parent 195a614 commit 98b6e2a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
6 changes: 3 additions & 3 deletions app/controllers/return-requirements.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const SelectReasonService = require('../services/return-requirements/reason.serv
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 SubmitCheckYourAnswersService = require('../services/return-requirements/submit-check-your-answers.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 @@ -217,9 +217,9 @@ async function submitAgreementsExceptions (request, h) {

async function submitCheckYourAnswers (request, h) {
const { sessionId } = request.params
await SubmitCheckYourAnswersService.go(sessionId)
const licenceId = await SubmitCheckYourAnswersService.go(sessionId)

return h.redirect(`/system/return-requirements/${sessionId}/approved`)
return h.redirect(`/system/return-requirements/${licenceId}/approved`)
}

async function submitExisting (request, h) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict'

/**
* Manages converting the session data to return requirement records when check your answers is confirmed
* @module SubmitCheckYoursAnswersService
Expand All @@ -24,14 +25,20 @@ const SessionModel = require('../../models/session.model.js')
*/
async function go (sessionId) {
const session = await SessionModel.query().findById(sessionId)
const licenceData = await CheckLicenceEndedService.go(session.data.licence.id)
const isLicenceEnded = await licenceData

if (isLicenceEnded) {
throw new ExpandedError('Invalid return requirement', { licenceData })
await _validateLicence(session.data.licence.id)

return session.data.licence.id
}

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

if (!licenceEnded) {
return
}

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

module.exports = {
Expand Down
1 change: 1 addition & 0 deletions test/controllers/return-requirements.controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ describe('Return requirements controller', () => {
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
Expand Up @@ -35,28 +35,26 @@ describe('Submit Check Your Answers service', () => {
Sinon.restore()
})

describe('POST /return-requirements/{sessionDd}/check-your-answers', () => {
describe('When called with a valid licence', () => {
beforeEach(() => {
Sinon.stub(CheckLicenceEndedService, 'go').resolves(false)
})

describe('When called with a valid licence', () => {
it('returns a valid licence', async () => {
const result = await SubmitCheckYourAnswersService.go(sessionId)
expect(result).to.be.false()
})
it('returns a valid licence', async () => {
const result = await SubmitCheckYourAnswersService.go(sessionId)
expect(result).to.equal(session.data.licence.id)
})
})

describe('When called with an invalid licence (expired, lapsed or revoked)', () => {
beforeEach(async () => {
Sinon.stub(SubmitCheckYourAnswersService, 'go').rejects(new ExpandedError('Invalid return requirement', {}))
})
describe('When called with an invalid licence (expired, lapsed or revoked)', () => {
beforeEach(async () => {
Sinon.stub(CheckLicenceEndedService, 'go').resolves(true)
})

it('throws an error', async () => {
const response = await expect(SubmitCheckYourAnswersService.go(sessionId)).to.reject()
expect(response).to.be.an.instanceOf(ExpandedError)
expect(response.message).to.equal('Invalid return requirement')
})
it('throws an error', async () => {
const response = await expect(SubmitCheckYourAnswersService.go(sessionId)).to.reject()
expect(response).to.be.an.instanceOf(ExpandedError)
expect(response.message).to.equal('Invalid licence for return requirements')
})
})
})

0 comments on commit 98b6e2a

Please sign in to comment.