fix: clean up hotel and its typings

This commit is contained in:
Simon Emanuelsson
2024-12-17 16:17:25 +01:00
parent ec74af8814
commit 13a164242f
110 changed files with 1931 additions and 1559 deletions

View File

@@ -4,25 +4,24 @@ import { unstable_cache } from "next/cache"
import * as api from "@/lib/api"
import {
apiCitiesByCountrySchema,
apiCitySchema,
apiCountriesSchema,
apiLocationsSchema,
type CitiesGroupedByCountry,
citiesByCountrySchema,
citiesSchema,
countriesSchema,
getHotelIdsByCityIdSchema,
locationsSchema,
} from "./output"
import {
getHotelIdsCounter,
getHotelIdsFailCounter,
getHotelIdsSuccessCounter,
} from "./telemetry"
import type { Country } from "@/types/enums/country"
import { PointOfInterestGroupEnum } from "@/types/enums/pointOfInterest"
import type { RequestOptionsWithOutBody } from "@/types/fetch"
import { PointOfInterestGroupEnum } from "@/types/hotel"
import type { HotelLocation } from "@/types/trpc/routers/hotel/locations"
import type {
CitiesGroupedByCountry,
Countries,
HotelLocation,
} from "@/types/trpc/routers/hotel/locations"
import type { Lang } from "@/constants/languages"
import type { Endpoint } from "@/lib/api/endpoints"
import { Country } from "@/types/enums/country"
import { metrics } from "./metrics"
export function getPoiGroupByCategoryName(category: string | undefined) {
if (!category) return PointOfInterestGroupEnum.LOCATION
@@ -75,7 +74,7 @@ export async function getCity(
}
const cityJson = await cityResponse.json()
const city = apiCitySchema.safeParse(cityJson)
const city = citiesSchema.safeParse(cityJson)
if (!city.success) {
console.info(`Validation of city failed`)
console.info(`cityUrl: ${locationCityUrl}`)
@@ -108,7 +107,7 @@ export async function getCountries(
}
const countriesJson = await countryResponse.json()
const countries = apiCountriesSchema.safeParse(countriesJson)
const countries = countriesSchema.safeParse(countriesJson)
if (!countries.success) {
console.info(`Validation for countries failed`)
console.error(countries.error)
@@ -149,8 +148,7 @@ export async function getCitiesByCountry(
}
const countryJson = await countryResponse.json()
const citiesByCountry =
apiCitiesByCountrySchema.safeParse(countryJson)
const citiesByCountry = citiesByCountrySchema.safeParse(countryJson)
if (!citiesByCountry.success) {
console.info(`Failed to validate Cities by Country payload`)
console.error(citiesByCountry.error)
@@ -200,7 +198,7 @@ export async function getLocations(
}
const apiJson = await apiResponse.json()
const verifiedLocations = apiLocationsSchema.safeParse(apiJson)
const verifiedLocations = locationsSchema.safeParse(apiJson)
if (!verifiedLocations.success) {
console.info(`Locations Verification Failed`)
console.error(verifiedLocations.error)
@@ -273,7 +271,7 @@ export async function getHotelIdsByCityId(
) {
return unstable_cache(
async function (params: URLSearchParams) {
getHotelIdsCounter.add(1, { params: params.toString() })
metrics.hotelIds.counter.add(1, { params: params.toString() })
console.info(
"api.hotel.hotel-ids start",
JSON.stringify({ params: params.toString() })
@@ -286,7 +284,7 @@ export async function getHotelIdsByCityId(
if (!apiResponse.ok) {
const responseMessage = await apiResponse.text()
getHotelIdsFailCounter.add(1, {
metrics.hotelIds.fail.add(1, {
params: params.toString(),
error_type: "http_error",
error: responseMessage,
@@ -309,7 +307,7 @@ export async function getHotelIdsByCityId(
const apiJson = await apiResponse.json()
const validatedHotelIds = getHotelIdsByCityIdSchema.safeParse(apiJson)
if (!validatedHotelIds.success) {
getHotelIdsFailCounter.add(1, {
metrics.hotelIds.fail.add(1, {
params: params.toString(),
error_type: "validation_error",
error: JSON.stringify(validatedHotelIds.error),
@@ -324,7 +322,7 @@ export async function getHotelIdsByCityId(
return null
}
getHotelIdsSuccessCounter.add(1, { cityId })
metrics.hotelIds.success.add(1, { cityId })
console.info(
"api.hotel.hotel-ids success",
JSON.stringify({ params: params.toString() })
@@ -344,7 +342,7 @@ export async function getHotelIdsByCountry(
) {
return unstable_cache(
async function (params: URLSearchParams) {
getHotelIdsCounter.add(1, { country })
metrics.hotelIds.counter.add(1, { country })
console.info(
"api.hotel.hotel-ids start",
JSON.stringify({ query: { country } })
@@ -357,7 +355,7 @@ export async function getHotelIdsByCountry(
if (!apiResponse.ok) {
const responseMessage = await apiResponse.text()
getHotelIdsFailCounter.add(1, {
metrics.hotelIds.fail.add(1, {
country,
error_type: "http_error",
error: responseMessage,
@@ -380,7 +378,7 @@ export async function getHotelIdsByCountry(
const apiJson = await apiResponse.json()
const validatedHotelIds = getHotelIdsByCityIdSchema.safeParse(apiJson)
if (!validatedHotelIds.success) {
getHotelIdsFailCounter.add(1, {
metrics.hotelIds.fail.add(1, {
country,
error_type: "validation_error",
error: JSON.stringify(validatedHotelIds.error),
@@ -395,7 +393,7 @@ export async function getHotelIdsByCountry(
return null
}
getHotelIdsSuccessCounter.add(1, { country })
metrics.hotelIds.success.add(1, { country })
console.info(
"api.hotel.hotel-ids success",
JSON.stringify({ query: { country } })