Refatoração e atualizações de dependências
- Modificado `InvoiceIngestionService` para usar `IServiceScopeFactory`, permitindo melhor gerenciamento do ciclo de vida de dependências. - Atualizações de pacotes em diversos projetos (`ComplianceNFs.Core`, `ComplianceNFs.Infrastructure`, `ComplianceNFs.Service`, `ComplianceNFs.Monitor`, `ComplianceNFs.Infrastructure.Tests`). - Alterado namespace de `AuditComplianceNFsTest.cs` para `ComplianceNFs.Infrastructure.Tests`. - Refatorações no `AttachmentRepository` e `ComplianceNFsDbContext` para simplificar código e melhorar legibilidade. - Atualizado teste `InvoiceIngestionServiceTests` para refletir mudanças no uso de escopos de serviço. - Melhorias gerais na arquitetura e alinhamento com boas práticas.
This commit is contained in:
parent
c48ba4710d
commit
e49192dac1
@ -8,6 +8,7 @@ using ComplianceNFs.Core.Ports;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace ComplianceNFs.Core.Application.Services
|
||||
{
|
||||
@ -15,15 +16,14 @@ namespace ComplianceNFs.Core.Application.Services
|
||||
public class InvoiceIngestionService : IInvoiceIngestionService
|
||||
{
|
||||
private readonly IMailListener _mailListener;
|
||||
private readonly IAttachmentRepository _attachmentRepository;
|
||||
private readonly IXmlParser _xmlParser;
|
||||
private readonly IPdfParser _pdfParser;
|
||||
private readonly ILogger<InvoiceIngestionService> _logger;
|
||||
|
||||
public InvoiceIngestionService(IMailListener mailListener, IAttachmentRepository attachmentRepository, IXmlParser xmlParser, IPdfParser pdfParser, ILogger<InvoiceIngestionService> logger)
|
||||
private readonly IServiceScopeFactory _scopeFactory;
|
||||
public InvoiceIngestionService(IMailListener mailListener, IServiceScopeFactory scopeFactory, IXmlParser xmlParser, IPdfParser pdfParser, ILogger<InvoiceIngestionService> logger)
|
||||
{
|
||||
_mailListener = mailListener;
|
||||
_attachmentRepository = attachmentRepository;
|
||||
_scopeFactory = scopeFactory;
|
||||
_xmlParser = xmlParser;
|
||||
_pdfParser = pdfParser;
|
||||
_logger = logger;
|
||||
@ -31,6 +31,8 @@ namespace ComplianceNFs.Core.Application.Services
|
||||
}
|
||||
private async void OnNewMailReceived(MailMessage mail)
|
||||
{
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
var _attachmentRepository = scope.ServiceProvider.GetRequiredService<IAttachmentRepository>();
|
||||
_logger.LogInformation("New mail received: {Subject}", mail.Subject);
|
||||
try
|
||||
{
|
||||
|
||||
@ -6,6 +6,7 @@
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="10.0.0-preview.4.25258.110" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="10.0.0-preview.4.25258.110" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.0-preview.4.25258.110" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@ -3,7 +3,7 @@ using System.Text.Json;
|
||||
using System.IO;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace ComplianceNFs.Tests
|
||||
namespace ComplianceNFs.Infrastructure.Tests
|
||||
{
|
||||
[TestFixture]
|
||||
public class AuditComplianceNFsTest
|
||||
|
||||
@ -10,13 +10,20 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="3.2.0" />
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.4">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="9.0.9" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.0-preview.4.25258.110" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
|
||||
<PackageReference Include="Moq" Version="4.20.72" />
|
||||
<PackageReference Include="NUnit" Version="4.3.2" />
|
||||
<PackageReference Include="xunit" Version="2.4.2" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" />
|
||||
<PackageReference Include="NUnit" Version="4.4.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.3" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@ -9,6 +9,7 @@ using ComplianceNFs.Core.Application.Services;
|
||||
using ComplianceNFs.Core.Ports;
|
||||
using ComplianceNFs.Core.Entities;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace ComplianceNFs.Infrastructure.Tests
|
||||
{
|
||||
@ -19,17 +20,26 @@ namespace ComplianceNFs.Infrastructure.Tests
|
||||
{
|
||||
// Arrange
|
||||
var mockMailListener = new Mock<IMailListener>();
|
||||
var mockScopeFactory = new Mock<IServiceScopeFactory>();
|
||||
var mockScope = new Mock<IServiceScope>();
|
||||
var mockProvider = new Mock<IServiceProvider>();
|
||||
var mockAttachmentRepo = new Mock<IAttachmentRepository>();
|
||||
var mockXmlParser = new Mock<IXmlParser>();
|
||||
var mockPdfParser = new Mock<IPdfParser>();
|
||||
var mockLogger = new Mock<ILogger<InvoiceIngestionService>>();
|
||||
|
||||
mockProvider.Setup(x => x.GetService(typeof(IAttachmentRepository)))
|
||||
.Returns(mockAttachmentRepo.Object);
|
||||
mockScope.Setup(x => x.ServiceProvider).Returns(mockProvider.Object);
|
||||
mockScopeFactory.Setup(x => x.CreateScope()).Returns(mockScope.Object);
|
||||
|
||||
|
||||
var testParsed = new ParsedInvoice { CnpjComp = "123", NumeroNF = "456" };
|
||||
mockXmlParser.Setup(x => x.Parse(It.IsAny<Stream>())).Returns(testParsed);
|
||||
|
||||
var service = new InvoiceIngestionService(
|
||||
mockMailListener.Object,
|
||||
mockAttachmentRepo.Object,
|
||||
mockScopeFactory.Object,
|
||||
mockXmlParser.Object,
|
||||
mockPdfParser.Object,
|
||||
mockLogger.Object
|
||||
|
||||
@ -5,22 +5,22 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MailKit" Version="4.12.1" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.6" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.6">
|
||||
<PackageReference Include="MailKit" Version="4.13.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.9" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.9">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="10.0.0-preview.4.25258.110" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="10.0.0-preview.4.25258.110" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.9" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="10.0.0-preview.4.25258.110" />
|
||||
<PackageReference Include="Microsoft.Office.Interop.Outlook" Version="15.0.4797.1004" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
|
||||
<PackageReference Include="Npgsql" Version="9.0.3" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
|
||||
<PackageReference Include="System.Data.OleDb" Version="10.0.0-preview.4.25258.110" />
|
||||
<PackageReference Include="Unimake.DFe" Version="20250610.1145.39" />
|
||||
<PackageReference Include="Unimake.DFe" Version="20250924.1732.21" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
|
||||
@ -10,16 +10,10 @@ using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace ComplianceNFs.Infrastructure.Repositories
|
||||
{
|
||||
public class AttachmentRepository : IAttachmentRepository
|
||||
public class AttachmentRepository(ComplianceNFsDbContext dbContext, ILogger<AttachmentRepository> logger) : IAttachmentRepository
|
||||
{
|
||||
private readonly ComplianceNFsDbContext _dbContext;
|
||||
private readonly ILogger<AttachmentRepository> _logger;
|
||||
|
||||
public AttachmentRepository(ComplianceNFsDbContext dbContext, ILogger<AttachmentRepository> logger)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_logger = logger;
|
||||
}
|
||||
private readonly ComplianceNFsDbContext _dbContext = dbContext;
|
||||
private readonly ILogger<AttachmentRepository> _logger = logger;
|
||||
|
||||
public async Task SaveRawAsync(EnergyInvoice invoice)
|
||||
{
|
||||
@ -40,11 +34,7 @@ namespace ComplianceNFs.Infrastructure.Repositories
|
||||
{
|
||||
try
|
||||
{
|
||||
var invoice = await _dbContext.EnergyInvoices.FirstOrDefaultAsync(e => e.InvoiceId == invoiceId);
|
||||
if (invoice == null)
|
||||
{
|
||||
throw new InvalidOperationException($"Invoice with ID {invoiceId} not found.");
|
||||
}
|
||||
var invoice = await _dbContext.EnergyInvoices.FirstOrDefaultAsync(e => e.InvoiceId == invoiceId) ?? throw new InvalidOperationException($"Invoice with ID {invoiceId} not found.");
|
||||
invoice.MatchedCodTE = matchedCodTE;
|
||||
invoice.Status = status;
|
||||
invoice.DiscrepancyNotes = notes;
|
||||
|
||||
@ -3,10 +3,8 @@ using ComplianceNFs.Core.Entities;
|
||||
|
||||
namespace ComplianceNFs.Infrastructure.Repositories
|
||||
{
|
||||
public class ComplianceNFsDbContext : DbContext
|
||||
public class ComplianceNFsDbContext(DbContextOptions<ComplianceNFsDbContext> options) : DbContext(options)
|
||||
{
|
||||
public ComplianceNFsDbContext(DbContextOptions<ComplianceNFsDbContext> options) : base(options) { }
|
||||
|
||||
public DbSet<EnergyInvoice> EnergyInvoices { get; set; }
|
||||
// Add other DbSets as needed (e.g., BuyingRecord, etc.)
|
||||
}
|
||||
|
||||
@ -3,8 +3,8 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="10.0.0-preview.4.25258.110" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="10.0.0-preview.4.25258.110" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.6" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="9.0.6" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.9" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="9.0.9" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.6">
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.9">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user