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
+50 -4
View File
@@ -134,23 +134,69 @@ namespace Fitness_core_BE.Repos
.ToListAsync();
}
public async Task<CustomerSubscription?> GetActiveSubscriptionByCustomerId(Guid customerId)
// A plan with no CustomerPlanClass links is an ordinary gym plan -- GrantsGymAccess is
// never consulted for it (always true in effect). A class-linked plan only grants gym
// access when GrantsGymAccess is explicitly set. Reused by every gym-access query below.
private IQueryable<CustomerPlan> GymAccessPlans() => _context.CustomerPlans.Where(p =>
!_context.CustomerPlanClasses.Any(c => c.FKCustomerPlanId == p.CustomerPlanId) || p.GrantsGymAccess);
public Task<bool> PlanGrantsGymAccessAsync(Guid customerPlanId, CancellationToken ct = default)
=> GymAccessPlans().AnyAsync(p => p.CustomerPlanId == customerPlanId, ct);
public async Task<CustomerSubscription?> GetActiveGymAccessSubscriptionAsync(Guid customerId)
{
return await _context.CustomerSubscriptions
.Include(s => s.TenantCustomerLink)
.Include(s => s.CustomerPlan)
.Where(s => s.FKCustomerId == customerId && s.IsActive)
.Where(s => GymAccessPlans().Any(p => p.CustomerPlanId == s.FKCustomerPlanId))
.OrderByDescending(s => s.CreatedAt)
.FirstOrDefaultAsync();
}
public async Task<CustomerSubscription?> GetActiveSubscriptionForPlanAsync(Guid customerId, Guid customerPlanId)
{
return await _context.CustomerSubscriptions
.Include(s => s.CustomerPlan)
.Where(s => s.FKCustomerId == customerId && s.IsActive && s.FKCustomerPlanId == customerPlanId)
.OrderByDescending(s => s.CreatedAt)
.FirstOrDefaultAsync();
}
/// <summary>Resolves which of the customer's (possibly several) active subscriptions
/// covers a specific class, preferring one that still has quota left this cycle over one
/// that's already exhausted it -- two different plans can link the same class.</summary>
public async Task<CustomerSubscription?> GetActiveSubscriptionForClassAsync(Guid customerId, Guid facilityClassId)
{
var candidates = await _context.CustomerSubscriptions
.Include(s => s.CustomerPlan)
.Where(s => s.FKCustomerId == customerId && s.IsActive)
.Where(s => _context.CustomerPlanClasses.Any(c =>
c.FKCustomerPlanId == s.FKCustomerPlanId && c.FKFacilityClassId == facilityClassId))
.OrderByDescending(s => s.CreatedAt)
.ToListAsync();
bool HasQuota(CustomerSubscription s)
{
if (!s.CustomerPlan.SessionsPerCycle.HasValue) return true;
var carriedOver = s.SessionsCarriedOverExpiresAt.HasValue && s.SessionsCarriedOverExpiresAt.Value < DateTime.UtcNow
? 0 : s.SessionsCarriedOver;
return s.SessionsUsedThisCycle < s.CustomerPlan.SessionsPerCycle.Value + carriedOver;
}
return candidates.FirstOrDefault(HasQuota) ?? candidates.FirstOrDefault();
}
// SubscriptionMembers always has a row for the primary too (CustomerSubscriptionFactory
// adds it alongside the CustomerSubscription itself), so this one query covers both
// "primary somewhere" and "secondary/group member somewhere" in a single check.
public Task<bool> HasAnyActiveSubscriptionAsync(Guid customerId, CancellationToken ct = default)
// "primary somewhere" and "secondary/group member somewhere" in a single check. Scoped
// to gym-access plans only -- a class-only subscription never blocks a second one via
// this check (unlimited concurrent class-only subscriptions are allowed).
public Task<bool> HasActiveGymAccessPlanAsync(Guid customerId, CancellationToken ct = default)
{
return _context.SubscriptionMembers.AnyAsync(m =>
m.FKCustomerId == customerId && m.IsActive && m.Subscription.IsActive, ct);
m.FKCustomerId == customerId && m.IsActive && m.Subscription.IsActive &&
GymAccessPlans().Any(p => p.CustomerPlanId == m.Subscription.FKCustomerPlanId), ct);
}
// ---------- Group membership ----------