Merged in chore/remove-enrichHotel-call (pull request #3244)
Chore/remove enrichHotel call * chore: remove enrichHotel api call * include cityId when errors happen * remove unused funciton and cleanup code * tests no longer expect us to have called a function that is not used * remove commented code Approved-by: Linus Flood
This commit is contained in:
@@ -1,16 +1,13 @@
|
||||
import deepmerge from "deepmerge"
|
||||
import { z } from "zod"
|
||||
|
||||
import { getCacheClient } from "@scandic-hotels/common/dataCache"
|
||||
import { createLogger } from "@scandic-hotels/common/logger/createLogger"
|
||||
import { chunk } from "@scandic-hotels/common/utils/chunk"
|
||||
|
||||
import * as api from "../../../api"
|
||||
import { serverErrorByStatus } from "../../../errors"
|
||||
import { toApiLang } from "../../../utils"
|
||||
import { locationCitySchema } from "../schemas/location/city"
|
||||
import { locationHotelSchema } from "../schemas/location/hotel"
|
||||
import { getCity } from "./getCity"
|
||||
|
||||
import type { Country } from "@scandic-hotels/common/constants/country"
|
||||
import type { Lang } from "@scandic-hotels/common/constants/language"
|
||||
@@ -21,6 +18,14 @@ type CitiesNamesByCountry = Record<
|
||||
Country | (string & {}),
|
||||
Array<{ name: string }>
|
||||
> | null
|
||||
type Hotel = Extract<
|
||||
z.infer<typeof locationsSchema>["data"][number],
|
||||
{ type: "hotels" }
|
||||
>
|
||||
type City = Extract<
|
||||
z.infer<typeof locationsSchema>["data"][number],
|
||||
{ type: "cities" }
|
||||
>
|
||||
|
||||
export async function getLocationsByCountries({
|
||||
lang,
|
||||
@@ -73,21 +78,11 @@ export async function getLocationsByCountries({
|
||||
const data = cleanData(verifiedLocations.data.data)
|
||||
const cities = data
|
||||
.filter((x) => x.type === "cities")
|
||||
.map((x) => enrichCity(x, citiesByCountry))
|
||||
.map((city) => addCountryDataToCity(city, citiesByCountry))
|
||||
|
||||
const chunkedHotels = chunk(
|
||||
data.filter((x) => x.type === "hotels"),
|
||||
10
|
||||
)
|
||||
const hotels = (
|
||||
await Promise.all(
|
||||
chunkedHotels.flatMap(async (chunk) => {
|
||||
return await Promise.all(
|
||||
chunk.flatMap(async (hotel) => enrichHotel(hotel, serviceToken))
|
||||
)
|
||||
})
|
||||
)
|
||||
).flat()
|
||||
const hotels = data
|
||||
.filter((x) => x.type === "hotels")
|
||||
.map((hotel) => addCityDataToHotel(hotel, cities))
|
||||
|
||||
let locations: z.infer<typeof locationsSchema>["data"] = [
|
||||
...cities,
|
||||
@@ -100,48 +95,32 @@ export async function getLocationsByCountries({
|
||||
)
|
||||
}
|
||||
|
||||
async function enrichHotel(
|
||||
hotel: Extract<
|
||||
z.infer<typeof locationsSchema>["data"][number],
|
||||
{ type: "hotels" }
|
||||
>,
|
||||
serviceToken: string
|
||||
): Promise<
|
||||
Extract<z.infer<typeof locationsSchema>["data"][number], { type: "hotels" }>
|
||||
> {
|
||||
if (hotel.type !== "hotels") {
|
||||
return hotel
|
||||
}
|
||||
|
||||
if (!hotel.relationships.city?.url) {
|
||||
return hotel
|
||||
}
|
||||
const city = await getCity({
|
||||
cityUrl: hotel.relationships.city.url,
|
||||
serviceToken,
|
||||
})
|
||||
|
||||
function addCityDataToHotel(hotel: Hotel, cities: City[]) {
|
||||
const city = cities.find((c) => c.id === hotel.relationships.city.id)
|
||||
if (!city) {
|
||||
hotelUtilsLogger.warn(
|
||||
`City with id ${hotel.relationships.city.id} not found for hotel ${hotel.id}`
|
||||
)
|
||||
return hotel
|
||||
}
|
||||
|
||||
return deepmerge(hotel, {
|
||||
return {
|
||||
...hotel,
|
||||
relationships: {
|
||||
city,
|
||||
city: {
|
||||
...city,
|
||||
cityIdentifier: city.cityIdentifier,
|
||||
url: hotel.relationships.city.url,
|
||||
keywords: city.keyWords ?? [],
|
||||
},
|
||||
},
|
||||
})
|
||||
} satisfies Hotel
|
||||
}
|
||||
|
||||
function enrichCity(
|
||||
city: Extract<
|
||||
z.infer<typeof locationsSchema>["data"][number],
|
||||
{ type: "cities" }
|
||||
>,
|
||||
function addCountryDataToCity(
|
||||
city: City,
|
||||
citiesByCountry: CitiesNamesByCountry | null
|
||||
): Extract<
|
||||
z.infer<typeof locationsSchema>["data"][number],
|
||||
{ type: "cities" }
|
||||
> {
|
||||
): City {
|
||||
if (!citiesByCountry) {
|
||||
return city
|
||||
}
|
||||
@@ -206,13 +185,13 @@ export const locationsSchema = z.object({
|
||||
id: location.id,
|
||||
relationships: {
|
||||
city: {
|
||||
cityIdentifier: "",
|
||||
ianaTimeZoneId: "",
|
||||
id: "",
|
||||
cityIdentifier: undefined as string | undefined,
|
||||
id: extractCityId(
|
||||
location.relationships?.city?.links?.related ?? ""
|
||||
),
|
||||
isPublished: false,
|
||||
keywords: [],
|
||||
name: "",
|
||||
timeZoneId: "",
|
||||
keywords: [] as string[],
|
||||
name: undefined as string | undefined,
|
||||
type: "cities",
|
||||
url: location?.relationships?.city?.links?.related ?? "",
|
||||
},
|
||||
@@ -223,3 +202,18 @@ export const locationsSchema = z.object({
|
||||
})
|
||||
),
|
||||
})
|
||||
|
||||
function extractCityId(cityUrl: string): string | null {
|
||||
try {
|
||||
const url = new URL(cityUrl)
|
||||
if (!url.pathname.toLowerCase().includes("/cities/")) {
|
||||
return null
|
||||
}
|
||||
|
||||
const id = url.pathname.split("/").at(-1)
|
||||
|
||||
return id ?? null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user