Merged in feature/SW-3505-fetch-eurobonus-points (pull request #2847)
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
This commit is contained in:
8
packages/trpc/env/server.ts
vendored
8
packages/trpc/env/server.ts
vendored
@@ -13,10 +13,10 @@ export const env = createEnv({
|
||||
server: {
|
||||
API_BASEURL: z.string(),
|
||||
BOOKING_ENCRYPTION_KEY: z.string(),
|
||||
SAS_API_ENDPOINT: z.string().default(""),
|
||||
SAS_AUTH_ENDPOINT: z.string().default(""),
|
||||
SAS_OCP_APIM: z.string().default(""),
|
||||
SAS_AUTH_CLIENTID: z.string().default(""),
|
||||
SAS_API_ENDPOINT: z.string(),
|
||||
SAS_AUTH_ENDPOINT: z.string(),
|
||||
SAS_OCP_APIM: z.string(),
|
||||
SAS_AUTH_CLIENTID: z.string(),
|
||||
CACHE_TIME_HOTELS: z.coerce
|
||||
.number()
|
||||
.default(TWENTYFOUR_HOURS)
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
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
|
||||
})
|
||||
@@ -1,6 +1,7 @@
|
||||
import { router } from "../../.."
|
||||
import { requestOtp } from "./otp/request/requestOtp"
|
||||
import { verifyOtp } from "./otp/verify/verifyOtp"
|
||||
import { getEuroBonusProfile } from "./getEuroBonusProfile"
|
||||
import { linkAccount } from "./linkAccount"
|
||||
import { performLevelUpgrade } from "./performLevelUpgrade"
|
||||
import { transferPoints } from "./transferPoints"
|
||||
@@ -13,4 +14,5 @@ export const sasRouter = router({
|
||||
unlinkAccount,
|
||||
performLevelUpgrade,
|
||||
transferPoints,
|
||||
getEuroBonusProfile,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user