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,87 @@
import { createCounter } from "@scandic-hotels/common/telemetry"
import { notFoundError } from "../../../errors"
import { safeProtectedServiceProcedure } from "../../../procedures"
import { findBooking } from "../../../services/booking/findBooking"
import { isValidSession } from "../../../utils/session"
import { getHotelPageUrls } from "../../contentstack/hotelPage/utils"
import { getHotel } from "../../hotels/services/getHotel"
import { getHotelRoom } from "../helpers"
import { findBookingInput } from "../input"
export const findBookingRoute = safeProtectedServiceProcedure
.input(findBookingInput)
.use(async ({ ctx, input, next }) => {
const lang = input.lang ?? ctx.lang
const token = isValidSession(ctx.session)
? ctx.session.token.access_token
: ctx.serviceToken
return next({
ctx: {
lang,
token,
},
})
})
.query(async function ({
ctx,
input: { confirmationNumber, lastName, firstName, email },
}) {
const { lang, token, serviceToken } = ctx
const findBookingCounter = createCounter("trpc.booking.findBooking")
const metricsFindBooking = findBookingCounter.init({ confirmationNumber })
metricsFindBooking.start()
const booking = await findBooking(
{ confirmationNumber, lang, lastName, firstName, email },
token
)
if (!booking) {
metricsFindBooking.dataError(
`Fail to find booking data for ${confirmationNumber}`,
{ confirmationNumber }
)
return null
}
const [hotelData, hotelPages] = await Promise.all([
getHotel(
{
hotelId: booking.hotelId,
isCardOnlyPayment: false,
language: lang,
},
serviceToken
),
getHotelPageUrls(lang),
])
const hotelPage = hotelPages.find(
(page) => page.hotelId === booking.hotelId
)
if (!hotelData) {
metricsFindBooking.dataError(
`Failed to find hotel data for ${booking.hotelId}`,
{
hotelId: booking.hotelId,
}
)
throw notFoundError({
message: "Hotel data not found",
errorDetails: { hotelId: booking.hotelId },
})
}
metricsFindBooking.success()
return {
...hotelData,
url: hotelPage?.url || null,
booking,
room: getHotelRoom(hotelData.roomCategories, booking.roomTypeCode),
}
})

View File

@@ -0,0 +1,84 @@
import { createCounter } from "@scandic-hotels/common/telemetry"
import { notFoundError } from "../../../errors"
import { createRefIdPlugin } from "../../../plugins/refIdToConfirmationNumber"
import { safeProtectedServiceProcedure } from "../../../procedures"
import { getBooking } from "../../../services/booking/getBooking"
import { getHotelPageUrls } from "../../contentstack/hotelPage/utils"
import { getHotel } from "../../hotels/services/getHotel"
import { getHotelRoom } from "../helpers"
import { getBookingInput } from "../input"
const refIdPlugin = createRefIdPlugin()
export const getBookingRoute = safeProtectedServiceProcedure
.input(getBookingInput)
.concat(refIdPlugin.toConfirmationNumber)
.use(async ({ ctx, input, next }) => {
const lang = input.lang ?? ctx.lang
const token = await ctx.getScandicUserToken()
return next({
ctx: {
lang,
token,
},
})
})
.query(async function ({ ctx }) {
const { confirmationNumber, lang, token, serviceToken } = ctx
const getBookingCounter = createCounter("trpc.booking.get")
const metricsGetBooking = getBookingCounter.init({ confirmationNumber })
metricsGetBooking.start()
const booking = await getBooking(
{ confirmationNumber, lang },
token ?? serviceToken
)
if (!booking) {
metricsGetBooking.dataError(
`Fail to get booking data for ${confirmationNumber}`,
{ confirmationNumber }
)
return null
}
const [hotelData, hotelPages] = await Promise.all([
getHotel(
{
hotelId: booking.hotelId,
isCardOnlyPayment: false,
language: lang,
},
serviceToken
),
getHotelPageUrls(lang),
])
const hotelPage = hotelPages.find(
(page) => page.hotelId === booking.hotelId
)
if (!hotelData) {
metricsGetBooking.dataError(
`Failed to get hotel data for ${booking.hotelId}`,
{
hotelId: booking.hotelId,
}
)
throw notFoundError({
message: "Hotel data not found",
errorDetails: { hotelId: booking.hotelId },
})
}
metricsGetBooking.success()
return {
...hotelData,
url: hotelPage?.url || null,
booking,
room: getHotelRoom(hotelData.roomCategories, booking.roomTypeCode),
}
})

View File

@@ -0,0 +1,33 @@
import { z } from "zod"
import { Lang } from "@scandic-hotels/common/constants/language"
import { createRefIdPlugin } from "../../../plugins/refIdToConfirmationNumber"
import { safeProtectedServiceProcedure } from "../../../procedures"
import { getBookingStatus } from "../../../services/booking/getBookingStatus"
import { encrypt } from "../../../utils/encryption"
const getBookingStatusInput = z.object({
lang: z.nativeEnum(Lang).optional(),
})
const refIdPlugin = createRefIdPlugin()
export const getBookingStatusRoute = safeProtectedServiceProcedure
.input(getBookingStatusInput)
.concat(refIdPlugin.toConfirmationNumber)
.query(async function ({ ctx, input }) {
const lang = input.lang ?? ctx.lang
const { confirmationNumber } = ctx
const booking = await getBookingStatus(
{ confirmationNumber, lang },
ctx.serviceToken
)
const expire = Math.floor(Date.now() / 1000) + 60 // 1 minute expiry
return {
booking,
sig: encrypt(expire.toString()),
}
})

View File

@@ -0,0 +1,34 @@
import { createRefIdPlugin } from "../../../plugins/refIdToConfirmationNumber"
import { safeProtectedServiceProcedure } from "../../../procedures"
import { getLinkedReservations } from "../../../services/booking/linkedReservations"
import { isValidSession } from "../../../utils/session"
import { getLinkedReservationsInput } from "../input"
const refIdPlugin = createRefIdPlugin()
export const getLinkedReservationsRoute = safeProtectedServiceProcedure
.input(getLinkedReservationsInput)
.concat(refIdPlugin.toConfirmationNumber)
.use(async ({ ctx, input, next }) => {
const lang = input.lang ?? ctx.lang
const token = isValidSession(ctx.session)
? ctx.session.token.access_token
: ctx.serviceToken
return next({
ctx: {
lang,
token,
},
})
})
.query(async function ({ ctx }) {
const { confirmationNumber, lang, token } = ctx
return getLinkedReservations(
{
confirmationNumber,
lang,
},
token
)
})