Fix loading sheet creation flow
This commit is contained in:
@@ -31,10 +31,13 @@ public class LoadingSheetController(LoadingSheetService service, ICurrentUserSer
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Create(CreateLoadingSheetRequest request, CancellationToken ct)
|
||||
{
|
||||
var userId = currentUser.UserId ?? throw new UnauthorizedAccessException();
|
||||
// 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.PopulatedByUserId;
|
||||
if (userId is null) return BadRequest(new { error = "PopulatedByUserId is required while auth is unavailable" });
|
||||
try
|
||||
{
|
||||
var result = await service.CreateAsync(request, userId, ct);
|
||||
var result = await service.CreateAsync(request, userId.Value, ct);
|
||||
return CreatedAtAction(nameof(GetById), new { id = result.Id }, result);
|
||||
}
|
||||
catch (KeyNotFoundException) { return NotFound(); }
|
||||
@@ -54,12 +57,15 @@ public class LoadingSheetController(LoadingSheetService service, ICurrentUserSer
|
||||
}
|
||||
|
||||
[HttpPost("{id:guid}/confirm")]
|
||||
public async Task<IActionResult> Confirm(Guid id, CancellationToken ct)
|
||||
public async Task<IActionResult> Confirm(Guid id, ConfirmLoadingSheetRequest request, CancellationToken ct)
|
||||
{
|
||||
var userId = currentUser.UserId ?? throw new UnauthorizedAccessException();
|
||||
// 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, ct);
|
||||
var result = await service.ConfirmAsync(id, userId.Value, ct);
|
||||
return Ok(result);
|
||||
}
|
||||
catch (KeyNotFoundException) { return NotFound(); }
|
||||
|
||||
@@ -25,7 +25,9 @@ public record LoadingSheetItemResponse(
|
||||
|
||||
public record LoadingSheetOrderResponse(Guid ShopPOId);
|
||||
|
||||
public record CreateLoadingSheetRequest(Guid FK_RideId);
|
||||
public record CreateLoadingSheetRequest(Guid FK_RideId, Guid? PopulatedByUserId = null);
|
||||
|
||||
public record SetItemsRequest(List<SetItemInput> Items);
|
||||
public record SetItemInput(Guid FK_ItemId, Guid FK_WarehouseId, decimal Quantity, Guid? FK_ShopPOItemId = null);
|
||||
|
||||
public record ConfirmLoadingSheetRequest(Guid? ConfirmedByUserId = null);
|
||||
|
||||
@@ -64,26 +64,47 @@ public class LoadingSheetService(
|
||||
if (sheet.Status != LoadingSheetStatus.Planned)
|
||||
throw new InvalidOperationException("Items can only be changed while the sheet is Planned");
|
||||
|
||||
var shopPOItemIds = request.Items
|
||||
.Where(i => i.FK_ShopPOItemId.HasValue)
|
||||
.Select(i => i.FK_ShopPOItemId!.Value)
|
||||
.ToList();
|
||||
var shopPOIds = shopPOItemIds.Count > 0
|
||||
? await repo.GetShopPOIdsByItemIdsAsync(shopPOItemIds)
|
||||
: new List<Guid>();
|
||||
|
||||
// Upsert by (item, warehouse, shop PO item): a matching row gets its quantity
|
||||
// updated in place, a new combination is added. Rows for combinations not present
|
||||
// in this request are left untouched, so a later save can top up the same PO
|
||||
// item's remaining quantity from a different warehouse without losing what was
|
||||
// already planned from the first one.
|
||||
await uow.BeginAsync(ct);
|
||||
try
|
||||
{
|
||||
repo.RemoveItems(sheet.Items);
|
||||
await repo.AddItemsAsync(request.Items.Select(i => new LoadingSheetItem
|
||||
var newItems = new List<LoadingSheetItem>();
|
||||
foreach (var input in request.Items)
|
||||
{
|
||||
FK_LoadingSheetId = sheet.Id,
|
||||
FK_ItemId = i.FK_ItemId,
|
||||
FK_WarehouseId = i.FK_WarehouseId,
|
||||
FK_ShopPOItemId = i.FK_ShopPOItemId,
|
||||
QuantityDispatched = i.Quantity
|
||||
}));
|
||||
var existing = sheet.Items.FirstOrDefault(i =>
|
||||
i.FK_ItemId == input.FK_ItemId &&
|
||||
i.FK_WarehouseId == input.FK_WarehouseId &&
|
||||
i.FK_ShopPOItemId == input.FK_ShopPOItemId);
|
||||
|
||||
if (existing is not null)
|
||||
existing.QuantityDispatched = input.Quantity;
|
||||
else
|
||||
newItems.Add(new LoadingSheetItem
|
||||
{
|
||||
FK_LoadingSheetId = sheet.Id,
|
||||
FK_ItemId = input.FK_ItemId,
|
||||
FK_WarehouseId = input.FK_WarehouseId,
|
||||
FK_ShopPOItemId = input.FK_ShopPOItemId,
|
||||
QuantityDispatched = input.Quantity
|
||||
});
|
||||
}
|
||||
if (newItems.Count > 0)
|
||||
await repo.AddItemsAsync(newItems);
|
||||
|
||||
var shopPOItemIds = sheet.Items.Select(i => i.FK_ShopPOItemId)
|
||||
.Concat(newItems.Select(i => i.FK_ShopPOItemId))
|
||||
.Where(x => x.HasValue)
|
||||
.Select(x => x!.Value)
|
||||
.Distinct()
|
||||
.ToList();
|
||||
var shopPOIds = shopPOItemIds.Count > 0
|
||||
? await repo.GetShopPOIdsByItemIdsAsync(shopPOItemIds)
|
||||
: new List<Guid>();
|
||||
|
||||
await repo.ReplaceOrdersAsync(sheet.Id, shopPOIds);
|
||||
await uow.CommitAsync(ct);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user