remove store kyc features
This commit is contained in:
@@ -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