Skip to content

Commit

Permalink
Handle API returning 404 for ACCT alerts
Browse files Browse the repository at this point in the history
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 acctAlerts array being empty,
we so we pass an empty array in this case.
  • Loading branch information
libuk committed Sep 14, 2023
1 parent cfe8137 commit 03385f8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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 = <SanitisedError>{ data: { status: 404 } }
const getAcctAlertsMock = jest.fn().mockImplementation(() => {
throw err
})

const personService = createMock<PersonService>({
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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 03385f8

Please sign in to comment.