From 426e98ec9bcab429a4416e3a29f6793014f7495b Mon Sep 17 00:00:00 2001 From: Daniel Liburd Date: Thu, 14 Sep 2023 14:52:10 +0100 Subject: [PATCH] Handle API returning 404 for Adjudications In the event the API returns a 404 we would throw an unhandled exception which would stop the user completing the referral. This change ensures that we handle this scenario and throw for anything else. The UI already handles the scenario of the adjudications array being empty, we so we pass an empty array in this case. --- .../prison-information/adjudications.test.ts | 19 +++++++++++++++++++ .../prison-information/adjudications.ts | 10 +++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/server/form-pages/apply/assess-placement-risks-and-needs/prison-information/adjudications.test.ts b/server/form-pages/apply/assess-placement-risks-and-needs/prison-information/adjudications.test.ts index 4c09bc54a..61cacbd87 100644 --- a/server/form-pages/apply/assess-placement-risks-and-needs/prison-information/adjudications.test.ts +++ b/server/form-pages/apply/assess-placement-risks-and-needs/prison-information/adjudications.test.ts @@ -5,6 +5,7 @@ import { adjudicationFactory, applicationFactory, personFactory } from '../../.. import { itShouldHaveNextValue, itShouldHavePreviousValue } from '../../../shared-examples' import { PageBodyAdjudication, mapAdjudicationsForPageBody } from '../../../utils' import Adjudications, { adjudicationResponse } from './adjudications' +import { SanitisedError } from '../../../../sanitisedError' jest.mock('../../../../services/personService') jest.mock('../../../utils') @@ -66,6 +67,24 @@ describe('Adjudications', () => { expect(getAdjudicationsMock).toHaveBeenCalledWith(callConfig, application.person.crn) expect(mapAdjudicationsForPageBody).toHaveBeenCalledWith(apiAdjudications) }) + + it('sets the number of adjudications to 0 if none are found', async () => { + const err = { data: { status: 404 } } + const getAdjudicationsMock = jest.fn().mockImplementation(() => { + throw err + }) + + const personService = createMock({ + getAdjudications: getAdjudicationsMock, + }) + + const page = await Adjudications.initialize({}, application, callConfig, { personService }) + + expect(page.body).toEqual({ adjudications }) + + expect(getAdjudicationsMock).toHaveBeenCalledWith(callConfig, application.person.crn) + expect(mapAdjudicationsForPageBody).toHaveBeenCalledWith([]) + }) }) itShouldHavePreviousValue(new Adjudications({}, application), 'dashboard') diff --git a/server/form-pages/apply/assess-placement-risks-and-needs/prison-information/adjudications.ts b/server/form-pages/apply/assess-placement-risks-and-needs/prison-information/adjudications.ts index c3bc7a797..0b233189b 100644 --- a/server/form-pages/apply/assess-placement-risks-and-needs/prison-information/adjudications.ts +++ b/server/form-pages/apply/assess-placement-risks-and-needs/prison-information/adjudications.ts @@ -43,7 +43,15 @@ export default class Adjudications implements TasklistPage { callConfig: CallConfig, dataServices: DataServices, ) { - const adjudications = await dataServices.personService.getAdjudications(callConfig, application.person.crn) + let adjudications: Adjudication[] = [] + + try { + adjudications = await dataServices.personService.getAdjudications(callConfig, application.person.crn) + } catch (e) { + if (e?.data?.status !== 404) { + throw e + } + } const page = new Adjudications({ adjudications: mapAdjudicationsForPageBody(adjudications) }, application)