Files
web/apps/partner-sas/lib/trpc/index.ts
T
Hrishikesh Vaipurkar 98e67f5eda Merged in fix/SW-3550-member-rates-should-only- (pull request #3005)
fix(SW-3550): Treat unlinked User as non-logged user within booking flow

Approved-by: Joakim Jäderberg
2025-10-27 14:34:19 +00:00

75 lines
2.1 KiB
TypeScript

import { headers } from "next/headers"
import { dt } from "@scandic-hotels/common/dt"
import { createContext } from "@scandic-hotels/trpc/context"
import { getEuroBonusProfileData } from "@scandic-hotels/trpc/routers/partners/sas/getEuroBonusProfile"
import { getVerifiedUser } from "@scandic-hotels/trpc/routers/user/utils/getVerifiedUser"
import {
appServerClient,
configureServerClient,
} from "@scandic-hotels/trpc/serverClient"
import { auth } from "@/auth"
import { getSession } from "@/auth/scandic/session"
import type { Lang } from "@scandic-hotels/common/constants/language"
export async function createAppContext() {
const headersList = await headers()
const ctx = createContext({
app: "partner-sas",
lang: headersList.get("x-lang") as Lang,
pathname: headersList.get("x-pathname")!,
uid: headersList.get("x-uid"),
url: headersList.get("x-url")!,
contentType: headersList.get("x-contenttype")!,
auth: async () => {
const session = await auth()
return session
},
getScandicUserToken: async () => {
const session = await getSession()
return session?.access_token ?? null
},
getUserPointsBalance: async () => {
const session = await auth()
if (!session) return null
const euroBonusProfile = await getEuroBonusProfileData({
accessToken: session.token.access_token,
loginType: session.token.loginType,
})
if (!euroBonusProfile) return null
return euroBonusProfile.points.total
},
getScandicUser: async () => {
const session = await getSession()
// The getSession will either return empty object or session object, hence we need to validate if the object is empty or not
if (!session?.access_token) return null
return await getVerifiedUser({
token: {
expires_at: dt(session.expires_at).unix() * 1000,
access_token: session.access_token,
},
})
},
})
return ctx
}
export function configureTrpc() {
configureServerClient(createAppContext)
}
export async function serverClient() {
const ctx = await createAppContext()
return appServerClient(ctx)
}