From b51091237eccb98ca7cdfb99254c5772709b1e0a Mon Sep 17 00:00:00 2001 From: rkontogianni Date: Fri, 17 Jan 2025 17:12:37 +0000 Subject: [PATCH] feat(DTFS2-7706): add getFormattedDate function --- .../cover-end-date/get-cover-end-date.ts | 4 +-- .../cover-end-date/validation.test.ts | 25 +++++++++---------- .../amendments/cover-end-date/validation.ts | 6 ++--- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/gef-ui/server/controllers/amendments/cover-end-date/get-cover-end-date.ts b/gef-ui/server/controllers/amendments/cover-end-date/get-cover-end-date.ts index 8b57237bff..e24e8cc386 100644 --- a/gef-ui/server/controllers/amendments/cover-end-date/get-cover-end-date.ts +++ b/gef-ui/server/controllers/amendments/cover-end-date/get-cover-end-date.ts @@ -50,13 +50,13 @@ export const getCoverEndDate = async (req: GetCoverEndDateRequest, res: Response const currentCoverEndDate: Date | undefined = (amendment.coverEndDate && fromUnixTime(amendment.coverEndDate * 1000)) || undefined; - const coverEndDate: DayMonthYearInput | undefined = currentCoverEndDate && convertDateToDayMonthYearInput(currentCoverEndDate); + const coverEndDateDayMonthYear: DayMonthYearInput | undefined = currentCoverEndDate && convertDateToDayMonthYearInput(currentCoverEndDate); const viewModel: CoverEndDateViewModel = { exporterName: deal.exporter.companyName, cancelUrl: getAmendmentsUrl({ dealId, facilityId, amendmentId, page: PORTAL_AMENDMENT_PAGES.CANCEL }), previousPage: getPreviousPage(PORTAL_AMENDMENT_PAGES.COVER_END_DATE, amendment), - coverEndDate, + coverEndDate: coverEndDateDayMonthYear, }; return res.render('partials/amendments/cover-end-date.njk', viewModel); diff --git a/gef-ui/server/controllers/amendments/cover-end-date/validation.test.ts b/gef-ui/server/controllers/amendments/cover-end-date/validation.test.ts index d3793451d2..8b2d08d620 100644 --- a/gef-ui/server/controllers/amendments/cover-end-date/validation.test.ts +++ b/gef-ui/server/controllers/amendments/cover-end-date/validation.test.ts @@ -1,4 +1,4 @@ -import { applyStandardValidationAndParseDateInput, COVER_END_DATE_MAXIMUM_YEARS_IN_FUTURE } from '@ukef/dtfs2-common'; +import { applyStandardValidationAndParseDateInput, COVER_END_DATE_MAXIMUM_YEARS_IN_FUTURE, DayMonthYearInput } from '@ukef/dtfs2-common'; import { add, format, startOfDay, getUnixTime } from 'date-fns'; import { validateAndParseCoverEndDate } from './validation'; import { mapValidationError } from '../../../utils/map-validation-error'; @@ -6,6 +6,14 @@ import { mapValidationError } from '../../../utils/map-validation-error'; const valueName = 'cover end date'; const valueRef = 'coverEndDate'; +const getFormattedDate = (coverEndDate: Date): DayMonthYearInput => { + return { + day: format(coverEndDate, 'd'), + month: format(coverEndDate, 'M'), + year: format(coverEndDate, 'yyyy'), + }; +}; + describe('validateAndParseCoverEndDate', () => { afterAll(() => { jest.resetAllMocks(); @@ -34,10 +42,7 @@ describe('validateAndParseCoverEndDate', () => { const coverStartDate = add(new Date(), { days: 1 }); // Act - const result = validateAndParseCoverEndDate( - { day: format(coverEndDate, 'd'), month: format(coverEndDate, 'M'), year: format(coverEndDate, 'yyyy') }, - coverStartDate, - ); + const result = validateAndParseCoverEndDate(getFormattedDate(coverEndDate), coverStartDate); // Assert expect(result).toEqual({ @@ -57,10 +62,7 @@ describe('validateAndParseCoverEndDate', () => { const coverStartDate = add(new Date(), { days: 1 }); // Act - const result = validateAndParseCoverEndDate( - { day: format(coverEndDate, 'd'), month: format(coverEndDate, 'M'), year: format(coverEndDate, 'yyyy') }, - coverStartDate, - ); + const result = validateAndParseCoverEndDate(getFormattedDate(coverEndDate), coverStartDate); // Assert expect(result).toEqual({ @@ -80,10 +82,7 @@ describe('validateAndParseCoverEndDate', () => { const coverStartDate = add(new Date(), { days: 1 }); // Act - const result = validateAndParseCoverEndDate( - { day: format(coverEndDate, 'd'), month: format(coverEndDate, 'M'), year: format(coverEndDate, 'yyyy') }, - coverStartDate, - ); + const result = validateAndParseCoverEndDate(getFormattedDate(coverEndDate), coverStartDate); // Assert expect(result).toEqual({ diff --git a/gef-ui/server/controllers/amendments/cover-end-date/validation.ts b/gef-ui/server/controllers/amendments/cover-end-date/validation.ts index e93de6505a..575171a13b 100644 --- a/gef-ui/server/controllers/amendments/cover-end-date/validation.ts +++ b/gef-ui/server/controllers/amendments/cover-end-date/validation.ts @@ -4,9 +4,9 @@ import { ErrorsOrValue } from '../../../types/errors-or-value'; import { mapValidationError } from '../../../utils/map-validation-error'; /** - * @param {DayMonthYearInput} [params.dayMonthYear] - The cover end date as a DayMonthYearInput object - * @param {Date} [params.dayMonthYear] - The cover start date as Date - * @returns ErrorsOrValue the value or errors depending on the validation result + * @param params.dayMonthYear - The cover end date as a DayMonthYearInput object + * @param params.dayMonthYear - The cover start date as Date + * @returns the value or errors depending on the validation result */ export const validateAndParseCoverEndDate = (dayMonthYear: DayMonthYearInput, coverStartDate: Date): ErrorsOrValue => { const errRef = 'coverEndDate';