Files
web/apps/partner-sas/lib/trpc/index.ts
Joakim Jäderberg a4f1a55e56 Merged in feat/SW-3549-handle-unlinked-account (pull request #3019)
fix(SW-3549): update social session management functions for clarity and consistency

* refactor(SW-3549): rename session management functions for clarity and consistency

* merge


Approved-by: Hrishikesh Vaipurkar
2025-10-28 09:51:30 +00:00

75 lines
2.0 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 { getSocialSession } 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 getSocialSession()
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 getSocialSession()
if (!session) return null
const user = await getVerifiedUser({
token: {
expires_at: dt(session.expires_at).unix() * 1000,
access_token: session.access_token,
},
})
return user ?? null
},
})
return ctx
}
export function configureTrpc() {
configureServerClient(createAppContext)
}
export async function serverClient() {
const ctx = await createAppContext()
return appServerClient(ctx)
}