Skip to content

Commit

Permalink
Merge pull request #2139 from City-of-Helsinki/HL-782-deminimis
Browse files Browse the repository at this point in the history
HL-782: Validate  granted_at so that it cannot be more than 4 years in the past
  • Loading branch information
mjturt authored Jul 17, 2023
2 parents 47b43a7 + 92ce0d1 commit 059d184
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
DE_MINIMIS_AID_GRANTED_AT_MAX_DATE,
DE_MINIMIS_AID_GRANTED_AT_MIN_DATE,
MAX_DEMINIMIS_AID_TOTAL_AMOUNT,
} from 'benefit/applicant/constants';
import { DE_MINIMIS_AID_KEYS } from 'benefit-shared/constants';
Expand Down Expand Up @@ -108,6 +109,7 @@ const DeMinimisAidForm: React.FC<DeMinimisAidFormProps> = ({ data }) => {
invalid={!!getErrorMessage(DE_MINIMIS_AID_KEYS.GRANTED_AT)}
aria-invalid={!!getErrorMessage(DE_MINIMIS_AID_KEYS.GRANTED_AT)}
errorText={getErrorMessage(DE_MINIMIS_AID_KEYS.GRANTED_AT)}
minDate={DE_MINIMIS_AID_GRANTED_AT_MIN_DATE}
maxDate={DE_MINIMIS_AID_GRANTED_AT_MAX_DATE}
required
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { DE_MINIMIS_AID_GRANTED_AT_MAX_DATE } from 'benefit/applicant/constants';
import {
DE_MINIMIS_AID_GRANTED_AT_MAX_DATE,
DE_MINIMIS_AID_GRANTED_AT_MIN_DATE,
} from 'benefit/applicant/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 { isBefore, isFuture } from 'date-fns';
import { TFunction } from 'next-i18next';
import { convertToUIDateFormat, parseDate } from 'shared/utils/date.utils';
import { getNumberValue } from 'shared/utils/string.utils';
Expand Down Expand Up @@ -42,5 +45,20 @@ export const getValidationSchema = (t: TFunction): Yup.SchemaOf<DeMinimisAid> =>
}
return true;
},
})
.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 && isBefore(date, DE_MINIMIS_AID_GRANTED_AT_MIN_DATE)) {
return false;
}
return true;
},
}),
});
3 changes: 3 additions & 0 deletions frontend/benefit/applicant/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ export const PRIVACY_POLICY_LINKS = {

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);

export const APPLICATION_INITIAL_VALUES = {
Expand Down

0 comments on commit 059d184

Please sign in to comment.