Files
Core_Be/Repos/FacilityClassRepository.cs
T
HarithaRandunu 5d711bd13e 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>
2026-10-09 14:26:59 +05:30

133 lines
5.6 KiB
C#

using Fitness_core_BE.Interfaces;
using Fitness_core_BE.Models;
using Microsoft.EntityFrameworkCore;
namespace Fitness_core_BE.Repos
{
public class FacilityClassRepository : IFacilityClassRepository
{
private readonly TenantDbContext _context;
public FacilityClassRepository(TenantDbContext context)
{
_context = context;
}
public async Task<FacilityClass> CreateFacilityClass(FacilityClass facilityClass)
{
facilityClass.FacilityClassId = Guid.NewGuid();
facilityClass.CreatedAt = DateTime.UtcNow;
facilityClass.IsActive = true;
_context.FacilityClasses.Add(facilityClass);
return facilityClass;
}
public async Task<List<FacilityClass>> GetFacilityClasses(Guid clientId, bool includeInactive = false)
{
var query = _context.FacilityClasses
.Include(fc => fc.Facility)
.Where(fc => fc.FKClientId == clientId);
if (!includeInactive)
query = query.Where(fc => fc.IsActive);
return await query.OrderBy(fc => fc.Name).ToListAsync();
}
public Task<FacilityClass?> GetFacilityClassById(Guid facilityClassId)
=> _context.FacilityClasses
.Include(fc => fc.Facility)
.FirstOrDefaultAsync(fc => fc.FacilityClassId == facilityClassId);
public async Task<FacilityClass> UpdateFacilityClass(FacilityClass facilityClass)
{
var existing = await _context.FacilityClasses.FindAsync(facilityClass.FacilityClassId)
?? throw new KeyNotFoundException("Class not found");
existing.Name = facilityClass.Name;
existing.Description = facilityClass.Description;
existing.RecurrenceType = facilityClass.RecurrenceType;
existing.StartTimeOfDay = facilityClass.StartTimeOfDay;
existing.DurationMinutes = facilityClass.DurationMinutes;
existing.WeekDaysMask = facilityClass.WeekDaysMask;
existing.MonthDays = facilityClass.MonthDays;
existing.SeriesStartDate = facilityClass.SeriesStartDate;
existing.SeriesEndDate = facilityClass.SeriesEndDate;
existing.Capacity = facilityClass.Capacity;
existing.IsUnlimitedCapacity = facilityClass.IsUnlimitedCapacity;
existing.IsActive = facilityClass.IsActive;
existing.OccurrencesGeneratedThrough = facilityClass.OccurrencesGeneratedThrough;
return existing;
}
public Task<List<FacilityClass>> GetClassesNeedingGenerationAsync(Guid clientId, DateTime horizon)
=> _context.FacilityClasses
.Where(fc => fc.FKClientId == clientId && fc.IsActive && fc.RecurrenceType != "Custom"
&& fc.OccurrencesGeneratedThrough < horizon)
.ToListAsync();
public async Task<HashSet<DateTime>> GetExistingOccurrenceStartTimesAsync(Guid facilityClassId, DateTime from, DateTime to)
{
var times = await _context.FacilitySessions
.Where(s => s.FKFacilityClassId == facilityClassId && s.StartTime >= from && s.StartTime < to)
.Select(s => s.StartTime)
.ToListAsync();
return times.ToHashSet();
}
public async Task AddOccurrencesAsync(IEnumerable<FacilitySession> occurrences)
{
var list = occurrences.ToList();
foreach (var occurrence in list)
{
occurrence.FacilitySessionId = Guid.NewGuid();
occurrence.CreatedAt = DateTime.UtcNow;
occurrence.IsCancelled = false;
}
await _context.FacilitySessions.AddRangeAsync(list);
}
public Task<List<FacilitySession>> GetOccurrencesForClassAsync(Guid facilityClassId, bool includeCancelled)
{
var query = _context.FacilitySessions.Where(s => s.FKFacilityClassId == facilityClassId);
if (!includeCancelled)
query = query.Where(s => !s.IsCancelled);
return query.OrderByDescending(s => s.StartTime).ToListAsync();
}
public async Task<int> DeleteFutureUnbookedOccurrencesAsync(Guid facilityClassId, DateTime nowUtc)
{
var candidates = await _context.FacilitySessions
.Where(s => s.FKFacilityClassId == facilityClassId && s.StartTime > nowUtc)
.Where(s => !_context.Bookings.Any(b => b.FKFacilitySessionId == s.FacilitySessionId))
.ToListAsync();
_context.FacilitySessions.RemoveRange(candidates);
return candidates.Count;
}
public async Task LinkPlanToClassAsync(Guid customerPlanId, Guid facilityClassId)
{
_context.CustomerPlanClasses.Add(new CustomerPlanClass
{
CustomerPlanClassId = Guid.NewGuid(),
FKCustomerPlanId = customerPlanId,
FKFacilityClassId = facilityClassId
});
}
public Task<List<Booking>> GetSchedulingConflictsAsync(Guid clientId)
{
return _context.Bookings
.Include(b => b.Facility)
.Where(b => b.FKClientId == clientId && b.FKFacilitySessionId == null && b.Status == "Confirmed")
.Where(b => _context.FacilitySessions.Any(s =>
s.FKFacilityId == b.FKFacilityId && s.FKFacilityClassId != null && !s.IsCancelled &&
s.StartTime < b.EndTime && s.EndTime > b.StartTime))
.ToListAsync();
}
}
}