diff --git a/server/form-pages/utils/index.test.ts b/server/form-pages/utils/index.test.ts index 750ee655c..37b675533 100644 --- a/server/form-pages/utils/index.test.ts +++ b/server/form-pages/utils/index.test.ts @@ -434,6 +434,31 @@ describe('utils', () => { }) }) + describe('isApplicationEligibleFromApplication', () => { + it('returns whether the application is eligible for CAS3 from the application', () => { + const application = applicationFactory.build({ + data: { + eligibility: { + 'eligibility-reason': { reason: 'homelessFromCustody' }, + }, + }, + }) + expect(utils.isApplicationEligibleFromApplication(application)).toEqual(true) + }) + + it('throws an error when the eligibility data is not present', () => { + const application = applicationFactory.build({ + data: { + eligibility: {}, + }, + }) + + expect(() => utils.isApplicationEligibleFromApplication(application)).toThrow( + new SessionDataError('No application eligibility data'), + ) + }) + }) + describe('dateBodyProperties', () => { it('returns date field names for use in page body properties', () => { expect(utils.dateBodyProperties('someDate')).toEqual([ diff --git a/server/form-pages/utils/index.ts b/server/form-pages/utils/index.ts index 9592e3884..7a655f7fe 100644 --- a/server/form-pages/utils/index.ts +++ b/server/form-pages/utils/index.ts @@ -8,6 +8,7 @@ import type { YesOrNo, YesOrNoWithDetail, } from '@approved-premises/ui' +import { eligibilityReasons } from '../apply/accommodation-need/eligibility/eligibilityReason' import { Adjudication, TemporaryAccommodationApplication as Application, @@ -255,6 +256,21 @@ export const needsAccessiblePropertyFromApplication = (application: Application) return true } +export const isApplicationEligibleFromApplication = (application: Application): boolean => { + const eligibilityReason: string = (application.data as Record)?.eligibility?.['eligibility-reason'] + ?.reason + + if (!eligibilityReason) { + throw new SessionDataError('No application eligibility data') + } + + if (Object.keys(eligibilityReasons).includes(eligibilityReason)) { + return true + } + + return false +} + export const dateBodyProperties = (root: string) => { return [root, `${root}-year`, `${root}-month`, `${root}-day`] } diff --git a/server/utils/applications/getApplicationData.test.ts b/server/utils/applications/getApplicationData.test.ts index d14b34b76..01c53f865 100644 --- a/server/utils/applications/getApplicationData.test.ts +++ b/server/utils/applications/getApplicationData.test.ts @@ -28,6 +28,7 @@ describe('getApplicationSubmissionData', () => { isDutyToReferSubmitted: true, dutyToReferSubmissionDate: '2022-04-12', needsAccessibleProperty: true, + isApplicationEligible: true, }) }) }) diff --git a/server/utils/applications/getApplicationData.ts b/server/utils/applications/getApplicationData.ts index 415078c9c..750317641 100644 --- a/server/utils/applications/getApplicationData.ts +++ b/server/utils/applications/getApplicationData.ts @@ -6,6 +6,7 @@ import { import { arrivalDateFromApplication, dutyToReferSubmissionDateFromApplication, + isApplicationEligibleFromApplication, isDutyToReferSubmittedFromApplication, needsAccessiblePropertyFromApplication, } from '../../form-pages/utils' @@ -27,5 +28,6 @@ export const getApplicationSubmissionData = (application: Application): SubmitAp isDutyToReferSubmitted: isDutyToReferSubmittedFromApplication(application), dutyToReferSubmissionDate: dutyToReferSubmissionDateFromApplication(application), needsAccessibleProperty: needsAccessiblePropertyFromApplication(application), + isApplicationEligible: isApplicationEligibleFromApplication(application), } }