Feat/SW-3028 hotel page campaigns
* feat(SW-3028): Added query and typings to fetch campaigns by hotelUid * feat(SW-3028): Added components for campaigns to the hotel page * feat(SW-3028): Implemented prioritized campaigns list * chore(SW-3028): Refactor how campaigns are fetched on hotel pages * feat(SW-3028): Added offers/campaigns to tab navigation Approved-by: Matilda Landström
This commit is contained in:
@@ -70,6 +70,34 @@ export const hotelPageSchema = z.object({
|
||||
hotel_page_id: z.string(),
|
||||
title: z.string(),
|
||||
url: z.string(),
|
||||
campaigns: z
|
||||
.object({
|
||||
heading: z.string(),
|
||||
preamble: z.string(),
|
||||
prioritized_campaignsConnection: z.object({
|
||||
edges: z.array(
|
||||
z.object({
|
||||
node: z.object({
|
||||
system: z.object({
|
||||
uid: z.string(),
|
||||
}),
|
||||
}),
|
||||
})
|
||||
),
|
||||
}),
|
||||
})
|
||||
.nullish()
|
||||
.transform((data) => {
|
||||
const prioritizedCampaigns =
|
||||
data?.prioritized_campaignsConnection.edges.map(
|
||||
(edge) => edge.node.system.uid
|
||||
) || []
|
||||
return {
|
||||
heading: data?.heading,
|
||||
preamble: data?.preamble,
|
||||
prioritizedCampaigns,
|
||||
}
|
||||
}),
|
||||
system: systemSchema.merge(
|
||||
z.object({
|
||||
created_at: z.string(),
|
||||
|
||||
@@ -6,7 +6,9 @@ import { GetHotelPage } from "../../../graphql/Query/HotelPage/HotelPage.graphql
|
||||
import { request } from "../../../graphql/request"
|
||||
import { contentstackExtendedProcedureUID } from "../../../procedures"
|
||||
import { generateTag } from "../../../utils/generateTag"
|
||||
import { getCampaignPagesByHotelPageUid } from "../campaignPage/utils"
|
||||
import { hotelPageSchema } from "./output"
|
||||
import { getSortedCampaigns } from "./utils"
|
||||
|
||||
import type { GetHotelPageData } from "../../../types/hotelPage"
|
||||
|
||||
@@ -22,7 +24,7 @@ export const hotelPageQueryRouter = router({
|
||||
|
||||
metricsGetHotelPage.start()
|
||||
|
||||
const response = await request<GetHotelPageData>(
|
||||
const hotelPageResponse = await request<GetHotelPageData>(
|
||||
GetHotelPage,
|
||||
{
|
||||
locale: lang,
|
||||
@@ -33,13 +35,14 @@ export const hotelPageQueryRouter = router({
|
||||
ttl: "max",
|
||||
}
|
||||
)
|
||||
if (!response.data) {
|
||||
const notFoundError = notFound(response)
|
||||
|
||||
if (!hotelPageResponse.data) {
|
||||
const notFoundError = notFound(hotelPageResponse)
|
||||
metricsGetHotelPage.noDataError()
|
||||
throw notFoundError
|
||||
}
|
||||
|
||||
const validatedHotelPage = hotelPageSchema.safeParse(response.data)
|
||||
const validatedHotelPage = hotelPageSchema.safeParse(hotelPageResponse.data)
|
||||
|
||||
if (!validatedHotelPage.success) {
|
||||
metricsGetHotelPage.validationError(validatedHotelPage.error)
|
||||
@@ -48,6 +51,19 @@ export const hotelPageQueryRouter = router({
|
||||
|
||||
metricsGetHotelPage.success()
|
||||
|
||||
return validatedHotelPage.data.hotel_page
|
||||
const hotelCampaigns = await getCampaignPagesByHotelPageUid(uid, lang)
|
||||
|
||||
const hotelPage = validatedHotelPage.data.hotel_page
|
||||
const { prioritizedCampaigns, ...campaignsBlockContent } =
|
||||
hotelPage.campaigns
|
||||
return {
|
||||
...hotelPage,
|
||||
campaignsBlock: hotelCampaigns?.length
|
||||
? {
|
||||
...campaignsBlockContent,
|
||||
campaigns: getSortedCampaigns(prioritizedCampaigns, hotelCampaigns),
|
||||
}
|
||||
: null,
|
||||
}
|
||||
}),
|
||||
})
|
||||
|
||||
@@ -7,6 +7,7 @@ import { batchedHotelPageUrlsSchema, hotelPageCountSchema } from "./output"
|
||||
|
||||
import type { Lang } from "@scandic-hotels/common/constants/language"
|
||||
|
||||
import type { Campaigns } from "../../../types/campaignPage"
|
||||
import type {
|
||||
GetHotelPageCountData,
|
||||
GetHotelPageUrlsData,
|
||||
@@ -97,3 +98,16 @@ export async function getHotelPageUrls(lang: Lang) {
|
||||
|
||||
return validatedResponse.data
|
||||
}
|
||||
|
||||
export function getSortedCampaigns(
|
||||
prioritizedCampaignUids: string[],
|
||||
campaigns: Campaigns
|
||||
) {
|
||||
const prioritizedSet = new Set(prioritizedCampaignUids)
|
||||
const prioritized = prioritizedCampaignUids.flatMap((id) => {
|
||||
const found = campaigns.find((c) => c.id === id)
|
||||
return found ? [found] : []
|
||||
})
|
||||
const others = campaigns.filter((c) => !prioritizedSet.has(c.id))
|
||||
return [...prioritized, ...others]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user