From 29ff94fd8bed226a2c0d649ceebea99501ab2ec5 Mon Sep 17 00:00:00 2001 From: Daniel Liburd Date: Tue, 26 Sep 2023 13:33:18 +0100 Subject: [PATCH] Send application eligibility data with application We want to add data to the MI report. This commit adds application eligibility data as a first class property when submitting an application. If the reason matches one of the 4 reasons from the application it'll return true, which should be always. However, to guard against future updates, we return false if the reason is not found. --- server/form-pages/utils/index.test.ts | 25 +++++++++++++++++++ server/form-pages/utils/index.ts | 16 ++++++++++++ .../applications/getApplicationData.test.ts | 1 + .../utils/applications/getApplicationData.ts | 2 ++ 4 files changed, 44 insertions(+) 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), } }