Merged in chore/refactor-hotel-trpc-routes (pull request #2891)
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
This commit is contained in:
81
packages/trpc/lib/routers/hotels/availability/myStay.ts
Normal file
81
packages/trpc/lib/routers/hotels/availability/myStay.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import { z } from "zod"
|
||||
|
||||
import { Lang } from "@scandic-hotels/common/constants/language"
|
||||
import { createLogger } from "@scandic-hotels/common/logger/createLogger"
|
||||
|
||||
import { SEARCH_TYPE_REDEMPTION } from "../../../constants/booking"
|
||||
import { unauthorizedError } from "../../../errors"
|
||||
import { safeProtectedServiceProcedure } from "../../../procedures"
|
||||
import { getVerifiedUser } from "../../user/utils/getVerifiedUser"
|
||||
import { baseBookingSchema, baseRoomSchema, selectedRoomSchema } from "../input"
|
||||
import { getRoomsAvailability } from "../services/getRoomsAvailability"
|
||||
import { getSelectedRoomAvailability } from "../utils"
|
||||
|
||||
export const myStayRoomAvailabilityInputSchema = z.object({
|
||||
booking: baseBookingSchema.extend({
|
||||
room: baseRoomSchema.merge(selectedRoomSchema),
|
||||
}),
|
||||
lang: z.nativeEnum(Lang),
|
||||
})
|
||||
|
||||
const logger = createLogger("trpc:availability:myStay")
|
||||
export const myStay = safeProtectedServiceProcedure
|
||||
.input(myStayRoomAvailabilityInputSchema)
|
||||
.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 bookingRoom = input.booking.room
|
||||
const selected = getSelectedRoomAvailability(
|
||||
bookingRoom.rateCode,
|
||||
availability.rateDefinitions,
|
||||
availability.roomConfigurations,
|
||||
bookingRoom.roomTypeCode,
|
||||
ctx.userPoints
|
||||
)
|
||||
|
||||
if (!selected) {
|
||||
logger.error("Unable to find selected room")
|
||||
return null
|
||||
}
|
||||
|
||||
return {
|
||||
product: selected.product,
|
||||
selectedRoom: selected.selectedRoom,
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user