Facility booking-mode redesign, multi-plan subscriptions, and Classes

Implements the facility-classes-booking-dashboard plan (backend):

- Facility.BookingMode (single Scheduled/AdHoc choice) replaced with
  independent SupportsClasses/SupportsBooking flags, so one facility
  can run a class and accept walk-in bookings; a running class now
  blocks walk-in booking for its window on both create and reschedule.
- CustomerPlan.GrantsGymAccess + CustomerPlanClass join table let a
  customer hold one gym-access plan plus any number of class-only
  plans. Replaced the old single-subscription resolution
  (GetActiveSubscriptionByCustomerId/HasAnyActiveSubscriptionAsync)
  with gym-access-aware and class-aware resolvers across booking
  creation/cancellation, plan assignment, and customer self-service.
  Fixed ChangeMembershipPlan/renewal plan-changes, which resolved "the
  subscription to replace" by most-recent-created and would have
  deactivated a member's class subscription instead of their gym one.
- New FacilityClass model: recurring class definitions (Daily/Weekly/
  Monthly/Custom) with lazy occurrence generation (8-week horizon,
  race-safe via a unique index) and proper unlimited-capacity handling.
  "Mark not held" replaces the old bare-cancel: cascades to cancel and
  refund every confirmed booking on the occurrence through the same
  core CancelBooking uses, and frees the slot for walk-in booking.
  Scheduling conflicts (a walk-in booking now overlapping a class) are
  surfaced for staff to resolve via the existing Reschedule flow.
- Renamed the pre-existing "AdHoc" booking-mode terminology to
  "WalkIn" throughout (routes, methods, DTOs, the stored BookingMode
  value) for a clearer, simpler name.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-10-09 14:26:59 +05:30
