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:
@@ -9,7 +9,8 @@ import { SEARCH_TYPE_REDEMPTION } from "../../../constants/booking"
|
||||
import { AvailabilityEnum } from "../../../enums/selectHotel"
|
||||
import { unauthorizedError } from "../../../errors"
|
||||
import { safeProtectedServiceProcedure } from "../../../procedures"
|
||||
import { getVerifiedUser } from "../../user/utils/getVerifiedUser"
|
||||
import { getRedemptionTokenSafely } from "../../../utils/getRedemptionTokenSafely"
|
||||
import { getUserPointsBalance } from "../../../utils/getUserPointsBalance"
|
||||
import { baseBookingSchema, baseRoomSchema, selectedRoomSchema } from "../input"
|
||||
import { getHotel } from "../services/getHotel"
|
||||
import { getRoomsAvailability } from "../services/getRoomsAvailability"
|
||||
@@ -37,13 +38,15 @@ export const enterDetails = safeProtectedServiceProcedure
|
||||
.use(async ({ ctx, input, next }) => {
|
||||
if (input.booking.searchType === SEARCH_TYPE_REDEMPTION) {
|
||||
if (ctx.session?.token.access_token) {
|
||||
const verifiedUser = await getVerifiedUser({ session: ctx.session })
|
||||
if (!verifiedUser?.error) {
|
||||
const pointsValue = await getUserPointsBalance(ctx.session)
|
||||
const token = getRedemptionTokenSafely(ctx.session, ctx.serviceToken)
|
||||
if (pointsValue && token) {
|
||||
return next({
|
||||
ctx: {
|
||||
token: ctx.session.token.access_token,
|
||||
userPoints: verifiedUser?.data.membership?.currentPoints ?? 0,
|
||||
token: token,
|
||||
userPoints: pointsValue ?? 0,
|
||||
},
|
||||
input,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,8 @@ import { env } from "../../../../env/server"
|
||||
import { unauthorizedError } from "../../../errors"
|
||||
import { safeProtectedServiceProcedure } from "../../../procedures"
|
||||
import { toApiLang } from "../../../utils"
|
||||
import { getVerifiedUser } from "../../user/utils/getVerifiedUser"
|
||||
import { getRedemptionTokenSafely } from "../../../utils/getRedemptionTokenSafely"
|
||||
import { getUserPointsBalance } from "../../../utils/getUserPointsBalance"
|
||||
import { getHotelsAvailabilityByCity } from "../services/getHotelsAvailabilityByCity"
|
||||
|
||||
export type HotelsAvailabilityInputSchema = z.output<
|
||||
@@ -68,12 +69,13 @@ export const hotelsByCity = safeProtectedServiceProcedure
|
||||
.use(async ({ ctx, input, next }) => {
|
||||
if (input.redemption) {
|
||||
if (ctx.session?.token.access_token) {
|
||||
const verifiedUser = await getVerifiedUser({ session: ctx.session })
|
||||
if (!verifiedUser?.error) {
|
||||
const pointsValue = await getUserPointsBalance(ctx.session)
|
||||
const token = getRedemptionTokenSafely(ctx.session, ctx.serviceToken)
|
||||
if (pointsValue && token) {
|
||||
return next({
|
||||
ctx: {
|
||||
token: ctx.session.token.access_token,
|
||||
userPoints: verifiedUser?.data.membership?.currentPoints ?? 0,
|
||||
token: token,
|
||||
userPoints: pointsValue ?? 0,
|
||||
},
|
||||
input,
|
||||
})
|
||||
|
||||
@@ -4,7 +4,8 @@ import { z } from "zod"
|
||||
import { unauthorizedError } from "../../../errors"
|
||||
import { safeProtectedServiceProcedure } from "../../../procedures"
|
||||
import { toApiLang } from "../../../utils"
|
||||
import { getVerifiedUser } from "../../user/utils/getVerifiedUser"
|
||||
import { getRedemptionTokenSafely } from "../../../utils/getRedemptionTokenSafely"
|
||||
import { getUserPointsBalance } from "../../../utils/getUserPointsBalance"
|
||||
import { getHotelsAvailabilityByHotelIds } from "../services/getHotelsAvailabilityByHotelIds"
|
||||
|
||||
export type HotelsByHotelIdsAvailabilityInputSchema = z.output<
|
||||
@@ -65,12 +66,13 @@ export const hotelsByHotelIds = safeProtectedServiceProcedure
|
||||
.use(async ({ ctx, input, next }) => {
|
||||
if (input.redemption) {
|
||||
if (ctx.session?.token.access_token) {
|
||||
const verifiedUser = await getVerifiedUser({ session: ctx.session })
|
||||
if (!verifiedUser?.error) {
|
||||
const pointsValue = await getUserPointsBalance(ctx.session)
|
||||
const token = getRedemptionTokenSafely(ctx.session, ctx.serviceToken)
|
||||
if (pointsValue && token) {
|
||||
return next({
|
||||
ctx: {
|
||||
token: ctx.session.token.access_token,
|
||||
userPoints: verifiedUser?.data.membership?.currentPoints ?? 0,
|
||||
token: token,
|
||||
userPoints: pointsValue ?? 0,
|
||||
},
|
||||
input,
|
||||
})
|
||||
|
||||
@@ -6,7 +6,8 @@ import { createLogger } from "@scandic-hotels/common/logger/createLogger"
|
||||
import { SEARCH_TYPE_REDEMPTION } from "../../../constants/booking"
|
||||
import { unauthorizedError } from "../../../errors"
|
||||
import { safeProtectedServiceProcedure } from "../../../procedures"
|
||||
import { getVerifiedUser } from "../../user/utils/getVerifiedUser"
|
||||
import { getRedemptionTokenSafely } from "../../../utils/getRedemptionTokenSafely"
|
||||
import { getUserPointsBalance } from "../../../utils/getUserPointsBalance"
|
||||
import { baseBookingSchema, baseRoomSchema, selectedRoomSchema } from "../input"
|
||||
import { getRoomsAvailability } from "../services/getRoomsAvailability"
|
||||
import { getSelectedRoomAvailability } from "../utils"
|
||||
@@ -24,13 +25,15 @@ export const myStay = safeProtectedServiceProcedure
|
||||
.use(async ({ ctx, input, next }) => {
|
||||
if (input.booking.searchType === SEARCH_TYPE_REDEMPTION) {
|
||||
if (ctx.session?.token.access_token) {
|
||||
const verifiedUser = await getVerifiedUser({ session: ctx.session })
|
||||
if (!verifiedUser?.error) {
|
||||
const pointsValue = await getUserPointsBalance(ctx.session)
|
||||
const token = getRedemptionTokenSafely(ctx.session, ctx.serviceToken)
|
||||
if (pointsValue && token) {
|
||||
return next({
|
||||
ctx: {
|
||||
token: ctx.session.token.access_token,
|
||||
userPoints: verifiedUser?.data.membership?.currentPoints ?? 0,
|
||||
token: token,
|
||||
userPoints: pointsValue ?? 0,
|
||||
},
|
||||
input,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,8 @@ import { Lang } from "@scandic-hotels/common/constants/language"
|
||||
import { SEARCH_TYPE_REDEMPTION } from "../../../../constants/booking"
|
||||
import { unauthorizedError } from "../../../../errors"
|
||||
import { safeProtectedServiceProcedure } from "../../../../procedures"
|
||||
import { getVerifiedUser } from "../../../user/utils/getVerifiedUser"
|
||||
import { getRedemptionTokenSafely } from "../../../../utils/getRedemptionTokenSafely"
|
||||
import { getUserPointsBalance } from "../../../../utils/getUserPointsBalance"
|
||||
import { baseBookingSchema, baseRoomSchema } from "../../input"
|
||||
import { getRoomsAvailability } from "../../services/getRoomsAvailability"
|
||||
import { mergeRoomTypes } from "../../utils"
|
||||
@@ -22,15 +23,15 @@ export const room = safeProtectedServiceProcedure
|
||||
.use(async ({ ctx, input, next }) => {
|
||||
if (input.booking.searchType === SEARCH_TYPE_REDEMPTION) {
|
||||
if (ctx.session?.token.access_token) {
|
||||
const verifiedUser = await getVerifiedUser({
|
||||
session: ctx.session,
|
||||
})
|
||||
if (!verifiedUser?.error) {
|
||||
const pointsValue = await getUserPointsBalance(ctx.session)
|
||||
const token = getRedemptionTokenSafely(ctx.session, ctx.serviceToken)
|
||||
if (pointsValue && token) {
|
||||
return next({
|
||||
ctx: {
|
||||
token: ctx.session.token.access_token,
|
||||
userPoints: verifiedUser?.data.membership?.currentPoints ?? 0,
|
||||
token: token,
|
||||
userPoints: pointsValue ?? 0,
|
||||
},
|
||||
input,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,8 @@ import "server-only"
|
||||
import { SEARCH_TYPE_REDEMPTION } from "../../../../../constants/booking"
|
||||
import { unauthorizedError } from "../../../../../errors"
|
||||
import { safeProtectedServiceProcedure } from "../../../../../procedures"
|
||||
import { getVerifiedUser } from "../../../../user/utils/getVerifiedUser"
|
||||
import { getRedemptionTokenSafely } from "../../../../../utils/getRedemptionTokenSafely"
|
||||
import { getUserPointsBalance } from "../../../../../utils/getUserPointsBalance"
|
||||
import { getRoomsAvailability } from "../../../services/getRoomsAvailability"
|
||||
import { mergeRoomTypes } from "../../../utils"
|
||||
import { selectRateRoomsAvailabilityInputSchema } from "./schema"
|
||||
@@ -13,15 +14,15 @@ export const rooms = safeProtectedServiceProcedure
|
||||
.use(async ({ ctx, input, next }) => {
|
||||
if (input.booking.searchType === SEARCH_TYPE_REDEMPTION) {
|
||||
if (ctx.session?.token.access_token) {
|
||||
const verifiedUser = await getVerifiedUser({
|
||||
session: ctx.session,
|
||||
})
|
||||
if (!verifiedUser?.error) {
|
||||
const pointsValue = await getUserPointsBalance(ctx.session)
|
||||
const token = getRedemptionTokenSafely(ctx.session, ctx.serviceToken)
|
||||
if (pointsValue && token) {
|
||||
return next({
|
||||
ctx: {
|
||||
token: ctx.session.token.access_token,
|
||||
userPoints: verifiedUser?.data.membership?.currentPoints ?? 0,
|
||||
token: token,
|
||||
userPoints: pointsValue ?? 0,
|
||||
},
|
||||
input,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
})
|
||||
|
||||
@@ -9,7 +9,7 @@ import { creditCardsSchema } from "../../routers/user/output"
|
||||
import { toApiLang } from "../../utils"
|
||||
import { encrypt } from "../../utils/encryption"
|
||||
import { isValidSession } from "../../utils/session"
|
||||
import { getUserSchema } from "./output"
|
||||
import { getVerifiedUser } from "./utils/getVerifiedUser"
|
||||
import { type FriendTransaction, getStaysSchema, type Stay } from "./output"
|
||||
|
||||
import type { Lang } from "@scandic-hotels/common/constants/language"
|
||||
@@ -29,78 +29,6 @@ export async function getMembershipNumber(
|
||||
return verifiedUser.data.membershipNumber
|
||||
}
|
||||
|
||||
export const getVerifiedUser = cache(
|
||||
async ({
|
||||
session,
|
||||
includeExtendedPartnerData,
|
||||
}: {
|
||||
session: Session
|
||||
includeExtendedPartnerData?: boolean
|
||||
}) => {
|
||||
const getVerifiedUserCounter = createCounter("user", "getVerifiedUser")
|
||||
const metricsGetVerifiedUser = getVerifiedUserCounter.init()
|
||||
|
||||
metricsGetVerifiedUser.start()
|
||||
|
||||
const now = Date.now()
|
||||
if (session.token.expires_at && session.token.expires_at < now) {
|
||||
metricsGetVerifiedUser.dataError(`Token expired`)
|
||||
return { error: true, cause: "token_expired" } as const
|
||||
}
|
||||
|
||||
const apiResponse = await api.get(
|
||||
api.endpoints.v2.Profile.profile,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${session.token.access_token}`,
|
||||
},
|
||||
},
|
||||
includeExtendedPartnerData
|
||||
? { includes: "extendedPartnerInformation" }
|
||||
: {}
|
||||
)
|
||||
|
||||
if (!apiResponse.ok) {
|
||||
await metricsGetVerifiedUser.httpError(apiResponse)
|
||||
|
||||
if (apiResponse.status === 401) {
|
||||
return { error: true, cause: "unauthorized" } as const
|
||||
} else if (apiResponse.status === 403) {
|
||||
return { error: true, cause: "forbidden" } as const
|
||||
} else if (apiResponse.status === 404) {
|
||||
return { error: true, cause: "notfound" } as const
|
||||
}
|
||||
|
||||
return {
|
||||
error: true,
|
||||
cause: "unknown",
|
||||
status: apiResponse.status,
|
||||
} as const
|
||||
}
|
||||
|
||||
const apiJson = await apiResponse.json()
|
||||
if (!apiJson.data?.attributes) {
|
||||
metricsGetVerifiedUser.dataError(
|
||||
`Missing data attributes in API response`,
|
||||
{
|
||||
data: apiJson,
|
||||
}
|
||||
)
|
||||
return null
|
||||
}
|
||||
const verifiedData = getUserSchema.safeParse(apiJson)
|
||||
|
||||
if (!verifiedData.success) {
|
||||
metricsGetVerifiedUser.validationError(verifiedData.error)
|
||||
return null
|
||||
}
|
||||
|
||||
metricsGetVerifiedUser.success()
|
||||
|
||||
return verifiedData
|
||||
}
|
||||
)
|
||||
|
||||
export async function getPreviousStays(
|
||||
accessToken: string,
|
||||
limit: number = 10,
|
||||
|
||||
23
packages/trpc/lib/utils/getRedemptionTokenSafely.ts
Normal file
23
packages/trpc/lib/utils/getRedemptionTokenSafely.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { isValidSession } from "./session"
|
||||
|
||||
import type { Session } from "next-auth"
|
||||
|
||||
export function getRedemptionTokenSafely(
|
||||
session: Session,
|
||||
serviceToken: string
|
||||
): string | undefined {
|
||||
if (!isValidSession(session)) return undefined
|
||||
|
||||
// ToDo- Get Curity based token when linked user is logged in
|
||||
// const token =
|
||||
// session.token.loginType === "sas"
|
||||
// ? session.token.curity_access_token ?? serviceToken
|
||||
// : session.token.access_token
|
||||
|
||||
const token =
|
||||
session.token.loginType === "sas"
|
||||
? serviceToken
|
||||
: session.token.access_token
|
||||
|
||||
return token
|
||||
}
|
||||
27
packages/trpc/lib/utils/getUserPointsBalance.ts
Normal file
27
packages/trpc/lib/utils/getUserPointsBalance.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { getEuroBonusProfileData } from "../routers/partners/sas/getEuroBonusProfile"
|
||||
import { getVerifiedUser } from "../routers/user/utils/getVerifiedUser"
|
||||
import { isValidSession } from "./session"
|
||||
|
||||
import type { Session } from "next-auth"
|
||||
|
||||
export async function getUserPointsBalance(
|
||||
session: Session | null
|
||||
): Promise<number | undefined> {
|
||||
if (!isValidSession(session)) return undefined
|
||||
|
||||
const verifiedUser =
|
||||
session.token.loginType === "sas"
|
||||
? await getEuroBonusProfileData(session)
|
||||
: await getVerifiedUser({ session })
|
||||
|
||||
if (!verifiedUser || "error" in verifiedUser) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
const points =
|
||||
"points" in verifiedUser.data
|
||||
? verifiedUser.data.points.total
|
||||
: verifiedUser.data.membership?.currentPoints
|
||||
|
||||
return points ?? 0
|
||||
}
|
||||
Reference in New Issue
Block a user