* feat(SW-2271): Changes to hotel data types in preperation for filtering * feat(SW-2271): Added filter and sort functionality Approved-by: Matilda Landström
137 lines
3.5 KiB
TypeScript
137 lines
3.5 KiB
TypeScript
import { ApiCountry } from "../../../types/country"
|
|
import { HotelSortOption } from "../../../types/hotel"
|
|
import { getFiltersFromHotels } from "../../../utils/getFiltersFromHotels"
|
|
import { getSortedCities } from "../../../utils/getSortedCities"
|
|
import {
|
|
getCityByCityIdentifier,
|
|
getHotelIdsByCityIdentifier,
|
|
getHotelIdsByCountry,
|
|
getHotelsByHotelIds,
|
|
} from "../../hotels/utils"
|
|
import { getCityPages } from "../destinationCountryPage/utils"
|
|
|
|
import type { Lang } from "@scandic-hotels/common/constants/language"
|
|
|
|
import type { MetadataInputSchema } from "./input"
|
|
import type { RawMetadataSchema } from "./output"
|
|
|
|
export const affix = "metadata"
|
|
|
|
export async function getCityData(
|
|
data: RawMetadataSchema,
|
|
input: MetadataInputSchema,
|
|
serviceToken: string,
|
|
lang: Lang
|
|
) {
|
|
const destinationSettings = data.destination_settings
|
|
const filter = input.filterFromUrl
|
|
|
|
if (destinationSettings) {
|
|
const {
|
|
city_sweden,
|
|
city_norway,
|
|
city_denmark,
|
|
city_finland,
|
|
city_germany,
|
|
city_poland,
|
|
} = destinationSettings
|
|
const cities = [
|
|
city_denmark,
|
|
city_finland,
|
|
city_germany,
|
|
city_poland,
|
|
city_norway,
|
|
city_sweden,
|
|
].filter((city): city is string => Boolean(city))
|
|
|
|
const cityIdentifier = cities[0]
|
|
|
|
if (cityIdentifier) {
|
|
const cityData = await getCityByCityIdentifier({
|
|
cityIdentifier,
|
|
serviceToken,
|
|
lang,
|
|
})
|
|
const hotelIds = await getHotelIdsByCityIdentifier(
|
|
cityIdentifier,
|
|
serviceToken
|
|
)
|
|
|
|
const hotels = await getHotelsByHotelIds({ hotelIds, lang, serviceToken })
|
|
|
|
let filterType
|
|
if (filter) {
|
|
const allFilters = getFiltersFromHotels(hotels, lang)
|
|
const facilityFilter = allFilters.facilityFilters.find(
|
|
(f) => f.slug === filter
|
|
)
|
|
const surroudingsFilter = allFilters.surroundingsFilters.find(
|
|
(f) => f.slug === filter
|
|
)
|
|
|
|
if (facilityFilter) {
|
|
filterType = "facility"
|
|
} else if (surroudingsFilter) {
|
|
filterType = "surroundings"
|
|
}
|
|
}
|
|
|
|
return {
|
|
location: cityData?.name,
|
|
filter,
|
|
filterType,
|
|
hotelCount: hotelIds.length,
|
|
}
|
|
}
|
|
}
|
|
return null
|
|
}
|
|
|
|
export async function getCountryData(
|
|
data: RawMetadataSchema,
|
|
input: MetadataInputSchema,
|
|
serviceToken: string,
|
|
lang: Lang
|
|
) {
|
|
const country = data.destination_settings?.country
|
|
const filter = input.filterFromUrl
|
|
|
|
if (country) {
|
|
const translatedCountry = ApiCountry[lang][country]
|
|
let filterType
|
|
|
|
const cities = await getCityPages(lang, serviceToken, country)
|
|
const sortedCities = getSortedCities(cities, HotelSortOption.Recommended)
|
|
const hotelIds = await getHotelIdsByCountry({
|
|
country,
|
|
serviceToken,
|
|
})
|
|
|
|
const hotels = await getHotelsByHotelIds({ hotelIds, lang, serviceToken })
|
|
|
|
if (filter) {
|
|
const allFilters = getFiltersFromHotels(hotels, lang)
|
|
const facilityFilter = allFilters.facilityFilters.find(
|
|
(f) => f.slug === filter
|
|
)
|
|
const surroudingsFilter = allFilters.surroundingsFilters.find(
|
|
(f) => f.slug === filter
|
|
)
|
|
|
|
if (facilityFilter) {
|
|
filterType = "facility"
|
|
} else if (surroudingsFilter) {
|
|
filterType = "surroundings"
|
|
}
|
|
}
|
|
return {
|
|
location: translatedCountry,
|
|
filter,
|
|
filterType,
|
|
cities: sortedCities.slice(0, 2).map(({ cityName }) => cityName),
|
|
hotelCount: hotelIds.length,
|
|
}
|
|
}
|
|
return null
|
|
}
|