Files
web/packages/trpc/lib/routers/hotels/services/getHotel.ts
Linus Flood 40e1efa81f Merged in feat/SW-3688-nerbyhotels (pull request #3372)
feat(SW-3688): remove nearbyHotels prop/fetch from hotelscheme

* feat(SW-3688): remove nearbyHotels prop/fetch from hotelscheme

* Cleanup


Approved-by: Joakim Jäderberg
2025-12-18 13:43:56 +00:00

96 lines
2.8 KiB
TypeScript

import { getCacheClient } from "@scandic-hotels/common/dataCache"
import { createCounter } from "@scandic-hotels/common/telemetry"
import { env } from "../../../../env/server"
import * as api from "../../../api"
import { cache } from "../../../DUPLICATED/cache"
import { HotelTypeEnum } from "../../../enums/hotelType"
import { badRequestError } from "../../../errors"
import { toApiLang } from "../../../utils"
import { hotelSchema } from "../output"
import type { HotelInput } from "../../../types/hotel"
export const getHotel = cache(
async (input: HotelInput, serviceToken: string) => {
const { language, isCardOnlyPayment } = input
const hotelId = input.hotelId.trim()
const getHotelCounter = createCounter("hotel.getHotel")
const metricsGetHotel = getHotelCounter.init({
hotelId,
language,
isCardOnlyPayment,
})
metricsGetHotel.start()
const cacheClient = await getCacheClient()
const result = await cacheClient.cacheOrGet(
`${language}:hotel:${hotelId}:${!!isCardOnlyPayment}`,
async () => {
/**
* Since API expects the params appended and not just
* a comma separated string we need to initialize the
* SearchParams with a sequence of pairs
* (include=City&include=NearbyHotels&include=Restaurants etc.)
**/
const params = new URLSearchParams([
["include", "AdditionalData"],
["include", "City"],
["include", "Restaurants"],
["include", "RoomCategories"],
["language", toApiLang(language)],
])
const apiResponse = await api.get(
api.endpoints.v1.Hotel.Hotels.hotel(hotelId),
{
headers: {
Authorization: `Bearer ${serviceToken}`,
},
},
params
)
if (!apiResponse.ok) {
await metricsGetHotel.httpError(apiResponse)
return null
}
const apiJson = await apiResponse.json()
const validateHotelData = hotelSchema.safeParse(apiJson)
if (!validateHotelData.success) {
metricsGetHotel.validationError(validateHotelData.error)
throw badRequestError()
}
const hotelData = validateHotelData.data
if (isCardOnlyPayment) {
hotelData.hotel.merchantInformationData.alternatePaymentOptions = []
}
const gallery = hotelData.additionalData?.gallery
if (gallery) {
const smallerImages = gallery.smallerImages
const hotelGalleryImages =
hotelData.hotel.hotelType === HotelTypeEnum.Signature
? smallerImages.slice(0, 10)
: smallerImages.slice(0, 6)
hotelData.hotel.galleryImages = hotelGalleryImages
}
return hotelData
},
env.CACHE_TIME_HOTELS
)
metricsGetHotel.success()
return result
}
)