feat(SW-2863): Move contentstack router to trpc package * Add exports to packages and lint rule to prevent relative imports * Add env to trpc package * Add eslint to trpc package * Apply lint rules * Use direct imports from trpc package * Add lint-staged config to trpc * Move lang enum to common * Restructure trpc package folder structure * WIP first step * update internal imports in trpc * Fix most errors in scandic-web Just 100 left... * Move Props type out of trpc * Fix CategorizedFilters types * Move more schemas in hotel router * Fix deps * fix getNonContentstackUrls * Fix import error * Fix entry error handling * Fix generateMetadata metrics * Fix alertType enum * Fix duplicated types * lint:fix * Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package * Fix broken imports * Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package Approved-by: Linus Flood
137 lines
3.5 KiB
TypeScript
137 lines
3.5 KiB
TypeScript
import { SortOption } from "../../../enums/destinationFilterAndSort"
|
|
import { ApiCountry } from "../../../types/country"
|
|
import { getSortedCities } from "../../../utils/getSortedCities"
|
|
import {
|
|
getCityByCityIdentifier,
|
|
getHotelIdsByCityIdentifier,
|
|
getHotelIdsByCountry,
|
|
getHotelsByHotelIds,
|
|
} from "../../hotels/utils"
|
|
import { getCityPages } from "../destinationCountryPage/utils"
|
|
import { getFiltersFromHotels } from "./helpers"
|
|
|
|
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)
|
|
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, SortOption.Recommended)
|
|
const hotelIds = await getHotelIdsByCountry({
|
|
country,
|
|
serviceToken,
|
|
})
|
|
|
|
const hotels = await getHotelsByHotelIds({ hotelIds, lang, serviceToken })
|
|
|
|
if (filter) {
|
|
const allFilters = getFiltersFromHotels(hotels)
|
|
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
|
|
}
|