Skip to content

Commit

Permalink
fix: start_date can now be in the future (#2686)
Browse files Browse the repository at this point in the history
  • Loading branch information
rikuke authored Jan 3, 2024
1 parent b5c94c4 commit 4137a9c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
getMaxEndDate,
getMinEndDate,
validateDateWithinFourMonths,
} from '@frontend/benefit-shared/src/utils/dates';
import { BENEFIT_TYPES } from 'benefit-shared/constants';

Expand Down Expand Up @@ -63,4 +64,35 @@ describe('dates', () => {
expect(maxEndDate2).toBeUndefined();
});
});

describe('validateDateWithinFourMonths', () => {
it('should return true when date is exactly four months ago', () => {
const fourMonthsAgo = new Date();
fourMonthsAgo.setMonth(fourMonthsAgo.getMonth() - 4);

expect(validateDateWithinFourMonths(fourMonthsAgo)).toBe(true);
});

it('should return true when date is within four months', () => {
const twoMonthsAgo = new Date();
twoMonthsAgo.setMonth(twoMonthsAgo.getMonth() - 2);

expect(validateDateWithinFourMonths(twoMonthsAgo)).toBe(true);
});

it('should return true when date is two months in the future', () => {
const twoMonthsInTheFuture = new Date();
twoMonthsInTheFuture.setMonth(twoMonthsInTheFuture.getMonth() + 2);

expect(validateDateWithinFourMonths(twoMonthsInTheFuture)).toBe(true);
});


it('should return false when date is more than four months in the past', () => {
const fiveMonthsAgo = new Date();
fiveMonthsAgo.setMonth(fiveMonthsAgo.getMonth() - 5);

expect(validateDateWithinFourMonths(fiveMonthsAgo)).toBe(false);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
PAY_SUBSIDY_GRANTED,
VALIDATION_MESSAGE_KEYS,
} from 'benefit-shared/constants';
import { validateIsTodayOrPastDate } from 'benefit-shared/utils/dates';
import { validateDateWithinFourMonths } from 'benefit-shared/utils/dates';
import subMonths from 'date-fns/subMonths';
import { FinnishSSN } from 'finnish-ssn';
import { TFunction } from 'next-i18next';
Expand Down Expand Up @@ -47,7 +47,7 @@ export const getValidationSchema = (
message: t(VALIDATION_MESSAGE_KEYS.DATE_MIN, {
min: convertToUIDateFormat(subMonths(new Date(), 4)),
}),
test: (value = '') => validateIsTodayOrPastDate(value),
test: (value = '') => validateDateWithinFourMonths(value),
}),
[APPLICATION_FIELDS_STEP2_KEYS.END_DATE]: Yup.string().required(
t(VALIDATION_MESSAGE_KEYS.REQUIRED)
Expand Down
18 changes: 15 additions & 3 deletions frontend/benefit/shared/src/utils/dates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import {
APPLICATION_START_DATE,
BENEFIT_TYPES,
} from 'benefit-shared/constants';
import { isFuture, parse, startOfYear } from 'date-fns';
import addMonths from 'date-fns/addMonths';
import subDays from 'date-fns/subDays';
import { addMonths, isAfter, isEqual, isFuture, parse, startOfDay, startOfYear, subDays, subMonths } from 'date-fns';
import { parseDate } from 'shared/utils/date.utils';

export const getMinEndDate = (
Expand Down Expand Up @@ -73,3 +71,17 @@ export const validateIsTodayOrPastDate = (value: string): boolean => {
}
return true;
};

export const validateDateWithinFourMonths = (value: string): boolean => {
const isFinnishDate = validateFinnishDatePattern(value);
const date = isFinnishDate
? parse(value, 'd.M.yyyy', new Date())
: parseDate(value);

if (!date || !date?.toJSON()) {
return false;
}
const startOfDayDate = startOfDay(date);
const fourMonthsAgo = startOfDay(subMonths(new Date(), 4));
return isEqual(startOfDayDate, fourMonthsAgo) || isAfter(startOfDayDate, fourMonthsAgo);
}

0 comments on commit 4137a9c

Please sign in to comment.