Files
meezi/src/Meezi.API/Controllers/InventoryController.cs
T
soroush.asadi 15def7ff1c
CI/CD / CI · API (dotnet build + test) (push) Successful in 1m10s
CI/CD / CI · Admin API (dotnet build) (push) Successful in 52s
CI/CD / CI · Dashboard (tsc) (push) Successful in 1m5s
CI/CD / CI · Admin Web (tsc) (push) Successful in 35s
CI/CD / CI · Website (tsc) (push) Successful in 45s
CI/CD / CI · Koja (tsc) (push) Successful in 55s
CI/CD / Deploy · all services (push) Successful in 3m29s
feat: delete actions for warehouse/reservations/coupons/customers + Koja listing toggle
Delete (every manageable entity that only had "add" now has delete):
- Ingredients (warehouse): new DELETE /inventory/ingredients/{id} (soft-delete via
  the global DeletedAt filter — no FK trouble with recipes/movements) + NoOp stub +
  trash button in the materials cards.
- Reservations: new DELETE /reservations/{id} (soft-delete) + per-card delete button.
- Coupons & Customers: backend DELETE already existed; wired delete buttons in the UI.
- Shared ConfirmDialog component used by all delete flows (RTL-aware).
- Audit result: tables/branches/taxes/kitchen-stations/expenses/menu/terminals already
  had delete; HR has no "add" so no delete needed; shifts intentionally excluded
  (financial open/close records, not add-style entities).

Koja visibility:
- New Cafe.ShowOnKoja flag, default TRUE (DB default true so existing cafés stay
  listed). Discover query now filters IsVerified && !Deleted && ShowOnKoja.
- public-profile GET/PUT expose showOnKoja; dashboard public-profile panel has an
  on-by-default toggle that persists immediately. Platform IsVerified gate unchanged.
- EF migration AddCafeShowOnKoja (defaultValue: true).

Also: added the missing errors.generic i18n key (fa/en/ar) so useApiError's fallback
resolves instead of rendering the literal "errors.generic". 81 API tests pass.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-02 16:14:40 +03:30

