From b5c94c40449a47ac4524d2c3c5fedc1fab15fa6b Mon Sep 17 00:00:00 2001 From: rikuke <33894149+rikuke@users.noreply.github.com> Date: Wed, 3 Jan 2024 12:04:26 +0200 Subject: [PATCH] fix: HL-1093 year 2024 bug (#2684) * fix: allow past year in HDS datepicker * fix: frontend validation for application start * fix: backend validation for application start * fix: lint files --------- Co-authored-by: Sampo Tawast --- .../applications/api/v1/serializers/application.py | 13 +++++++++---- .../applications/tests/test_applications_api.py | 2 +- backend/benefit/locale/en/LC_MESSAGES/django.po | 3 +++ backend/benefit/locale/fi/LC_MESSAGES/django.po | 3 +++ backend/benefit/locale/sv/LC_MESSAGES/django.po | 3 +++ .../forms/application/step2/utils/validation.ts | 8 ++++---- frontend/benefit/applicant/src/constants.ts | 3 ++- 7 files changed, 25 insertions(+), 10 deletions(-) diff --git a/backend/benefit/applications/api/v1/serializers/application.py b/backend/benefit/applications/api/v1/serializers/application.py index 26767a3b30..a29f24561b 100755 --- a/backend/benefit/applications/api/v1/serializers/application.py +++ b/backend/benefit/applications/api/v1/serializers/application.py @@ -703,13 +703,18 @@ def _validate_de_minimis_aid_set( ) def _validate_date_range_on_submit(self, start_date, end_date): - if start_date < date(date.today().year, 1, 1): + four_months_ago = date.today() - relativedelta(months=4) + if start_date < four_months_ago: raise serializers.ValidationError( - {"start_date": _("start_date must not be in a past year")} + { + "start_date": _( + "start_date must not be more than 4 months in the past" + ) + } ) - if end_date < date(date.today().year, 1, 1): + if end_date < start_date: raise serializers.ValidationError( - {"end_date": _("end_date must not be in a past year")} + {"end_date": _("application end_date can not be less than start_date")} ) def _validate_date_range(self, start_date, end_date, benefit_type): diff --git a/backend/benefit/applications/tests/test_applications_api.py b/backend/benefit/applications/tests/test_applications_api.py index 40faafd2f1..fbe1c3a7f8 100755 --- a/backend/benefit/applications/tests/test_applications_api.py +++ b/backend/benefit/applications/tests/test_applications_api.py @@ -977,7 +977,7 @@ def test_application_date_range( ( "2021-01-01", "2021-02-28", - 200, + 400, ), # start_date in current year (relative to freeze_time date) ( "2022-12-31", diff --git a/backend/benefit/locale/en/LC_MESSAGES/django.po b/backend/benefit/locale/en/LC_MESSAGES/django.po index fe7b898ff9..4e2c1967a8 100644 --- a/backend/benefit/locale/en/LC_MESSAGES/django.po +++ b/backend/benefit/locale/en/LC_MESSAGES/django.po @@ -104,6 +104,9 @@ msgstr "" msgid "start_date must not be in a past year" msgstr "" +msgid "start_date must not be more than 4 months in the past" +msgstr "" + msgid "end_date must not be in a past year" msgstr "" diff --git a/backend/benefit/locale/fi/LC_MESSAGES/django.po b/backend/benefit/locale/fi/LC_MESSAGES/django.po index 026be54f68..6ad344ae32 100644 --- a/backend/benefit/locale/fi/LC_MESSAGES/django.po +++ b/backend/benefit/locale/fi/LC_MESSAGES/django.po @@ -109,6 +109,9 @@ msgstr "De minimis -tuen kokonaismäärä liian suuri" msgid "start_date must not be in a past year" msgstr "Aloituspäivä ei saa olla menneessä vuodessa" +msgid "start_date must not be more than 4 months in the past" +msgstr "Aloituspäivä voi olla enintään 4 kuukautta menneisyydessä" + msgid "end_date must not be in a past year" msgstr "Päättymispäivä ei saa olla menneessä vuodessa" diff --git a/backend/benefit/locale/sv/LC_MESSAGES/django.po b/backend/benefit/locale/sv/LC_MESSAGES/django.po index d39a55e82c..b2546bd00b 100644 --- a/backend/benefit/locale/sv/LC_MESSAGES/django.po +++ b/backend/benefit/locale/sv/LC_MESSAGES/django.po @@ -107,6 +107,9 @@ msgstr "Totalt belopp av de minimis-stöd är för stort" msgid "start_date must not be in a past year" msgstr "Startdatum får inte vara under föregående år" +msgid "start_date must not be more than 4 months in the past" +msgstr "Startdatumet får inte ligga mer än fyra månader bakåt i tiden" + msgid "end_date must not be in a past year" msgstr "Slutdatum får inte vara under föregående år" diff --git a/frontend/benefit/applicant/src/components/applications/forms/application/step2/utils/validation.ts b/frontend/benefit/applicant/src/components/applications/forms/application/step2/utils/validation.ts index b503a8131d..b48c7c95e7 100644 --- a/frontend/benefit/applicant/src/components/applications/forms/application/step2/utils/validation.ts +++ b/frontend/benefit/applicant/src/components/applications/forms/application/step2/utils/validation.ts @@ -11,8 +11,8 @@ import { PAY_SUBSIDY_GRANTED, VALIDATION_MESSAGE_KEYS, } from 'benefit-shared/constants'; -import { validateDateIsFromCurrentYearOnwards } from 'benefit-shared/utils/dates'; -import startOfYear from 'date-fns/startOfYear'; +import { validateIsTodayOrPastDate } from 'benefit-shared/utils/dates'; +import subMonths from 'date-fns/subMonths'; import { FinnishSSN } from 'finnish-ssn'; import { TFunction } from 'next-i18next'; import { NAMES_REGEX } from 'shared/constants'; @@ -45,9 +45,9 @@ export const getValidationSchema = ( .required(t(VALIDATION_MESSAGE_KEYS.REQUIRED)) .test({ message: t(VALIDATION_MESSAGE_KEYS.DATE_MIN, { - min: convertToUIDateFormat(startOfYear(new Date())), + min: convertToUIDateFormat(subMonths(new Date(), 4)), }), - test: (value = '') => validateDateIsFromCurrentYearOnwards(value), + test: (value = '') => validateIsTodayOrPastDate(value), }), [APPLICATION_FIELDS_STEP2_KEYS.END_DATE]: Yup.string().required( t(VALIDATION_MESSAGE_KEYS.REQUIRED) diff --git a/frontend/benefit/applicant/src/constants.ts b/frontend/benefit/applicant/src/constants.ts index 29da06a39c..b025c21071 100644 --- a/frontend/benefit/applicant/src/constants.ts +++ b/frontend/benefit/applicant/src/constants.ts @@ -4,6 +4,7 @@ import { APPLICATION_FIELDS_STEP2_KEYS, APPLICATION_STATUSES, } from 'benefit-shared/constants'; +import subMonths from 'date-fns/subMonths'; export const IS_CLIENT = typeof window !== 'undefined'; @@ -52,7 +53,7 @@ export const DE_MINIMIS_AID_GRANTED_AT_MIN_DATE = new Date( 1 ); -export const APPLICATION_START_DATE = new Date(new Date().getFullYear(), 0, 1); +export const APPLICATION_START_DATE = subMonths(new Date(), 4); export const APPLICATION_INITIAL_VALUES = { status: APPLICATION_STATUSES.DRAFT,