diff --git a/apps/scandic-web/app/[lang]/(live)/(public)/hotelreservation/(standard)/select-hotel/utils.ts b/apps/scandic-web/app/[lang]/(live)/(public)/hotelreservation/(standard)/select-hotel/utils.ts index 9b61a5351..d54df6248 100644 --- a/apps/scandic-web/app/[lang]/(live)/(public)/hotelreservation/(standard)/select-hotel/utils.ts +++ b/apps/scandic-web/app/[lang]/(live)/(public)/hotelreservation/(standard)/select-hotel/utils.ts @@ -36,9 +36,8 @@ const hotelFacilitiesFilterNames = [ export async function fetchAvailableHotels( input: AvailabilityInput ): Promise { - 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 [] diff --git a/apps/scandic-web/components/HotelReservation/SelectHotel/helpers.ts b/apps/scandic-web/components/HotelReservation/SelectHotel/helpers.ts index 1a6f79c24..55859ca9b 100644 --- a/apps/scandic-web/components/HotelReservation/SelectHotel/helpers.ts +++ b/apps/scandic-web/components/HotelReservation/SelectHotel/helpers.ts @@ -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( diff --git a/apps/scandic-web/server/routers/hotels/query.ts b/apps/scandic-web/server/routers/hotels/query.ts index 5a59c1bb1..6b709fcad 100644 --- a/apps/scandic-web/server/routers/hotels/query.ts +++ b/apps/scandic-web/server/routers/hotels/query.ts @@ -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 }) => { diff --git a/apps/scandic-web/server/trpc.ts b/apps/scandic-web/server/trpc.ts index 957b72f97..97c9ed2cf 100644 --- a/apps/scandic-web/server/trpc.ts +++ b/apps/scandic-web/server/trpc.ts @@ -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)