feat(SW-3526): Show EB points rate and label in booking flow * feat(SW-3526): Show EB points rate and label in booking flow * feat(SW-3526) Optimized points currency code * feat(SW-3526) Removed extra multiplication for token expiry after rebase * feat(SW-3526): Updated to exhaustive check and thow if type error Approved-by: Anton Gunnarsson
85 lines
2.5 KiB
TypeScript
85 lines
2.5 KiB
TypeScript
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 { getRedemptionTokenSafely } from "../../../utils/getRedemptionTokenSafely"
|
|
import { getUserPointsBalance } from "../../../utils/getUserPointsBalance"
|
|
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 pointsValue = await getUserPointsBalance(ctx.session)
|
|
const token = getRedemptionTokenSafely(ctx.session, ctx.serviceToken)
|
|
if (pointsValue && token) {
|
|
return next({
|
|
ctx: {
|
|
token: token,
|
|
userPoints: pointsValue ?? 0,
|
|
},
|
|
input,
|
|
})
|
|
}
|
|
}
|
|
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,
|
|
}
|
|
})
|