From 6abef835e851e68a62eae3ac9c8880f572362173 Mon Sep 17 00:00:00 2001 From: Haritha Randunu Date: Wed, 30 Sep 2026 11:41:57 +0530 Subject: [PATCH] Add PricePerHour to booking requests and update validation logic --- Controllers/BookingController.cs | 3 ++- DTOs/Booking/BookingRequests.cs | 5 +++++ Services/BookingService.cs | 21 ++++++++++++++++----- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/Controllers/BookingController.cs b/Controllers/BookingController.cs index 5923f31..d12318f 100644 --- a/Controllers/BookingController.cs +++ b/Controllers/BookingController.cs @@ -78,7 +78,8 @@ namespace Fitness_core_BE.Controllers durationMinutes = request.DurationMinutes, guestName = request.GuestName, guestPhone = request.GuestPhone, - partySize = request.PartySize + partySize = request.PartySize, + pricePerHour = request.PricePerHour }, HttpContext)); // The actual "copy the link and share it" page -- BookingDto.ShareUrl points here. diff --git a/DTOs/Booking/BookingRequests.cs b/DTOs/Booking/BookingRequests.cs index 690d2ae..d81c174 100644 --- a/DTOs/Booking/BookingRequests.cs +++ b/DTOs/Booking/BookingRequests.cs @@ -52,6 +52,11 @@ namespace Fitness_core_BE.DTOs.Booking public string GuestPhone { get; set; } = string.Empty; public int? PartySize { get; set; } + + /// Overrides the facility's own PricePerHour for this booking's charge only -- + /// the FE pre-fills this from the facility but lets staff adjust it. + [Range(typeof(decimal), "0", "79228162514264337593543950335", ErrorMessage = "PricePerHour cannot be negative")] + public decimal? PricePerHour { get; set; } } public class MarkGuestBookingPaidRequest diff --git a/Services/BookingService.cs b/Services/BookingService.cs index e591dc6..3e0acc9 100644 --- a/Services/BookingService.cs +++ b/Services/BookingService.cs @@ -164,7 +164,7 @@ namespace Fitness_core_BE.Services await _uow.BeginAsync(); try { - var (facility, startTime, endTime, partySize) = await ValidateAdhocRequestAsync(payload); + var (facility, startTime, endTime, partySize, _) = await ValidateAdhocRequestAsync(payload); var cusId = await _userRoleHelper.GetCusId(DataExtractor.GetUserIdFromContext(httpContext)); @@ -202,7 +202,7 @@ namespace Fitness_core_BE.Services await _uow.BeginAsync(); try { - var (facility, startTime, endTime, partySize) = await ValidateAdhocRequestAsync(payload); + var (facility, startTime, endTime, partySize, pricePerHour) = await ValidateAdhocRequestAsync(payload); var data = DeserializePayload.Deserialize(payload); var guestName = data["guestName"].GetString() ?? throw new ArgumentException("guestName is required"); @@ -223,7 +223,10 @@ namespace Fitness_core_BE.Services GuestName = guestName, GuestPhone = guestPhone, OtpCode = GenerateOtp(), - AmountCharged = Math.Round(facility.PricePerHour * durationHours, 2), + // Staff can override the facility's default rate per booking (e.g. a discount) + // -- the pre-filled suggestion in the FE comes from facility.PricePerHour, but + // the charge actually uses whatever rate was submitted. + AmountCharged = Math.Round((pricePerHour ?? facility.PricePerHour) * durationHours, 2), PaymentStatus = "Unpaid", Status = "Confirmed" }; @@ -244,7 +247,7 @@ namespace Fitness_core_BE.Services /// requested window must have a free unit. Locks the facility row for the duration of the /// caller's transaction so two concurrent claims on the same facility serialize instead of /// both reading "room available" before either commits. - private async Task<(Facility facility, DateTime startTime, DateTime endTime, int? partySize)> ValidateAdhocRequestAsync(object payload) + private async Task<(Facility facility, DateTime startTime, DateTime endTime, int? partySize, decimal? pricePerHour)> ValidateAdhocRequestAsync(object payload) { var data = DeserializePayload.Deserialize(payload); var facilityId = Guid.Parse(data["facilityId"].GetString()!); @@ -257,6 +260,14 @@ namespace Fitness_core_BE.Services ? data["partySize"].GetInt32() : null; + // Optional per-booking rate override -- the FE pre-fills this from the facility's + // own PricePerHour but lets staff adjust it (e.g. a discount) before charging. + decimal? pricePerHour = data.ContainsKey("pricePerHour") && data["pricePerHour"].ValueKind != JsonValueKind.Null + ? data["pricePerHour"].GetDecimal() + : null; + if (pricePerHour.HasValue && pricePerHour.Value < 0) + throw new ArgumentException("pricePerHour cannot be negative"); + var facility = await _facilityRepository.GetFacilityById(facilityId) ?? throw new KeyNotFoundException("Facility not found"); if (facility.BookingMode != "AdHoc") @@ -269,7 +280,7 @@ namespace Fitness_core_BE.Services if (overlapping >= facility.NumberOfUnits) throw new ArgumentException("No units are free for the requested time"); - return (facility, startTime, endTime, partySize); + return (facility, startTime, endTime, partySize, pricePerHour); } // ---------- Payment / check-in / lifecycle ----------