From c8c0a41ed8bd8b1018de84c793b5a56b4988c822 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20J=C3=A4derberg?= Date: Fri, 22 Nov 2024 10:30:33 +0100 Subject: [PATCH] fix: move date logic to its own file with tests --- .../select-rate/getValidDates.test.ts | 51 +++++++++++++++++ .../(standard)/select-rate/getValidDates.ts | 55 +++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 app/[lang]/(live)/(public)/hotelreservation/(standard)/select-rate/getValidDates.test.ts create mode 100644 app/[lang]/(live)/(public)/hotelreservation/(standard)/select-rate/getValidDates.ts diff --git a/app/[lang]/(live)/(public)/hotelreservation/(standard)/select-rate/getValidDates.test.ts b/app/[lang]/(live)/(public)/hotelreservation/(standard)/select-rate/getValidDates.test.ts new file mode 100644 index 000000000..2b4478964 --- /dev/null +++ b/app/[lang]/(live)/(public)/hotelreservation/(standard)/select-rate/getValidDates.test.ts @@ -0,0 +1,51 @@ +import { beforeAll, describe, expect, it } from "@jest/globals" + +import { getValidFromDate, getValidToDate } from "./getValidDates" + +const NOW = new Date("2020-10-01T00:00:00Z") +describe("getValidFromDate", () => { + beforeAll(() => { + jest.useFakeTimers({ now: NOW }) + }) + + it("returns today when empty string is provided", () => { + const actual = getValidFromDate("") + expect(actual.toISOString()).toBe("2020-10-01T00:00:00.000Z") + }) + + it("returns today when undefined is provided", () => { + const actual = getValidFromDate(undefined) + expect(actual.toISOString()).toBe("2020-10-01T00:00:00.000Z") + }) + + it("returns given date in utc", () => { + const actual = getValidFromDate("2024-01-01") + expect(actual.toISOString()).toBe("2023-12-31T23:00:00.000Z") + }) +}) + +describe("getValidToDate", () => { + beforeAll(() => { + jest.useFakeTimers({ now: NOW }) + }) + + it("returns day after fromDate when empty string is provided", () => { + const actual = getValidToDate("", NOW) + expect(actual.toISOString()).toBe("2020-10-02T00:00:00.000Z") + }) + + it("returns day after fromDate when undefined is provided", () => { + const actual = getValidToDate(undefined, NOW) + expect(actual.toISOString()).toBe("2020-10-02T00:00:00.000Z") + }) + + it("returns given date in utc", () => { + const actual = getValidToDate("2024-01-01", NOW) + expect(actual.toISOString()).toBe("2023-12-31T23:00:00.000Z") + }) + + it("fallsback to day after fromDate when given date is before fromDate", () => { + const actual = getValidToDate("2020-09-30", NOW) + expect(actual.toISOString()).toBe("2020-10-02T00:00:00.000Z") + }) +}) diff --git a/app/[lang]/(live)/(public)/hotelreservation/(standard)/select-rate/getValidDates.ts b/app/[lang]/(live)/(public)/hotelreservation/(standard)/select-rate/getValidDates.ts new file mode 100644 index 000000000..d9bcbf09e --- /dev/null +++ b/app/[lang]/(live)/(public)/hotelreservation/(standard)/select-rate/getValidDates.ts @@ -0,0 +1,55 @@ +import { Dayjs } from "dayjs" + +import { dt } from "@/lib/dt" + +/** + * Get valid dates from stringFromDate and stringToDate making sure that they are not in the past and chronologically correct + * @example const { fromDate, toDate} = getValidDates("2021-01-01", "2021-01-02") + */ +export function getValidDates( + stringFromDate: string | undefined, + stringToDate: string | undefined +): { fromDate: Dayjs; toDate: Dayjs } { + const fromDate = getValidFromDate(stringFromDate) + const toDate = getValidToDate(stringToDate, fromDate) + + return { fromDate, toDate } +} + +/** + * Get valid fromDate from stringFromDate making sure that it is not in the past + */ +export function getValidFromDate(stringFromDate: string | undefined): Dayjs { + const now = dt().utc() + if (!stringFromDate) { + return now + } + const toDate = dt(stringFromDate) + + const yesterday = now.subtract(1, "day") + if (!toDate.isAfter(yesterday)) { + return now + } + + return toDate +} + +/** + * Get valid toDate from stringToDate making sure that it is after fromDate + */ +export function getValidToDate( + stringToDate: string | undefined, + fromDate: Dayjs | Date +): Dayjs { + const tomorrow = dt().utc().add(1, "day") + if (!stringToDate) { + return tomorrow + } + + const toDate = dt(stringToDate) + if (toDate.isAfter(fromDate)) { + return toDate + } + + return tomorrow +}