Files
web/packages/trpc/lib/routers/hotels/availability/hotelsByHotelIds.ts
Anton Gunnarsson de94c47f3f Merged in fix/sw-3667-not-enough-points (pull request #3337)
fix(SW-3667): Remove conditional on Scandic user token

* Remove conditional on Scandic user token


Approved-by: Joakim Jäderberg
2025-12-12 09:34:07 +00:00

103 lines
2.5 KiB
TypeScript

import dayjs from "dayjs"
import { z } from "zod"
import { unauthorizedError } from "../../../errors"
import { safeProtectedServiceProcedure } from "../../../procedures"
import { isValidSession } from "../../../utils/session"
import { getHotelsAvailabilityByHotelIds } from "../services/getHotelsAvailabilityByHotelIds"
export type HotelsByHotelIdsAvailabilityInputSchema = z.output<
typeof getHotelsByHotelIdsAvailabilityInputSchema
>
export const getHotelsByHotelIdsAvailabilityInputSchema = z
.object({
hotelIds: z.array(z.number()),
roomStayStartDate: z.string().refine(
(val) => {
const fromDate = dayjs(val)
return fromDate.isValid()
},
{
message: "FROMDATE_INVALID",
}
),
roomStayEndDate: z.string().refine(
(val) => {
const toDate = dayjs(val)
return toDate.isValid()
},
{
message: "TODATE_INVALID",
}
),
adults: z.number(),
children: z.string().optional(),
bookingCode: z.string().optional().default(""),
redemption: z.boolean().optional().default(false),
})
.refine(
(data) => {
const fromDate = dayjs(data.roomStayStartDate).startOf("day")
const toDate = dayjs(data.roomStayEndDate).startOf("day")
return fromDate.isBefore(toDate)
},
{
message: "FROMDATE_BEFORE_TODATE",
}
)
.refine(
(data) => {
const fromDate = dayjs(data.roomStayStartDate)
const today = dayjs().startOf("day")
return fromDate.isSameOrAfter(today)
},
{
message: "FROMDATE_CANNOT_BE_IN_THE_PAST",
}
)
type Context = {
userToken: string | null
userPoints?: number
}
export const hotelsByHotelIds = safeProtectedServiceProcedure
.input(getHotelsByHotelIdsAvailabilityInputSchema)
.use(async ({ ctx, input, next }) => {
const userToken = await ctx.getScandicUserToken()
if (input.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 ({ input, ctx }) => {
return getHotelsAvailabilityByHotelIds({
input,
lang: ctx.lang,
userToken: ctx.userToken,
serviceToken: ctx.serviceToken,
userPoints: ctx.userPoints,
})
})