Files
web/server/routers/contentstack/hotelPage/query.ts
2024-08-23 09:46:19 +02:00

42 lines
1.2 KiB
TypeScript

import { GetHotelPage } from "@/lib/graphql/Query/HotelPage.graphql"
import { request } from "@/lib/graphql/request"
import { notFound } from "@/server/errors/trpc"
import { contentstackBaseProcedure, router } from "@/server/trpc"
import { HotelPage, HotelPageDataRaw, validateHotelPageSchema } from "./output"
export const hotelPageQueryRouter = router({
get: contentstackBaseProcedure.query(async ({ ctx }) => {
const { lang, uid } = ctx
console.info("contentstack.hotelPage start ", JSON.stringify({ lang, uid }))
const response = await request<HotelPageDataRaw>(GetHotelPage, {
locale: lang,
uid,
})
if (!response.data) {
console.error(
"contentstack.hotelPage not found error",
JSON.stringify({ lang, uid })
)
throw notFound(response)
}
const validatedHotelPage = validateHotelPageSchema.safeParse(response.data)
if (!validatedHotelPage.success) {
console.error(
"contentstack.hotelPage validation error",
JSON.stringify({ lang, uid }),
JSON.stringify(validatedHotelPage.error)
)
return null
}
const hotelPage = {
...validatedHotelPage.data.hotel_page,
} as HotelPage
return hotelPage
}),
})