import { z } from "zod" import { Lang } from "@scandic-hotels/common/constants/language" import { getCacheClient } from "@scandic-hotels/common/dataCache" import { serviceProcedure } from "../../../procedures" import { getCitiesByCountry } from "../services/getCitiesByCountry" import { getCountries } from "../services/getCountries" import { getLocationsByCountries } from "../services/getLocationsByCountries" const getLocationsInput = z.object({ lang: z.nativeEnum(Lang), }) export const get = serviceProcedure .input(getLocationsInput) .query(async function ({ ctx, input }) { const lang = input.lang ?? ctx.lang const cacheClient = await getCacheClient() return await cacheClient.cacheOrGet( `${lang}:getLocations`, async () => { const countries = await getCountries({ lang: lang, serviceToken: ctx.serviceToken, }) if (!countries) { throw new Error("Unable to fetch countries") } const countryNames = countries.data.map((country) => country.name) const citiesByCountry = await getCitiesByCountry({ countries: countryNames, serviceToken: ctx.serviceToken, lang, }) const locations = await getLocationsByCountries({ lang, serviceToken: ctx.serviceToken, citiesByCountry, }) if (!locations) { throw new Error("Unable to fetch locations") } return locations }, "max" ) })