Merged in fix/warmup-autocomplete-data-2 (pull request #2218)
fix: chunked data overwrote it self * fix: chunked data overwrote it self Approved-by: Linus Flood
This commit is contained in:
@@ -41,7 +41,6 @@ export const getDestinationsAutoCompleteRoute = safeProtectedServiceProcedure
|
||||
.input(destinationsAutoCompleteInputSchema)
|
||||
.query(async ({ ctx, input }): Promise<DestinationsAutoCompleteOutput> => {
|
||||
const lang = input.lang || ctx.lang
|
||||
|
||||
const locations: AutoCompleteLocation[] =
|
||||
await getAutoCompleteDestinationsData({
|
||||
lang,
|
||||
@@ -107,6 +106,7 @@ export async function getAutoCompleteDestinationsData({
|
||||
})
|
||||
|
||||
if (!countries) {
|
||||
console.error("Unable to fetch countries")
|
||||
throw new Error("Unable to fetch countries")
|
||||
}
|
||||
|
||||
@@ -142,6 +142,7 @@ export async function getAutoCompleteDestinationsData({
|
||||
!cityUrls ||
|
||||
!countryUrls
|
||||
) {
|
||||
console.error("Unable to fetch location URLs")
|
||||
throw new Error("Unable to fetch location URLs")
|
||||
}
|
||||
|
||||
|
||||
@@ -253,7 +253,6 @@ export async function getLocations({
|
||||
serviceToken: string
|
||||
}) {
|
||||
const cacheClient = await getCacheClient()
|
||||
|
||||
return await cacheClient.cacheOrGet(
|
||||
`${lang}:locations`.toLowerCase(),
|
||||
async () => {
|
||||
@@ -286,53 +285,53 @@ export async function getLocations({
|
||||
console.error(verifiedLocations.error)
|
||||
throw new Error("Unable to parse locations")
|
||||
}
|
||||
|
||||
const chunkedLocations = chunk(verifiedLocations.data.data, 10)
|
||||
|
||||
let locations: z.infer<typeof locationsSchema>["data"] = []
|
||||
|
||||
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
|
||||
)
|
||||
const chunkLocations = 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,
|
||||
},
|
||||
})
|
||||
)
|
||||
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 location
|
||||
})
|
||||
)
|
||||
|
||||
locations.push(...chunkLocations)
|
||||
}
|
||||
|
||||
return locations
|
||||
@@ -523,44 +522,43 @@ export async function getHotelsByHotelIds({
|
||||
|
||||
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 chunkedHotels = await Promise.all(
|
||||
hotelIdChunk.map(async (hotelId) => {
|
||||
const hotelResponse = await getHotel(
|
||||
{ hotelId, language: lang, isCardOnlyPayment: false },
|
||||
serviceToken
|
||||
)
|
||||
|
||||
if (!hotelResponse) {
|
||||
throw new Error(`Hotel not found: ${hotelId}`)
|
||||
}
|
||||
if (!hotelResponse) {
|
||||
throw new Error(`Hotel not found: ${hotelId}`)
|
||||
}
|
||||
|
||||
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 ?? "",
|
||||
}
|
||||
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 data
|
||||
})
|
||||
)
|
||||
|
||||
hotels.push(...chunkedHotels)
|
||||
}
|
||||
return hotels.filter(
|
||||
(hotel): hotel is DestinationPagesHotelData => !!hotel
|
||||
|
||||
25
apps/scandic-web/utils/chunk.test.ts
Normal file
25
apps/scandic-web/utils/chunk.test.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { describe, expect } from "@jest/globals"
|
||||
|
||||
import { chunk } from "./chunk"
|
||||
|
||||
describe("chunk", () => {
|
||||
it("should split an array into equally sized chunks of specified size", () => {
|
||||
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
const size = 3
|
||||
const expected = [
|
||||
[1, 2, 3],
|
||||
[4, 5, 6],
|
||||
[7, 8, 9],
|
||||
]
|
||||
|
||||
expect(chunk(array, size)).toEqual(expected)
|
||||
})
|
||||
|
||||
it("should split an array into equally sized chunks of specified size", () => {
|
||||
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
const size = 4
|
||||
const expected = [[1, 2, 3, 4], [5, 6, 7, 8], [9]]
|
||||
|
||||
expect(chunk(array, size)).toEqual(expected)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user