File size: 6,453 Bytes
de8b914
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using Dapper;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SCM.Application.Authentication;
using SCM.Application.Common;
using SCM.Domain.Cadastros;
using SCM.Domain.Consultas;
using SCM.Domain.Contratos;
using SCM.Domain.Controle;
using SCM.Domain.Materiais;
using SCM.Domain.Medicao;
using SCM.Domain.Obras;
using SCM.Domain.Planejamento;
using SCM.Domain.Projects;
using SCM.Domain.Qualidade;
using SCM.Domain.Qualificacoes;
using SCM.Domain.Users;
using SCM.Infrastructure.HealthChecks;
using SCM.Infrastructure.Persistence;
using SCM.Infrastructure.Persistence.Repositories;
using SCM.Infrastructure.Security;

namespace SCM.Infrastructure;

public static class DependencyInjection
{
    public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
    {
        var connectionString = configuration.GetConnectionString("Postgres")
            ?? throw new InvalidOperationException("ConnectionStrings:Postgres não configurada.");

        // Mapeia colunas snake_case do PostgreSQL para propriedades PascalCase (ex.: start_date -> StartDate).
        DefaultTypeMap.MatchNamesWithUnderscores = true;

        // Suporte a DateOnly como parâmetro/coluna (Dapper atual não trata nativamente).
        SqlMapper.AddTypeHandler(new DateOnlyTypeHandler());

        // Simplifica o tratamento de timestamp (sem distinção estrita de Kind UTC/local no Npgsql).
        // TODO: revisar para usar timestamptz + UTC de forma explícita quando o modelo amadurecer.
        AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);

        services.AddSingleton<IDbConnectionFactory>(new NpgsqlConnectionFactory(connectionString));
        services.AddScoped<IUnitOfWork, UnitOfWork>();

        services.AddScoped<IProjectRepository, ProjectRepository>();
        services.AddScoped<IUserRepository, UserRepository>();
        services.AddScoped<IGrupoConsultaRepository, GrupoConsultaRepository>();
        services.AddScoped<IConsultaPersonalizadaRepository, ConsultaPersonalizadaRepository>();
        services.AddScoped<ICriterioEapRepository, CriterioEapRepository>();
        services.AddScoped<IEapRepository, EapRepository>();
        services.AddScoped<ICriterioEapItemRepository, CriterioEapItemRepository>();
        services.AddScoped<SCM.Application.Medicao.IEapPlanejamentoRepository, EapPlanejamentoRepository>();
        services.AddScoped<SCM.Application.Medicao.ILinhaBaseRepository, LinhaBaseRepository>();
        services.AddScoped<IAgrupadorTipoMaterialRepository, AgrupadorTipoMaterialRepository>();
        services.AddScoped<ITipoMaterialRepository, TipoMaterialRepository>();
        services.AddScoped<ITipoMacroMaterialRepository, TipoMacroMaterialRepository>();
        services.AddScoped<IDeliveryOrderRepository, DeliveryOrderRepository>();
        services.AddScoped<IRirRepository, RirRepository>();
        services.AddScoped<IAgenteRepository, AgenteRepository>();
        services.AddScoped<ISubAtividadePadraoRepository, SubAtividadePadraoRepository>();
        services.AddScoped<ICampoInspecaoRepository, CampoInspecaoRepository>();
        services.AddScoped<IMascaraTecnicaRepository, MascaraTecnicaRepository>();
        services.AddScoped<IQualificacaoRepository, QualificacaoRepository>();
        services.AddScoped<IQualificacaoAgenteRepository, QualificacaoAgenteRepository>();
        services.AddScoped<IItemControleRepository, ItemControleRepository>();
        services.AddScoped<IItemProgramadoRepository, ItemProgramadoRepository>();
        services.AddScoped<IExecutorRepository, ExecutorRepository>();
        services.AddScoped<ICronogramaAtividadeRepository, CronogramaAtividadeRepository>();
        services.AddScoped<IProducaoControleRepository, ProducaoControleRepository>();
        services.AddScoped<IProducaoMedicaoRepository, ProducaoMedicaoRepository>();
        services.AddScoped<IColetaRepository, ColetaRepository>();
        services.AddScoped<IFatorProdutividadeRepository, FatorProdutividadeRepository>();
        services.AddScoped<ISolicitacaoRecalculoRepository, SolicitacaoRecalculoRepository>();
        services.AddScoped<IAcompanhamentoRepository, AcompanhamentoRepository>();
        services.AddScoped<IPrecedenciaRepository, PrecedenciaRepository>();
        services.AddScoped<ICaracteristicaRepository, CaracteristicaRepository>();
        services.AddScoped<SCM.Application.Medicao.IFaturamentoRepository, FaturamentoRepository>();
        services.AddScoped<IRelatorioTecnicoRepository, RelatorioTecnicoRepository>();
        services.AddSingleton<SCM.Application.Relatorios.IPdfReportService, SCM.Infrastructure.Relatorios.PdfReportService>();
        services.AddScoped<SCM.Application.Consultas.IDashboardRepository, DashboardRepository>();
        services.AddScoped<ICroquiRepository, CroquiRepository>();
        services.AddScoped<IFuncionarioRepository, FuncionarioRepository>();
        services.AddScoped<IEquipeRepository, EquipeRepository>();
        services.AddScoped<IFolhaTarefaRepository, FolhaTarefaRepository>();
        services.AddScoped<IFolhaTarefaItemRepository, FolhaTarefaItemRepository>();
        services.AddScoped<IApropriacaoHorasRepository, ApropriacaoHorasRepository>();
        services.AddScoped<IUnidadeMedidaRepository, UnidadeMedidaRepository>();
        services.AddScoped<IConversaoUnidadeMedidaRepository, ConversaoUnidadeMedidaRepository>();
        services.AddScoped<IObraRepository, ObraRepository>();
        services.AddScoped<IContratoRepository, ContratoRepository>();
        services.AddScoped<SCM.Domain.AtividadesCiclicas.IGrupoTrabalhoRepository, Persistence.Repositories.GrupoTrabalhoRepository>();
        services.AddScoped<SCM.Domain.AtividadesCiclicas.IAtividadeCiclicaRepository, Persistence.Repositories.AtividadeCiclicaRepository>();
        services.AddScoped<SCM.Domain.AtividadesCiclicas.IOcorrenciaAtividadeRepository, Persistence.Repositories.OcorrenciaAtividadeRepository>();

        services.Configure<JwtOptions>(configuration.GetSection(JwtOptions.SectionName));
        services.AddSingleton<IJwtTokenGenerator, JwtTokenGenerator>();
        services.AddSingleton<IPasswordHasherService, PasswordHasherService>();

        services.AddHealthChecks()
            .AddCheck<DatabaseHealthCheck>("postgres", tags: new[] { "db", "critical" });

        return services;
    }
}