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:
@@ -0,0 +1,97 @@
|
||||
import dayjs from "dayjs"
|
||||
import { z } from "zod"
|
||||
|
||||
import { unauthorizedError } from "../../../errors"
|
||||
import { safeProtectedServiceProcedure } from "../../../procedures"
|
||||
import { toApiLang } from "../../../utils"
|
||||
import { getVerifiedUser } from "../../user/utils/getVerifiedUser"
|
||||
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",
|
||||
}
|
||||
)
|
||||
|
||||
export const hotelsByHotelIds = safeProtectedServiceProcedure
|
||||
.input(getHotelsByHotelIdsAvailabilityInputSchema)
|
||||
.use(async ({ ctx, input, next }) => {
|
||||
if (input.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,
|
||||
},
|
||||
input,
|
||||
})
|
||||
}
|
||||
}
|
||||
throw unauthorizedError()
|
||||
}
|
||||
return next({
|
||||
ctx: {
|
||||
token: ctx.serviceToken,
|
||||
},
|
||||
input,
|
||||
})
|
||||
})
|
||||
.query(async ({ input, ctx }) => {
|
||||
const { lang } = ctx
|
||||
const apiLang = toApiLang(lang)
|
||||
return getHotelsAvailabilityByHotelIds(
|
||||
input,
|
||||
apiLang,
|
||||
ctx.token,
|
||||
ctx.userPoints
|
||||
)
|
||||
})
|
||||
Reference in New Issue
Block a user