public async Task<Response<bool>> Handle(UpdateLastOperationCommand request, CancellationToken cancellationToken)
{
var routeCard = await _myDbContext.RouteCards.FirstOrDefaultAsync(n => n.RouteNo == request.RouteNo);
var routeOperations =
await _myDbContext.RouteCardOps.Where(n => n.RouteNo == request.RouteNo).ToListAsync();
var ordered = routeOperations.OrderBy(n => n.Seq);
var lastOp = ordered.LastOrDefault();
if (lastOp == null)
{
return new Response<bool>() { Succeeded = true, Data = false, Message = "No operation to update" };
}
routeCard.QtyFinished = lastOp.QtyComplete;
if (routeCard.QtyFinished >= routeCard.BatchQty)
{
Log.Information($"RouteCard should be marked as complete");
routeCard.RouteStatus = "CMP";
routeCard.StatusDate = DateTime.Now;
routeCard.CompletedDate = DateTime.Now;
routeCard.SignOff = lastOp.SignOff;
routeCard.SignOffOn = lastOp.SignOffOn;
}
await _myDbContext.SaveChangesAsync();
Log.Information($"Successfully updated route card {request.RouteNo}");
return new Response<bool>() { Succeeded = true, Data = true };
}