feat(BOOK-463): Fetching hotel filters from CMS and using these inside the destination pages and select hotel page
* feat(BOOK-463): Fetching hotel filters from CMS and using these inside the destination pages * fix(BOOK-698): fetch hotel filters from CMS on select hotel page Approved-by: Bianca Widstam
This commit is contained in:
6
packages/trpc/lib/routers/hotels/filters/get.ts
Normal file
6
packages/trpc/lib/routers/hotels/filters/get.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { contentstackBaseProcedure } from "../../../procedures"
|
||||
import { getHotelFilters } from "./utils"
|
||||
|
||||
export const get = contentstackBaseProcedure.query(async ({ ctx }) => {
|
||||
return getHotelFilters(ctx.lang)
|
||||
})
|
||||
6
packages/trpc/lib/routers/hotels/filters/index.ts
Normal file
6
packages/trpc/lib/routers/hotels/filters/index.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { router } from "../../.."
|
||||
import { get } from "./get"
|
||||
|
||||
export const filtersRouter = router({
|
||||
get,
|
||||
})
|
||||
61
packages/trpc/lib/routers/hotels/filters/output.ts
Normal file
61
packages/trpc/lib/routers/hotels/filters/output.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { z } from "zod"
|
||||
|
||||
import { Lang } from "@scandic-hotels/common/constants/language"
|
||||
|
||||
import { getCountryFilters } from "./utils"
|
||||
|
||||
export const FILTER_TYPES = ["surroundings", "facility", "country"] as const
|
||||
export type FilterType = (typeof FILTER_TYPES)[number]
|
||||
|
||||
export const hotelFilterSchema = z
|
||||
.object({
|
||||
category: z.enum(FILTER_TYPES),
|
||||
facility_id: z.number().transform((id) => id.toString()),
|
||||
slug: z.string(),
|
||||
title: z.string(),
|
||||
sort_order: z.number().nullish(),
|
||||
})
|
||||
.transform(({ facility_id, title, category, slug, sort_order }) => ({
|
||||
id: facility_id,
|
||||
name: title,
|
||||
filterType: category,
|
||||
slug,
|
||||
sortOrder: sort_order ?? 0,
|
||||
hotelCount: 0,
|
||||
cityCount: 0,
|
||||
}))
|
||||
|
||||
export const hotelFiltersSchema = z
|
||||
.object({
|
||||
all_hotel_filter: z.object({
|
||||
items: z.array(hotelFilterSchema),
|
||||
}),
|
||||
lang: z.nativeEnum(Lang),
|
||||
})
|
||||
.transform((data) => {
|
||||
const filters = data.all_hotel_filter.items
|
||||
|
||||
if (!filters?.length) {
|
||||
return {
|
||||
facilityFilters: [],
|
||||
surroundingsFilters: [],
|
||||
countryFilters: [],
|
||||
}
|
||||
}
|
||||
|
||||
const facilityFilters = filters.filter(
|
||||
(filter) => filter.filterType === "facility"
|
||||
)
|
||||
const surroundingsFilters = filters.filter(
|
||||
(filter) => filter.filterType === "surroundings"
|
||||
)
|
||||
|
||||
return {
|
||||
facilityFilters,
|
||||
surroundingsFilters,
|
||||
countryFilters: getCountryFilters(data.lang),
|
||||
}
|
||||
})
|
||||
|
||||
export type HotelFilters = z.output<typeof hotelFiltersSchema>
|
||||
export type HotelFilter = HotelFilters[keyof HotelFilters][number]
|
||||
98
packages/trpc/lib/routers/hotels/filters/utils.ts
Normal file
98
packages/trpc/lib/routers/hotels/filters/utils.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
import { CountryCode } from "@scandic-hotels/common/constants/country"
|
||||
import { getCacheClient } from "@scandic-hotels/common/dataCache"
|
||||
import { createCounter } from "@scandic-hotels/common/telemetry"
|
||||
|
||||
import { GetHotelFilters } from "../../../graphql/Query/HotelFilters.graphql"
|
||||
import { request } from "../../../graphql/request"
|
||||
import { hotelFiltersSchema } from "./output"
|
||||
|
||||
import type { Lang } from "@scandic-hotels/common/constants/language"
|
||||
|
||||
import type { FilterType, HotelFilter } from "./output"
|
||||
|
||||
export async function getHotelFilters(lang: Lang) {
|
||||
const cacheClient = await getCacheClient()
|
||||
const cacheKey = `${lang}:getHotelFilters`
|
||||
|
||||
return await cacheClient.cacheOrGet(
|
||||
cacheKey,
|
||||
async () => {
|
||||
const getHotelFiltersCounter = createCounter(
|
||||
"trpc.contentstack.hotelFilters.getAll"
|
||||
)
|
||||
const metricsGetHotelFilters = getHotelFiltersCounter.init({ lang })
|
||||
|
||||
metricsGetHotelFilters.start()
|
||||
|
||||
const response = await request<unknown>(
|
||||
GetHotelFilters,
|
||||
{ locale: lang },
|
||||
{ key: `${lang}:hotel_filters`, ttl: "1d" }
|
||||
)
|
||||
|
||||
if (!response.data) {
|
||||
metricsGetHotelFilters.noDataError()
|
||||
return {
|
||||
facilityFilters: [],
|
||||
surroundingsFilters: [],
|
||||
countryFilters: [],
|
||||
}
|
||||
}
|
||||
|
||||
const validatedResponse = hotelFiltersSchema.safeParse({
|
||||
...response.data,
|
||||
lang,
|
||||
})
|
||||
if (!validatedResponse.success) {
|
||||
metricsGetHotelFilters.validationError(validatedResponse.error)
|
||||
return {
|
||||
facilityFilters: [],
|
||||
surroundingsFilters: [],
|
||||
countryFilters: [],
|
||||
}
|
||||
}
|
||||
|
||||
metricsGetHotelFilters.success()
|
||||
|
||||
const { facilityFilters, surroundingsFilters, countryFilters } =
|
||||
validatedResponse.data
|
||||
return {
|
||||
facilityFilters: sortFilters(facilityFilters),
|
||||
surroundingsFilters: sortFilters(surroundingsFilters),
|
||||
countryFilters: sortFilters(countryFilters),
|
||||
}
|
||||
},
|
||||
"1d"
|
||||
)
|
||||
}
|
||||
|
||||
export function getCountryFilters(lang: Lang) {
|
||||
const countryCodes = Object.values(CountryCode)
|
||||
const localizedCountries = new Intl.DisplayNames([lang], { type: "region" })
|
||||
return [...countryCodes]
|
||||
.map((countryCode) => {
|
||||
const localizedCountryName = localizedCountries.of(countryCode)
|
||||
|
||||
return localizedCountryName
|
||||
? {
|
||||
id: countryCode,
|
||||
name: localizedCountryName,
|
||||
slug: countryCode.toLowerCase(),
|
||||
filterType: "country" as FilterType,
|
||||
sortOrder: 0,
|
||||
hotelCount: 0,
|
||||
cityCount: 0,
|
||||
}
|
||||
: null
|
||||
})
|
||||
.filter((filter) => !!filter)
|
||||
}
|
||||
|
||||
// First sort by sortOrder descending. The higher the sortOrder, the earlier it appears.
|
||||
// If sortOrder is the same, sort by name as secondary criterion
|
||||
export function sortFilters(filters: HotelFilter[]) {
|
||||
return [...filters].sort((a, b) => {
|
||||
const orderDiff = b.sortOrder - a.sortOrder
|
||||
return orderDiff === 0 ? a.name.localeCompare(b.name) : orderDiff
|
||||
})
|
||||
}
|
||||
@@ -47,6 +47,7 @@ import { getHotelsByHotelIds } from "./services/getHotelsByHotelIds"
|
||||
import { getLocationsByCountries } from "./services/getLocationsByCountries"
|
||||
import { getPackages } from "./services/getPackages"
|
||||
import { availability } from "./availability"
|
||||
import { filtersRouter } from "./filters"
|
||||
import { locationsRouter } from "./locations"
|
||||
|
||||
import type { HotelListingHotelData } from "../../types/hotel"
|
||||
@@ -331,6 +332,7 @@ export const hotelQueryRouter = router({
|
||||
env.CACHE_TIME_HOTELS
|
||||
)
|
||||
}),
|
||||
filters: filtersRouter,
|
||||
locations: locationsRouter,
|
||||
map: router({
|
||||
city: serviceProcedure
|
||||
|
||||
Reference in New Issue
Block a user