feat(SW-237): Detached booking widget toggle graphql query from page settings
This commit is contained in:
5
server/routers/contentstack/bookingwidget/index.ts
Normal file
5
server/routers/contentstack/bookingwidget/index.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { mergeRouters } from "@/server/trpc"
|
||||
|
||||
import { bookingwidgetQueryRouter } from "./query"
|
||||
|
||||
export const bookingwidgetRouter = mergeRouters(bookingwidgetQueryRouter)
|
||||
21
server/routers/contentstack/bookingwidget/output.ts
Normal file
21
server/routers/contentstack/bookingwidget/output.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { z } from "zod"
|
||||
|
||||
const bookingWidgetToggleSchema = z
|
||||
.object({
|
||||
page_settings: z.object({
|
||||
hide_booking_widget: z.boolean(),
|
||||
}),
|
||||
})
|
||||
.optional()
|
||||
|
||||
export const validateBookingWidgetToggleSchema = z.object({
|
||||
account_page: bookingWidgetToggleSchema,
|
||||
loyalty_page: bookingWidgetToggleSchema,
|
||||
content_page: bookingWidgetToggleSchema,
|
||||
hotel_page: bookingWidgetToggleSchema,
|
||||
current_blocks_page: bookingWidgetToggleSchema,
|
||||
})
|
||||
|
||||
export type ValidateBookingWidgetToggleType = z.infer<
|
||||
typeof validateBookingWidgetToggleSchema
|
||||
>
|
||||
92
server/routers/contentstack/bookingwidget/query.ts
Normal file
92
server/routers/contentstack/bookingwidget/query.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
import { ValueOf } from "next/dist/shared/lib/constants"
|
||||
|
||||
import {
|
||||
GetAccountPageSettings,
|
||||
GetContentPageSettings,
|
||||
GetCurrentBlocksPageSettings,
|
||||
GetHotelPageSettings,
|
||||
GetLoyaltyPageSettings,
|
||||
} from "@/lib/graphql/Query/BookingWidgetToggle.graphql"
|
||||
import { request } from "@/lib/graphql/request"
|
||||
import { contentstackExtendedProcedureUID, router } from "@/server/trpc"
|
||||
|
||||
import { generateTag } from "@/utils/generateTag"
|
||||
|
||||
import {
|
||||
validateBookingWidgetToggleSchema,
|
||||
ValidateBookingWidgetToggleType,
|
||||
} from "./output"
|
||||
|
||||
import { ContentTypeEnum } from "@/types/requests/contentType"
|
||||
|
||||
export const bookingwidgetQueryRouter = router({
|
||||
getToggle: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
|
||||
const failedResponse = { hideBookingWidget: false }
|
||||
const { contentType, uid, lang } = ctx
|
||||
|
||||
// This condition is to handle 404 page case
|
||||
if (!contentType || !uid) {
|
||||
console.log("No proper params defined: ", contentType, uid)
|
||||
return failedResponse
|
||||
}
|
||||
let GetPageSettings = ""
|
||||
const contentTypeCMS = <ValueOf<typeof ContentTypeEnum>>(
|
||||
contentType.replaceAll("-", "_")
|
||||
)
|
||||
|
||||
switch (contentTypeCMS) {
|
||||
case ContentTypeEnum.accountPage:
|
||||
GetPageSettings = GetAccountPageSettings
|
||||
break
|
||||
case ContentTypeEnum.loyaltyPage:
|
||||
GetPageSettings = GetLoyaltyPageSettings
|
||||
break
|
||||
case ContentTypeEnum.contentPage:
|
||||
GetPageSettings = GetContentPageSettings
|
||||
break
|
||||
case ContentTypeEnum.hotelPage:
|
||||
GetPageSettings = GetHotelPageSettings
|
||||
break
|
||||
case ContentTypeEnum.currentBlocksPage:
|
||||
GetPageSettings = GetCurrentBlocksPageSettings
|
||||
break
|
||||
}
|
||||
|
||||
if (!GetPageSettings) {
|
||||
console.error("No proper Content type defined: ", contentType)
|
||||
return failedResponse
|
||||
}
|
||||
|
||||
const response = await request<ValidateBookingWidgetToggleType>(
|
||||
GetPageSettings,
|
||||
{
|
||||
uid: uid,
|
||||
locale: lang,
|
||||
},
|
||||
{
|
||||
next: {
|
||||
tags: [generateTag(lang, uid, "bookingwidget")],
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
const bookingWidgetToggleData = validateBookingWidgetToggleSchema.safeParse(
|
||||
response.data
|
||||
)
|
||||
if (!bookingWidgetToggleData.success) {
|
||||
console.error(
|
||||
"Flag hide_booking_widget fetch error: ",
|
||||
bookingWidgetToggleData.error
|
||||
)
|
||||
return failedResponse
|
||||
}
|
||||
|
||||
const hideBookingWidget =
|
||||
bookingWidgetToggleData.data[contentTypeCMS]?.page_settings
|
||||
?.hide_booking_widget
|
||||
|
||||
return {
|
||||
hideBookingWidget,
|
||||
}
|
||||
}),
|
||||
})
|
||||
Reference in New Issue
Block a user