From fb76c67ceea6ced80438b7e2059d46119bd2ef15 Mon Sep 17 00:00:00 2001 From: Christel Westerberg Date: Tue, 5 Nov 2024 13:41:22 +0100 Subject: [PATCH] fix: calc total price for summary --- .../EnterDetails/Summary/index.tsx | 25 +++++++++++++------ next-env.d.ts | 2 +- server/routers/hotels/query.ts | 16 ++++++------ 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/components/HotelReservation/EnterDetails/Summary/index.tsx b/components/HotelReservation/EnterDetails/Summary/index.tsx index 91bf36e15..f8fe7f218 100644 --- a/components/HotelReservation/EnterDetails/Summary/index.tsx +++ b/components/HotelReservation/EnterDetails/Summary/index.tsx @@ -21,6 +21,10 @@ import { RoomsData } from "@/types/components/hotelReservation/enterDetails/book import { BreakfastPackage } from "@/types/components/hotelReservation/enterDetails/breakfast" import { BreakfastPackageEnum } from "@/types/enums/breakfast" +function parsePrice(price: string | undefined) { + return price ? parseInt(price) : 0 +} + export default function Summary({ showMemberPrice, room, @@ -33,8 +37,8 @@ export default function Summary({ BreakfastPackage | BreakfastPackageEnum.NO_BREAKFAST >() const [totalPrice, setTotalPrice] = useState({ - local: parseInt(room.localPrice.price ?? "0"), - euro: parseInt(room.euroPrice.price ?? "0"), + local: parsePrice(room.localPrice.price), + euro: parsePrice(room.euroPrice.price), }) const intl = useIntl() const lang = useLang() @@ -64,14 +68,19 @@ export default function Summary({ if (breakfast) { setChosenBreakfast(breakfast) - if (breakfast !== BreakfastPackageEnum.NO_BREAKFAST) { + if (breakfast === BreakfastPackageEnum.NO_BREAKFAST) { + setTotalPrice({ + local: parsePrice(room.localPrice.price), + euro: parsePrice(room.euroPrice.price), + }) + } else { setTotalPrice({ local: - parseInt(room.localPrice.price ?? "0") + - parseInt(breakfast.localPrice.price ?? "0"), + parsePrice(room.localPrice.price) + + parsePrice(breakfast.localPrice.totalPrice), euro: - parseInt(room.euroPrice.price ?? "0") + - parseInt(breakfast.requestedPrice.price ?? "0"), + parsePrice(room.euroPrice.price) + + parsePrice(breakfast.requestedPrice.totalPrice), }) } } @@ -164,7 +173,7 @@ export default function Summary({ {intl.formatMessage( { id: "{amount} {currency}" }, { - amount: chosenBreakfast.localPrice.price, + amount: chosenBreakfast.localPrice.totalPrice, currency: chosenBreakfast.localPrice.currency, } )} diff --git a/next-env.d.ts b/next-env.d.ts index 40c3d6809..4f11a03dc 100644 --- a/next-env.d.ts +++ b/next-env.d.ts @@ -2,4 +2,4 @@ /// // NOTE: This file should not be edited -// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information. +// see https://nextjs.org/docs/basic-features/typescript for more information. diff --git a/server/routers/hotels/query.ts b/server/routers/hotels/query.ts index e08111d07..626ae359d 100644 --- a/server/routers/hotels/query.ts +++ b/server/routers/hotels/query.ts @@ -731,13 +731,11 @@ export const hotelQueryRouter = router({ return null } - const memberRate = selectedRoom.products.find( - (rate) => rate.productType.member?.rateCode === rateCode - )?.productType.member - - const publicRate = selectedRoom.products.find( - (rate) => rate.productType.public?.rateCode === rateCode - )?.productType.public + const rateTypes = selectedRoom.products.find( + (rate) => + rate.productType.public?.rateCode === rateCode || + rate.productType.member?.rateCode === rateCode + )?.productType const mustBeGuaranteed = validateAvailabilityData.data.rateDefinitions.filter( @@ -787,8 +785,8 @@ export const hotelQueryRouter = router({ selectedRoom, mustBeGuaranteed, cancellationText, - memberRate, - publicRate, + memberRate: rateTypes?.member, + publicRate: rateTypes?.public, bedTypes, } }),