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,88 @@
import "server-only"
import { PaymentMethodEnum } from "@scandic-hotels/common/constants/paymentMethod"
import { createCounter } from "@scandic-hotels/common/telemetry"
import * as api from "../../../api"
import {
badGatewayError,
extractResponseDetails,
serverErrorByStatus,
} from "../../../errors"
import { toApiLang } from "../../../utils"
import { createBookingSchema } from "./schema"
import type { CreateBookingInput } from "../../../routers/booking/mutation/createBookingRoute/schema"
export async function createBooking(input: CreateBookingInput, token: string) {
validateInputData(input)
const createBookingCounter = createCounter("trpc.booking.create")
const metricsCreateBooking = createBookingCounter.init({
...input,
rooms: input.rooms.map(({ guest, ...room }) => {
const { becomeMember, membershipNumber } = guest
return { ...room, guest: { becomeMember, membershipNumber } }
}),
})
metricsCreateBooking.start()
const apiResponse = await api.post(
api.endpoints.v1.Booking.bookings,
{
headers: {
Authorization: `Bearer ${token}`,
},
body: input,
},
{ language: toApiLang(input.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
}
throw serverErrorByStatus(
apiResponse.status,
await extractResponseDetails(apiResponse),
"createBooking failed"
)
}
const apiJson = await apiResponse.json()
const verifiedData = createBookingSchema.safeParse(apiJson)
if (!verifiedData.success) {
metricsCreateBooking.validationError(verifiedData.error)
throw badGatewayError({
message: "Invalid response from createBooking",
errorDetails: { validationError: verifiedData.error },
})
}
metricsCreateBooking.success()
return verifiedData.data
}
function validateInputData(input: CreateBookingInput) {
if (!input.payment) {
return
}
if (input.payment.paymentMethod !== PaymentMethodEnum.PartnerPoints) {
return
}
if (!input.partnerSpecific?.eurobonusAccessToken) {
throw new Error(
"Missing partnerSpecific data for PartnerPoints payment method"
)
}
}

View File

@@ -0,0 +1,84 @@
import { z } from "zod"
import {
nullableStringEmailValidator,
nullableStringValidator,
} from "@scandic-hotels/common/utils/zod/stringValidator"
import { calculateRefId } from "../../../utils/refId"
const guestSchema = z.object({
email: nullableStringEmailValidator,
firstName: nullableStringValidator,
lastName: nullableStringValidator,
membershipNumber: nullableStringValidator,
phoneNumber: nullableStringValidator,
countryCode: nullableStringValidator,
})
export const createBookingSchema = z
.object({
data: z.object({
attributes: z.object({
reservationStatus: z.string(),
guest: guestSchema.optional(),
paymentUrl: z.string().nullable().optional(),
paymentMethod: 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,
paymentMethod: d.data.attributes.paymentMethod,
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,
}))