NTP_V2/src/NTP.Application/Grains/Orders/WeightsAndDestinationsServi...

94 lines
4.5 KiB
C#

using FluentValidation;
using NTP.Application.Abstractions;
using NTP.Application.Common;
using NTP.Domain.Grains.Orders;
namespace NTP.Application.Grains.Orders;
public interface IGrainsWeightService
{
Task<PermitWeightsDto> GetAsync(long pn, CancellationToken ct);
Task<OrdersResult> SaveAsync(SaveWeightsRequest request, CancellationToken ct);
}
/// <summary>Port of grains.cfc permitAutoComplete / saveGrainsWeights (addGrainsWeights.cfm).</summary>
public sealed class GrainsWeightService(
IGrainsWeightRepository repository,
IEventLogger events,
IUnitOfWork unitOfWork,
ICurrentUser user,
IValidator<SaveWeightsRequest> validator) : IGrainsWeightService
{
public async Task<PermitWeightsDto> GetAsync(long pn, CancellationToken ct)
{
var permit = await repository.GetPermitAsync(pn, ct) ?? throw OrdersErrors.WeightsPermitNotFound();
var saved = await repository.GetWeightsAsync(permit.Id, ct);
return new PermitWeightsDto(pn, permit.Tn, permit.Trn, permit.Drvr, saved?.EmptyWeight, saved?.GrossWeight, saved?.DocumentNumber);
}
public async Task<OrdersResult> SaveAsync(SaveWeightsRequest request, CancellationToken ct)
{
await validator.ValidateAndThrowAsync(request, ct);
// Legacy only checked the empty weight (a copy/paste bug); the page mask requires 5 digits for both.
if (!GrainsWeightRules.IsValidWeight(request.EmptyWeight) || !GrainsWeightRules.IsValidWeight(request.GrossWeight))
throw OrdersErrors.WeightLength();
return await unitOfWork.ExecuteInTransactionAsync(async token =>
{
var permit = await repository.GetPermitAsync(request.Pn, token) ?? throw OrdersErrors.WeightsPermitNotFound();
var existing = await repository.GetWeightsAsync(permit.Id, token);
if (existing is not null)
await repository.UpdateWeightsAsync(existing.Id, request.EmptyWeight, request.GrossWeight, request.DocumentNumber, user.UserId, token);
else
await repository.InsertWeightsAsync(permit.Id, request.EmptyWeight, request.GrossWeight, request.DocumentNumber, user.UserId, token);
await events.AddAsync(new EventEntry(
"PERMITID", permit.Id, permit.ShipmentStatusCode, permit.ShipmentStatusCode, user.UserId, "ADD_WEIGHTS", "GN", null,
"NTP", null,
new Dictionary<string, object?> { ["REMARKS"] = GrainsWeightRules.EventRemarks(request.GrossWeight, request.EmptyWeight) }), token);
return new OrdersResult(OrdersErrors.Success, permit.Id);
}, ct);
}
}
public interface ICompanyDestinationService
{
Task<CompanyDestinationsDto> GetAsync(long scac, CancellationToken ct);
Task<OrdersResult> SaveAsync(long scac, SaveCompanyDestinationsRequest request, CancellationToken ct);
}
/// <summary>Port of grains.cfc getCompDest / saveCompDest (compdest.cfm) over GRAINS_COMPANIES_LOCATION.</summary>
public sealed class CompanyDestinationService(
ICompanyDestinationRepository repository,
IOrderLookupRepository lookups,
IUnitOfWork unitOfWork,
ICurrentUser user,
IValidator<SaveCompanyDestinationsRequest> validator) : ICompanyDestinationService
{
public async Task<CompanyDestinationsDto> GetAsync(long scac, CancellationToken ct) =>
new(scac, await repository.GetLocationIdsAsync(scac, ct));
public async Task<OrdersResult> SaveAsync(long scac, SaveCompanyDestinationsRequest request, CancellationToken ct)
{
await validator.ValidateAndThrowAsync(request, ct);
if (!(await lookups.GetCompaniesAsync(ct)).Any(c => c.Scac == scac)) throw OrdersErrors.CompanyNotFound();
var packed = new HashSet<long>(); // every destination the screen can show: packed, bulk and bulk tipper
foreach (var gnType in new[] { "PCKD", "BULK", "BULK_GALLAB" })
foreach (var d in await lookups.GetDestinationsByGnTypeAsync(gnType, ct)) packed.Add(d.Id);
var chosen = request.LocationIds.Distinct().ToList();
if (chosen.Any(id => !packed.Contains(id))) throw OrdersErrors.InvalidDestination();
// Only the destinations the screen can show (packed, bulk and bulk tipper) are replaced; other rows of the company are kept.
await unitOfWork.ExecuteInTransactionAsync(async token =>
{
await repository.ReplaceAsync(scac, packed, chosen, user.UserId, token);
return 0;
}, ct);
return new OrdersResult(OrdersErrors.Success, scac);
}
}