7a5ea75b50
CI/CD / CI · API (dotnet build + test) (push) Successful in 40s
CI/CD / CI · Admin API (dotnet build) (push) Successful in 30s
CI/CD / CI · Dashboard (tsc) (push) Successful in 1m9s
CI/CD / CI · Admin Web (tsc) (push) Successful in 37s
CI/CD / CI · Website (tsc) (push) Successful in 45s
CI/CD / CI · Koja (tsc) (push) Has been cancelled
CI/CD / Deploy · all services (push) Has been cancelled
Closes the gap where the custom-role matrix was defined but unenforced — most write endpoints only checked café membership, so the API would accept writes a role's UI hid. Adds EnsurePermission(...) to all mutating/sensitive endpoints across 32 controllers, mapped to the granular catalog: - menu/inventory/coupons/customers/expenses/reservations/taxes/branches → CRUD perms - tables/queue/kitchen-stations/print-settings → manage perms - orders → ProcessOrders / EditOrder / VoidOrder / UpdateOrderStatus / HandlePayments, payment corrections → ManageFinancials - HR → CreateStaff / ManageSchedules / ReviewLeave / View+ManageSalaries / ManageStaffCredentials (self-service clock-in/leave preserved) - reports → ViewReports, export → ExportReports, audit → ViewAuditLog - billing → ManageBilling, sms → SendSms/ManageSmsSettings, reviews → ManageReviews, discover/public profile → ManageDiscoverProfile, café settings → ManageCafeSettings, custom roles → ManageRoles Removes legacy [Authorize(Roles=...)] attributes that would have overridden the permission model (orders, branch-menu, pos-device, print). Manual discount/comp have no backend endpoint yet (discounts come from coupons) — gated on the POS UI. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
162 lines
6.1 KiB
C#
162 lines
6.1 KiB
C#
using FluentValidation;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Meezi.API.Models.Orders;
|
|
using Meezi.API.Models.Tables;
|
|
using Meezi.API.Services;
|
|
using Meezi.Core.Authorization;
|
|
using Meezi.Core.Interfaces;
|
|
using Meezi.Shared;
|
|
|
|
namespace Meezi.API.Controllers;
|
|
|
|
[Route("api/cafes/{cafeId}/tables")]
|
|
public class TablesController : CafeApiControllerBase
|
|
{
|
|
private readonly ITableService _tableService;
|
|
private readonly IOrderService _orderService;
|
|
private readonly IValidator<CreateTableRequest> _createValidator;
|
|
private readonly IValidator<PatchTableRequest> _patchValidator;
|
|
private readonly IValidator<SetTableCleaningRequest> _cleaningValidator;
|
|
|
|
public TablesController(
|
|
ITableService tableService,
|
|
IOrderService orderService,
|
|
IValidator<CreateTableRequest> createValidator,
|
|
IValidator<PatchTableRequest> patchValidator,
|
|
IValidator<SetTableCleaningRequest> cleaningValidator)
|
|
{
|
|
_tableService = tableService;
|
|
_orderService = orderService;
|
|
_createValidator = createValidator;
|
|
_patchValidator = patchValidator;
|
|
_cleaningValidator = cleaningValidator;
|
|
}
|
|
|
|
[HttpGet("board")]
|
|
public async Task<IActionResult> GetBoard(
|
|
string cafeId,
|
|
ITenantContext tenant,
|
|
[FromQuery] bool activeOnly = true,
|
|
[FromQuery] string? branchId = null,
|
|
CancellationToken ct = default)
|
|
{
|
|
if (EnsureCafeAccess(cafeId, tenant) is { } denied) return denied;
|
|
var data = await _tableService.GetTableBoardAsync(cafeId, activeOnly, branchId, ct);
|
|
return Ok(new ApiResponse<IReadOnlyList<TableBoardDto>>(true, data));
|
|
}
|
|
|
|
[HttpGet]
|
|
public async Task<IActionResult> GetTables(
|
|
string cafeId,
|
|
ITenantContext tenant,
|
|
[FromQuery] string? branchId = null,
|
|
CancellationToken ct = default)
|
|
{
|
|
if (EnsureCafeAccess(cafeId, tenant) is { } denied) return denied;
|
|
var data = await _tableService.GetTablesAsync(cafeId, branchId, ct);
|
|
return Ok(new ApiResponse<IReadOnlyList<TableDto>>(true, data));
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> CreateTable(
|
|
string cafeId,
|
|
[FromBody] CreateTableRequest request,
|
|
ITenantContext tenant,
|
|
CancellationToken ct)
|
|
{
|
|
if (EnsureCafeAccess(cafeId, tenant) is { } denied) return denied;
|
|
if (EnsurePermission(tenant, Permission.ManageTables) is { } permDenied) return permDenied;
|
|
var validation = await _createValidator.ValidateAsync(request, ct);
|
|
if (!validation.IsValid) return BadRequest(ValidationError(validation));
|
|
|
|
var data = await _tableService.CreateTableAsync(cafeId, request, ct);
|
|
if (data is null) return NotFoundError("Branch not found.");
|
|
return Ok(new ApiResponse<TableDto>(true, data));
|
|
}
|
|
|
|
[HttpPatch("{id}")]
|
|
public async Task<IActionResult> PatchTable(
|
|
string cafeId,
|
|
string id,
|
|
[FromBody] PatchTableRequest request,
|
|
ITenantContext tenant,
|
|
CancellationToken ct)
|
|
{
|
|
if (EnsureCafeAccess(cafeId, tenant) is { } denied) return denied;
|
|
if (EnsurePermission(tenant, Permission.ManageTables) is { } permDenied) return permDenied;
|
|
var validation = await _patchValidator.ValidateAsync(request, ct);
|
|
if (!validation.IsValid) return BadRequest(ValidationError(validation));
|
|
|
|
var data = await _tableService.PatchTableAsync(cafeId, id, request, ct);
|
|
if (data is null) return NotFoundError();
|
|
return Ok(new ApiResponse<TableDto>(true, data));
|
|
}
|
|
|
|
[HttpGet("{id}/active-order")]
|
|
public async Task<IActionResult> GetActiveOrder(
|
|
string cafeId,
|
|
string id,
|
|
ITenantContext tenant,
|
|
CancellationToken ct)
|
|
{
|
|
if (EnsureCafeAccess(cafeId, tenant) is { } denied) return denied;
|
|
var data = await _orderService.GetActiveOrderByTableAsync(cafeId, id, ct);
|
|
if (data is null) return NotFoundError("No active order for this table.");
|
|
return Ok(new ApiResponse<OrderDto>(true, data));
|
|
}
|
|
|
|
[HttpDelete("{id}")]
|
|
public async Task<IActionResult> DeleteTable(
|
|
string cafeId,
|
|
string id,
|
|
ITenantContext tenant,
|
|
CancellationToken ct)
|
|
{
|
|
if (EnsureCafeAccess(cafeId, tenant) is { } denied) return denied;
|
|
if (EnsurePermission(tenant, Permission.ManageTables) is { } permDenied) return permDenied;
|
|
|
|
var result = await _tableService.DeleteTableAsync(cafeId, id, ct);
|
|
if (!result.Success)
|
|
{
|
|
var status = result.ErrorCode == "TABLE_HAS_OPEN_ORDER"
|
|
? StatusCodes.Status409Conflict
|
|
: StatusCodes.Status400BadRequest;
|
|
return StatusCode(status,
|
|
new ApiResponse<object>(false, null, new ApiError(result.ErrorCode!, result.Message ?? result.ErrorCode!)));
|
|
}
|
|
|
|
return Ok(new ApiResponse<object>(true, result.Data));
|
|
}
|
|
|
|
[HttpPatch("{id}/cleaning")]
|
|
public async Task<IActionResult> SetCleaning(
|
|
string cafeId,
|
|
string id,
|
|
[FromBody] SetTableCleaningRequest request,
|
|
ITenantContext tenant,
|
|
CancellationToken ct)
|
|
{
|
|
if (EnsureCafeAccess(cafeId, tenant) is { } denied) return denied;
|
|
if (EnsurePermission(tenant, Permission.ManageTables) is { } permDenied) return permDenied;
|
|
var validation = await _cleaningValidator.ValidateAsync(request, ct);
|
|
if (!validation.IsValid) return BadRequest(ValidationError(validation));
|
|
|
|
var data = await _tableService.SetTableCleaningAsync(cafeId, id, request.IsCleaning, ct);
|
|
if (data is null) return NotFoundError();
|
|
return Ok(new ApiResponse<TableBoardDto>(true, data));
|
|
}
|
|
|
|
[HttpGet("{id}/qr")]
|
|
public async Task<IActionResult> GetQrPng(
|
|
string cafeId,
|
|
string id,
|
|
ITenantContext tenant,
|
|
CancellationToken ct)
|
|
{
|
|
if (EnsureCafeAccess(cafeId, tenant) is { } denied) return denied;
|
|
var png = await _tableService.GetQrPngAsync(cafeId, id, ct);
|
|
if (png is null) return NotFoundError();
|
|
return File(png, "image/png", $"table-{id}-qr.png");
|
|
}
|
|
}
|