diff --git a/server/form-pages/apply/assess-placement-risks-and-needs/prison-information/acctAlerts.test.ts b/server/form-pages/apply/assess-placement-risks-and-needs/prison-information/acctAlerts.test.ts index fc2eb57c6..919b63bc2 100644 --- a/server/form-pages/apply/assess-placement-risks-and-needs/prison-information/acctAlerts.test.ts +++ b/server/form-pages/apply/assess-placement-risks-and-needs/prison-information/acctAlerts.test.ts @@ -5,6 +5,7 @@ import { acctAlertFactory, applicationFactory, personFactory } from '../../../.. import { itShouldHaveNextValue, itShouldHavePreviousValue } from '../../../shared-examples' import { PageBodyPersonAcctAlert, mapAcctAlertsForPageBody } from '../../../utils' import AcctAlerts, { acctAlertResponse } from './acctAlerts' +import { SanitisedError } from '../../../../sanitisedError' jest.mock('../../../../services/personService') jest.mock('../../../utils') @@ -78,6 +79,24 @@ describe('AcctAlerts', () => { expect(getAcctAlertsMock).toHaveBeenCalledWith(callConfig, application.person.crn) expect(mapAcctAlertsForPageBody).toHaveBeenCalledWith(apiAcctAlerts) }) + + it('sets the number of acctAlerts to 0 if none are found', async () => { + const err = { data: { status: 404 } } + const getAcctAlertsMock = jest.fn().mockImplementation(() => { + throw err + }) + + const personService = createMock({ + getAcctAlerts: getAcctAlertsMock, + }) + + const page = await AcctAlerts.initialize({}, application, callConfig, { personService }) + + expect(page.body).toEqual({ acctAlerts }) + + expect(getAcctAlertsMock).toHaveBeenCalledWith(callConfig, application.person.crn) + expect(mapAcctAlertsForPageBody).toHaveBeenCalledWith([]) + }) }) itShouldHavePreviousValue(new AcctAlerts({}, application), 'adjudications') diff --git a/server/form-pages/apply/assess-placement-risks-and-needs/prison-information/acctAlerts.ts b/server/form-pages/apply/assess-placement-risks-and-needs/prison-information/acctAlerts.ts index f37c71ef0..504bd4b1d 100644 --- a/server/form-pages/apply/assess-placement-risks-and-needs/prison-information/acctAlerts.ts +++ b/server/form-pages/apply/assess-placement-risks-and-needs/prison-information/acctAlerts.ts @@ -41,7 +41,16 @@ export default class AcctAlerts implements TasklistPage { callConfig: CallConfig, dataServices: DataServices, ) { - const acctAlerts = await dataServices.personService.getAcctAlerts(callConfig, application.person.crn) + let acctAlerts: PersonAcctAlert[] = [] + + try { + acctAlerts = await dataServices.personService.getAcctAlerts(callConfig, application.person.crn) + } catch (e) { + if (e?.data?.status !== 404) { + throw e + } + } + const page = new AcctAlerts({ acctAlerts: mapAcctAlertsForPageBody(acctAlerts) }, application) page.importDate = DateFormats.dateObjToIsoDate(new Date())