40 lines
1.0 KiB
TypeScript
40 lines
1.0 KiB
TypeScript
import { z } from "zod"
|
|
|
|
import { dt } from "@/lib/dt"
|
|
|
|
import { AlertTypeEnum } from "@/types/enums/alert"
|
|
|
|
const specialAlertSchema = z.object({
|
|
type: z.string(),
|
|
title: z.string().optional(),
|
|
description: z.string().optional(),
|
|
displayInBookingFlow: z.boolean(),
|
|
startDate: z.string().optional(),
|
|
endDate: z.string().optional(),
|
|
})
|
|
|
|
export const specialAlertsSchema = z
|
|
.array(specialAlertSchema)
|
|
.transform((data) => {
|
|
const now = dt().utc().format("YYYY-MM-DD")
|
|
const filteredAlerts = data.filter((alert) => {
|
|
let shouldShowNow = true
|
|
|
|
if (alert.startDate && alert.startDate > now) {
|
|
shouldShowNow = false
|
|
}
|
|
if (alert.endDate && alert.endDate < now) {
|
|
shouldShowNow = false
|
|
}
|
|
const hasText = alert.description || alert.title
|
|
return shouldShowNow && hasText
|
|
})
|
|
return filteredAlerts.map((alert, idx) => ({
|
|
id: `alert-${alert.type}-${idx}`,
|
|
type: AlertTypeEnum.Info,
|
|
heading: alert.title || null,
|
|
text: alert.description || null,
|
|
}))
|
|
})
|
|
.default([])
|