feat(SW-3505): add endpoint for getting eurobonus profile * feat(SW-3505): add endpoint for getting eurobonus profile * make sure we add loginType to session * no need to run zod parsing twice * Make SAS environment variables mandatory Approved-by: Anton Gunnarsson
62 lines
1.8 KiB
TypeScript
62 lines
1.8 KiB
TypeScript
import { z } from "zod"
|
|
|
|
import { createLogger } from "@scandic-hotels/common/logger/createLogger"
|
|
|
|
import { env } from "../../../../env/server"
|
|
import { protectedProcedure } from "../../../procedures"
|
|
|
|
const outputSchema = z.object({
|
|
eurobonusNumber: z.string(),
|
|
linkStatus: z.enum(["UNLINKED", "LINKED"]),
|
|
isBlocked: z.boolean(),
|
|
enrollmentDate: z.string(),
|
|
birthdate: z.string(),
|
|
mobileNumber: z.string(),
|
|
email: z.string().email(),
|
|
points: z.object({
|
|
balances: z.array(z.unknown()),
|
|
total: z.number(),
|
|
}),
|
|
tier: z.string(),
|
|
tierStartDate: z.string(),
|
|
tierEndDate: z.string().nullable(),
|
|
lifeTimeGold: z.boolean(),
|
|
tierBoostRequestedByScandic: z.boolean(),
|
|
firstName: z.string(),
|
|
lastName: z.string(),
|
|
whoBoosted: z.enum(["NO-BOOST", "BOOSTED"]),
|
|
})
|
|
|
|
const sasLogger = createLogger("SAS")
|
|
const url = new URL("/api/scandic-partnership/v1/profile", env.SAS_API_ENDPOINT)
|
|
|
|
export const getEuroBonusProfile = protectedProcedure
|
|
.output(outputSchema)
|
|
.query(async function ({ ctx }) {
|
|
if (ctx.session.token.loginType !== "sas") {
|
|
throw new Error(
|
|
`Failed to fetch EuroBonus profile, expected loginType to be "sas" but was ${ctx.session.token.loginType}`
|
|
)
|
|
}
|
|
|
|
const response = await fetch(url, {
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"Ocp-Apim-Subscription-Key": env.SAS_OCP_APIM,
|
|
Authorization: `Bearer ${ctx.session?.token?.access_token}`,
|
|
},
|
|
})
|
|
|
|
if (!response.ok) {
|
|
sasLogger.error(
|
|
`Failed to get EuroBonus profile, status: ${response.status}, statusText: ${response.statusText}`
|
|
)
|
|
throw new Error("Failed to fetch EuroBonus profile", {
|
|
cause: { status: response.status, statusText: response.statusText },
|
|
})
|
|
}
|
|
|
|
const data = await response.json()
|
|
return data
|
|
})
|