Files
web/packages/trpc/lib/routers/user/services/getPreviousStays.ts
Joakim Jäderberg 8b94540d19 Merged in chore/redirect-counter (pull request #3302)
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
2025-12-08 10:24:05 +00:00

60 lines
1.3 KiB
TypeScript

import { createCounter } from "@scandic-hotels/common/telemetry"
import * as api from "../../../api"
import { toApiLang } from "../../../utils"
import { getStaysSchema } from "../output"
import type { Lang } from "@scandic-hotels/common/constants/language"
export async function getPreviousStays(
accessToken: string,
limit: number = 10,
language: Lang,
cursor?: string
) {
const getPreviousStaysCounter = createCounter("user.getPreviousStays")
const metricsGetPreviousStays = getPreviousStaysCounter.init({
limit,
cursor,
language,
})
metricsGetPreviousStays.start()
const params: Record<string, string> = {
limit: String(limit),
language: toApiLang(language),
}
if (cursor) {
params.offset = cursor
}
const apiResponse = await api.get(
api.endpoints.v1.Booking.Stays.past,
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
},
params
)
if (!apiResponse.ok) {
await metricsGetPreviousStays.httpError(apiResponse)
return null
}
const apiJson = await apiResponse.json()
const verifiedData = getStaysSchema.safeParse(apiJson)
if (!verifiedData.success) {
metricsGetPreviousStays.validationError(verifiedData.error)
return null
}
metricsGetPreviousStays.success()
return verifiedData.data
}