feat: SW-2028 Removed session passing

This commit is contained in:
Hrishikesh Vaipurkar
2025-03-31 18:53:29 +02:00
parent 96fd0b73e4
commit 221f842552
4 changed files with 46 additions and 57 deletions

View File

@@ -36,9 +36,8 @@ const hotelFacilitiesFilterNames = [
export async function fetchAvailableHotels( export async function fetchAvailableHotels(
input: AvailabilityInput input: AvailabilityInput
): Promise<NullableHotelData[]> { ): Promise<NullableHotelData[]> {
const availableHotels = input.redemption const availableHotels =
? await serverClient().hotel.availability.hotelsByCityWithRedemption(input) await serverClient().hotel.availability.hotelsByCity(input)
: await serverClient().hotel.availability.hotelsByCity(input)
if (!availableHotels) return [] if (!availableHotels) return []

View File

@@ -77,12 +77,6 @@ async function fetchAvailableHotels(input: AvailabilityInput) {
return await serverClient().hotel.availability.hotelsByCity(input) return await serverClient().hotel.availability.hotelsByCity(input)
} }
async function fetchAvailableHotelsWithRedemption(input: AvailabilityInput) {
return await serverClient().hotel.availability.hotelsByCityWithRedemption(
input
)
}
async function fetchBookingCodeAvailableHotels(input: AvailabilityInput) { async function fetchBookingCodeAvailableHotels(input: AvailabilityInput) {
return await serverClient().hotel.availability.hotelsByCityWithBookingCode( return await serverClient().hotel.availability.hotelsByCityWithBookingCode(
input 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 { } else {
availableHotelsResponse = await Promise.allSettled( availableHotelsResponse = await Promise.allSettled(
booking.rooms.map( booking.rooms.map(

View File

@@ -6,7 +6,6 @@ import { dt } from "@/lib/dt"
import { badRequestError, unauthorizedError } from "@/server/errors/trpc" import { badRequestError, unauthorizedError } from "@/server/errors/trpc"
import { import {
contentStackBaseWithServiceProcedure, contentStackBaseWithServiceProcedure,
protectedProcedure,
publicProcedure, publicProcedure,
router, router,
safeProtectedServiceProcedure, safeProtectedServiceProcedure,
@@ -67,8 +66,6 @@ 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"
@@ -212,7 +209,7 @@ async function getHotelsAvailabilityByCity(
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 userPoints: number = 0
) { ) {
const { const {
cityId, cityId,
@@ -324,16 +321,12 @@ async function getHotelsAvailabilityByCity(
query: { cityId, params: params }, query: { cityId, params: params },
}) })
) )
if (redemption && session) { if (redemption) {
const verifiedUser = await getVerifiedUser({ session }) validateAvailabilityData.data.data.forEach((data) => {
if (!verifiedUser?.error) { data.attributes.productType?.redemptions?.forEach((r) => {
const userPoints = verifiedUser?.data.membership?.currentPoints ?? 0 r.hasEnoughPoints = userPoints >= r.localPrice.pointsPerStay
validateAvailabilityData.data.data.forEach((data) => {
data.attributes.productType?.redemptions?.forEach((r) => {
r.hasEnoughPoints = userPoints >= r.localPrice.pointsPerStay
})
}) })
} })
} }
return { return {
@@ -480,9 +473,32 @@ export const getHotelsAvailabilityByHotelIds = async (
export const hotelQueryRouter = router({ export const hotelQueryRouter = router({
availability: router({ availability: router({
hotelsByCity: serviceProcedure hotelsByCity: safeProtectedServiceProcedure
.input(hotelsAvailabilityInputSchema) .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 { lang } = ctx
const apiLang = toApiLang(lang) const apiLang = toApiLang(lang)
const { const {
@@ -492,29 +508,28 @@ export const hotelQueryRouter = router({
adults, adults,
children, children,
bookingCode, bookingCode,
redemption,
} = input } = 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( return await cacheClient.cacheOrGet(
`${cityId}:${roomStayStartDate}:${roomStayEndDate}:${adults}:${children}:${bookingCode}`, `${cityId}:${roomStayStartDate}:${roomStayEndDate}:${adults}:${children}:${bookingCode}`,
async () => { async () => {
return getHotelsAvailabilityByCity(input, apiLang, ctx.serviceToken) return getHotelsAvailabilityByCity(input, apiLang, ctx.token)
}, },
env.CACHE_TIME_CITY_SEARCH 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 hotelsByHotelIds: serviceProcedure
.input(getHotelsByHotelIdsAvailabilityInputSchema) .input(getHotelsByHotelIdsAvailabilityInputSchema)
.query(async ({ input, ctx }) => { .query(async ({ input, ctx }) => {

View File

@@ -209,8 +209,5 @@ export const contentStackBaseWithProtectedProcedure =
export const safeProtectedServiceProcedure = export const safeProtectedServiceProcedure =
safeProtectedProcedure.unstable_concat(serviceProcedure) safeProtectedProcedure.unstable_concat(serviceProcedure)
export const protectedServiceProcedure =
protectedProcedure.unstable_concat(serviceProcedure)
export const languageProtectedProcedure = export const languageProtectedProcedure =
protectedProcedure.unstable_concat(languageProcedure) protectedProcedure.unstable_concat(languageProcedure)