Counter name is now searchable and add counter for redirects * refactor: createCounter() only takes one argument, the name of the counter. Makes it easier to search for * feat: add counter when we do a redirect from redirect-service Approved-by: Linus Flood
73 lines
1.9 KiB
TypeScript
73 lines
1.9 KiB
TypeScript
import stringify from "json-stable-stringify-without-jsonify"
|
|
|
|
import { getCacheClient } from "@scandic-hotels/common/dataCache"
|
|
import { createCounter } from "@scandic-hotels/common/telemetry"
|
|
|
|
import * as api from "../../../api"
|
|
import { toApiLang } from "../../../utils"
|
|
import { packagesSchema } from "../output"
|
|
|
|
import type { PackagesOutput } from "../../../types/packages"
|
|
|
|
export async function getPackages(input: PackagesOutput, serviceToken: string) {
|
|
const { adults, children, endDate, hotelId, lang, packageCodes, startDate } =
|
|
input
|
|
|
|
const getPackagesCounter = createCounter("hotel.getPackages")
|
|
const metricsGetPackages = getPackagesCounter.init({
|
|
input,
|
|
})
|
|
|
|
metricsGetPackages.start()
|
|
|
|
const cacheClient = await getCacheClient()
|
|
|
|
const result = cacheClient.cacheOrGet(
|
|
stringify(input),
|
|
async function () {
|
|
const apiLang = toApiLang(lang)
|
|
|
|
const searchParams = new URLSearchParams({
|
|
adults: adults.toString(),
|
|
children: children.toString(),
|
|
endDate,
|
|
language: apiLang,
|
|
startDate,
|
|
})
|
|
|
|
packageCodes.forEach((code) => {
|
|
searchParams.append("packageCodes", code)
|
|
})
|
|
|
|
const apiResponse = await api.get(
|
|
api.endpoints.v1.Package.Packages.hotel(hotelId),
|
|
{
|
|
headers: {
|
|
Authorization: `Bearer ${serviceToken}`,
|
|
},
|
|
},
|
|
searchParams
|
|
)
|
|
|
|
if (!apiResponse.ok) {
|
|
await metricsGetPackages.httpError(apiResponse)
|
|
return null
|
|
}
|
|
|
|
const apiJson = await apiResponse.json()
|
|
const validatedPackagesData = packagesSchema.safeParse(apiJson)
|
|
if (!validatedPackagesData.success) {
|
|
metricsGetPackages.validationError(validatedPackagesData.error)
|
|
return null
|
|
}
|
|
|
|
return validatedPackagesData.data
|
|
},
|
|
"3h"
|
|
)
|
|
|
|
metricsGetPackages.success()
|
|
|
|
return result
|
|
}
|