fix(SW-3667): Remove conditional on Scandic user token * Remove conditional on Scandic user token Approved-by: Joakim Jäderberg
78 lines
2.0 KiB
TypeScript
78 lines
2.0 KiB
TypeScript
import { z } from "zod"
|
|
|
|
import { Lang } from "@scandic-hotels/common/constants/language"
|
|
|
|
import { SEARCH_TYPE_REDEMPTION } from "../../../../constants/booking"
|
|
import { unauthorizedError } from "../../../../errors"
|
|
import { safeProtectedServiceProcedure } from "../../../../procedures"
|
|
import { isValidSession } from "../../../../utils/session"
|
|
import { baseBookingSchema, baseRoomSchema } from "../../input"
|
|
import { getRoomsAvailability } from "../../services/getRoomsAvailability"
|
|
import { mergeRoomTypes } from "../../utils"
|
|
|
|
export const selectRateRoomAvailabilityInputSchema = z.object({
|
|
booking: baseBookingSchema.extend({
|
|
room: baseRoomSchema,
|
|
}),
|
|
lang: z.nativeEnum(Lang),
|
|
})
|
|
|
|
type Context = {
|
|
userToken: string | null
|
|
userPoints?: number
|
|
}
|
|
|
|
export const room = safeProtectedServiceProcedure
|
|
.input(selectRateRoomAvailabilityInputSchema)
|
|
.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 }) {
|
|
const [availability] = await getRoomsAvailability(
|
|
{
|
|
booking: {
|
|
...input.booking,
|
|
rooms: [input.booking.room],
|
|
},
|
|
lang: input.lang,
|
|
},
|
|
ctx.userToken,
|
|
ctx.serviceToken,
|
|
ctx.userPoints
|
|
)
|
|
|
|
if (!availability || "error" in availability) {
|
|
return null
|
|
}
|
|
|
|
const roomConfigurations = mergeRoomTypes(availability.roomConfigurations)
|
|
|
|
return {
|
|
...availability,
|
|
roomConfigurations,
|
|
}
|
|
})
|