feat: SW-2028 Removed session passing
This commit is contained in:
@@ -36,9 +36,8 @@ const hotelFacilitiesFilterNames = [
|
||||
export async function fetchAvailableHotels(
|
||||
input: AvailabilityInput
|
||||
): Promise<NullableHotelData[]> {
|
||||
const availableHotels = input.redemption
|
||||
? await serverClient().hotel.availability.hotelsByCityWithRedemption(input)
|
||||
: await serverClient().hotel.availability.hotelsByCity(input)
|
||||
const availableHotels =
|
||||
await serverClient().hotel.availability.hotelsByCity(input)
|
||||
|
||||
if (!availableHotels) return []
|
||||
|
||||
|
||||
@@ -77,12 +77,6 @@ async function fetchAvailableHotels(input: AvailabilityInput) {
|
||||
return await serverClient().hotel.availability.hotelsByCity(input)
|
||||
}
|
||||
|
||||
async function fetchAvailableHotelsWithRedemption(input: AvailabilityInput) {
|
||||
return await serverClient().hotel.availability.hotelsByCityWithRedemption(
|
||||
input
|
||||
)
|
||||
}
|
||||
|
||||
async function fetchBookingCodeAvailableHotels(input: AvailabilityInput) {
|
||||
return await serverClient().hotel.availability.hotelsByCityWithBookingCode(
|
||||
input
|
||||
@@ -198,22 +192,6 @@ export async function getHotels(
|
||||
})
|
||||
})
|
||||
)
|
||||
} else if (redemption) {
|
||||
availableHotelsResponse = await Promise.allSettled(
|
||||
booking.rooms.map(
|
||||
async (room) =>
|
||||
await fetchAvailableHotelsWithRedemption({
|
||||
adults: room.adults,
|
||||
children: room.childrenInRoom
|
||||
? generateChildrenString(room.childrenInRoom)
|
||||
: undefined,
|
||||
cityId: city.id,
|
||||
redemption,
|
||||
roomStayEndDate: booking.toDate,
|
||||
roomStayStartDate: booking.fromDate,
|
||||
})
|
||||
)
|
||||
)
|
||||
} else {
|
||||
availableHotelsResponse = await Promise.allSettled(
|
||||
booking.rooms.map(
|
||||
|
||||
@@ -6,7 +6,6 @@ import { dt } from "@/lib/dt"
|
||||
import { badRequestError, unauthorizedError } from "@/server/errors/trpc"
|
||||
import {
|
||||
contentStackBaseWithServiceProcedure,
|
||||
protectedProcedure,
|
||||
publicProcedure,
|
||||
router,
|
||||
safeProtectedServiceProcedure,
|
||||
@@ -67,8 +66,6 @@ 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"
|
||||
@@ -212,7 +209,7 @@ async function getHotelsAvailabilityByCity(
|
||||
input: HotelsAvailabilityInputSchema,
|
||||
apiLang: string,
|
||||
token: string, // Either service token or user access token in case of redemption search
|
||||
session?: Session
|
||||
userPoints: number = 0
|
||||
) {
|
||||
const {
|
||||
cityId,
|
||||
@@ -324,16 +321,12 @@ async function getHotelsAvailabilityByCity(
|
||||
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
|
||||
})
|
||||
if (redemption) {
|
||||
validateAvailabilityData.data.data.forEach((data) => {
|
||||
data.attributes.productType?.redemptions?.forEach((r) => {
|
||||
r.hasEnoughPoints = userPoints >= r.localPrice.pointsPerStay
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -480,9 +473,32 @@ export const getHotelsAvailabilityByHotelIds = async (
|
||||
|
||||
export const hotelQueryRouter = router({
|
||||
availability: router({
|
||||
hotelsByCity: serviceProcedure
|
||||
hotelsByCity: safeProtectedServiceProcedure
|
||||
.input(hotelsAvailabilityInputSchema)
|
||||
.query(async ({ input, ctx }) => {
|
||||
.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 ({ ctx, input }) => {
|
||||
const { lang } = ctx
|
||||
const apiLang = toApiLang(lang)
|
||||
const {
|
||||
@@ -492,29 +508,28 @@ export const hotelQueryRouter = router({
|
||||
adults,
|
||||
children,
|
||||
bookingCode,
|
||||
redemption,
|
||||
} = input
|
||||
const cacheClient = await getCacheClient()
|
||||
|
||||
// In case of redemption do not cache result
|
||||
if (redemption) {
|
||||
return getHotelsAvailabilityByCity(
|
||||
input,
|
||||
apiLang,
|
||||
ctx.token,
|
||||
ctx.userPoints
|
||||
)
|
||||
}
|
||||
|
||||
const cacheClient = await getCacheClient()
|
||||
return await cacheClient.cacheOrGet(
|
||||
`${cityId}:${roomStayStartDate}:${roomStayEndDate}:${adults}:${children}:${bookingCode}`,
|
||||
async () => {
|
||||
return getHotelsAvailabilityByCity(input, apiLang, ctx.serviceToken)
|
||||
return getHotelsAvailabilityByCity(input, apiLang, ctx.token)
|
||||
},
|
||||
env.CACHE_TIME_CITY_SEARCH
|
||||
)
|
||||
}),
|
||||
hotelsByCityWithRedemption: protectedProcedure
|
||||
.input(hotelsAvailabilityInputSchema)
|
||||
.query(async ({ input, ctx }) => {
|
||||
const { lang } = ctx
|
||||
const apiLang = toApiLang(lang)
|
||||
return getHotelsAvailabilityByCity(
|
||||
input,
|
||||
apiLang,
|
||||
ctx.session.token.access_token,
|
||||
ctx.session
|
||||
)
|
||||
}),
|
||||
hotelsByHotelIds: serviceProcedure
|
||||
.input(getHotelsByHotelIdsAvailabilityInputSchema)
|
||||
.query(async ({ input, ctx }) => {
|
||||
|
||||
@@ -209,8 +209,5 @@ export const contentStackBaseWithProtectedProcedure =
|
||||
export const safeProtectedServiceProcedure =
|
||||
safeProtectedProcedure.unstable_concat(serviceProcedure)
|
||||
|
||||
export const protectedServiceProcedure =
|
||||
protectedProcedure.unstable_concat(serviceProcedure)
|
||||
|
||||
export const languageProtectedProcedure =
|
||||
protectedProcedure.unstable_concat(languageProcedure)
|
||||
|
||||
Reference in New Issue
Block a user