Merged in fix/warmup-autocomplete-data (pull request #2212)
warmup autocomplete data * warmup autocomplete data Approved-by: Anton Gunnarsson
This commit is contained in:
@@ -61,6 +61,8 @@ import type {
|
||||
RoomConfiguration,
|
||||
} from "@/types/trpc/routers/hotel/roomAvailability"
|
||||
import type { Endpoint } from "@/lib/api/endpoints"
|
||||
import { chunk } from "@/utils/chunk"
|
||||
import { z } from "zod"
|
||||
|
||||
export function getPoiGroupByCategoryName(category: string | undefined) {
|
||||
if (!category) return PointOfInterestGroupEnum.LOCATION
|
||||
@@ -275,6 +277,7 @@ export async function getLocations({
|
||||
}
|
||||
throw new Error("downstream error")
|
||||
}
|
||||
|
||||
const apiJson = await apiResponse.json()
|
||||
const verifiedLocations = locationsSchema.safeParse(apiJson)
|
||||
if (!verifiedLocations.success) {
|
||||
@@ -283,46 +286,55 @@ export async function getLocations({
|
||||
throw new Error("Unable to parse locations")
|
||||
}
|
||||
|
||||
return await Promise.all(
|
||||
verifiedLocations.data.data.map(async (location) => {
|
||||
if (location.type === "cities") {
|
||||
if (citiesByCountry) {
|
||||
const country = Object.keys(citiesByCountry).find((country) =>
|
||||
citiesByCountry[country].find(
|
||||
(loc) => loc.name === location.name
|
||||
)
|
||||
)
|
||||
if (country) {
|
||||
return {
|
||||
...location,
|
||||
country,
|
||||
}
|
||||
} else {
|
||||
console.info(
|
||||
`Location cannot be found in any of the countries cities`
|
||||
)
|
||||
console.info(location)
|
||||
}
|
||||
}
|
||||
} else if (location.type === "hotels") {
|
||||
if (location.relationships.city?.url) {
|
||||
const city = await getCity({
|
||||
cityUrl: location.relationships.city.url,
|
||||
serviceToken,
|
||||
})
|
||||
if (city) {
|
||||
return deepmerge(location, {
|
||||
relationships: {
|
||||
city,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
const chunkedLocations = chunk(verifiedLocations.data.data, 10)
|
||||
let locations: z.infer<typeof locationsSchema>["data"] = []
|
||||
|
||||
return location
|
||||
})
|
||||
)
|
||||
for (const chunk of chunkedLocations) {
|
||||
locations = [
|
||||
...(await Promise.all(
|
||||
chunk.map(async (location) => {
|
||||
if (location.type === "cities") {
|
||||
if (citiesByCountry) {
|
||||
const country = Object.keys(citiesByCountry).find((country) =>
|
||||
citiesByCountry[country].find(
|
||||
(loc) => loc.name === location.name
|
||||
)
|
||||
)
|
||||
if (country) {
|
||||
return {
|
||||
...location,
|
||||
country,
|
||||
}
|
||||
} else {
|
||||
console.info(
|
||||
`Location cannot be found in any of the countries cities`
|
||||
)
|
||||
console.info(location)
|
||||
}
|
||||
}
|
||||
} else if (location.type === "hotels") {
|
||||
if (location.relationships.city?.url) {
|
||||
const city = await getCity({
|
||||
cityUrl: location.relationships.city.url,
|
||||
serviceToken,
|
||||
})
|
||||
if (city) {
|
||||
return deepmerge(location, {
|
||||
relationships: {
|
||||
city,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return location
|
||||
})
|
||||
)),
|
||||
]
|
||||
}
|
||||
|
||||
return locations
|
||||
},
|
||||
"1d"
|
||||
)
|
||||
@@ -506,40 +518,49 @@ export async function getHotelsByHotelIds({
|
||||
cacheKey,
|
||||
async () => {
|
||||
const hotelPages = await getHotelPageUrls(lang)
|
||||
const hotels = await Promise.all(
|
||||
hotelIds.map(async (hotelId) => {
|
||||
const hotelResponse = await getHotel(
|
||||
{ hotelId, language: lang, isCardOnlyPayment: false },
|
||||
serviceToken
|
||||
)
|
||||
const chunkedHotelIds = chunk(hotelIds, 10)
|
||||
|
||||
if (!hotelResponse) {
|
||||
throw new Error(`Hotel not found: ${hotelId}`)
|
||||
}
|
||||
const hotels: DestinationPagesHotelData[] = []
|
||||
for (const hotelIdChunk of chunkedHotelIds) {
|
||||
hotels.push(
|
||||
...(await Promise.all(
|
||||
hotelIdChunk.map(async (hotelId) => {
|
||||
const hotelResponse = await getHotel(
|
||||
{ hotelId, language: lang, isCardOnlyPayment: false },
|
||||
serviceToken
|
||||
)
|
||||
|
||||
const hotelPage = hotelPages.find((page) => page.hotelId === hotelId)
|
||||
const { hotel, cities } = hotelResponse
|
||||
const data: DestinationPagesHotelData = {
|
||||
hotel: {
|
||||
id: hotel.id,
|
||||
galleryImages: hotel.galleryImages,
|
||||
name: hotel.name,
|
||||
tripadvisor: hotel.ratings?.tripAdvisor?.rating,
|
||||
detailedFacilities: hotel.detailedFacilities || [],
|
||||
location: hotel.location,
|
||||
hotelType: hotel.hotelType,
|
||||
type: hotel.type,
|
||||
address: hotel.address,
|
||||
cityIdentifier: cities?.[0]?.cityIdentifier,
|
||||
hotelDescription: hotel.hotelContent?.texts.descriptions?.short,
|
||||
},
|
||||
url: hotelPage?.url ?? "",
|
||||
}
|
||||
if (!hotelResponse) {
|
||||
throw new Error(`Hotel not found: ${hotelId}`)
|
||||
}
|
||||
|
||||
return { ...data, url: hotelPage?.url ?? null }
|
||||
})
|
||||
)
|
||||
const hotelPage = hotelPages.find(
|
||||
(page) => page.hotelId === hotelId
|
||||
)
|
||||
const { hotel, cities } = hotelResponse
|
||||
const data: DestinationPagesHotelData = {
|
||||
hotel: {
|
||||
id: hotel.id,
|
||||
galleryImages: hotel.galleryImages,
|
||||
name: hotel.name,
|
||||
tripadvisor: hotel.ratings?.tripAdvisor?.rating,
|
||||
detailedFacilities: hotel.detailedFacilities || [],
|
||||
location: hotel.location,
|
||||
hotelType: hotel.hotelType,
|
||||
type: hotel.type,
|
||||
address: hotel.address,
|
||||
cityIdentifier: cities?.[0]?.cityIdentifier,
|
||||
hotelDescription:
|
||||
hotel.hotelContent?.texts.descriptions?.short,
|
||||
},
|
||||
url: hotelPage?.url ?? "",
|
||||
}
|
||||
|
||||
return data
|
||||
})
|
||||
))
|
||||
)
|
||||
}
|
||||
return hotels.filter(
|
||||
(hotel): hotel is DestinationPagesHotelData => !!hotel
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user