fix(SW-3667): Remove conditional on Scandic user token * Remove conditional on Scandic user token Approved-by: Joakim Jäderberg
67 lines
1.7 KiB
TypeScript
67 lines
1.7 KiB
TypeScript
import "server-only"
|
|
|
|
import { SEARCH_TYPE_REDEMPTION } from "../../../../../constants/booking"
|
|
import { unauthorizedError } from "../../../../../errors"
|
|
import { safeProtectedServiceProcedure } from "../../../../../procedures"
|
|
import { isValidSession } from "../../../../../utils/session"
|
|
import { getRoomsAvailability } from "../../../services/getRoomsAvailability"
|
|
import { mergeRoomTypes } from "../../../utils"
|
|
import { selectRateRoomsAvailabilityInputSchema } from "./schema"
|
|
|
|
type Context = {
|
|
userToken: string | null
|
|
userPoints?: number
|
|
}
|
|
|
|
export const rooms = safeProtectedServiceProcedure
|
|
.input(selectRateRoomsAvailabilityInputSchema)
|
|
.use(async ({ ctx, input, next }) => {
|
|
const userToken = await ctx.getScandicUserToken()
|
|
|
|
if (input.booking.searchType === SEARCH_TYPE_REDEMPTION) {
|
|
const hasValidSession = isValidSession(ctx.session)
|
|
if (!hasValidSession) {
|
|
throw unauthorizedError()
|
|
}
|
|
|
|
const pointsValue = await ctx.getUserPointsBalance()
|
|
if (pointsValue) {
|
|
return next<Context>({
|
|
ctx: {
|
|
userToken,
|
|
userPoints: pointsValue,
|
|
},
|
|
})
|
|
}
|
|
}
|
|
|
|
return next<Context>({
|
|
ctx: {
|
|
userToken,
|
|
},
|
|
})
|
|
})
|
|
.query(async function ({ ctx, input }) {
|
|
input.booking.rooms = input.booking.rooms.map((room) => ({
|
|
...room,
|
|
bookingCode: input.booking.bookingCode,
|
|
}))
|
|
|
|
const availability = await getRoomsAvailability(
|
|
input,
|
|
ctx.userToken,
|
|
ctx.serviceToken,
|
|
ctx.userPoints
|
|
)
|
|
|
|
for (const room of availability) {
|
|
if (!room || "error" in room) {
|
|
continue
|
|
}
|
|
|
|
room.roomConfigurations = mergeRoomTypes(room.roomConfigurations)
|
|
}
|
|
|
|
return availability
|
|
})
|