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
70 lines
1.9 KiB
TypeScript
70 lines
1.9 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 { getVerifiedUser } from "../../../user/utils/getVerifiedUser"
|
|
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),
|
|
})
|
|
|
|
export const room = safeProtectedServiceProcedure
|
|
.input(selectRateRoomAvailabilityInputSchema)
|
|
.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 }) {
|
|
const [availability] = await getRoomsAvailability(
|
|
{
|
|
booking: {
|
|
...input.booking,
|
|
rooms: [input.booking.room],
|
|
},
|
|
lang: input.lang,
|
|
},
|
|
ctx.token,
|
|
ctx.serviceToken,
|
|
ctx.userPoints
|
|
)
|
|
|
|
if (!availability || "error" in availability) {
|
|
return null
|
|
}
|
|
|
|
const roomConfigurations = mergeRoomTypes(availability.roomConfigurations)
|
|
|
|
return {
|
|
...availability,
|
|
roomConfigurations,
|
|
}
|
|
})
|