using FluentValidation; using NTP.Application.Abstractions; using NTP.Application.Common; using NTP.Domain.Grains.Orders; namespace NTP.Application.Grains.Orders; public interface IGrainsWeightService { Task GetAsync(long pn, CancellationToken ct); Task SaveAsync(SaveWeightsRequest request, CancellationToken ct); } /// Port of grains.cfc permitAutoComplete / saveGrainsWeights (addGrainsWeights.cfm). public sealed class GrainsWeightService( IGrainsWeightRepository repository, IEventLogger events, IUnitOfWork unitOfWork, ICurrentUser user, IValidator validator) : IGrainsWeightService { public async Task 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 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 { ["REMARKS"] = GrainsWeightRules.EventRemarks(request.GrossWeight, request.EmptyWeight) }), token); return new OrdersResult(OrdersErrors.Success, permit.Id); }, ct); } } public interface ICompanyDestinationService { Task GetAsync(long scac, CancellationToken ct); Task SaveAsync(long scac, SaveCompanyDestinationsRequest request, CancellationToken ct); } /// Port of grains.cfc getCompDest / saveCompDest (compdest.cfm) over GRAINS_COMPANIES_LOCATION. public sealed class CompanyDestinationService( ICompanyDestinationRepository repository, IOrderLookupRepository lookups, IUnitOfWork unitOfWork, ICurrentUser user, IValidator validator) : ICompanyDestinationService { public async Task GetAsync(long scac, CancellationToken ct) => new(scac, await repository.GetLocationIdsAsync(scac, ct)); public async Task 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(); // 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); } }