From 53e3b5df789302f09302af80278d86cfb2823d00 Mon Sep 17 00:00:00 2001 From: Sampo Tawast <5328394+sirtawast@users.noreply.github.com> Date: Thu, 18 Apr 2024 09:19:58 +0300 Subject: [PATCH] fix(handler): de minimis validation as in applicant form (#2932) --- .../deMinimisAid/utils/validation.ts | 16 +++++++++++++--- frontend/benefit/handler/src/constants.ts | 6 ++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/frontend/benefit/handler/src/components/applicationForm/formContent/companySection/deMinimisAid/utils/validation.ts b/frontend/benefit/handler/src/components/applicationForm/formContent/companySection/deMinimisAid/utils/validation.ts index 631e32b097..57484e2747 100644 --- a/frontend/benefit/handler/src/components/applicationForm/formContent/companySection/deMinimisAid/utils/validation.ts +++ b/frontend/benefit/handler/src/components/applicationForm/formContent/companySection/deMinimisAid/utils/validation.ts @@ -1,10 +1,14 @@ -import { DE_MINIMIS_AID_GRANTED_AT_MAX_DATE } from 'benefit/handler/constants'; +import { + DE_MINIMIS_AID_GRANTED_AT_MAX_DATE, + DE_MINIMIS_AID_GRANTED_AT_MIN_DATE, +} from 'benefit/handler/constants'; import { DE_MINIMIS_AID_KEYS, VALIDATION_MESSAGE_KEYS, } from 'benefit-shared/constants'; import { DeMinimisAid } from 'benefit-shared/types/application'; -import isFuture from 'date-fns/isFuture'; +import { validateIsTodayOrPastDate } from 'benefit-shared/utils/dates'; +import isBefore from 'date-fns/isBefore'; import { TFunction } from 'next-i18next'; import { convertToUIDateFormat, parseDate } from 'shared/utils/date.utils'; import { getNumberValue } from 'shared/utils/string.utils'; @@ -33,12 +37,18 @@ export const getValidationSchema = (t: TFunction): Yup.SchemaOf => message: t(VALIDATION_MESSAGE_KEYS.DATE_MAX, { max: convertToUIDateFormat(DE_MINIMIS_AID_GRANTED_AT_MAX_DATE), }), + test: (value) => validateIsTodayOrPastDate(value), + }) + .test({ + message: t(VALIDATION_MESSAGE_KEYS.DATE_MIN, { + min: convertToUIDateFormat(DE_MINIMIS_AID_GRANTED_AT_MIN_DATE), + }), test: (value) => { if (!value) return false; const date = parseDate(value); - if (date && isFuture(date)) { + if (date && isBefore(date, DE_MINIMIS_AID_GRANTED_AT_MIN_DATE)) { return false; } return true; diff --git a/frontend/benefit/handler/src/constants.ts b/frontend/benefit/handler/src/constants.ts index f81ee543ee..139c0df38b 100644 --- a/frontend/benefit/handler/src/constants.ts +++ b/frontend/benefit/handler/src/constants.ts @@ -94,6 +94,12 @@ export const ACTIONLESS_STATUSES: APPLICATION_STATUSES[] = [ ]; export const DE_MINIMIS_AID_GRANTED_AT_MAX_DATE = new Date(); +// Set the minimum date of the deMinimimis aid granted at datepicker to the beginning of the year 4 years ago +export const DE_MINIMIS_AID_GRANTED_AT_MIN_DATE = new Date( + new Date().getFullYear() - 4, + 0, + 1 +); export const APPLICATION_START_DATE = new Date(new Date().getFullYear(), 0, 1);