Files
web/apps/scandic-web/server/routers/hotels/schemas/hotel/specialAlerts.ts
Matilda Landström be643e68a7 Merged in fix/SW-2980-alert (pull request #2312)
Fix/SW-2980: Only filter out child alert on hotel page

Approved-by: Erik Tiekstra
2025-06-10 07:35:12 +00:00

41 lines
1.2 KiB
TypeScript

import { z } from "zod"
import { dt } from "@/lib/dt"
import { nullableStringValidator } from "@/utils/zod/stringValidator"
import { AlertTypeEnum } from "@/types/enums/alert"
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 shouldShowNow =
alert.startDate && alert.endDate
? alert.startDate <= now && alert.endDate >= now
: true
const hasText = alert.description || alert.title
return shouldShowNow && 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,
}))
})