import * as Sentry from "@sentry/nextjs" import { createCounter } from "@scandic-hotels/common/telemetry" import * as api from "../../../api" import { cache } from "../../../DUPLICATED/cache" import { extractResponseDetails, internalServerError, serverErrorByStatus, sessionExpiredError, } from "../../../errors" import { getUserSchema } from "../output" import type { z } from "zod" import type { DeepPartial } from "../../../types/deepPartial" export const getVerifiedUser = cache( async ({ token, includeExtendedPartnerData, }: { token: { expires_at?: number; access_token: string } includeExtendedPartnerData?: boolean }) => { const getVerifiedUserCounter = createCounter("user.getVerifiedUser") const metricsGetVerifiedUser = getVerifiedUserCounter.init() metricsGetVerifiedUser.start() const now = Date.now() if (token.expires_at && token.expires_at < now) { throw sessionExpiredError() } const apiResponse = await api.get( api.endpoints.v2.Profile.profile, { headers: { Authorization: `Bearer ${token.access_token}`, }, }, includeExtendedPartnerData ? { includes: "extendedPartnerInformation" } : {} ) if (!apiResponse.ok) { await metricsGetVerifiedUser.httpError(apiResponse) throw serverErrorByStatus( apiResponse.status, await extractResponseDetails(apiResponse), "getVerifiedUser failed" ) } const apiJson = await apiResponse.json() if (!apiJson.data?.attributes) { metricsGetVerifiedUser.dataError( `Missing data attributes in API response`, { data: apiJson, } ) throw internalServerError("Missing data attributes in API response") } const verifiedData = getUserSchema.safeParse(apiJson) if (!verifiedData.success) { addUserToSentry(apiJson) metricsGetVerifiedUser.validationError(verifiedData.error) throw verifiedData.error } metricsGetVerifiedUser.success() return verifiedData.data } ) function addUserToSentry(apiJson: unknown) { const typedData = apiJson as DeepPartial> if ( typeof typedData.data?.attributes?.profileId === "undefined" || typeof typedData.data?.attributes?.membershipNumber === "undefined" ) { return } Sentry.setUser({ id: typedData?.data?.attributes?.profileId, username: typedData?.data?.attributes?.membershipNumber, }) }