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
71 lines
2.0 KiB
TypeScript
71 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 { getRedemptionTokenSafely } from "../../../../utils/getRedemptionTokenSafely"
|
|
import { getUserPointsBalance } from "../../../../utils/getUserPointsBalance"
|
|
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 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 roomConfigurations = mergeRoomTypes(availability.roomConfigurations)
|
|
|
|
return {
|
|
...availability,
|
|
roomConfigurations,
|
|
}
|
|
})
|