Spaces:
Runtime error
Runtime error
Download src/SCM.Application/Materiais/DeliveryOrderService.cs from rafaeldavila/scm-api: direct link, hf CLI and curl.
- Browser
- Download file 6.05 kB
-
https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Application/Materiais/DeliveryOrderService.cs
- Command line
-
hf download hf://spaces/rafaeldavila/scm-api/src/SCM.Application/Materiais/DeliveryOrderService.cs
-
curl -L -o DeliveryOrderService.cs https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Application/Materiais/DeliveryOrderService.cs
6.05 kB
| using SCM.Application.Common; | |
| using SCM.Application.Materiais.Dtos; | |
| using SCM.Domain.Materiais; | |
| using SCM.Shared.Exceptions; | |
| namespace SCM.Application.Materiais; | |
| public interface IDeliveryOrderService | |
| { | |
| Task<IReadOnlyList<DeliveryOrderResponse>> GetByObraAsync(Guid obraId, string? notaFiscal = null, Guid? tipoMaterialMacroId = null, CancellationToken ct = default); | |
| Task<DeliveryOrderResponse> GetByIdAsync(Guid id, CancellationToken ct = default); | |
| Task<DeliveryOrderResponse> CreateAsync(CreateDeliveryOrderRequest request, CancellationToken ct = default); | |
| Task<DeliveryOrderResponse> UpdateAsync(Guid id, UpdateDeliveryOrderRequest request, CancellationToken ct = default); | |
| Task DeleteAsync(Guid id, CancellationToken ct = default); | |
| } | |
| public sealed class DeliveryOrderService : IDeliveryOrderService | |
| { | |
| private readonly IDeliveryOrderRepository _repository; | |
| private readonly IUnitOfWork _unitOfWork; | |
| public DeliveryOrderService(IDeliveryOrderRepository repository, IUnitOfWork unitOfWork) | |
| { | |
| _repository = repository; | |
| _unitOfWork = unitOfWork; | |
| } | |
| public async Task<IReadOnlyList<DeliveryOrderResponse>> GetByObraAsync( | |
| Guid obraId, string? notaFiscal = null, Guid? tipoMaterialMacroId = null, CancellationToken ct = default) | |
| => (await _repository.GetByObraAsync(obraId, notaFiscal, tipoMaterialMacroId, ct)).Select(ToResponse).ToList(); | |
| public async Task<DeliveryOrderResponse> GetByIdAsync(Guid id, CancellationToken ct = default) | |
| => ToResponse(await _repository.GetByIdAsync(id, ct) | |
| ?? throw new NotFoundException($"Delivery Order {id} não encontrada.")); | |
| public async Task<DeliveryOrderResponse> CreateAsync(CreateDeliveryOrderRequest r, CancellationToken ct = default) | |
| { | |
| if (!await _repository.ObraExistsAsync(r.ObraId, ct)) | |
| throw new NotFoundException($"Obra {r.ObraId} não encontrada."); | |
| var d = DeliveryOrder.Create( | |
| r.ObraId, r.NotaFiscal, r.ItemRecebido, r.OrdemCompra, r.Quantidade, r.DataRecebimento, | |
| r.UnidadeMedidaId, r.BookMaterialId, r.TipoMaterialMacroId, | |
| r.Peso, r.Comprimento, r.Largura, r.Espessura, r.Diametro, | |
| r.Generico1, r.Generico2, r.Generico3, r.Generico4, r.Generico5); | |
| await GarantirClassificacoesAsync(d, ct); | |
| // Regra do legado: par nota fiscal + item recebido único na obra (DO_EXISTENTE). | |
| if (await _repository.ParExistsAsync(d.ObraId, d.NotaFiscal, d.ItemRecebido, null, ct)) | |
| throw new ConflictException($"Já existe uma DO com a nota fiscal '{d.NotaFiscal}' e item '{d.ItemRecebido}' nesta obra."); | |
| await ExecuteInTransactionAsync(() => _repository.AddAsync(d, ct)); | |
| return ToResponse(d); | |
| } | |
| public async Task<DeliveryOrderResponse> UpdateAsync(Guid id, UpdateDeliveryOrderRequest r, CancellationToken ct = default) | |
| { | |
| var d = await _repository.GetByIdAsync(id, ct) | |
| ?? throw new NotFoundException($"Delivery Order {id} não encontrada."); | |
| d.Atualizar( | |
| r.NotaFiscal, r.ItemRecebido, r.OrdemCompra, r.Quantidade, r.DataRecebimento, | |
| r.UnidadeMedidaId, r.BookMaterialId, r.TipoMaterialMacroId, | |
| r.Peso, r.Comprimento, r.Largura, r.Espessura, r.Diametro, | |
| r.Generico1, r.Generico2, r.Generico3, r.Generico4, r.Generico5); | |
| await GarantirClassificacoesAsync(d, ct); | |
| if (await _repository.ParExistsAsync(d.ObraId, d.NotaFiscal, d.ItemRecebido, id, ct)) | |
| throw new ConflictException($"Já existe uma DO com a nota fiscal '{d.NotaFiscal}' e item '{d.ItemRecebido}' nesta obra."); | |
| await ExecuteInTransactionAsync(() => _repository.UpdateAsync(d, ct)); | |
| return ToResponse(d); | |
| } | |
| public async Task DeleteAsync(Guid id, CancellationToken ct = default) | |
| { | |
| var d = await _repository.GetByIdAsync(id, ct) | |
| ?? throw new NotFoundException($"Delivery Order {id} não encontrada."); | |
| // Trava ativada na fatia 09: DO consumida por RIR não pode ser excluída | |
| // (mais estrita que o legado, que travava só com inspeção — decisão documentada: | |
| // excluir a DO sob um RIR quebraria o saldo 100% das quebras). | |
| if (await _repository.ContarVinculosRirAsync(id, ct) > 0) | |
| throw new ConflictException("Não é possível excluir: a DO está vinculada a um RIR."); | |
| await ExecuteInTransactionAsync(() => _repository.DeleteAsync(d.Id, ct)); | |
| } | |
| private async Task GarantirClassificacoesAsync(DeliveryOrder d, CancellationToken ct) | |
| { | |
| if (!await _repository.UnidadeExistsAsync(d.UnidadeMedidaId, ct)) | |
| throw new NotFoundException($"Unidade de medida {d.UnidadeMedidaId} não encontrada."); | |
| if (!await _repository.BookMaterialExistsAsync(d.BookMaterialId, ct)) | |
| throw new NotFoundException($"Book de material {d.BookMaterialId} não encontrado."); | |
| var agrupador = await _repository.GetAgrupadorDoTipoMacroAsync(d.TipoMaterialMacroId, ct); | |
| if (agrupador is null) | |
| throw new NotFoundException($"Tipo macro {d.TipoMaterialMacroId} não encontrado."); | |
| // Regra do legado (TIPO_MATERIAL_SEM_AGRUPADOR): o tipo macro precisa ter agrupador. | |
| if (agrupador == Guid.Empty) | |
| throw new DomainException("O tipo macro de material não possui agrupador associado."); | |
| } | |
| private static DeliveryOrderResponse ToResponse(DeliveryOrder d) => new( | |
| d.Id, d.ObraId, d.NotaFiscal, d.ItemRecebido, d.OrdemCompra, d.Quantidade, d.DataRecebimento, | |
| d.UnidadeMedidaId, d.BookMaterialId, d.TipoMaterialMacroId, | |
| d.Peso, d.Comprimento, d.Largura, d.Espessura, d.Diametro, | |
| d.Generico1, d.Generico2, d.Generico3, d.Generico4, d.Generico5); | |
| private async Task ExecuteInTransactionAsync(Func<Task> action) | |
| { | |
| _unitOfWork.BeginTransaction(); | |
| try { await action(); _unitOfWork.Commit(); } | |
| catch { _unitOfWork.Rollback(); throw; } | |
| } | |
| } | |