Merged in feature/SW-3516-pass-eurobonus-number-on-booking (pull request #2902)

* feat(SW-3516): Include partnerLoyaltyNumber on bookings

- Added user context to BookingFlowProviders for user state management.
- Updated booking input and output schemas to accommodate new user data.
- Refactored booking mutation logic to include user-related information.
- Improved type definitions for better TypeScript support across booking components.


Approved-by: Anton Gunnarsson
This commit is contained in:
Joakim Jäderberg
2025-10-08 10:48:42 +00:00
parent b685c928e4
commit 17df3ee71a
18 changed files with 510 additions and 370 deletions

View File

@@ -17,6 +17,7 @@ type CreateContextOptions = {
url: string
webToken?: string
contentType?: string
app: "scandic-web" | "partner-sas"
}
export function createContext(opts: CreateContextOptions) {

View File

@@ -3,106 +3,6 @@ import { z } from "zod"
import { Lang } from "@scandic-hotels/common/constants/language"
import { langToApiLang } from "../../constants/apiLang"
import { ChildBedTypeEnum } from "../../enums/childBedTypeEnum"
const roomsSchema = z
.array(
z.object({
adults: z.number().int().nonnegative(),
bookingCode: z.string().nullish(),
childrenAges: z
.array(
z.object({
age: z.number().int().nonnegative(),
bedType: z.nativeEnum(ChildBedTypeEnum),
})
)
.default([]),
rateCode: z.string(),
redemptionCode: z.string().optional(),
roomTypeCode: z.coerce.string(),
guest: z.object({
becomeMember: z.boolean(),
countryCode: z.string(),
dateOfBirth: z.string().nullish(),
email: z.string().email(),
firstName: z.string(),
lastName: z.string(),
membershipNumber: z.string().nullish(),
postalCode: z.string().nullish(),
phoneNumber: z.string(),
}),
smsConfirmationRequested: z.boolean(),
specialRequest: z.object({
comment: z.string().optional(),
}),
packages: z.object({
breakfast: z.boolean(),
allergyFriendly: z.boolean(),
petFriendly: z.boolean(),
accessibility: z.boolean(),
}),
roomPrice: z.object({
memberPrice: z.number().nullish(),
publicPrice: z.number().nullish(),
}),
})
)
.superRefine((data, ctx) => {
data.forEach((room, idx) => {
if (idx === 0 && room.guest.becomeMember) {
if (!room.guest.dateOfBirth) {
ctx.addIssue({
code: z.ZodIssueCode.invalid_type,
expected: "string",
received: typeof room.guest.dateOfBirth,
path: ["guest", "dateOfBirth"],
})
}
if (!room.guest.postalCode) {
ctx.addIssue({
code: z.ZodIssueCode.invalid_type,
expected: "string",
received: typeof room.guest.postalCode,
path: ["guest", "postalCode"],
})
}
}
})
})
const paymentSchema = z.object({
paymentMethod: z.string(),
card: z
.object({
alias: z.string(),
expiryDate: z.string(),
cardType: z.string(),
})
.optional(),
cardHolder: z
.object({
email: z.string().email(),
name: z.string(),
phoneCountryCode: z.string(),
phoneSubscriber: z.string(),
})
.optional(),
success: z.string(),
error: z.string(),
cancel: z.string(),
})
// Mutation
export const createBookingInput = z.object({
hotelId: z.string(),
checkInDate: z.string(),
checkOutDate: z.string(),
rooms: roomsSchema,
payment: paymentSchema.optional(),
language: z.nativeEnum(Lang).transform((val) => langToApiLang[val]),
})
export const addPackageInput = z.object({
ancillaryComment: z.string(),

View File

@@ -0,0 +1,83 @@
import "server-only"
import { createCounter } from "@scandic-hotels/common/telemetry"
import * as api from "../../../../api"
import { safeProtectedServiceProcedure } from "../../../../procedures"
import { encrypt } from "../../../../utils/encryption"
import { isValidSession } from "../../../../utils/session"
import { getMembershipNumber } from "../../../user/utils"
import { createBookingInput, createBookingSchema } from "./schema"
export const create = safeProtectedServiceProcedure
.input(createBookingInput)
.use(async ({ ctx, next }) => {
const token = isValidSession(ctx.session)
? ctx.session.token.access_token
: ctx.serviceToken
return next({
ctx: {
token,
},
})
})
.mutation(async function ({ ctx, input }) {
const { language, ...inputWithoutLang } = input
const { rooms, ...loggableInput } = inputWithoutLang
const createBookingCounter = createCounter("trpc.booking", "create")
const metricsCreateBooking = createBookingCounter.init({
membershipNumber: await getMembershipNumber(ctx.session),
language,
...loggableInput,
rooms: inputWithoutLang.rooms.map(({ guest, ...room }) => {
const { becomeMember, membershipNumber } = guest
return { ...room, guest: { becomeMember, membershipNumber } }
}),
})
metricsCreateBooking.start()
const headers = {
Authorization: `Bearer ${ctx.token}`,
}
const apiResponse = await api.post(
api.endpoints.v1.Booking.bookings,
{
headers,
body: inputWithoutLang,
},
{ language }
)
if (!apiResponse.ok) {
await metricsCreateBooking.httpError(apiResponse)
const apiJson = await apiResponse.json()
if ("errors" in apiJson && apiJson.errors.length) {
const error = apiJson.errors[0]
return { error: true, cause: error.code } as const
}
return null
}
const apiJson = await apiResponse.json()
const verifiedData = createBookingSchema.safeParse(apiJson)
if (!verifiedData.success) {
metricsCreateBooking.validationError(verifiedData.error)
return null
}
metricsCreateBooking.success()
const expire = Math.floor(Date.now() / 1000) + 60 // 1 minute expiry
return {
booking: verifiedData.data,
sig: encrypt(expire.toString()),
}
})

View File

@@ -0,0 +1,173 @@
import { z } from "zod"
import { Lang } from "@scandic-hotels/common/constants/language"
import { langToApiLang } from "../../../../constants/apiLang"
import { ChildBedTypeEnum } from "../../../../enums/childBedTypeEnum"
import { calculateRefId } from "../../../../utils/refId"
import { guestSchema } from "../../output"
const roomsSchema = z
.array(
z.object({
adults: z.number().int().nonnegative(),
bookingCode: z.string().nullish(),
childrenAges: z
.array(
z.object({
age: z.number().int().nonnegative(),
bedType: z.nativeEnum(ChildBedTypeEnum),
})
)
.default([]),
rateCode: z.string(),
redemptionCode: z.string().optional(),
roomTypeCode: z.coerce.string(),
guest: z.object({
becomeMember: z.boolean(),
countryCode: z.string(),
dateOfBirth: z.string().nullish(),
email: z.string().email(),
firstName: z.string(),
lastName: z.string(),
membershipNumber: z.string().nullish(),
postalCode: z.string().nullish(),
phoneNumber: z.string(),
partnerLoyaltyNumber: z.string().nullable(),
}),
smsConfirmationRequested: z.boolean(),
specialRequest: z.object({
comment: z.string().optional(),
}),
packages: z.object({
breakfast: z.boolean(),
allergyFriendly: z.boolean(),
petFriendly: z.boolean(),
accessibility: z.boolean(),
}),
roomPrice: z.object({
memberPrice: z.number().nullish(),
publicPrice: z.number().nullish(),
}),
})
)
.superRefine((data, ctx) => {
data.forEach((room, idx) => {
if (idx === 0 && room.guest.becomeMember) {
if (!room.guest.dateOfBirth) {
ctx.addIssue({
code: z.ZodIssueCode.invalid_type,
expected: "string",
received: typeof room.guest.dateOfBirth,
path: ["guest", "dateOfBirth"],
})
}
if (!room.guest.postalCode) {
ctx.addIssue({
code: z.ZodIssueCode.invalid_type,
expected: "string",
received: typeof room.guest.postalCode,
path: ["guest", "postalCode"],
})
}
}
})
})
const paymentSchema = z.object({
paymentMethod: z.string(),
card: z
.object({
alias: z.string(),
expiryDate: z.string(),
cardType: z.string(),
})
.optional(),
cardHolder: z
.object({
email: z.string().email(),
name: z.string(),
phoneCountryCode: z.string(),
phoneSubscriber: z.string(),
})
.optional(),
success: z.string(),
error: z.string(),
cancel: z.string(),
})
export type CreateBookingInput = z.input<typeof createBookingInput>
export const createBookingInput = z.object({
hotelId: z.string(),
checkInDate: z.string(),
checkOutDate: z.string(),
rooms: roomsSchema,
payment: paymentSchema.optional(),
language: z.nativeEnum(Lang).transform((val) => langToApiLang[val]),
})
export const createBookingSchema = z
.object({
data: z.object({
attributes: z.object({
reservationStatus: z.string(),
guest: guestSchema.optional(),
paymentUrl: z.string().nullable().optional(),
rooms: z
.array(
z.object({
confirmationNumber: z.string(),
cancellationNumber: z.string().nullable(),
priceChangedMetadata: z
.object({
roomPrice: z.number(),
totalPrice: z.number(),
})
.nullable()
.optional(),
})
)
.default([]),
errors: z
.array(
z.object({
confirmationNumber: z.string().nullable().optional(),
errorCode: z.string(),
description: z.string().nullable().optional(),
meta: z
.record(z.string(), z.union([z.string(), z.number()]))
.nullable()
.optional(),
})
)
.default([]),
}),
type: z.string(),
id: z.string(),
links: z.object({
self: z.object({
href: z.string().url(),
meta: z.object({
method: z.string(),
}),
}),
}),
}),
})
.transform((d) => ({
id: d.data.id,
links: d.data.links,
type: d.data.type,
reservationStatus: d.data.attributes.reservationStatus,
paymentUrl: d.data.attributes.paymentUrl,
rooms: d.data.attributes.rooms.map((room) => {
const lastName = d.data.attributes.guest?.lastName ?? ""
return {
...room,
refId: calculateRefId(room.confirmationNumber, lastName),
}
}),
errors: d.data.attributes.errors,
guest: d.data.attributes.guest,
}))

View File

@@ -1,100 +1,28 @@
import { createLogger } from "@scandic-hotels/common/logger/createLogger"
import { createCounter } from "@scandic-hotels/common/telemetry"
import { router } from "../.."
import * as api from "../../api"
import { createRefIdPlugin } from "../../plugins/refIdToConfirmationNumber"
import { safeProtectedServiceProcedure } from "../../procedures"
import { encrypt } from "../../utils/encryption"
import { isValidSession } from "../../utils/session"
import { getMembershipNumber } from "../user/utils/getMemberShipNumber"
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,
createBookingInput,
guaranteeBookingInput,
removePackageInput,
updateBookingInput,
} from "./input"
import { bookingConfirmationSchema, createBookingSchema } from "./output"
import { cancelBooking } from "./utils"
} from "../input"
import { bookingConfirmationSchema } from "../output"
import { cancelBooking } from "../utils"
import { createBookingSchema } from "./create/schema"
import { create } from "./create"
const refIdPlugin = createRefIdPlugin()
const bookingLogger = createLogger("trpc.booking")
export const bookingMutationRouter = router({
create: safeProtectedServiceProcedure
.input(createBookingInput)
.use(async ({ ctx, next }) => {
const token = isValidSession(ctx.session)
? ctx.session.token.access_token
: ctx.serviceToken
return next({
ctx: {
token,
},
})
})
.mutation(async function ({ ctx, input }) {
const { language, ...inputWithoutLang } = input
const { rooms, ...loggableInput } = inputWithoutLang
const createBookingCounter = createCounter("trpc.booking", "create")
const metricsCreateBooking = createBookingCounter.init({
membershipNumber: await getMembershipNumber(ctx.session),
language,
...loggableInput,
rooms: inputWithoutLang.rooms.map(({ guest, ...room }) => {
const { becomeMember, membershipNumber } = guest
return { ...room, guest: { becomeMember, membershipNumber } }
}),
})
metricsCreateBooking.start()
const headers = {
Authorization: `Bearer ${ctx.token}`,
}
const apiResponse = await api.post(
api.endpoints.v1.Booking.bookings,
{
headers,
body: inputWithoutLang,
},
{ language }
)
if (!apiResponse.ok) {
await metricsCreateBooking.httpError(apiResponse)
const apiJson = await apiResponse.json()
if ("errors" in apiJson && apiJson.errors.length) {
const error = apiJson.errors[0]
return { error: true, cause: error.code } as const
}
return null
}
const apiJson = await apiResponse.json()
const verifiedData = createBookingSchema.safeParse(apiJson)
if (!verifiedData.success) {
metricsCreateBooking.validationError(verifiedData.error)
return null
}
metricsCreateBooking.success()
const expire = Math.floor(Date.now() / 1000) + 60 // 1 minute expiry
return {
booking: verifiedData.data,
sig: encrypt(expire.toString()),
}
}),
create,
priceChange: safeProtectedServiceProcedure
.concat(refIdPlugin.toConfirmationNumber)
.use(async ({ ctx, next }) => {

View File

@@ -13,7 +13,7 @@ import { BreakfastPackageEnum } from "../../enums/breakfast"
import { ChildBedTypeEnum } from "../../enums/childBedTypeEnum"
import { calculateRefId } from "../../utils/refId"
const guestSchema = z.object({
export const guestSchema = z.object({
email: nullableStringEmailValidator,
firstName: nullableStringValidator,
lastName: nullableStringValidator,
@@ -24,72 +24,6 @@ const guestSchema = z.object({
export type Guest = z.output<typeof guestSchema>
// MUTATION
export const createBookingSchema = z
.object({
data: z.object({
attributes: z.object({
reservationStatus: z.string(),
guest: guestSchema.optional(),
paymentUrl: z.string().nullable().optional(),
rooms: z
.array(
z.object({
confirmationNumber: z.string(),
cancellationNumber: z.string().nullable(),
priceChangedMetadata: z
.object({
roomPrice: z.number(),
totalPrice: z.number(),
})
.nullable()
.optional(),
})
)
.default([]),
errors: z
.array(
z.object({
confirmationNumber: z.string().nullable().optional(),
errorCode: z.string(),
description: z.string().nullable().optional(),
meta: z
.record(z.string(), z.union([z.string(), z.number()]))
.nullable()
.optional(),
})
)
.default([]),
}),
type: z.string(),
id: z.string(),
links: z.object({
self: z.object({
href: z.string().url(),
meta: z.object({
method: z.string(),
}),
}),
}),
}),
})
.transform((d) => ({
id: d.data.id,
links: d.data.links,
type: d.data.type,
reservationStatus: d.data.attributes.reservationStatus,
paymentUrl: d.data.attributes.paymentUrl,
rooms: d.data.attributes.rooms.map((room) => {
const lastName = d.data.attributes.guest?.lastName ?? ""
return {
...room,
refId: calculateRefId(room.confirmationNumber, lastName),
}
}),
errors: d.data.attributes.errors,
guest: d.data.attributes.guest,
}))
// QUERY
const childBedPreferencesSchema = z.object({
bedType: z.nativeEnum(ChildBedTypeEnum),

View File

@@ -12,6 +12,7 @@ import { toApiLang } from "../../utils"
import { encrypt } from "../../utils/encryption"
import { isValidSession } from "../../utils/session"
import { getHotel } from "../hotels/services/getHotel"
import { createBookingSchema } from "./mutation/create/schema"
import { getHotelRoom } from "./helpers"
import {
createRefIdInput,
@@ -20,7 +21,6 @@ import {
getBookingStatusInput,
getLinkedReservationsInput,
} from "./input"
import { createBookingSchema } from "./output"
import { findBooking, getBooking } from "./utils"
const refIdPlugin = createRefIdPlugin()

View File

@@ -3,7 +3,8 @@ import { createCounter } from "@scandic-hotels/common/telemetry"
import * as api from "../../api"
import { badRequestError, serverErrorByStatus } from "../../errors"
import { toApiLang } from "../../utils"
import { bookingConfirmationSchema, createBookingSchema } from "./output"
import { createBookingSchema } from "./mutation/create/schema"
import { bookingConfirmationSchema } from "./output"
import type { Lang } from "@scandic-hotels/common/constants/language"