parent 1a502767ba
commit 5d711bd13e
38 changed files with 1437 additions and 226 deletions
+1 -1
View File
@@ -14,7 +14,7 @@ namespace Fitness_core_BE.Interfaces
Task<List<Booking>> GetBookingsForCustomer(Guid customerId);
Task<bool> CustomerHasActiveBookingForSession(Guid facilitySessionId, Guid customerId);
/// <summary>Unified roster query covering both Scheduled and AdHoc bookings -- backs
/// <summary>Unified roster query covering both Scheduled and WalkIn bookings -- backs
/// GET api/client/bookings. All filters are optional/AND-combined.</summary>
Task<List<Booking>> GetBookings(Guid clientId, Guid? facilityId, Guid? facilitySessionId, DateTime? fromUtc, DateTime? toUtc, string? status);
+23 -6
View File
@@ -23,14 +23,31 @@ namespace Fitness_core_BE.Interfaces
Task<CustomerCards?> GetCardByIdAsync(Guid cardId, CancellationToken ct = default);
Task<CustomerSubscription?> GetCustomerSubscriptionById(Guid subscriptionId);
Task<List<CustomerSubscription>> GetCustomerSubscriptionsByCustomerId(Guid customerId);
Task<CustomerSubscription?> GetActiveSubscriptionByCustomerId(Guid customerId);
/// <summary>True for an ordinary plan (no CustomerPlanClass links) or a class-linked
/// plan with GrantsGymAccess == true.</summary>
Task<bool> PlanGrantsGymAccessAsync(Guid customerPlanId, CancellationToken ct = default);
/// <summary>The customer's active subscription whose plan grants gym access, if any --
/// at most one can exist (enforced by HasActiveGymAccessPlanAsync at assignment time).
/// Primary-only, same as the old GetActiveSubscriptionByCustomerId this replaces.</summary>
Task<CustomerSubscription?> GetActiveGymAccessSubscriptionAsync(Guid customerId);
/// <summary>The customer's active subscription on this exact plan, if any -- the
/// idempotency check for re-assigning a class-only plan (which, unlike a gym plan,
/// doesn't block on ANY other active plan, only a duplicate of itself).</summary>
Task<CustomerSubscription?> GetActiveSubscriptionForPlanAsync(Guid customerId, Guid customerPlanId);
/// <summary>The subscription that covers booking a specific class, preferring one with
/// quota left this cycle. Null if none of the customer's active subscriptions link to
/// this class at all.</summary>
Task<CustomerSubscription?> GetActiveSubscriptionForClassAsync(Guid customerId, Guid facilityClassId);
// Whether this customer is currently an active member (primary OR secondary/group) of
// ANY subscription at this gym. GetActiveSubscriptionByCustomerId only sees subscriptions
// where they're the primary -- this also catches a group co-member, so "one customer, one
// plan at a time" can be enforced everywhere a plan gets assigned, not just the primary
// path.
Task<bool> HasAnyActiveSubscriptionAsync(Guid customerId, CancellationToken ct = default);
// ANY gym-access subscription at this gym -- a class-only subscription never counts, so
// unlimited concurrent class-only subscriptions are allowed. "One customer, one
// GYM-ACCESS plan at a time" is enforced everywhere a gym plan gets assigned, not just
// the primary path.
Task<bool> HasActiveGymAccessPlanAsync(Guid customerId, CancellationToken ct = default);
Task<SubscriptionMember> AddSubscriptionMemberAsync(SubscriptionMember member);
Task<int> GetActiveMemberCountAsync(Guid subscriptionId);
+42
View File
@@ -0,0 +1,42 @@
using Fitness_core_BE.Models;
namespace Fitness_core_BE.Interfaces
{
public interface IFacilityClassRepository
{
Task<FacilityClass> CreateFacilityClass(FacilityClass facilityClass);
Task<List<FacilityClass>> GetFacilityClasses(Guid clientId, bool includeInactive = false);
Task<FacilityClass?> GetFacilityClassById(Guid facilityClassId);
Task<FacilityClass> UpdateFacilityClass(FacilityClass facilityClass);
/// <summary>Active, non-Custom classes whose OccurrencesGeneratedThrough hasn't reached
/// the horizon yet -- Custom classes are never generated, their occurrences are always
/// written explicitly.</summary>
Task<List<FacilityClass>> GetClassesNeedingGenerationAsync(Guid clientId, DateTime horizon);
/// <summary>Existing occurrence start times for this class in [from, to) -- for
/// pre-generation dedup. The unique (FKFacilityClassId, StartTime) index is the actual
/// race-safety net; this just avoids the common sequential case needlessly retrying.</summary>
Task<HashSet<DateTime>> GetExistingOccurrenceStartTimesAsync(Guid facilityClassId, DateTime from, DateTime to);
Task AddOccurrencesAsync(IEnumerable<FacilitySession> occurrences);
Task<List<FacilitySession>> GetOccurrencesForClassAsync(Guid facilityClassId, bool includeCancelled);
/// <summary>Deletes occurrences that are both in the future and have zero Booking rows
/// of ANY status (a Cancelled booking still holds the FK, so it would fail the delete) --
/// the safe-to-regenerate set when a class's recurrence rule changes. Returns the count
/// deleted.</summary>
Task<int> DeleteFutureUnbookedOccurrencesAsync(Guid facilityClassId, DateTime nowUtc);
/// <summary>Confirmed, non-cancelled ad-hoc bookings whose window overlaps an active
/// (non-cancelled) class occurrence on the same facility -- surfaced to staff as a
/// conflict to resolve via the existing Reschedule flow, not auto-resolved.</summary>
Task<List<Booking>> GetSchedulingConflictsAsync(Guid clientId);
/// <summary>Links a newly-created CustomerPlan to the class it's sold against -- the
/// "Manage Plan" action's whole reason for being a class-scoped entry point rather than
/// the general Plans tab.</summary>
Task LinkPlanToClassAsync(Guid customerPlanId, Guid facilityClassId);
}
}
+11 -6
View File
@@ -12,9 +12,14 @@ namespace Fitness_core_BE.Interfaces
Task<FacilitySession> CreateFacilitySession(FacilitySession session);
Task<FacilitySession?> GetFacilitySessionById(Guid facilitySessionId);
Task<List<FacilitySession>> GetSessions(Guid clientId, Guid? facilityId, DateTime fromUtc, DateTime toUtc);
Task<List<FacilitySession>> GetSessions(Guid clientId, Guid? facilityId, DateTime fromUtc, DateTime toUtc, bool includeCancelled = false);
Task<int> GetActiveBookingCount(Guid facilitySessionId);
Task<FacilitySession?> CancelFacilitySession(Guid facilitySessionId, Guid clientId);
Task<List<FacilitySession>> GetOverlappingActiveSessionsAsync(Guid facilityId, DateTime startUtc, DateTime endUtc);
/// <summary>"Mark not held" -- see FacilityService.CancelFacilitySession for the full
/// cascade this backs. Returns the session with its Bookings included so the caller can
/// cancel+refund each Confirmed one.</summary>
Task<FacilitySession?> MarkFacilitySessionNotHeldAsync(Guid facilitySessionId, Guid clientId, string? reason);
// Device binding (absorbed from the old IPlaceRepo -- one AccessDevice per Facility).
Task<AccessDevice?> GetDeviceByIdForClientAsync(Guid deviceId, Guid clientId);
@@ -30,15 +35,15 @@ namespace Fitness_core_BE.Interfaces
Task<Dictionary<Guid, List<Guid>>> GetFacilityIdsByPlanIdsAsync(List<Guid> customerPlanIds);
Task<Dictionary<Guid, string>> GetFacilityNamesByIdsAsync(List<Guid> facilityIds);
// AdHoc booking support (new). See TenantDbContext's Booking (FKFacilityId, StartTime,
// WalkIn booking support (new). See TenantDbContext's Booking (FKFacilityId, StartTime,
// EndTime) index comment -- overlap is checked here rather than by a DB constraint.
/// <summary>Counts non-cancelled/non-completed AdHoc bookings on this facility whose
/// <summary>Counts non-cancelled/non-completed WalkIn bookings on this facility whose
/// [StartTime, EndTime) overlaps the given window. Pass excludeBookingId when
/// re-checking a booking being rescheduled, so it doesn't count against itself.</summary>
Task<int> GetOverlappingAdhocBookingCountAsync(Guid facilityId, DateTime startTime, DateTime endTime, Guid? excludeBookingId = null);
Task<int> GetOverlappingWalkInBookingCountAsync(Guid facilityId, DateTime startTime, DateTime endTime, Guid? excludeBookingId = null);
/// <summary>Issues a row lock on the facility (SELECT ... FOR UPDATE) inside the caller's
/// already-open unit-of-work transaction, serializing concurrent AdHoc-slot claims on the
/// already-open unit-of-work transaction, serializing concurrent WalkIn-slot claims on the
/// same facility so two overlapping bookings can't both pass the overlap-count check.</summary>
Task LockFacilityForUpdateAsync(Guid facilityId);
}
+9 -5
View File
@@ -1,21 +1,25 @@
using Fitness_core_BE.Models;
using Microsoft.AspNetCore.Http;
namespace Fitness_core_BE.Interfaces.Services
{
public interface IBookingService
{
/// <summary>Shared cancel+refund core -- see BookingService.CancelBookingCore.</summary>
Task CancelBookingCore(Booking booking);
/// <summary>Logged-in member books a session against their own plan's allowance.</summary>
Task<object> CreateMemberBooking(object payload, HttpContext httpContext);
/// <summary>No membership at all -- pay-per-session, identity proof is the returned OTP.</summary>
Task<object> CreateGuestBooking(object payload, HttpContext httpContext);
/// <summary>Member books an AdHoc-mode facility directly for an arbitrary time window, no
/// <summary>Member books a WalkIn-mode facility directly for an arbitrary time window, no
/// pre-created session.</summary>
Task<object> CreateMemberAdhocBooking(object payload, HttpContext httpContext);
Task<object> CreateMemberWalkInBooking(object payload, HttpContext httpContext);
/// <summary>Guest/staff walk-in books an AdHoc-mode facility directly.</summary>
Task<object> CreateGuestAdhocBooking(object payload, HttpContext httpContext);
/// <summary>Guest/staff walk-in books a WalkIn-mode facility directly.</summary>
Task<object> CreateGuestWalkInBooking(object payload, HttpContext httpContext);
/// <summary>Renders the public, unauthenticated confirmation page a booking's ShareUrl
/// points to -- the "copy the link and share it" receipt. Returns null if not found, so
@@ -36,7 +40,7 @@ namespace Fitness_core_BE.Interfaces.Services
/// no cancel/recreate needed. Guest bookings only.</summary>
Task<object> ReassignBooking(object payload, HttpContext httpContext);
/// <summary>Staff moves a Confirmed booking to a different time (AdHoc) or session
/// <summary>Staff moves a Confirmed booking to a different time (WalkIn) or session
/// (Scheduled), only onto a slot with room.</summary>
Task<object> RescheduleBooking(object payload, HttpContext httpContext);
+18
View File
@@ -0,0 +1,18 @@
namespace Fitness_core_BE.Interfaces.Services
{
public interface IClassService
{
Task<object> GetFacilityClasses(object payload, HttpContext httpContext);
Task<object> CreateFacilityClass(object payload, HttpContext httpContext);
Task<object> UpdateFacilityClass(object payload, HttpContext httpContext);
Task<object> GetOccurrences(object payload, HttpContext httpContext);
Task<object> GetSchedulingConflicts(object payload, HttpContext httpContext);
/// <summary>Lazy occurrence generation, mirroring the existing SweepOverdueBookingsAsync
/// convention (no background job) -- generates the missing FacilitySession rows up to
/// "today + 8 weeks" for every active, non-Custom class whose watermark hasn't reached
/// it yet, and advances the watermark. Called from the top of FacilityService.GetSessions,
/// the one read path everything already goes through.</summary>
Task EnsureOccurrencesGeneratedAsync(Guid clientId);
}
}