Merged in fix/sw-3299-alerts (pull request #2692)
fix(SW-3299): hotel alerts now visible if booking dates starts/ends before the alert period * fix(SW-3299): hotel alerts now visible if booking dates starts/ends before the alert period Approved-by: Bianca Widstam Approved-by: Hrishikesh Vaipurkar
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
import { describe, expect, it } from "vitest"
|
||||
|
||||
import { dt } from "@scandic-hotels/common/dt"
|
||||
import { AlertTypeEnum } from "@scandic-hotels/trpc/types/alert"
|
||||
|
||||
import { getHotelAlertsForBookingDates } from "./index"
|
||||
|
||||
import type { specialAlertsSchema } from "@scandic-hotels/trpc/routers/hotels/schemas/hotel/specialAlerts"
|
||||
import type { z } from "zod"
|
||||
|
||||
type Alert = z.infer<typeof specialAlertsSchema>[number]
|
||||
|
||||
function makeAlert(start: string, end: string): Alert {
|
||||
return {
|
||||
id: "test-id",
|
||||
name: "Test Alert",
|
||||
heading: "Test Heading",
|
||||
text: "Some text",
|
||||
type: AlertTypeEnum.Alarm,
|
||||
displayInBookingFlow: true,
|
||||
startDate: start,
|
||||
endDate: end,
|
||||
}
|
||||
}
|
||||
|
||||
describe("getHotelAlertsForBookingDates", () => {
|
||||
const alert = makeAlert("2025-09-01", "2025-09-10")
|
||||
|
||||
it("shows alert if booking starts inside alert", () => {
|
||||
const result = getHotelAlertsForBookingDates(
|
||||
[alert],
|
||||
dt("2025-09-05"),
|
||||
dt("2025-09-12")
|
||||
)
|
||||
expect(result).toHaveLength(1)
|
||||
})
|
||||
|
||||
it("shows alert if booking ends inside alert", () => {
|
||||
const result = getHotelAlertsForBookingDates(
|
||||
[alert],
|
||||
dt("2025-08-28"),
|
||||
dt("2025-09-05")
|
||||
)
|
||||
expect(result).toHaveLength(1)
|
||||
})
|
||||
|
||||
it("shows alert if booking fully contains alert", () => {
|
||||
const result = getHotelAlertsForBookingDates(
|
||||
[alert],
|
||||
dt("2025-08-28"),
|
||||
dt("2025-09-15")
|
||||
)
|
||||
expect(result).toHaveLength(1)
|
||||
})
|
||||
|
||||
it("shows alert if alert fully contains booking", () => {
|
||||
const result = getHotelAlertsForBookingDates(
|
||||
[alert],
|
||||
dt("2025-09-03"),
|
||||
dt("2025-09-05")
|
||||
)
|
||||
expect(result).toHaveLength(1)
|
||||
})
|
||||
|
||||
it("does not show alert if no overlap", () => {
|
||||
const result = getHotelAlertsForBookingDates(
|
||||
[alert],
|
||||
dt("2025-08-01"),
|
||||
dt("2025-08-05")
|
||||
)
|
||||
expect(result).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
@@ -95,24 +95,33 @@ export function getHotelAlertsForBookingDates(
|
||||
) {
|
||||
return specialAlerts.filter((alert) => {
|
||||
if (alert.endDate && alert.startDate) {
|
||||
const alertEndDate = dt(alert.endDate)
|
||||
const alertStartDate = dt(alert.startDate)
|
||||
const alertEndDate = dt(alert.endDate)
|
||||
const bookingStart = dt(fromDate)
|
||||
const bookingEnd = dt(toDate)
|
||||
|
||||
const fromDateIsBetweenAlertDates = dt(fromDate).isBetween(
|
||||
const fromDateIsBetweenAlertDates = bookingStart.isBetween(
|
||||
alertStartDate,
|
||||
alertEndDate,
|
||||
"date",
|
||||
"[]"
|
||||
)
|
||||
const toDateIsBetweenAlertDates = dt(toDate).isBetween(
|
||||
const toDateIsBetweenAlertDates = bookingEnd.isBetween(
|
||||
alertStartDate,
|
||||
alertEndDate,
|
||||
"date",
|
||||
"[]"
|
||||
)
|
||||
const bookingSpanIsBetweenAlertDates =
|
||||
fromDateIsBetweenAlertDates || toDateIsBetweenAlertDates
|
||||
return bookingSpanIsBetweenAlertDates
|
||||
|
||||
const bookingFullyContainsAlert =
|
||||
bookingStart.isSameOrBefore(alertStartDate, "date") &&
|
||||
bookingEnd.isSameOrAfter(alertEndDate, "date")
|
||||
|
||||
return (
|
||||
fromDateIsBetweenAlertDates ||
|
||||
toDateIsBetweenAlertDates ||
|
||||
bookingFullyContainsAlert
|
||||
)
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user