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 26b5c7a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
4 changes: 2 additions & 2 deletions app/controllers/return-requirements.controller.js
Original file line number Diff line number Diff line change
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
Expand Up @@ -24,14 +24,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
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 26b5c7a

Please sign in to comment.