104 lines
4.2 KiB
C#
104 lines
4.2 KiB
C#
using Distribution.Infra.Authorization;
|
|
using Distribution.Models;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace Distribution.Features.ShopPO;
|
|
|
|
[ApiController]
|
|
[Route("api/shop-pos")]
|
|
public class ShopPOController(ShopPOService service, ICurrentUserService currentUser) : ControllerBase
|
|
{
|
|
[HttpGet]
|
|
public async Task<IActionResult> GetAll(
|
|
[FromQuery] Guid? agentId,
|
|
[FromQuery] Guid? routeId,
|
|
[FromQuery] Guid? regionId,
|
|
[FromQuery] Guid? shopId,
|
|
[FromQuery] bool? confirmed,
|
|
[FromQuery] ShopPOStatus? status,
|
|
[FromQuery] int page = 1,
|
|
[FromQuery] int pageSize = 20)
|
|
{
|
|
var result = await service.GetAllAsync(agentId, routeId, regionId, shopId, confirmed, status, page, pageSize);
|
|
return Ok(result);
|
|
}
|
|
|
|
[HttpGet("{id:guid}")]
|
|
public async Task<IActionResult> GetById(Guid id)
|
|
{
|
|
var result = await service.GetByIdAsync(id);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> Create(CreateShopPORequest request, CancellationToken ct)
|
|
{
|
|
// TEMPORARY: currentUser.UserId is always unset due to the broken auth middleware
|
|
// (testbug.md #28) — fall back to the explicitly supplied user until that's fixed.
|
|
var userId = currentUser.UserId ?? request.RaisedByUserId;
|
|
if (userId is null) return BadRequest(new { error = "RaisedByUserId is required while auth is unavailable" });
|
|
try
|
|
{
|
|
var result = await service.CreateAsync(request, userId.Value, ct);
|
|
return CreatedAtAction(nameof(GetById), new { id = result.Id }, result);
|
|
}
|
|
catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); }
|
|
}
|
|
|
|
[HttpPut("{id:guid}/items")]
|
|
public async Task<IActionResult> UpdateItems(Guid id, UpdateShopPOItemsRequest request, CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
var result = await service.UpdateItemsAsync(id, request, ct);
|
|
return Ok(result);
|
|
}
|
|
catch (KeyNotFoundException) { return NotFound(); }
|
|
catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); }
|
|
}
|
|
|
|
[HttpPost("{id:guid}/confirm")]
|
|
public async Task<IActionResult> Confirm(Guid id, ConfirmShopPORequest request, CancellationToken ct)
|
|
{
|
|
// TEMPORARY: currentUser.UserId is always unset due to the broken auth middleware
|
|
// (testbug.md #28) — fall back to the explicitly supplied user until that's fixed.
|
|
var userId = currentUser.UserId ?? request.ConfirmedByUserId;
|
|
if (userId is null) return BadRequest(new { error = "ConfirmedByUserId is required while auth is unavailable" });
|
|
try
|
|
{
|
|
var result = await service.ConfirmAsync(id, userId.Value, ct);
|
|
return Ok(result);
|
|
}
|
|
catch (KeyNotFoundException) { return NotFound(); }
|
|
catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); }
|
|
}
|
|
|
|
[HttpPost("{id:guid}/confirm-and-create-invoice")]
|
|
public async Task<IActionResult> ConfirmAndCreateInvoice(Guid id, ConfirmShopPORequest request, CancellationToken ct)
|
|
{
|
|
// TEMPORARY: currentUser.UserId is always unset due to the broken auth middleware
|
|
// (testbug.md #28) — fall back to the explicitly supplied user until that's fixed.
|
|
var userId = currentUser.UserId ?? request.ConfirmedByUserId;
|
|
if (userId is null) return BadRequest(new { error = "ConfirmedByUserId is required while auth is unavailable" });
|
|
try
|
|
{
|
|
var result = await service.ConfirmAndCreateInvoiceAsync(id, userId.Value, ct);
|
|
return Ok(result);
|
|
}
|
|
catch (KeyNotFoundException) { return NotFound(); }
|
|
catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); }
|
|
}
|
|
|
|
[HttpPost("{id:guid}/cancel")]
|
|
public async Task<IActionResult> Cancel(Guid id, CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
var result = await service.CancelAsync(id, ct);
|
|
return Ok(result);
|
|
}
|
|
catch (KeyNotFoundException) { return NotFound(); }
|
|
catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); }
|
|
}
|
|
}
|