feat: SW-2028 Updated validations at trpc query level
This commit is contained in:
@@ -67,6 +67,8 @@ import {
|
||||
getSelectedRoomAvailability,
|
||||
} from "./utils"
|
||||
|
||||
import type { Session } from "next-auth"
|
||||
|
||||
import type { BedTypeSelection } from "@/types/components/hotelReservation/enterDetails/bedType"
|
||||
import { BreakfastPackageEnum } from "@/types/enums/breakfast"
|
||||
import { HotelTypeEnum } from "@/types/enums/hotelType"
|
||||
@@ -209,7 +211,8 @@ export const getHotel = cache(
|
||||
export const getHotelsAvailabilityByCity = async (
|
||||
input: HotelsAvailabilityInputSchema,
|
||||
apiLang: string,
|
||||
token: string // Either service token or user access token in case of redemption search
|
||||
token: string, // Either service token or user access token in case of redemption search
|
||||
session?: Session
|
||||
) => {
|
||||
const {
|
||||
cityId,
|
||||
@@ -222,7 +225,7 @@ export const getHotelsAvailabilityByCity = async (
|
||||
} = input
|
||||
const cacheClient = await getCacheClient()
|
||||
return await cacheClient.cacheOrGet(
|
||||
`${cityId}:${roomStayStartDate}:${roomStayEndDate}:${adults}:${children}:${bookingCode}`,
|
||||
`${cityId}:${roomStayStartDate}:${roomStayEndDate}:${adults}:${children}:${bookingCode}:${redemption ? "isRedemption" : ""}`,
|
||||
async () => {
|
||||
const params: Record<string, string | number> = {
|
||||
roomStayStartDate,
|
||||
@@ -325,6 +328,18 @@ export const getHotelsAvailabilityByCity = async (
|
||||
query: { cityId, params: params },
|
||||
})
|
||||
)
|
||||
if (redemption && session) {
|
||||
const verifiedUser = await getVerifiedUser({ session })
|
||||
if (!verifiedUser?.error) {
|
||||
const userPoints = verifiedUser?.data.membership?.currentPoints ?? 0
|
||||
validateAvailabilityData.data.data.forEach((data) => {
|
||||
data.attributes.productType?.redemptions?.forEach((r) => {
|
||||
r.hasEnoughPoints = userPoints >= r.localPrice.pointsPerStay
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
availability: validateAvailabilityData.data.data.flatMap(
|
||||
(hotels) => hotels.attributes
|
||||
@@ -487,7 +502,8 @@ export const hotelQueryRouter = router({
|
||||
return getHotelsAvailabilityByCity(
|
||||
input,
|
||||
apiLang,
|
||||
ctx.session.token.access_token
|
||||
ctx.session.token.access_token,
|
||||
ctx.session,
|
||||
)
|
||||
}),
|
||||
hotelsByHotelIds: serviceProcedure
|
||||
@@ -606,6 +622,18 @@ export const hotelQueryRouter = router({
|
||||
)?.mustBeGuaranteed
|
||||
}
|
||||
|
||||
if (redemption && ctx.session) {
|
||||
const verifiedUser = await getVerifiedUser({ session: ctx.session })
|
||||
if (!verifiedUser?.error) {
|
||||
const userPoints = verifiedUser?.data.membership?.currentPoints ?? 0
|
||||
validateAvailabilityData.data.roomConfigurations.forEach((data) => {
|
||||
data.redemptions?.forEach(r => {
|
||||
r.redemption.hasEnoughPoints = userPoints >= r.redemption.localPrice.pointsPerStay
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return validateAvailabilityData.data
|
||||
})
|
||||
)
|
||||
|
||||
@@ -66,6 +66,7 @@ export const productTypePointsSchema = z
|
||||
.object({
|
||||
localPrice: redemptionSchema,
|
||||
requestedPrice: redemptionSchema.nullish(),
|
||||
hasEnoughPoints: z.boolean().optional().default(false),
|
||||
})
|
||||
.merge(partialPriceSchema)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user