From b20c8ce42b63df469ee61064e2707c3b0f811291 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20J=C3=A4derberg?= Date: Mon, 26 May 2025 09:39:48 +0000 Subject: [PATCH] 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 --- .../routers/autocomplete/destinations.ts | 3 +- .../server/routers/hotels/utils.ts | 144 +++++++++--------- apps/scandic-web/utils/chunk.test.ts | 25 +++ 3 files changed, 98 insertions(+), 74 deletions(-) create mode 100644 apps/scandic-web/utils/chunk.test.ts diff --git a/apps/scandic-web/server/routers/autocomplete/destinations.ts b/apps/scandic-web/server/routers/autocomplete/destinations.ts index 7dacea2ae..35d80a3c9 100644 --- a/apps/scandic-web/server/routers/autocomplete/destinations.ts +++ b/apps/scandic-web/server/routers/autocomplete/destinations.ts @@ -41,7 +41,6 @@ export const getDestinationsAutoCompleteRoute = safeProtectedServiceProcedure .input(destinationsAutoCompleteInputSchema) .query(async ({ ctx, input }): Promise => { 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") } diff --git a/apps/scandic-web/server/routers/hotels/utils.ts b/apps/scandic-web/server/routers/hotels/utils.ts index 2ca8bd260..6447f27e3 100644 --- a/apps/scandic-web/server/routers/hotels/utils.ts +++ b/apps/scandic-web/server/routers/hotels/utils.ts @@ -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["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 diff --git a/apps/scandic-web/utils/chunk.test.ts b/apps/scandic-web/utils/chunk.test.ts new file mode 100644 index 000000000..b446fe1d2 --- /dev/null +++ b/apps/scandic-web/utils/chunk.test.ts @@ -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) + }) +})