Chore/refactor hotel trpc routes * chore(SW-3519): refactor trpc hotel routers * chore(SW-3519): refactor trpc hotel routers * refactor * merge * Merge branch 'master' of bitbucket.org:scandic-swap/web into chore/refactor-hotel-trpc-routes Approved-by: Linus Flood
59 lines
1.7 KiB
TypeScript
59 lines
1.7 KiB
TypeScript
import "server-only"
|
|
|
|
import { SEARCH_TYPE_REDEMPTION } from "../../../../../constants/booking"
|
|
import { unauthorizedError } from "../../../../../errors"
|
|
import { safeProtectedServiceProcedure } from "../../../../../procedures"
|
|
import { getVerifiedUser } from "../../../../user/utils/getVerifiedUser"
|
|
import { getRoomsAvailability } from "../../../services/getRoomsAvailability"
|
|
import { mergeRoomTypes } from "../../../utils"
|
|
import { selectRateRoomsAvailabilityInputSchema } from "./schema"
|
|
|
|
export const rooms = safeProtectedServiceProcedure
|
|
.input(selectRateRoomsAvailabilityInputSchema)
|
|
.use(async ({ ctx, input, next }) => {
|
|
if (input.booking.searchType === SEARCH_TYPE_REDEMPTION) {
|
|
if (ctx.session?.token.access_token) {
|
|
const verifiedUser = await getVerifiedUser({
|
|
session: ctx.session,
|
|
})
|
|
if (!verifiedUser?.error) {
|
|
return next({
|
|
ctx: {
|
|
token: ctx.session.token.access_token,
|
|
userPoints: verifiedUser?.data.membership?.currentPoints ?? 0,
|
|
},
|
|
})
|
|
}
|
|
}
|
|
throw unauthorizedError()
|
|
}
|
|
return next({
|
|
ctx: {
|
|
token: ctx.serviceToken,
|
|
},
|
|
})
|
|
})
|
|
.query(async function ({ ctx, input }) {
|
|
input.booking.rooms = input.booking.rooms.map((room) => ({
|
|
...room,
|
|
bookingCode: room.bookingCode || input.booking.bookingCode,
|
|
}))
|
|
|
|
const availability = await getRoomsAvailability(
|
|
input,
|
|
ctx.token,
|
|
ctx.serviceToken,
|
|
ctx.userPoints
|
|
)
|
|
|
|
for (const room of availability) {
|
|
if (!room || "error" in room) {
|
|
continue
|
|
}
|
|
|
|
room.roomConfigurations = mergeRoomTypes(room.roomConfigurations)
|
|
}
|
|
|
|
return availability
|
|
})
|