feat: SW-2028 Updated validations at trpc query level
This commit is contained in:
@@ -44,7 +44,6 @@ function HotelCard({
|
|||||||
state = "default",
|
state = "default",
|
||||||
type = HotelCardListingTypeEnum.PageListing,
|
type = HotelCardListingTypeEnum.PageListing,
|
||||||
bookingCode = "",
|
bookingCode = "",
|
||||||
userPoints,
|
|
||||||
}: HotelCardProps) {
|
}: HotelCardProps) {
|
||||||
const params = useParams()
|
const params = useParams()
|
||||||
const lang = params.lang as Lang
|
const lang = params.lang as Lang
|
||||||
@@ -74,13 +73,7 @@ function HotelCard({
|
|||||||
availability.productType?.member?.rateType === RateTypeEnum.Regular
|
availability.productType?.member?.rateType === RateTypeEnum.Regular
|
||||||
const price = availability.productType
|
const price = availability.productType
|
||||||
|
|
||||||
const userHasEnoughPoints =
|
const userHasEnoughPoints = price?.redemptions?.some((r) => r.hasEnoughPoints)
|
||||||
userPoints !== undefined
|
|
||||||
? !!price?.redemptions?.some(
|
|
||||||
(r) => r.localPrice.pointsPerStay < userPoints
|
|
||||||
)
|
|
||||||
: false
|
|
||||||
|
|
||||||
const notEnoughPointsLabel = intl.formatMessage({ id: "Not enough points" })
|
const notEnoughPointsLabel = intl.formatMessage({ id: "Not enough points" })
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -218,7 +211,7 @@ function HotelCard({
|
|||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
) : null}
|
) : null}
|
||||||
{userPoints !== undefined && !userHasEnoughPoints ? (
|
{price?.redemptions?.length && !userHasEnoughPoints ? (
|
||||||
<Tooltip
|
<Tooltip
|
||||||
arrow="left"
|
arrow="left"
|
||||||
position="bottom"
|
position="bottom"
|
||||||
|
|||||||
@@ -30,7 +30,6 @@ import { RateTypeEnum } from "@/types/enums/rateType"
|
|||||||
export default function HotelCardListing({
|
export default function HotelCardListing({
|
||||||
hotelData,
|
hotelData,
|
||||||
type = HotelCardListingTypeEnum.PageListing,
|
type = HotelCardListingTypeEnum.PageListing,
|
||||||
userPoints,
|
|
||||||
}: HotelCardListingProps) {
|
}: HotelCardListingProps) {
|
||||||
const { data: session } = useSession()
|
const { data: session } = useSession()
|
||||||
const isUserLoggedIn = isValidClientSession(session)
|
const isUserLoggedIn = isValidClientSession(session)
|
||||||
@@ -122,7 +121,6 @@ export default function HotelCardListing({
|
|||||||
}
|
}
|
||||||
type={type}
|
type={type}
|
||||||
bookingCode={bookingCode}
|
bookingCode={bookingCode}
|
||||||
userPoints={userPoints}
|
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
))
|
))
|
||||||
|
|||||||
@@ -246,14 +246,6 @@ export async function getHotels(
|
|||||||
return hotels
|
return hotels
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getUserPoints() {
|
|
||||||
const membershipCard = await serverClient().user.safeMembershipLevel()
|
|
||||||
if (!membershipCard) {
|
|
||||||
return undefined
|
|
||||||
}
|
|
||||||
return membershipCard.currentPoints
|
|
||||||
}
|
|
||||||
|
|
||||||
const hotelSurroundingsFilterNames = [
|
const hotelSurroundingsFilterNames = [
|
||||||
"Hotel surroundings",
|
"Hotel surroundings",
|
||||||
"Hotel omgivelser",
|
"Hotel omgivelser",
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ import { convertObjToSearchParams } from "@/utils/url"
|
|||||||
|
|
||||||
import HotelCardListing from "../HotelCardListing"
|
import HotelCardListing from "../HotelCardListing"
|
||||||
import BookingCodeFilter from "./BookingCodeFilter"
|
import BookingCodeFilter from "./BookingCodeFilter"
|
||||||
import { getFiltersFromHotels, getHotels, getUserPoints } from "./helpers"
|
import { getFiltersFromHotels, getHotels } from "./helpers"
|
||||||
import HotelCount from "./HotelCount"
|
import HotelCount from "./HotelCount"
|
||||||
import HotelFilter from "./HotelFilter"
|
import HotelFilter from "./HotelFilter"
|
||||||
import HotelSorter from "./HotelSorter"
|
import HotelSorter from "./HotelSorter"
|
||||||
@@ -76,11 +76,6 @@ export default async function SelectHotel({
|
|||||||
!!redemption
|
!!redemption
|
||||||
)
|
)
|
||||||
|
|
||||||
let userPoints
|
|
||||||
if (redemption) {
|
|
||||||
userPoints = await getUserPoints()
|
|
||||||
}
|
|
||||||
|
|
||||||
const arrivalDate = new Date(selectHotelParams.fromDate)
|
const arrivalDate = new Date(selectHotelParams.fromDate)
|
||||||
const departureDate = new Date(selectHotelParams.toDate)
|
const departureDate = new Date(selectHotelParams.toDate)
|
||||||
|
|
||||||
@@ -252,10 +247,7 @@ export default async function SelectHotel({
|
|||||||
isAllUnavailable={isAllUnavailable}
|
isAllUnavailable={isAllUnavailable}
|
||||||
operaId={hotels?.[0]?.hotel.operaId}
|
operaId={hotels?.[0]?.hotel.operaId}
|
||||||
/>
|
/>
|
||||||
<HotelCardListing
|
<HotelCardListing hotelData={hotels} />
|
||||||
hotelData={hotels}
|
|
||||||
userPoints={userPoints ? userPoints : undefined}
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
</main>
|
</main>
|
||||||
<Suspense key={`${suspenseKey}-tracking`} fallback={null}>
|
<Suspense key={`${suspenseKey}-tracking`} fallback={null}>
|
||||||
|
|||||||
@@ -67,6 +67,8 @@ import {
|
|||||||
getSelectedRoomAvailability,
|
getSelectedRoomAvailability,
|
||||||
} from "./utils"
|
} from "./utils"
|
||||||
|
|
||||||
|
import type { Session } from "next-auth"
|
||||||
|
|
||||||
import type { BedTypeSelection } from "@/types/components/hotelReservation/enterDetails/bedType"
|
import type { BedTypeSelection } from "@/types/components/hotelReservation/enterDetails/bedType"
|
||||||
import { BreakfastPackageEnum } from "@/types/enums/breakfast"
|
import { BreakfastPackageEnum } from "@/types/enums/breakfast"
|
||||||
import { HotelTypeEnum } from "@/types/enums/hotelType"
|
import { HotelTypeEnum } from "@/types/enums/hotelType"
|
||||||
@@ -209,7 +211,8 @@ export const getHotel = cache(
|
|||||||
export const getHotelsAvailabilityByCity = async (
|
export const getHotelsAvailabilityByCity = async (
|
||||||
input: HotelsAvailabilityInputSchema,
|
input: HotelsAvailabilityInputSchema,
|
||||||
apiLang: string,
|
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 {
|
const {
|
||||||
cityId,
|
cityId,
|
||||||
@@ -222,7 +225,7 @@ export const getHotelsAvailabilityByCity = async (
|
|||||||
} = input
|
} = input
|
||||||
const cacheClient = await getCacheClient()
|
const cacheClient = await getCacheClient()
|
||||||
return await cacheClient.cacheOrGet(
|
return await cacheClient.cacheOrGet(
|
||||||
`${cityId}:${roomStayStartDate}:${roomStayEndDate}:${adults}:${children}:${bookingCode}`,
|
`${cityId}:${roomStayStartDate}:${roomStayEndDate}:${adults}:${children}:${bookingCode}:${redemption ? "isRedemption" : ""}`,
|
||||||
async () => {
|
async () => {
|
||||||
const params: Record<string, string | number> = {
|
const params: Record<string, string | number> = {
|
||||||
roomStayStartDate,
|
roomStayStartDate,
|
||||||
@@ -325,6 +328,18 @@ export const getHotelsAvailabilityByCity = async (
|
|||||||
query: { cityId, params: params },
|
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 {
|
return {
|
||||||
availability: validateAvailabilityData.data.data.flatMap(
|
availability: validateAvailabilityData.data.data.flatMap(
|
||||||
(hotels) => hotels.attributes
|
(hotels) => hotels.attributes
|
||||||
@@ -487,7 +502,8 @@ export const hotelQueryRouter = router({
|
|||||||
return getHotelsAvailabilityByCity(
|
return getHotelsAvailabilityByCity(
|
||||||
input,
|
input,
|
||||||
apiLang,
|
apiLang,
|
||||||
ctx.session.token.access_token
|
ctx.session.token.access_token,
|
||||||
|
ctx.session,
|
||||||
)
|
)
|
||||||
}),
|
}),
|
||||||
hotelsByHotelIds: serviceProcedure
|
hotelsByHotelIds: serviceProcedure
|
||||||
@@ -606,6 +622,18 @@ export const hotelQueryRouter = router({
|
|||||||
)?.mustBeGuaranteed
|
)?.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
|
return validateAvailabilityData.data
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -66,6 +66,7 @@ export const productTypePointsSchema = z
|
|||||||
.object({
|
.object({
|
||||||
localPrice: redemptionSchema,
|
localPrice: redemptionSchema,
|
||||||
requestedPrice: redemptionSchema.nullish(),
|
requestedPrice: redemptionSchema.nullish(),
|
||||||
|
hasEnoughPoints: z.boolean().optional().default(false),
|
||||||
})
|
})
|
||||||
.merge(partialPriceSchema)
|
.merge(partialPriceSchema)
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ export type HotelData = {
|
|||||||
export type HotelCardListingProps = {
|
export type HotelCardListingProps = {
|
||||||
hotelData: HotelResponse[]
|
hotelData: HotelResponse[]
|
||||||
type?: HotelCardListingTypeEnum
|
type?: HotelCardListingTypeEnum
|
||||||
userPoints?: number
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface NullableHotelData extends Omit<HotelData, "hotelData"> {
|
export interface NullableHotelData extends Omit<HotelData, "hotelData"> {
|
||||||
|
|||||||
@@ -7,5 +7,4 @@ export type HotelCardProps = {
|
|||||||
type?: HotelCardListingTypeEnum
|
type?: HotelCardListingTypeEnum
|
||||||
state?: "default" | "active"
|
state?: "default" | "active"
|
||||||
bookingCode?: string | null
|
bookingCode?: string | null
|
||||||
userPoints?: number
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user