using Dapper; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using NTP.Application.Abstractions; using NTP.Application.Common; using NTP.Application.Jobs; using NTP.Infrastructure.HealthChecks; using NTP.Infrastructure.Jobs; using NTP.Infrastructure.Persistence; namespace NTP.Infrastructure; public static class DependencyInjection { public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration) { services.AddOptions() .Bind(configuration.GetSection(OracleOptions.SectionName)) .PostConfigure(o => { // 1. ConnectionStrings:NAFITHTCS 2. ConnectionStrings:NTP (legacy) 3. Oracle:ConnectionString 4. Oracle:Host/Port/... parts var named = configuration.GetConnectionString(OracleOptions.ConnectionStringName); var legacy = configuration.GetConnectionString(OracleOptions.LegacyConnectionStringName); if (!string.IsNullOrWhiteSpace(named)) o.ConnectionString = named; else if (!string.IsNullOrWhiteSpace(legacy)) o.ConnectionString = legacy; else if (string.IsNullOrWhiteSpace(o.ConnectionString) && o.HasParts) o.ConnectionString = o.BuildFromParts(); }) .Validate(o => !string.IsNullOrWhiteSpace(o.ConnectionString), "No Oracle connection configured. Set ConnectionStrings:NAFITHTCS, Oracle:ConnectionString, " + "or Oracle:Host + ServiceName (or Sid) + UserId + Password in appsettings.json.") .Validate(o => o.CommandTimeoutSeconds is > 0 and <= 600, "Oracle:CommandTimeoutSeconds must be 1-600.") .ValidateOnStart(); services.AddOptions() .PostConfigure(o => o.ConnectionString = configuration.GetConnectionString(NtpSchemaOptions.ConnectionStringName) ?? string.Empty) .Validate(o => !string.IsNullOrWhiteSpace(o.ConnectionString), "No NTP-schema Oracle connection configured. Set ConnectionStrings:NtpSchema (user NTP).") .ValidateOnStart(); // SHIPMENT_NO -> ShipmentNo, CREATED_AT -> CreatedAt (case-insensitive, underscores ignored). DefaultTypeMap.MatchNamesWithUnderscores = true; services.AddScoped(); services.AddScoped(sp => sp.GetRequiredService()); services.AddScoped(sp => sp.GetRequiredService()); // Second connection (NTP schema). IDbSession / IUnitOfWork keep meaning the primary NAFITHTCS connection. services.AddScoped(); services.AddScoped(sp => sp.GetRequiredService()); services.AddScoped(sp => sp.GetRequiredService()); services.AddScoped(); services.AddSingleton(); // scheduled jobs: the runner starts a job in its own scope, the worker looks for due jobs (screen "المهام المجدولة", SU only) services.AddSingleton(); services.AddHostedService(); // mobile notifications run in the background in their own scope (see MobileNotificationGatewayService) services.AddScoped(); // Refinery: Jopetrol claim web service (OFF unless Refinery:Jopetrol:Enabled; credentials only from env / .env) services.AddOptions() .Bind(configuration.GetSection(NTP.Infrastructure.Integrations.JopetrolOptions.SectionName)); services.AddHttpClient(NTP.Infrastructure.Integrations.JopetrolOptions.HttpClientName); services.AddScoped(); // Convention: every concrete *Repository / *Service in this assembly is registered against I{Name} (scoped). // Do NOT add per-table lines here. services.AddConventionalServices(typeof(DependencyInjection).Assembly); // Refinery: Dinarak payment API (no "Service" suffix, so not convention registered). Off unless Refinery:Dinarak:Enabled. services.AddOptions() .Bind(configuration.GetSection(NTP.Infrastructure.Integrations.DinarakOptions.SectionName)); services.AddHttpClient(NTP.Infrastructure.Integrations.DinarakOptions.HttpClientName, (sp, client) => client.Timeout = TimeSpan.FromSeconds(Math.Max(5, sp.GetRequiredService>().Value.TimeoutSeconds))); services.AddScoped(); services.AddScoped(sp => sp.GetRequiredService()); services.AddScoped(sp => sp.GetRequiredService()); services.AddHealthChecks() .AddCheck("oracle", tags: ["ready"]) .AddCheck("oracle-ntp", tags: ["ready"]); return services; } }