Skip to content

Commit

Permalink
fix(handler): de minimis validation as in applicant form (#2932)
Browse files Browse the repository at this point in the history
  • Loading branch information
sirtawast authored Apr 18, 2024
1 parent 4eda910 commit 53e3b5d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -33,12 +37,18 @@ export const getValidationSchema = (t: TFunction): Yup.SchemaOf<DeMinimisAid> =>
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;
Expand Down
6 changes: 6 additions & 0 deletions frontend/benefit/handler/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 53e3b5d

Please sign in to comment.