feat (SW-2864): Move booking router to trpc package * Add env to trpc package * Add eslint to trpc package * Apply lint rules * Use direct imports from trpc package * Add lint-staged config to trpc * Move lang enum to common * Restructure trpc package folder structure * WIP first step * update internal imports in trpc * Fix most errors in scandic-web Just 100 left... * Move Props type out of trpc * Fix CategorizedFilters types * Move more schemas in hotel router * Fix deps * fix getNonContentstackUrls * Fix import error * Fix entry error handling * Fix generateMetadata metrics * Fix alertType enum * Fix duplicated types * lint:fix * Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package * Fix broken imports * Move booking router to trpc package * Merge branch 'master' into feat/sw-2864-move-hotels-router-to-trpc-package Approved-by: Linus Flood
80 lines
2.1 KiB
TypeScript
80 lines
2.1 KiB
TypeScript
import { createCounter } from "@scandic-hotels/common/telemetry"
|
|
|
|
import * as api from "../../api"
|
|
import { cache } from "../../DUPLICATED/cache"
|
|
import { getUserSchema } from "./output"
|
|
|
|
import type { Session } from "next-auth"
|
|
|
|
export const getVerifiedUser = cache(
|
|
async ({
|
|
session,
|
|
includeExtendedPartnerData,
|
|
}: {
|
|
session: Session
|
|
includeExtendedPartnerData?: boolean
|
|
}) => {
|
|
const getVerifiedUserCounter = createCounter("user", "getVerifiedUser")
|
|
const metricsGetVerifiedUser = getVerifiedUserCounter.init()
|
|
|
|
metricsGetVerifiedUser.start()
|
|
|
|
const now = Date.now()
|
|
if (session.token.expires_at && session.token.expires_at < now) {
|
|
metricsGetVerifiedUser.dataError(`Token expired`)
|
|
return { error: true, cause: "token_expired" } as const
|
|
}
|
|
|
|
const apiResponse = await api.get(
|
|
api.endpoints.v2.Profile.profile,
|
|
{
|
|
headers: {
|
|
Authorization: `Bearer ${session.token.access_token}`,
|
|
},
|
|
},
|
|
includeExtendedPartnerData
|
|
? { includes: "extendedPartnerInformation" }
|
|
: {}
|
|
)
|
|
|
|
if (!apiResponse.ok) {
|
|
await metricsGetVerifiedUser.httpError(apiResponse)
|
|
|
|
if (apiResponse.status === 401) {
|
|
return { error: true, cause: "unauthorized" } as const
|
|
} else if (apiResponse.status === 403) {
|
|
return { error: true, cause: "forbidden" } as const
|
|
} else if (apiResponse.status === 404) {
|
|
return { error: true, cause: "notfound" } as const
|
|
}
|
|
|
|
return {
|
|
error: true,
|
|
cause: "unknown",
|
|
status: apiResponse.status,
|
|
} as const
|
|
}
|
|
|
|
const apiJson = await apiResponse.json()
|
|
if (!apiJson.data?.attributes) {
|
|
metricsGetVerifiedUser.dataError(
|
|
`Missing data attributes in API response`,
|
|
{
|
|
data: apiJson,
|
|
}
|
|
)
|
|
return null
|
|
}
|
|
|
|
const verifiedData = getUserSchema.safeParse(apiJson)
|
|
if (!verifiedData.success) {
|
|
metricsGetVerifiedUser.validationError(verifiedData.error)
|
|
return null
|
|
}
|
|
|
|
metricsGetVerifiedUser.success()
|
|
|
|
return verifiedData
|
|
}
|
|
)
|