Skip to content

Commit

Permalink
fix: HL-1093 year 2024 bug (#2684)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
rikuke and sirtawast authored Jan 3, 2024
1 parent 07efaee commit b5c94c4
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 10 deletions.
13 changes: 9 additions & 4 deletions backend/benefit/applications/api/v1/serializers/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions backend/benefit/locale/en/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""

Expand Down
3 changes: 3 additions & 0 deletions backend/benefit/locale/fi/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
3 changes: 3 additions & 0 deletions backend/benefit/locale/sv/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion frontend/benefit/applicant/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit b5c94c4

Please sign in to comment.