154 lines
5.8 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Meezi.API.Services;
using Meezi.Core.Interfaces;
using Meezi.Shared;
namespace Meezi.API.Controllers;
[Route("api/cafes/{cafeId}/inventory")]
public class InventoryController : CafeApiControllerBase
{
private readonly IInventoryService _inventory;
public InventoryController(IInventoryService inventory) => _inventory = inventory;
[HttpGet("ingredients")]
public async Task<IActionResult> List(string cafeId, ITenantContext tenant, CancellationToken ct)
{
if (EnsureCafeAccess(cafeId, tenant) is { } denied) return denied;
var data = await _inventory.ListAsync(cafeId, ct);
return Ok(new ApiResponse<object>(true, data));
}
[HttpGet("low-stock")]
public async Task<IActionResult> LowStock(string cafeId, ITenantContext tenant, CancellationToken ct)
{
if (EnsureCafeAccess(cafeId, tenant) is { } denied) return denied;
var data = await _inventory.LowStockAsync(cafeId, ct);
return Ok(new ApiResponse<object>(true, data));
}
[HttpPost("ingredients")]
public async Task<IActionResult> Create(
string cafeId,
[FromBody] CreateIngredientRequest request,
ITenantContext tenant,
CancellationToken ct)
{
if (EnsureCafeAccess(cafeId, tenant) is { } denied) return denied;
if (string.IsNullOrWhiteSpace(request.Name))
return BadRequest(new ApiResponse<object>(false, null, new ApiError("VALIDATION_ERROR", "Name is required.")));
if (request.QuantityOnHand > 0 && request.TotalPaidToman > 0 && string.IsNullOrWhiteSpace(request.BranchId))
return BadRequest(new ApiResponse<object>(false, null,
new ApiError("BRANCH_ID_REQUIRED", "Branch is required when recording purchase cost.")));
var created = await _inventory.CreateAsync(cafeId, request, ct);
return Ok(new ApiResponse<object>(true, created));
}
[HttpPatch("ingredients/{ingredientId}")]
public async Task<IActionResult> Update(
string cafeId,
string ingredientId,
[FromBody] UpdateIngredientRequest request,
ITenantContext tenant,
CancellationToken ct)
{
if (EnsureCafeAccess(cafeId, tenant) is { } denied) return denied;
var updated = await _inventory.UpdateAsync(cafeId, ingredientId, request, ct);
if (updated is null) return NotFoundError();
return Ok(new ApiResponse<object>(true, updated));
}
[HttpDelete("ingredients/{ingredientId}")]
public async Task<IActionResult> Delete(
string cafeId,
string ingredientId,
ITenantContext tenant,
CancellationToken ct)
{
if (EnsureCafeAccess(cafeId, tenant) is { } denied) return denied;
var deleted = await _inventory.DeleteAsync(cafeId, ingredientId, ct);
if (!deleted) return NotFoundError();
return Ok(new ApiResponse<object>(true, new { id = ingredientId }));
}
[HttpPost("ingredients/{ingredientId}/adjust")]
public async Task<IActionResult> Adjust(
string cafeId,
string ingredientId,
[FromBody] AdjustStockRequest request,
ITenantContext tenant,
CancellationToken ct)
{
if (EnsureCafeAccess(cafeId, tenant) is { } denied) return denied;
try
{
var updated = await _inventory.AdjustAsync(cafeId, ingredientId, request, tenant.UserId, ct);
if (updated is null) return NotFoundError();
return Ok(new ApiResponse<object>(true, updated));
}
catch (InvalidOperationException ex) when (ex.Message is "TOTAL_PAID_REQUIRED" or "BRANCH_ID_REQUIRED")
{
return BadRequest(new ApiResponse<object>(false, null,
new ApiError(ex.Message, ex.Message switch
{
"TOTAL_PAID_REQUIRED" => "Enter total paid for stock received.",
_ => "Branch is required for purchase cost."
})));
}
}
[HttpGet("purchases")]
public async Task<IActionResult> PurchasesSummary(
string cafeId,
[FromQuery] string branchId,
[FromQuery] DateOnly? from,
[FromQuery] DateOnly? to,
ITenantContext tenant,
CancellationToken ct)
{
if (EnsureCafeAccess(cafeId, tenant) is { } denied) return denied;
if (string.IsNullOrWhiteSpace(branchId))
return BadRequest(new ApiResponse<object>(false, null,
new ApiError("BRANCH_ID_REQUIRED", "branchId is required.")));
var today = DateOnly.FromDateTime(DateTime.UtcNow);
var summary = await _inventory.GetPurchasesSummaryAsync(
cafeId,
branchId,
from ?? today.AddDays(-30),
to ?? today,
ct);
return Ok(new ApiResponse<object>(true, summary));
}
[HttpGet("menu-items/{menuItemId}/recipe")]
public async Task<IActionResult> GetRecipe(
string cafeId,
string menuItemId,
ITenantContext tenant,
CancellationToken ct)
{
if (EnsureCafeAccess(cafeId, tenant) is { } denied) return denied;
var recipe = await _inventory.GetRecipeAsync(cafeId, menuItemId, ct);
if (recipe is null) return NotFoundError("Menu item not found.");
return Ok(new ApiResponse<object>(true, recipe));
}
[HttpPut("menu-items/{menuItemId}/recipe")]
public async Task<IActionResult> SetRecipe(
string cafeId,
string menuItemId,
[FromBody] SetMenuItemRecipeRequest request,
ITenantContext tenant,
CancellationToken ct)
{
if (EnsureCafeAccess(cafeId, tenant) is { } denied) return denied;
var recipe = await _inventory.SetRecipeAsync(cafeId, menuItemId, request, ct);
if (recipe is null) return NotFoundError("Menu item not found.");
return Ok(new ApiResponse<object>(true, recipe));
}
}