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
This commit is contained in:
@@ -6,3 +6,13 @@ export enum LoginTypeEnum {
|
||||
"eurobonus" = "eurobonus",
|
||||
}
|
||||
export type LoginType = keyof typeof LoginTypeEnum
|
||||
export type ScandicLoginType = Exclude<LoginType, "eurobonus">
|
||||
export type PartnerLoginType = Exclude<LoginType, ScandicLoginType>
|
||||
|
||||
export const partnerLoginTypes: PartnerLoginType[] = [LoginTypeEnum.eurobonus]
|
||||
|
||||
export function isPartnerLoginType(
|
||||
loginType: LoginType
|
||||
): loginType is PartnerLoginType {
|
||||
return partnerLoginTypes.includes(loginType as PartnerLoginType)
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ export const create = safeProtectedServiceProcedure
|
||||
|
||||
const createBookingCounter = createCounter("trpc.booking", "create")
|
||||
const user = await ctx.getScandicUser()
|
||||
|
||||
const metricsCreateBooking = createBookingCounter.init({
|
||||
membershipNumber: user?.membershipNumber,
|
||||
language,
|
||||
@@ -36,7 +37,7 @@ export const create = safeProtectedServiceProcedure
|
||||
|
||||
metricsCreateBooking.start()
|
||||
const headers = {
|
||||
Authorization: `Bearer ${ctx.token || ctx.serviceToken}`,
|
||||
Authorization: `Bearer ${ctx.token ?? ctx.serviceToken}`,
|
||||
}
|
||||
|
||||
const apiResponse = await api.post(
|
||||
|
||||
@@ -5,7 +5,6 @@ import { router } from "../../.."
|
||||
import * as api from "../../../api"
|
||||
import { createRefIdPlugin } from "../../../plugins/refIdToConfirmationNumber"
|
||||
import { safeProtectedServiceProcedure } from "../../../procedures"
|
||||
import { isValidSession } from "../../../utils/session"
|
||||
import {
|
||||
addPackageInput,
|
||||
cancelBookingsInput,
|
||||
@@ -14,7 +13,7 @@ import {
|
||||
updateBookingInput,
|
||||
} from "../input"
|
||||
import { bookingConfirmationSchema } from "../output"
|
||||
import { cancelBooking, isPartnerLoggedInUser } from "../utils"
|
||||
import { cancelBooking } from "../utils"
|
||||
import { createBookingSchema } from "./create/schema"
|
||||
import { create } from "./create"
|
||||
|
||||
@@ -26,10 +25,7 @@ export const bookingMutationRouter = router({
|
||||
priceChange: safeProtectedServiceProcedure
|
||||
.concat(refIdPlugin.toConfirmationNumber)
|
||||
.use(async ({ ctx, next }) => {
|
||||
const token =
|
||||
isValidSession(ctx.session) && !isPartnerLoggedInUser(ctx.session)
|
||||
? ctx.session.token.access_token
|
||||
: ctx.serviceToken
|
||||
const token = await ctx.getScandicUserToken()
|
||||
|
||||
return next({
|
||||
ctx: {
|
||||
@@ -38,13 +34,14 @@ export const bookingMutationRouter = router({
|
||||
})
|
||||
})
|
||||
.mutation(async function ({ ctx }) {
|
||||
const { confirmationNumber, token } = ctx
|
||||
const { confirmationNumber } = ctx
|
||||
|
||||
const priceChangeCounter = createCounter("trpc.booking", "price-change")
|
||||
const metricsPriceChange = priceChangeCounter.init({ confirmationNumber })
|
||||
|
||||
metricsPriceChange.start()
|
||||
|
||||
const token = ctx.token ?? ctx.serviceToken
|
||||
const headers = {
|
||||
Authorization: `Bearer ${token}`,
|
||||
}
|
||||
@@ -76,10 +73,7 @@ export const bookingMutationRouter = router({
|
||||
.input(cancelBookingsInput)
|
||||
.concat(refIdPlugin.toConfirmationNumbers)
|
||||
.use(async ({ ctx, next }) => {
|
||||
const token =
|
||||
isValidSession(ctx.session) && !isPartnerLoggedInUser(ctx.session)
|
||||
? ctx.session.token.access_token
|
||||
: ctx.serviceToken
|
||||
const token = await ctx.getScandicUserToken()
|
||||
|
||||
return next({
|
||||
ctx: {
|
||||
@@ -88,9 +82,10 @@ export const bookingMutationRouter = router({
|
||||
})
|
||||
})
|
||||
.mutation(async function ({ ctx, input }) {
|
||||
const { confirmationNumbers, token } = ctx
|
||||
const { confirmationNumbers } = ctx
|
||||
const { language } = input
|
||||
|
||||
const token = ctx.token ?? ctx.serviceToken
|
||||
const responses = await Promise.allSettled(
|
||||
confirmationNumbers.map((confirmationNumber) =>
|
||||
cancelBooking(confirmationNumber, language, token)
|
||||
@@ -120,11 +115,7 @@ export const bookingMutationRouter = router({
|
||||
.input(addPackageInput)
|
||||
.concat(refIdPlugin.toConfirmationNumber)
|
||||
.use(async ({ ctx, next }) => {
|
||||
const token =
|
||||
isValidSession(ctx.session) && !isPartnerLoggedInUser(ctx.session)
|
||||
? ctx.session.token.access_token
|
||||
: ctx.serviceToken
|
||||
|
||||
const token = await ctx.getScandicUserToken()
|
||||
return next({
|
||||
ctx: {
|
||||
token,
|
||||
@@ -132,7 +123,7 @@ export const bookingMutationRouter = router({
|
||||
})
|
||||
})
|
||||
.mutation(async function ({ ctx, input }) {
|
||||
const { confirmationNumber, token } = ctx
|
||||
const { confirmationNumber } = ctx
|
||||
const { language, refId, ...body } = input
|
||||
|
||||
const addPackageCounter = createCounter("trpc.booking", "package.add")
|
||||
@@ -143,6 +134,7 @@ export const bookingMutationRouter = router({
|
||||
|
||||
metricsAddPackage.start()
|
||||
|
||||
const token = ctx.token ?? ctx.serviceToken
|
||||
const headers = {
|
||||
Authorization: `Bearer ${token}`,
|
||||
}
|
||||
@@ -176,10 +168,7 @@ export const bookingMutationRouter = router({
|
||||
.input(guaranteeBookingInput)
|
||||
.concat(refIdPlugin.toConfirmationNumber)
|
||||
.use(async ({ ctx, next }) => {
|
||||
const token =
|
||||
isValidSession(ctx.session) && !isPartnerLoggedInUser(ctx.session)
|
||||
? ctx.session.token.access_token
|
||||
: ctx.serviceToken
|
||||
const token = await ctx.getScandicUserToken()
|
||||
|
||||
return next({
|
||||
ctx: {
|
||||
@@ -188,7 +177,7 @@ export const bookingMutationRouter = router({
|
||||
})
|
||||
})
|
||||
.mutation(async function ({ ctx, input }) {
|
||||
const { confirmationNumber, token } = ctx
|
||||
const { confirmationNumber } = ctx
|
||||
const { language, refId, ...body } = input
|
||||
|
||||
const guaranteeBookingCounter = createCounter("trpc.booking", "guarantee")
|
||||
@@ -199,6 +188,7 @@ export const bookingMutationRouter = router({
|
||||
|
||||
metricsGuaranteeBooking.start()
|
||||
|
||||
const token = ctx.token ?? ctx.serviceToken
|
||||
const headers = {
|
||||
Authorization: `Bearer ${token}`,
|
||||
}
|
||||
@@ -232,10 +222,7 @@ export const bookingMutationRouter = router({
|
||||
.input(updateBookingInput)
|
||||
.concat(refIdPlugin.toConfirmationNumber)
|
||||
.use(async ({ ctx, next }) => {
|
||||
const token =
|
||||
isValidSession(ctx.session) && !isPartnerLoggedInUser(ctx.session)
|
||||
? ctx.session.token.access_token
|
||||
: ctx.serviceToken
|
||||
const token = await ctx.getScandicUserToken()
|
||||
|
||||
return next({
|
||||
ctx: {
|
||||
@@ -244,7 +231,7 @@ export const bookingMutationRouter = router({
|
||||
})
|
||||
})
|
||||
.mutation(async function ({ ctx, input }) {
|
||||
const { confirmationNumber, token } = ctx
|
||||
const { confirmationNumber } = ctx
|
||||
const { language, refId, ...body } = input
|
||||
|
||||
const updateBookingCounter = createCounter("trpc.booking", "update")
|
||||
@@ -254,7 +241,7 @@ export const bookingMutationRouter = router({
|
||||
})
|
||||
|
||||
metricsUpdateBooking.start()
|
||||
|
||||
const token = ctx.token ?? ctx.serviceToken
|
||||
const apiResponse = await api.put(
|
||||
api.endpoints.v1.Booking.booking(confirmationNumber),
|
||||
{
|
||||
@@ -287,10 +274,7 @@ export const bookingMutationRouter = router({
|
||||
.input(removePackageInput)
|
||||
.concat(refIdPlugin.toConfirmationNumber)
|
||||
.use(async ({ ctx, next }) => {
|
||||
const token =
|
||||
isValidSession(ctx.session) && !isPartnerLoggedInUser(ctx.session)
|
||||
? ctx.session.token.access_token
|
||||
: ctx.serviceToken
|
||||
const token = await ctx.getScandicUserToken()
|
||||
|
||||
return next({
|
||||
ctx: {
|
||||
@@ -299,7 +283,7 @@ export const bookingMutationRouter = router({
|
||||
})
|
||||
})
|
||||
.mutation(async function ({ ctx, input }) {
|
||||
const { confirmationNumber, token } = ctx
|
||||
const { confirmationNumber } = ctx
|
||||
const { codes, language } = input
|
||||
|
||||
const removePackageCounter = createCounter(
|
||||
@@ -314,6 +298,7 @@ export const bookingMutationRouter = router({
|
||||
|
||||
metricsRemovePackage.start()
|
||||
|
||||
const token = ctx.token ?? ctx.serviceToken
|
||||
const headers = {
|
||||
Authorization: `Bearer ${token}`,
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ import {
|
||||
getBookingStatusInput,
|
||||
getLinkedReservationsInput,
|
||||
} from "./input"
|
||||
import { findBooking, getBooking, isPartnerLoggedInUser } from "./utils"
|
||||
import { findBooking, getBooking } from "./utils"
|
||||
|
||||
const refIdPlugin = createRefIdPlugin()
|
||||
|
||||
@@ -31,10 +31,7 @@ export const bookingQueryRouter = router({
|
||||
.concat(refIdPlugin.toConfirmationNumber)
|
||||
.use(async ({ ctx, input, next }) => {
|
||||
const lang = input.lang ?? ctx.lang
|
||||
const token =
|
||||
isValidSession(ctx.session) && !isPartnerLoggedInUser(ctx.session)
|
||||
? ctx.session.token.access_token
|
||||
: ctx.serviceToken
|
||||
const token = await ctx.getScandicUserToken()
|
||||
|
||||
return next({
|
||||
ctx: {
|
||||
@@ -51,7 +48,11 @@ export const bookingQueryRouter = router({
|
||||
|
||||
metricsGetBooking.start()
|
||||
|
||||
const booking = await getBooking(confirmationNumber, lang, token)
|
||||
const booking = await getBooking(
|
||||
confirmationNumber,
|
||||
lang,
|
||||
token ?? serviceToken
|
||||
)
|
||||
|
||||
if (!booking) {
|
||||
metricsGetBooking.dataError(
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { LoginTypeEnum } from "@scandic-hotels/common/constants/loginType"
|
||||
import { createCounter } from "@scandic-hotels/common/telemetry"
|
||||
|
||||
import * as api from "../../api"
|
||||
@@ -8,7 +7,6 @@ import { createBookingSchema } from "./mutation/create/schema"
|
||||
import { bookingConfirmationSchema } from "./output"
|
||||
|
||||
import type { Lang } from "@scandic-hotels/common/constants/language"
|
||||
import type { Session } from "next-auth"
|
||||
|
||||
export async function getBooking(
|
||||
confirmationNumber: string,
|
||||
@@ -159,9 +157,3 @@ export async function cancelBooking(
|
||||
|
||||
return verifiedData.data
|
||||
}
|
||||
|
||||
// ToDo - Update the function to return true for Scandic site and
|
||||
// in case of Partner sites fetch the Scandic Curity token for linked user and service token for unlinked user
|
||||
export function isPartnerLoggedInUser(session: Session) {
|
||||
return session.token.loginType === LoginTypeEnum.eurobonus
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ export function isValidSession(session: Session | null): session is Session {
|
||||
if (!session) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (session.error) {
|
||||
sessionLogger.error(`Session error: ${session.error}`)
|
||||
return false
|
||||
|
||||
Reference in New Issue
Block a user