Files
web/server/routers/contentstack/destinationCityPage/utils.ts
Erik Tiekstra eb0b88f8ea Merged in feat/SW-1584-destination-content-blocks (pull request #1278)
Feat/SW-1584 destination content blocks

* feat(SW-1584): Added accordion and content blocks to destination city pages

* feat(SW-1584): Added accordion and content blocks to destination country pages


Approved-by: Matilda Landström
2025-02-10 09:28:32 +00:00

85 lines
2.5 KiB
TypeScript

import { generateTag, generateTagsFromSystem } from "@/utils/generateTag"
import { getHotel } from "../../hotels/query"
import { getHotelIdsByCityIdentifier } from "../../hotels/utils"
import { getHotelPageUrl } from "../hotelPage/utils"
import { DestinationCityPageEnum } from "@/types/enums/destinationCityPage"
import type { HotelData } from "@/types/hotel"
import type { System } from "@/types/requests/system"
import type { DestinationCityPageRefs } from "@/types/trpc/routers/contentstack/destinationCityPage"
import type { Lang } from "@/constants/languages"
export function generatePageTags(
validatedData: DestinationCityPageRefs,
lang: Lang
): string[] {
const connections = getConnections(validatedData)
return [
generateTagsFromSystem(lang, connections),
generateTag(lang, validatedData.destination_city_page.system.uid),
].flat()
}
export function getConnections({
destination_city_page,
}: DestinationCityPageRefs) {
const connections: System["system"][] = [destination_city_page.system]
if (destination_city_page.blocks) {
destination_city_page.blocks.forEach((block) => {
switch (block.__typename) {
case DestinationCityPageEnum.ContentStack.blocks.Accordion: {
if (block.accordion.length) {
connections.push(...block.accordion)
}
break
}
case DestinationCityPageEnum.ContentStack.blocks.Content:
{
if (block.content.length) {
// TS has trouble infering the filtered types
// @ts-ignore
connections.push(...block.content)
}
}
break
}
})
}
if (destination_city_page.sidepeek_content) {
destination_city_page.sidepeek_content.content.embedded_itemsConnection.edges.forEach(
({ node }) => {
connections.push(node.system)
}
)
}
return connections
}
export async function getHotelListData(
lang: Lang,
serviceToken: string,
cityIdentifier: string
) {
const hotelIds = await getHotelIdsByCityIdentifier(
cityIdentifier,
serviceToken
)
const hotels = await Promise.all(
hotelIds.map(async (hotelId) => {
const [hotelData, url] = await Promise.all([
getHotel({ hotelId, language: lang }, serviceToken),
getHotelPageUrl(lang, hotelId),
])
return hotelData ? { ...hotelData, url } : null
})
)
return hotels.filter(
(hotel): hotel is HotelData & { url: string | null } => !!hotel
)
}