Chore/refactor hotel trpc routes * chore(SW-3519): refactor trpc hotel routers * chore(SW-3519): refactor trpc hotel routers * refactor * merge * Merge branch 'master' of bitbucket.org:scandic-swap/web into chore/refactor-hotel-trpc-routes Approved-by: Linus Flood
60 lines
1.3 KiB
TypeScript
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
|
|
}
|