Skip to content

Commit

Permalink
feat(DTFS2-7706): add getFormattedDate function
Browse files Browse the repository at this point in the history
  • Loading branch information
rkontogianni committed Jan 17, 2025
1 parent 8167fb2 commit b510912
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
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';

const valueName = 'cover end date';
const valueRef = 'coverEndDate';

const getFormattedDate = (coverEndDate: Date): DayMonthYearInput => {
return {
day: format(coverEndDate, 'd'),
month: format(coverEndDate, 'M'),

Check failure on line 12 in gef-ui/server/controllers/amendments/cover-end-date/validation.test.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

gef-ui/server/controllers/amendments/cover-end-date/validation.test.ts#L12

Unsafe call of an `error` type typed value.
year: format(coverEndDate, 'yyyy'),

Check failure on line 13 in gef-ui/server/controllers/amendments/cover-end-date/validation.test.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

gef-ui/server/controllers/amendments/cover-end-date/validation.test.ts#L13

Unsafe call of an `error` type typed value.
};
};

describe('validateAndParseCoverEndDate', () => {
afterAll(() => {
jest.resetAllMocks();
Expand Down Expand Up @@ -34,10 +42,7 @@ describe('validateAndParseCoverEndDate', () => {
const coverStartDate = add(new Date(), { days: 1 });

Check failure on line 42 in gef-ui/server/controllers/amendments/cover-end-date/validation.test.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

gef-ui/server/controllers/amendments/cover-end-date/validation.test.ts#L42

Unsafe assignment of an error typed value.

// 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({
Expand All @@ -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({
Expand All @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<UnixTimestampSeconds> 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<UnixTimestampSeconds> => {
const errRef = 'coverEndDate';
Expand Down

0 comments on commit b510912

Please sign in to comment.