Skip to content

Commit

Permalink
Render guidance if we fail to fetch offences
Browse files Browse the repository at this point in the history
Previously, if we received a 404 when fetching the offences for a PoP
we'd render the generic error page.

As we've identified this scenario to be caused by a missing NOMS number
for the CRN, we want to show guidance to the user in how to correct it.

This commit conditionally renders the missing NOMS guidance page if we
receive a 404 when fetching offences.
  • Loading branch information
libuk committed Sep 20, 2023
1 parent a5245e0 commit f3920ca
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
15 changes: 15 additions & 0 deletions server/controllers/apply/applicationsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import paths from '../../paths/apply'
import { firstPageOfApplicationJourney, getResponses } from '../../utils/applicationUtils'
import { DateFormats } from '../../utils/dateUtils'
import extractCallConfig from '../../utils/restUtils'
import { SanitisedError } from '../../sanitisedError'

jest.mock('../../utils/validation')
jest.mock('../../utils/applicationUtils')
Expand Down Expand Up @@ -133,6 +134,20 @@ describe('applicationsController', () => {
personService.getOffences.mockResolvedValue([offence])
})

describe('if we fail to fetch offences', () => {
it('should render guidance on missing NOMS number', async () => {
const err = <SanitisedError>{ data: { status: 404 } }
personService.getOffences.mockImplementation(() => {
throw err
})
const requestHandler = applicationsController.new()

await requestHandler(request, response, next)

expect(response.render).toHaveBeenCalledWith('applications/people/missingNoms')
})
})

describe('if an error has not been sent to the flash', () => {
beforeEach(() => {
;(fetchErrorsAndUserInput as jest.Mock).mockImplementation(() => {
Expand Down
33 changes: 21 additions & 12 deletions server/controllers/apply/applicationsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,28 @@ export default class ApplicationsController {
if (crnArr.length) {
const crn = crnArr[0]
const person = await this.personService.findByCrn(callConfig, crn)
const offences = await this.personService.getOffences(callConfig, crn)

const offenceId = offences.length === 1 ? offences[0].offenceId : null

return res.render(`applications/people/confirm`, {
person,
date: DateFormats.dateObjtoUIDate(new Date()),
crn,
offenceId,
errors,
errorSummary,
...userInput,
})
try {
const offences = await this.personService.getOffences(callConfig, crn)

const offenceId = offences.length === 1 ? offences[0].offenceId : null

return res.render(`applications/people/confirm`, {
person,
date: DateFormats.dateObjtoUIDate(new Date()),
crn,
offenceId,
errors,
errorSummary,
...userInput,
})
} catch (e) {
if (e?.data?.status === 404) {
return res.render('applications/people/missingNoms')
}

throw e
}
}

return res.render('applications/new', {
Expand Down

0 comments on commit f3920ca

Please sign in to comment.