fix(SW-2926): Display alerts in booking flow if date range matches search dates and in hotel page if current date is in date range * fix(SW-2926): Display alerts in booking flow if date range matches search dates and in hotel page if current date is in date range * fix(SW-2926) Updated hotel alerts with respect to booking dates * fix(SW-2926): Optimized code Approved-by: Matilda Landström
43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
import { z } from "zod"
|
|
|
|
import { dt } from "@scandic-hotels/common/dt"
|
|
import { nullableStringValidator } from "@scandic-hotels/common/utils/zod/stringValidator"
|
|
|
|
import { AlertTypeEnum } from "../../../../types/alertType"
|
|
|
|
const specialAlertSchema = z.object({
|
|
description: nullableStringValidator,
|
|
displayInBookingFlow: z.boolean().default(false),
|
|
endDate: nullableStringValidator,
|
|
startDate: nullableStringValidator,
|
|
title: nullableStringValidator,
|
|
type: nullableStringValidator,
|
|
})
|
|
|
|
export const specialAlertsSchema = z
|
|
.array(specialAlertSchema)
|
|
.nullish()
|
|
.transform((arr) => (arr ? arr.filter(Boolean) : []))
|
|
.transform((data) => {
|
|
const now = dt().utc().format("YYYY-MM-DD")
|
|
const filteredAlerts = data.filter((alert) => {
|
|
const hasText = alert.description || alert.title
|
|
const hasDates = alert.startDate && alert.endDate
|
|
if (!hasDates) {
|
|
return hasText
|
|
}
|
|
const isAlertExpired = alert.endDate < now
|
|
return !isAlertExpired && hasText
|
|
})
|
|
return filteredAlerts.map((alert, idx) => ({
|
|
heading: alert.title || null,
|
|
id: `alert-${alert.type}-${idx}`,
|
|
name: alert.type,
|
|
text: alert.description || null,
|
|
type: AlertTypeEnum.Info,
|
|
displayInBookingFlow: alert.displayInBookingFlow,
|
|
endDate: alert.endDate,
|
|
startDate: alert.startDate,
|
|
}))
|
|
})
|