118 lines
2.7 KiB
TypeScript
118 lines
2.7 KiB
TypeScript
import { z } from "zod"
|
|
|
|
import { discriminatedUnionArray } from "@/lib/discriminatedUnion"
|
|
import {
|
|
campaignPageHotelListing,
|
|
heroSchema,
|
|
includedHotelsSchema,
|
|
} from "@/server/routers/contentstack/campaignPage/output"
|
|
import {
|
|
linkAndTitleSchema,
|
|
linkConnectionRefs,
|
|
} from "@/server/routers/contentstack/schemas/linkConnection"
|
|
|
|
import { systemSchema } from "../schemas/system"
|
|
|
|
import { CampaignPageEnum } from "@/types/enums/campaignPage"
|
|
|
|
const navigationLinksSchema = z
|
|
.array(linkAndTitleSchema)
|
|
.nullable()
|
|
.transform((data) => {
|
|
if (!data) {
|
|
return null
|
|
}
|
|
|
|
return data
|
|
.filter((item) => !!item.link)
|
|
.map((item) => ({
|
|
url: item.link!.url,
|
|
title: item.title || item.link!.title,
|
|
}))
|
|
})
|
|
|
|
export const blocksSchema = z.discriminatedUnion("__typename", [
|
|
campaignPageHotelListing,
|
|
])
|
|
|
|
const topCampaignSchema = z
|
|
.object({
|
|
heading: z.string(),
|
|
hero: heroSchema,
|
|
included_hotels: includedHotelsSchema,
|
|
blocks: discriminatedUnionArray(blocksSchema.options),
|
|
url: z.string(),
|
|
})
|
|
.transform((data) => {
|
|
const { blocks, included_hotels, ...rest } = data
|
|
const hotelListingBlock = blocks.find(
|
|
(block) =>
|
|
block.__typename === CampaignPageEnum.ContentStack.blocks.HotelListing
|
|
)
|
|
|
|
return {
|
|
...rest,
|
|
hotel_listing: {
|
|
heading: hotelListingBlock?.hotel_listing.heading || "",
|
|
hotelIds: included_hotels,
|
|
},
|
|
}
|
|
})
|
|
|
|
export const campaignOverviewPageSchema = z.object({
|
|
campaign_overview_page: z
|
|
.object({
|
|
title: z.string(),
|
|
header: z.object({
|
|
heading: z.string(),
|
|
preamble: z.string(),
|
|
navigation_links: navigationLinksSchema,
|
|
}),
|
|
top_campaignConnection: z.object({
|
|
edges: z.array(
|
|
z.object({
|
|
node: topCampaignSchema,
|
|
})
|
|
),
|
|
}),
|
|
system: systemSchema.merge(
|
|
z.object({
|
|
created_at: z.string(),
|
|
updated_at: z.string(),
|
|
})
|
|
),
|
|
})
|
|
.transform((data) => {
|
|
const { top_campaignConnection, ...rest } = data
|
|
return {
|
|
...rest,
|
|
topCampaign: top_campaignConnection.edges.map(({ node }) => node)[0],
|
|
}
|
|
}),
|
|
trackingProps: z.object({
|
|
url: z.string(),
|
|
}),
|
|
})
|
|
|
|
/** REFS */
|
|
|
|
const campaignOverviewPageHeaderRefs = z.object({
|
|
navigation_links: z.array(linkConnectionRefs),
|
|
})
|
|
|
|
export const campaignOverviewPageRefsSchema = z.object({
|
|
campaign_overview_page: z.object({
|
|
header: campaignOverviewPageHeaderRefs,
|
|
top_campaignConnection: z.object({
|
|
edges: z.array(
|
|
z.object({
|
|
node: z.object({
|
|
system: systemSchema,
|
|
}),
|
|
})
|
|
),
|
|
}),
|
|
system: systemSchema,
|
|
}),
|
|
})
|