Merged in chore/refactor-trpc-booking-routes (pull request #3510)

feat(BOOK-750): refactor booking endpoints

* WIP

* wip

* wip

* parse dates in UTC

* wip

* no more errors

* Merge branch 'master' of bitbucket.org:scandic-swap/web into chore/refactor-trpc-booking-routes

* .

* cleanup

* import named z from zod

* fix(BOOK-750): updateBooking api endpoint expects dateOnly, we passed ISO date


Approved-by: Anton Gunnarsson
This commit is contained in:
Joakim Jäderberg
2026-02-02 14:28:14 +00:00
parent 8ac2c4ba22
commit 16cc26632e
44 changed files with 1621 additions and 1041 deletions

View File

@@ -0,0 +1,46 @@
import "server-only"
import { PaymentMethodEnum } from "@scandic-hotels/common/constants/paymentMethod"
import { safeProtectedServiceProcedure } from "../../../../procedures"
import { createBooking } from "../../../../services/booking/createBooking"
import { encrypt } from "../../../../utils/encryption"
import { createBookingInput } from "./schema"
export const createBookingRoute = safeProtectedServiceProcedure
.input(createBookingInput)
.use(async ({ ctx, next }) => {
const token = await ctx.getScandicUserToken()
return next({
ctx: {
token,
},
})
})
.mutation(async function ({ ctx, input }) {
if (input.payment?.paymentMethod === PaymentMethodEnum.PartnerPoints) {
const session = await ctx.auth()
const token = session?.token.access_token
if (!token) {
throw new Error(
"Cannot create booking with partner points without partner token"
)
}
input.partnerSpecific = {
eurobonusAccessToken: session?.token.access_token,
}
}
const booking = await createBooking(input, ctx.token ?? ctx.serviceToken)
if ("error" in booking) {
return { ...booking }
}
const expire = Math.floor(Date.now() / 1000) + 60 // 1 minute expiry
return {
booking,
sig: encrypt(expire.toString()),
}
})

View File

@@ -0,0 +1,111 @@
import { z } from "zod"
import { Lang } from "@scandic-hotels/common/constants/language"
import { PaymentMethodEnum } from "@scandic-hotels/common/constants/paymentMethod"
import { ChildBedTypeEnum } from "../../../../enums/childBedTypeEnum"
const paymentSchema = z.object({
paymentMethod: z.nativeEnum(PaymentMethodEnum),
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(),
})
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"],
})
}
}
})
})
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),
partnerSpecific: z
.object({
eurobonusAccessToken: z.string(),
})
.optional(),
})