diff --git a/TrafagSalesExporter/Services/DatabaseSeedService.cs b/TrafagSalesExporter/Services/DatabaseSeedService.cs index 2a27a26..e608e4a 100644 --- a/TrafagSalesExporter/Services/DatabaseSeedService.cs +++ b/TrafagSalesExporter/Services/DatabaseSeedService.cs @@ -12,6 +12,7 @@ public class DatabaseSeedService : IDatabaseSeedService SeedIfEmpty(db); EnsureRecommendedTransformationRules(db); EnsureSourceSystemDefinitions(db); + EnsureIndiaSageHanaConfiguration(db); EnsureCentralHanaServerRecords(db); EnsureSpainManualExcelSite(db); EnsureGermanyManualExcelSite(db); @@ -308,6 +309,128 @@ public class DatabaseSeedService : IDatabaseSeedService db.SaveChanges(); } + private static void EnsureIndiaSageHanaConfiguration(AppDbContext db) + { + const string sageSourceSystem = "SAGE"; + const string indiaTsc = "TRIN"; + const string indiaSchema = "TRAFAG_LIVE"; + const string indiaHost = "20.197.20.60"; + const int indiaPort = 30015; + + var site = db.Sites + .Include(x => x.HanaServer) + .OrderBy(x => x.Id) + .FirstOrDefault(x => x.TSC == indiaTsc || x.Land == "Indien" || x.Land == "India"); + + if (site is null) + return; + + var changed = false; + var sourceServer = db.HanaServers + .OrderBy(x => x.Id) + .FirstOrDefault(x => x.Host == indiaHost) + ?? site.HanaServer; + + var centralSageServer = db.HanaServers + .OrderBy(x => x.Id) + .FirstOrDefault(x => x.SourceSystem == sageSourceSystem); + + if (centralSageServer is null) + { + centralSageServer = sourceServer ?? new HanaServer + { + Name = sageSourceSystem, + Host = indiaHost, + Port = indiaPort, + Username = string.Empty, + Password = string.Empty, + DatabaseName = string.Empty, + AdditionalParams = string.Empty + }; + + if (centralSageServer.Id == 0) + { + db.HanaServers.Add(centralSageServer); + } + } + + if (centralSageServer.SourceSystem != sageSourceSystem) + { + centralSageServer.SourceSystem = sageSourceSystem; + changed = true; + } + + if (string.IsNullOrWhiteSpace(centralSageServer.Name)) + { + centralSageServer.Name = sageSourceSystem; + changed = true; + } + + if (string.IsNullOrWhiteSpace(centralSageServer.Host)) + { + centralSageServer.Host = !string.IsNullOrWhiteSpace(sourceServer?.Host) + ? sourceServer.Host + : indiaHost; + changed = true; + } + + if (centralSageServer.Port <= 0) + { + centralSageServer.Port = sourceServer?.Port > 0 ? sourceServer.Port : indiaPort; + changed = true; + } + + if (sourceServer is not null && !ReferenceEquals(sourceServer, centralSageServer)) + { + if (string.IsNullOrWhiteSpace(centralSageServer.DatabaseName) && + !string.IsNullOrWhiteSpace(sourceServer.DatabaseName)) + { + centralSageServer.DatabaseName = sourceServer.DatabaseName; + changed = true; + } + + if (string.IsNullOrWhiteSpace(centralSageServer.AdditionalParams) && + !string.IsNullOrWhiteSpace(sourceServer.AdditionalParams)) + { + centralSageServer.AdditionalParams = sourceServer.AdditionalParams; + changed = true; + } + + if (centralSageServer.UseSsl != sourceServer.UseSsl) + { + centralSageServer.UseSsl = sourceServer.UseSsl; + changed = true; + } + + if (centralSageServer.ValidateCertificate != sourceServer.ValidateCertificate) + { + centralSageServer.ValidateCertificate = sourceServer.ValidateCertificate; + changed = true; + } + } + + if (site.SourceSystem != sageSourceSystem) + { + site.SourceSystem = sageSourceSystem; + changed = true; + } + + if (string.IsNullOrWhiteSpace(site.Schema)) + { + site.Schema = indiaSchema; + changed = true; + } + + if (site.HanaServerId != centralSageServer.Id || site.HanaServerId is null) + { + site.HanaServer = centralSageServer; + changed = true; + } + + if (changed || centralSageServer.Id == 0) + db.SaveChanges(); + } + private static void EnsureSourceSystemDefinitions(AppDbContext db) { var defaults = new[] diff --git a/TrafagSalesExporter/TrafagSalesExporter.Tests/DatabaseInitializationServiceTests.cs b/TrafagSalesExporter/TrafagSalesExporter.Tests/DatabaseInitializationServiceTests.cs index 1e342ac..588b59d 100644 --- a/TrafagSalesExporter/TrafagSalesExporter.Tests/DatabaseInitializationServiceTests.cs +++ b/TrafagSalesExporter/TrafagSalesExporter.Tests/DatabaseInitializationServiceTests.cs @@ -112,6 +112,29 @@ public class DatabaseInitializationServiceTests : IDisposable Assert.Contains(db.SapFieldMappings, x => x.SiteId == purchasing.Id && x.TargetField == "NetValueChf" && x.SourceExpression == "EKPO.NetwrChf"); } + [Fact] + public async Task InitializeAsync_Repairs_India_Sage_Hana_Mapping() + { + await PrepareIndiaSourceSystemDriftAsync(); + + var service = CreateService(); + await service.InitializeAsync(); + + await using var db = await _dbFactory.CreateDbContextAsync(); + var india = Assert.Single(db.Sites.Include(x => x.HanaServer), x => x.TSC == "TRIN"); + var sageServer = db.HanaServers + .OrderBy(x => x.Id) + .First(x => x.SourceSystem == "SAGE"); + + Assert.Equal("SAGE", india.SourceSystem); + Assert.Equal("TRAFAG_LIVE", india.Schema); + Assert.Equal("india-user", india.UsernameOverride); + Assert.Equal("india-password", india.PasswordOverride); + Assert.Equal("20.197.20.60", sageServer.Host); + Assert.Equal(30015, sageServer.Port); + Assert.Equal(sageServer.Id, india.HanaServerId); + } + private async Task PrepareLegacySitesTableAsync() { await using var db = await _dbFactory.CreateDbContextAsync(); @@ -164,6 +187,59 @@ VALUES ( await db.Database.ExecuteSqlRawAsync("PRAGMA foreign_keys = ON;"); } + private async Task PrepareIndiaSourceSystemDriftAsync() + { + await using var db = await _dbFactory.CreateDbContextAsync(); + + db.HanaServers.RemoveRange(db.HanaServers); + db.Sites.RemoveRange(db.Sites); + await db.SaveChangesAsync(); + + var bi1Server = new HanaServer + { + SourceSystem = "BI1", + Name = "Internal", + Host = "travtrp0", + Port = 30015, + Username = string.Empty, + Password = string.Empty + }; + var indiaServer = new HanaServer + { + SourceSystem = string.Empty, + Name = "India", + Host = "20.197.20.60", + Port = 30015, + Username = string.Empty, + Password = string.Empty + }; + var emptySageServer = new HanaServer + { + SourceSystem = "SAGE", + Name = "SAGE", + Host = string.Empty, + Port = 30015, + Username = string.Empty, + Password = string.Empty + }; + + db.HanaServers.AddRange(bi1Server, indiaServer, emptySageServer); + await db.SaveChangesAsync(); + + db.Sites.Add(new Site + { + HanaServerId = indiaServer.Id, + Schema = "TRAFAG_LIVE", + TSC = "TRIN", + Land = "Indien", + SourceSystem = "BI1", + UsernameOverride = "india-user", + PasswordOverride = "india-password", + IsActive = true + }); + await db.SaveChangesAsync(); + } + private async Task PrepareBrokenHanaServerForeignKeyAsync() { await using var db = await _dbFactory.CreateDbContextAsync();