remove store kyc features
This commit is contained in:
@@ -10,6 +10,7 @@ public interface IShopPORepository
|
||||
Task<List<Models.ShopPO>> GetConfirmedWithoutInvoiceAsync(Guid? agentId, Guid? routeId, Guid? shopId);
|
||||
Task<List<Models.ShopPOItem>> GetActiveItemsByShopPOIdAsync(Guid shopPOId);
|
||||
Task<List<Models.ShopPO>> GetByStoreIdAsync(Guid storeId);
|
||||
Task<Dictionary<Guid, (string Name, string Role)>> GetUserInfoAsync(IEnumerable<Guid> userIds);
|
||||
Task AddAsync(Models.ShopPO entity);
|
||||
void Update(Models.ShopPO entity);
|
||||
void Remove(Models.ShopPO entity);
|
||||
|
||||
@@ -90,11 +90,15 @@ public class ShopPOController(ShopPOService service, ICurrentUserService current
|
||||
}
|
||||
|
||||
[HttpPost("{id:guid}/cancel")]
|
||||
public async Task<IActionResult> Cancel(Guid id, CancellationToken ct)
|
||||
public async Task<IActionResult> Cancel(Guid id, CancelShopPORequest 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.CancelledByUserId;
|
||||
if (userId is null) return BadRequest(new { error = "CancelledByUserId is required while auth is unavailable" });
|
||||
try
|
||||
{
|
||||
var result = await service.CancelAsync(id, ct);
|
||||
var result = await service.CancelAsync(id, userId.Value, request.Reason, ct);
|
||||
return Ok(result);
|
||||
}
|
||||
catch (KeyNotFoundException) { return NotFound(); }
|
||||
|
||||
@@ -6,6 +6,7 @@ public record CreateShopPORequest(Guid StoreId, List<ShopPOItemRequest> Items, G
|
||||
public record ShopPOItemRequest(Guid ItemId, int Quantity, decimal? UnitPrice = null);
|
||||
public record UpdateShopPOItemsRequest(List<ShopPOItemRequest> Items);
|
||||
public record ConfirmShopPORequest(Guid? ConfirmedByUserId = null);
|
||||
public record CancelShopPORequest(string Reason, Guid? CancelledByUserId = null);
|
||||
|
||||
public record ShopPOItemResponse(Guid Id, Guid ItemId, string ItemName, int Quantity, int QuantityFulfilled, decimal UnitPrice, bool IsActive);
|
||||
|
||||
@@ -22,4 +23,13 @@ public record ShopPOResponse(
|
||||
DateTime? FulfilledAt,
|
||||
List<ShopPOItemResponse> Items,
|
||||
bool IsActive,
|
||||
DateTime CreatedAt);
|
||||
DateTime CreatedAt,
|
||||
string? RaisedByName = null,
|
||||
string? RaisedByRole = null,
|
||||
string? ConfirmedByName = null,
|
||||
string? ConfirmedByRole = null,
|
||||
Guid? CancelledBy = null,
|
||||
DateTime? CancelledAt = null,
|
||||
string? CancelReason = null,
|
||||
string? CancelledByName = null,
|
||||
string? CancelledByRole = null);
|
||||
|
||||
@@ -64,6 +64,16 @@ public class ShopPORepository(AppDBContext context) : IShopPORepository
|
||||
.Where(p => p.FK_StoreId == storeId && p.IsActive)
|
||||
.ToListAsync();
|
||||
|
||||
public async Task<Dictionary<Guid, (string Name, string Role)>> GetUserInfoAsync(IEnumerable<Guid> userIds)
|
||||
{
|
||||
var ids = userIds.Distinct().ToList();
|
||||
var users = await context.Users
|
||||
.Where(u => ids.Contains(u.Id))
|
||||
.Select(u => new { u.Id, u.FullName, RoleName = u.Role.Name })
|
||||
.ToListAsync();
|
||||
return users.ToDictionary(u => u.Id, u => (u.FullName, u.RoleName));
|
||||
}
|
||||
|
||||
public async Task AddAsync(Models.ShopPO entity) =>
|
||||
await context.ShopPOs.AddAsync(entity);
|
||||
|
||||
|
||||
@@ -12,26 +12,46 @@ public class ShopPOService(
|
||||
PriceListService priceListService,
|
||||
IUnitOfWork uow)
|
||||
{
|
||||
private static ShopPOResponse ToResponse(Models.ShopPO p) =>
|
||||
private static ShopPOResponse ToResponse(Models.ShopPO p, Dictionary<Guid, (string Name, string Role)>? users = null) =>
|
||||
new(p.Id, p.FK_StoreId, p.Store?.StoreName ?? string.Empty,
|
||||
p.RaisedById, p.InvoiceId, p.TotalAmount, p.Status,
|
||||
p.ConfirmedAt, p.ConfirmedBy, p.FulfilledAt,
|
||||
p.Items.Where(i => i.IsActive).Select(i =>
|
||||
new ShopPOItemResponse(i.Id, i.FK_ItemId, i.Item?.Name ?? string.Empty,
|
||||
i.Quantity, i.QuantityFulfilled, i.UnitPrice, i.IsActive)).ToList(),
|
||||
p.IsActive, p.CreatedAt);
|
||||
p.IsActive, p.CreatedAt,
|
||||
users != null && users.TryGetValue(p.RaisedById, out var u) ? u.Name : null,
|
||||
users != null && users.TryGetValue(p.RaisedById, out var r) ? r.Role : null,
|
||||
users != null && p.ConfirmedBy is Guid cb && users.TryGetValue(cb, out var c) ? c.Name : null,
|
||||
users != null && p.ConfirmedBy is Guid cb2 && users.TryGetValue(cb2, out var c2) ? c2.Role : null,
|
||||
p.CancelledBy, p.CancelledAt, p.CancelReason,
|
||||
users != null && p.CancelledBy is Guid xb && users.TryGetValue(xb, out var x) ? x.Name : null,
|
||||
users != null && p.CancelledBy is Guid xb2 && users.TryGetValue(xb2, out var x2) ? x2.Role : null);
|
||||
|
||||
private static IEnumerable<Guid> UserIds(Models.ShopPO p)
|
||||
{
|
||||
yield return p.RaisedById;
|
||||
if (p.ConfirmedBy is Guid cb) yield return cb;
|
||||
if (p.CancelledBy is Guid xb) yield return xb;
|
||||
}
|
||||
|
||||
private async Task<ShopPOResponse> ToResponseWithUserAsync(Models.ShopPO p) =>
|
||||
ToResponse(p, await repo.GetUserInfoAsync(UserIds(p)));
|
||||
|
||||
public async Task<PagedResponse<ShopPOResponse>> GetAllAsync(Guid? agentId, Guid? routeId, Guid? regionId, Guid? shopId, bool? confirmed, ShopPOStatus? status, int page, int pageSize)
|
||||
{
|
||||
var total = await repo.GetCountAsync(agentId, routeId, regionId, shopId, confirmed, status);
|
||||
var items = await repo.GetAllAsync(agentId, routeId, regionId, shopId, confirmed, status, page, pageSize);
|
||||
return new PagedResponse<ShopPOResponse>(page, pageSize, total, items.Select(ToResponse).ToList());
|
||||
var users = await repo.GetUserInfoAsync(items.SelectMany(UserIds));
|
||||
return new PagedResponse<ShopPOResponse>(page, pageSize, total, items.Select(p => ToResponse(p, users)).ToList());
|
||||
}
|
||||
|
||||
public async Task<ShopPOResponse?> GetByIdAsync(Guid id)
|
||||
{
|
||||
var po = await repo.GetByIdAsync(id);
|
||||
return po is null ? null : ToResponse(po);
|
||||
if (po is null) return null;
|
||||
var users = await repo.GetUserInfoAsync(UserIds(po));
|
||||
return ToResponse(po, users);
|
||||
}
|
||||
|
||||
public async Task<ShopPOResponse> CreateAsync(CreateShopPORequest request, Guid raisedById, CancellationToken ct = default)
|
||||
@@ -77,7 +97,7 @@ public class ShopPOService(
|
||||
catch { await uow.RollbackAsync(ct); throw; }
|
||||
|
||||
var saved = await repo.GetByIdAsync(po.Id);
|
||||
return ToResponse(saved!);
|
||||
return await ToResponseWithUserAsync(saved!);
|
||||
}
|
||||
|
||||
public async Task<ShopPOResponse> UpdateItemsAsync(Guid id, UpdateShopPOItemsRequest request, CancellationToken ct = default)
|
||||
@@ -85,6 +105,9 @@ public class ShopPOService(
|
||||
var po = await repo.GetByIdAsync(id)
|
||||
?? throw new KeyNotFoundException("Shop PO not found");
|
||||
|
||||
if (po.Status == ShopPOStatus.Cancelled)
|
||||
throw new InvalidOperationException("Cannot edit a cancelled Shop PO");
|
||||
|
||||
if (po.ConfirmedAt is not null)
|
||||
throw new InvalidOperationException("Items can only be changed before the Shop PO is confirmed");
|
||||
|
||||
@@ -123,7 +146,7 @@ public class ShopPOService(
|
||||
catch { await uow.RollbackAsync(ct); throw; }
|
||||
|
||||
var saved = await repo.GetByIdAsync(id);
|
||||
return ToResponse(saved!);
|
||||
return await ToResponseWithUserAsync(saved!);
|
||||
}
|
||||
|
||||
public async Task<ShopPOResponse> ConfirmAsync(Guid id, Guid confirmedBy, CancellationToken ct = default)
|
||||
@@ -144,7 +167,7 @@ public class ShopPOService(
|
||||
try { repo.Update(po); await uow.CommitAsync(ct); }
|
||||
catch { await uow.RollbackAsync(ct); throw; }
|
||||
|
||||
return ToResponse(po);
|
||||
return await ToResponseWithUserAsync(po);
|
||||
}
|
||||
|
||||
public async Task<ShopPOResponse> ConfirmAndCreateInvoiceAsync(Guid id, Guid userId, CancellationToken ct = default)
|
||||
@@ -181,11 +204,14 @@ public class ShopPOService(
|
||||
catch { await uow.RollbackAsync(ct); throw; }
|
||||
|
||||
var saved = await repo.GetByIdAsync(id);
|
||||
return ToResponse(saved!);
|
||||
return await ToResponseWithUserAsync(saved!);
|
||||
}
|
||||
|
||||
public async Task<ShopPOResponse> CancelAsync(Guid id, CancellationToken ct = default)
|
||||
public async Task<ShopPOResponse> CancelAsync(Guid id, Guid cancelledBy, string reason, CancellationToken ct = default)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(reason))
|
||||
throw new InvalidOperationException("A cancellation reason is required");
|
||||
|
||||
var po = await repo.GetByIdAsync(id)
|
||||
?? throw new KeyNotFoundException("Shop PO not found");
|
||||
|
||||
@@ -196,11 +222,14 @@ public class ShopPOService(
|
||||
throw new InvalidOperationException($"Cannot cancel a Shop PO in {po.Status} status");
|
||||
|
||||
po.Status = ShopPOStatus.Cancelled;
|
||||
po.CancelledAt = DateTime.Now;
|
||||
po.CancelledBy = cancelledBy;
|
||||
po.CancelReason = reason.Trim();
|
||||
|
||||
await uow.BeginAsync(ct);
|
||||
try { repo.Update(po); await uow.CommitAsync(ct); }
|
||||
catch { await uow.RollbackAsync(ct); throw; }
|
||||
|
||||
return ToResponse(po);
|
||||
return await ToResponseWithUserAsync(po);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user