Files
Core_Be/Controllers/FacilityController.cs
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

125 lines
6.0 KiB
C#

using Fitness_core_BE.DTOs.Facility;
using Fitness_core_BE.Helpers;
using Fitness_core_BE.Interfaces.Services;
using Fitness_core_BE.Models.IOs;
using Microsoft.AspNetCore.Mvc;
using static Fitness_core_BE.Helpers.JwtMiddleware;
using static Fitness_core_BE.Helpers.ApiResponseHelper;
namespace Fitness_core_BE.Controllers
{
// Staff/owner-facing facility (bookable area + access zone -- merged from the former
// Facility/Place split) management. Reads stay on the "bookings" nav permission (the
// Bookings page's tabs need the list); mutations (create/edit/status/device-assign) are on
// "memberships" since facility setup now lives on the Memberships page.
[ApiController]
[Route("api/client/facilities")]
public class FacilityController(IFacilityService facilityService) : ControllerBase
{
private readonly IFacilityService _facilityService = facilityService;
[CustomMiddleware]
[TenantResolution]
[RequiresPermission("bookings")]
[HttpGet("")]
public async Task<ActionResult<ApiResponse<object>>> GetAllFacilities([FromQuery] bool includeInactive = false)
=> Response(await _facilityService.GetAllFacilities(new { includeInactive }, HttpContext));
[CustomMiddleware]
[TenantResolution]
[RequiresPermission("memberships")]
[HttpPost("")]
public async Task<ActionResult<ApiResponse<object>>> CreateFacility([FromBody] CreateFacilityRequest request)
=> Response(await _facilityService.CreateFacility(new
{
name = request.Name,
supportsClasses = request.SupportsClasses,
supportsBooking = request.SupportsBooking,
description = request.Description,
defaultCapacity = request.DefaultCapacity,
numberOfUnits = request.NumberOfUnits,
pricePerHour = request.PricePerHour
}, HttpContext));
[CustomMiddleware]
[TenantResolution]
[RequiresPermission("memberships")]
[HttpPut("{id:guid}")]
public async Task<ActionResult<ApiResponse<object>>> UpdateFacility(Guid id, [FromBody] UpdateFacilityRequest request)
{
var payload = new Dictionary<string, object> { ["facilityId"] = id };
if (request.Name != null) payload["name"] = request.Name;
if (request.SupportsClasses.HasValue) payload["supportsClasses"] = request.SupportsClasses.Value;
if (request.SupportsBooking.HasValue) payload["supportsBooking"] = request.SupportsBooking.Value;
if (request.Description != null) payload["description"] = request.Description;
if (request.DefaultCapacity.HasValue) payload["defaultCapacity"] = request.DefaultCapacity.Value;
if (request.NumberOfUnits.HasValue) payload["numberOfUnits"] = request.NumberOfUnits.Value;
if (request.PricePerHour.HasValue) payload["pricePerHour"] = request.PricePerHour.Value;
if (request.IsActive.HasValue) payload["isActive"] = request.IsActive.Value;
return Response(await _facilityService.UpdateFacility(payload, HttpContext));
}
[CustomMiddleware]
[TenantResolution]
[RequiresPermission("memberships")]
[HttpPatch("{id:guid}/status")]
public async Task<ActionResult<ApiResponse<object>>> SetFacilityStatus(Guid id, [FromBody] SetFacilityStatusRequest request)
=> Response(await _facilityService.SetFacilityStatus(new { facilityId = id, isActive = request.IsActive }, HttpContext));
[CustomMiddleware]
[TenantResolution]
[RequiresPermission("memberships")]
[HttpPut("{id:guid}/device")]
public async Task<ActionResult<ApiResponse<object>>> AssignDevice(Guid id, [FromBody] AssignFacilityDeviceRequest request)
=> Response(await _facilityService.AssignDevice(new { facilityId = id, deviceId = request.DeviceId }, HttpContext));
[CustomMiddleware]
[TenantResolution]
[RequiresPermission("bookings")]
[HttpPost("sessions")]
public async Task<ActionResult<ApiResponse<object>>> CreateFacilitySession([FromBody] CreateFacilitySessionRequest request)
=> Response(await _facilityService.CreateFacilitySession(new
{
facilityId = request.FacilityId,
startTime = request.StartTime.ToString("o"),
endTime = request.EndTime.ToString("o"),
capacity = request.Capacity
}, HttpContext));
[CustomMiddleware]
[TenantResolution]
[RequiresPermission("bookings")]
[HttpGet("sessions")]
public async Task<ActionResult<ApiResponse<object>>> GetSessions(
[FromQuery] Guid? facilityId, [FromQuery] DateTime? fromDate, [FromQuery] DateTime? toDate, [FromQuery] bool includeCancelled = false)
=> Response(await _facilityService.GetSessions(new
{
facilityId,
fromDate = fromDate?.ToString("o"),
toDate = toDate?.ToString("o"),
includeCancelled
}, HttpContext));
[CustomMiddleware]
[TenantResolution]
[RequiresPermission("bookings")]
[HttpDelete("sessions/{id:guid}")]
public async Task<ActionResult<ApiResponse<object>>> CancelFacilitySession(Guid id, [FromBody] MarkNotHeldRequest? request)
=> Response(await _facilityService.CancelFacilitySession(new { facilitySessionId = id, reason = request?.Reason }, HttpContext));
[CustomMiddleware]
[TenantResolution]
[RequiresPermission("bookings")]
[HttpGet("availability")]
public async Task<ActionResult<ApiResponse<object>>> GetAvailability(
[FromQuery] Guid? facilityId, [FromQuery] DateTime startTime, [FromQuery] int durationMinutes)
=> Response(await _facilityService.GetAvailability(new
{
facilityId,
startTime = startTime.ToString("o"),
durationMinutes
}, HttpContext));
}
}