Merged in feat/SW-3526-show-sas-eb-points-rate-in- (pull request #2933)

feat(SW-3526): Show EB points rate and label in booking flow

* feat(SW-3526): Show EB points rate and label in booking flow

* feat(SW-3526) Optimized points currency code

* feat(SW-3526) Removed extra multiplication for token expiry after rebase

* feat(SW-3526): Updated to exhaustive check and thow if type error

Approved-by: Anton Gunnarsson
This commit is contained in:
Hrishikesh Vaipurkar
2025-10-15 06:54:44 +00:00
parent 73af1eed9b
commit 78ede453a2
27 changed files with 281 additions and 176 deletions

View File

@@ -5,6 +5,8 @@ import { createLogger } from "@scandic-hotels/common/logger/createLogger"
import { env } from "../../../../env/server"
import { protectedProcedure } from "../../../procedures"
import type { Session } from "next-auth"
const outputSchema = z.object({
eurobonusNumber: z.string(),
firstName: z.string().optional(),
@@ -46,32 +48,67 @@ const outputSchema = z.object({
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}`,
export async function getEuroBonusProfileData(session: Session) {
if (session.token.loginType !== "sas") {
return {
error: {
message: `Failed to fetch EuroBonus profile, expected loginType to be "sas" but was ${session.token.loginType}`,
},
})
} as const
}
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 },
})
}
if (!session.token.expires_at || session.token.expires_at < Date.now()) {
return {
error: {
message: "Token expired sas",
},
} as const
}
const data = await response.json()
return data
const response = await fetch(url, {
headers: {
"Content-Type": "application/json",
"Ocp-Apim-Subscription-Key": env.SAS_OCP_APIM,
Authorization: `Bearer ${session?.token?.access_token}`,
},
})
if (!response.ok) {
sasLogger.error(
`Failed to get EuroBonus profile, status: ${response.status}, statusText: ${response.statusText}`
)
return {
error: {
message: "Failed to fetch EuroBonus profile",
cause: { status: response.status, statusText: response.statusText },
},
} as const
}
const responseJson = await response.json()
const data = outputSchema.safeParse(responseJson)
if (!data.success) {
sasLogger.error(
`Failed to parse EuroBonus profile, cause: ${data.error.cause}, message: ${data.error.message}`
)
return {
error: {
message: `Failed to parse EuroBonus profile: ${data.error.message}`,
cause: { status: response.status, statusText: response.statusText },
},
} as const
}
return data
}
export const getEuroBonusProfile = protectedProcedure.query(async function ({
ctx,
}) {
const verifiedSasUser = await getEuroBonusProfileData(ctx.session)
if ("error" in verifiedSasUser) {
throw new Error(verifiedSasUser.error?.message, {
cause: verifiedSasUser.error?.cause,
})
}
return verifiedSasUser.data
})