From fe0a4b1c7ca21e8829e87292ff13b8ad9e87e205 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sedat=20=C3=96ZT=C3=9CRK?= <76204082+iamsedatozturk@users.noreply.github.com> Date: Thu, 30 Oct 2025 17:10:13 +0300 Subject: [PATCH] =?UTF-8?q?Country=20Curreny=20d=C3=BCzenlemesi=20ve=20Vie?= =?UTF-8?q?w=20Creator?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Seeds/HostDataSeeder.cs | 7 +- .../Seeds/ListFormSeeder.cs | 53 ++++++--- .../Enums/LookUpQueryValues.cs | 4 +- .../PlatformConsts.cs | 5 + .../TableNameResolver.cs | 13 +++ .../Entities/Branch/Accounting/BankAccount.cs | 1 - .../Entities/Host/Country.cs | 6 +- .../Entities/Tenant/Hr/CostCenter.cs | 1 - .../Entities/Tenant/Hr/Employee.cs | 1 - .../Entities/Tenant/Hr/Expense.cs | 1 - .../Entities/Tenant/Hr/JobPosition.cs | 1 - .../EntityFrameworkCore/PlatformDbContext.cs | 8 -- ....cs => 20251030134034_Initial.Designer.cs} | 48 +------- ...5_Initial.cs => 20251030134034_Initial.cs} | 75 +------------ .../PlatformDbContextModelSnapshot.cs | 46 +------- .../Tenants/TenantDatabaseViewCreator.cs | 105 ++++++++++++++++++ 16 files changed, 180 insertions(+), 195 deletions(-) rename api/src/Kurs.Platform.EntityFrameworkCore/Migrations/{20251030104555_Initial.Designer.cs => 20251030134034_Initial.Designer.cs} (99%) rename api/src/Kurs.Platform.EntityFrameworkCore/Migrations/{20251030104555_Initial.cs => 20251030134034_Initial.cs} (99%) create mode 100644 api/src/Kurs.Platform.EntityFrameworkCore/Tenants/TenantDatabaseViewCreator.cs diff --git a/api/src/Kurs.Platform.DbMigrator/Seeds/HostDataSeeder.cs b/api/src/Kurs.Platform.DbMigrator/Seeds/HostDataSeeder.cs index e05ea7b9..64f7213c 100644 --- a/api/src/Kurs.Platform.DbMigrator/Seeds/HostDataSeeder.cs +++ b/api/src/Kurs.Platform.DbMigrator/Seeds/HostDataSeeder.cs @@ -7,7 +7,6 @@ using System.Threading.Tasks; using Kurs.Languages.Entities; using Kurs.Platform.Entities; using Kurs.Platform.Enums; -using Kurs.Platform.Seeds; using Kurs.Settings.Entities; using Microsoft.Extensions.Configuration; using Volo.Abp.Data; @@ -175,12 +174,14 @@ public class HostDataSeeder : IDataSeedContributor, ITransientDependency if (!seenCodes.Add(item.Code) || existingCodes.Contains(item.Code)) continue; + var currency = await _currencyRepository.FirstOrDefaultAsync(c => c.Code == item.CurrencyCode); + buffer.Add(new Country( Guid.NewGuid(), item.Code, item.Name, item.GroupName, - item.CurrencyCode, + currency != null ? currency.Id : null, item.PhoneCode, item.TaxLabel )); @@ -525,7 +526,7 @@ public class HostDataSeeder : IDataSeedContributor, ITransientDependency Symbol = item.Symbol, Name = item.Name, IsActive = item.IsActive - }); + }, autoSave: true); } } diff --git a/api/src/Kurs.Platform.DbMigrator/Seeds/ListFormSeeder.cs b/api/src/Kurs.Platform.DbMigrator/Seeds/ListFormSeeder.cs index bf6db0ee..d75bbcae 100644 --- a/api/src/Kurs.Platform.DbMigrator/Seeds/ListFormSeeder.cs +++ b/api/src/Kurs.Platform.DbMigrator/Seeds/ListFormSeeder.cs @@ -43704,7 +43704,7 @@ public class ListFormSeeder : IDataSeedContributor, ITransientDependency IsOrganizationUnit = false, Description = AppCodes.SupplyChain.MaterialGroup, SelectCommandType = SelectCommandTypeEnum.Table, - SelectCommand = TableNameResolver.GetFullTableName(nameof(TableNameEnum.MaterialGroup)), + SelectCommand = TableNameResolver.GetFullViewName(nameof(TableNameEnum.MaterialGroup)), KeyFieldName = "Id", KeyFieldDbSourceType = DbType.Guid, DefaultFilter = "\"IsDeleted\" = 'false'", @@ -43774,7 +43774,7 @@ public class ListFormSeeder : IDataSeedContributor, ITransientDependency AllowDeleting = true, AllowAdding = true, AllowUpdating = true, - SendOnlyChangedFormValuesUpdate = false, + SendOnlyChangedFormValuesUpdate = true, }), EditingFormJson = JsonSerializer.Serialize(new List() { new() { Order=1,ColCount=1,ColSpan=2,ItemType="group", Items= @@ -43856,7 +43856,7 @@ public class ListFormSeeder : IDataSeedContributor, ITransientDependency CultureName = LanguageCodes.En, SourceDbType = DbType.String, FieldName = "Code", - Width = 250, + Width = 200, ListOrderNo = 2, Visible = true, IsActive = true, @@ -43890,13 +43890,11 @@ public class ListFormSeeder : IDataSeedContributor, ITransientDependency CultureName = LanguageCodes.En, SourceDbType = DbType.String, FieldName = "Name", - Width = 350, + Width = 250, ListOrderNo = 3, Visible = true, IsActive = true, IsDeleted = false, - SortIndex = 1, - SortDirection = GridColumnOptions.SortOrderAsc, AllowSearch = true, ValidationRuleJson = JsonSerializer.Serialize(new ValidationRuleDto[] { new ValidationRuleDto() { Type = Enum.GetName(UiColumnValidationRuleTypeEnum.required) } @@ -43926,7 +43924,7 @@ public class ListFormSeeder : IDataSeedContributor, ITransientDependency CultureName = LanguageCodes.En, SourceDbType = DbType.String, FieldName = "Description", - Width = 500, + Width = 300, ListOrderNo = 4, Visible = true, IsActive = true, @@ -43988,7 +43986,7 @@ public class ListFormSeeder : IDataSeedContributor, ITransientDependency CultureName = LanguageCodes.En, SourceDbType = DbType.Guid, FieldName = "ParentGroupId", - Width = 200, + Width = 400, ListOrderNo = 6, Visible = true, IsActive = true, @@ -44017,13 +44015,40 @@ public class ListFormSeeder : IDataSeedContributor, ITransientDependency PivotSettingsJson = JsonSerializer.Serialize(new ListFormFieldPivotSettingsDto { IsPivot = true - }), - GroupingJson = JsonSerializer.Serialize(new ColumnGroupingDto - { - GroupIndex = 1, - AllowGrouping = true, - AutoExpandGroup = true }) + }, + new() { + ListFormCode = listFormMaterialGroup.ListFormCode, + RoleId = null, + UserId = null, + CultureName = LanguageCodes.En, + SourceDbType = DbType.String, + FieldName = "HierarchyPath", + Width = 500, + ListOrderNo = 7, + Visible = true, + IsActive = true, + IsDeleted = false, + AllowSearch = true, + SortIndex = 1, + SortDirection = GridColumnOptions.SortOrderAsc, + ColumnCustomizationJson = JsonSerializer.Serialize(new ColumnCustomizationDto + { + AllowReordering = true, + }), + PermissionJson = JsonSerializer.Serialize(new ListFormFieldPermissionDto + { + C = AppCodes.SupplyChain.MaterialGroup + ".Create", + R = AppCodes.SupplyChain.MaterialGroup, + U = AppCodes.SupplyChain.MaterialGroup + ".Update", + E = true, + I = true, + Deny = false + }), + PivotSettingsJson = JsonSerializer.Serialize(new ListFormFieldPivotSettingsDto + { + IsPivot = true + }), } }); #endregion diff --git a/api/src/Kurs.Platform.Domain.Shared/Enums/LookUpQueryValues.cs b/api/src/Kurs.Platform.Domain.Shared/Enums/LookUpQueryValues.cs index 259ff92a..5729fb71 100644 --- a/api/src/Kurs.Platform.Domain.Shared/Enums/LookUpQueryValues.cs +++ b/api/src/Kurs.Platform.Domain.Shared/Enums/LookUpQueryValues.cs @@ -304,7 +304,7 @@ public static class LookupQueryValues public static string MaterialGroupValues = $"SELECT " + $"\"Id\" AS \"Key\", " + - $"\"Name\" AS \"Name\" " + - $"FROM \"{FullNameTable(TableNameEnum.MaterialGroup)}\" " + + $"\"HierarchyPath\" AS \"Name\" " + + $"FROM \"{FullNameView(TableNameEnum.MaterialGroup)}\" " + $"WHERE \"IsDeleted\" = 'false' "; } \ No newline at end of file diff --git a/api/src/Kurs.Platform.Domain.Shared/PlatformConsts.cs b/api/src/Kurs.Platform.Domain.Shared/PlatformConsts.cs index 598ef78c..3c06142f 100644 --- a/api/src/Kurs.Platform.Domain.Shared/PlatformConsts.cs +++ b/api/src/Kurs.Platform.Domain.Shared/PlatformConsts.cs @@ -48,6 +48,11 @@ public static class PlatformConsts return TableNameResolver.GetFullTableName($"{TableName}"); } + public static string FullNameView(TableNameEnum TableName) + { + return TableNameResolver.GetFullViewName($"{TableName}"); + } + public static class TablePrefix { public static string PlatformByName(MenuPrefix MenuGroup, TableNameEnum tableName) diff --git a/api/src/Kurs.Platform.Domain.Shared/TableNameResolver.cs b/api/src/Kurs.Platform.Domain.Shared/TableNameResolver.cs index 8a5dc5ca..f5329bf5 100644 --- a/api/src/Kurs.Platform.Domain.Shared/TableNameResolver.cs +++ b/api/src/Kurs.Platform.Domain.Shared/TableNameResolver.cs @@ -5,6 +5,8 @@ namespace Kurs.Platform.Enums; public static class TableNameResolver { + public const string ViewPrefix = "V_"; + // Tüm tabloların hangi prefix tipine ve menü grubuna ait olduğunu belirten harita private static readonly Dictionary Method, MenuPrefix PrefixGroup)> _map = new(StringComparer.OrdinalIgnoreCase) @@ -174,4 +176,15 @@ public static class TableNameResolver return entry.Method(entry.PrefixGroup, tableEnum); } + + public static string GetFullViewName(string tableName) + { + if (!Enum.TryParse(tableName, out var tableEnum)) + throw new ArgumentException($"'{tableName}' geçerli bir TableNameEnum değil."); + + if (!_map.TryGetValue(tableName, out var entry)) + throw new KeyNotFoundException($"'{tableName}' için tablo eşlemesi bulunamadı."); + + return ViewPrefix + entry.Method(entry.PrefixGroup, tableEnum); + } } diff --git a/api/src/Kurs.Platform.Domain/Entities/Branch/Accounting/BankAccount.cs b/api/src/Kurs.Platform.Domain/Entities/Branch/Accounting/BankAccount.cs index 315a4c62..4b32f3f0 100644 --- a/api/src/Kurs.Platform.Domain/Entities/Branch/Accounting/BankAccount.cs +++ b/api/src/Kurs.Platform.Domain/Entities/Branch/Accounting/BankAccount.cs @@ -14,7 +14,6 @@ public class BankAccount : FullAuditedEntity, IMultiTenant public Bank Bank { get; set; } public string AccountOwner { get; set; } public Guid? CurrencyId { get; set; } // Currency entity'sine referans - public Currency Currency { get; set; } public bool CanTransferMoney { get; set; } public string Company { get; set; } diff --git a/api/src/Kurs.Platform.Domain/Entities/Host/Country.cs b/api/src/Kurs.Platform.Domain/Entities/Host/Country.cs index efef1b27..7338eb6d 100644 --- a/api/src/Kurs.Platform.Domain/Entities/Host/Country.cs +++ b/api/src/Kurs.Platform.Domain/Entities/Host/Country.cs @@ -9,7 +9,7 @@ public class Country : FullAuditedEntity public string Code { get; set; } // TR, US public string Name { get; set; } public string GroupName { get; set; } - public string CurrencyCode { get; set; } + public Guid? CurrencyId { get; set; } public int PhoneCode { get; set; } public string TaxLabel { get; set; } public bool ZipRequired { get; set; } @@ -24,7 +24,7 @@ public class Country : FullAuditedEntity string code, string name, string groupName, - string currentyCode, + Guid? currencyId, int phoneCode, string taxLabel, bool zipRequired = false, @@ -34,7 +34,7 @@ public class Country : FullAuditedEntity Code = code; Name = name; GroupName = groupName; - CurrencyCode = currentyCode; + CurrencyId = currencyId; PhoneCode = phoneCode; TaxLabel = taxLabel; ZipRequired = zipRequired; diff --git a/api/src/Kurs.Platform.Domain/Entities/Tenant/Hr/CostCenter.cs b/api/src/Kurs.Platform.Domain/Entities/Tenant/Hr/CostCenter.cs index 2dab94a3..b8def60b 100644 --- a/api/src/Kurs.Platform.Domain/Entities/Tenant/Hr/CostCenter.cs +++ b/api/src/Kurs.Platform.Domain/Entities/Tenant/Hr/CostCenter.cs @@ -27,7 +27,6 @@ public class CostCenter : FullAuditedEntity, IMultiTenant public decimal BudgetedAmount { get; set; } public decimal ActualAmount { get; set; } public Guid? CurrencyId { get; set; } - public Currency Currency { get; set; } public string FiscalYear { get; set; } public bool IsActive { get; set; } } diff --git a/api/src/Kurs.Platform.Domain/Entities/Tenant/Hr/Employee.cs b/api/src/Kurs.Platform.Domain/Entities/Tenant/Hr/Employee.cs index 62eb7050..ae057ca2 100644 --- a/api/src/Kurs.Platform.Domain/Entities/Tenant/Hr/Employee.cs +++ b/api/src/Kurs.Platform.Domain/Entities/Tenant/Hr/Employee.cs @@ -53,7 +53,6 @@ public class Employee : FullAuditedEntity, IMultiTenant public decimal BaseSalary { get; set; } public Guid? CurrencyId { get; set; } - public Currency Currency { get; set; } public string PayrollGroup { get; set; } // e.g., Monthly, Biweekly, Weekly diff --git a/api/src/Kurs.Platform.Domain/Entities/Tenant/Hr/Expense.cs b/api/src/Kurs.Platform.Domain/Entities/Tenant/Hr/Expense.cs index d343d412..463b67f6 100644 --- a/api/src/Kurs.Platform.Domain/Entities/Tenant/Hr/Expense.cs +++ b/api/src/Kurs.Platform.Domain/Entities/Tenant/Hr/Expense.cs @@ -14,7 +14,6 @@ public class Expense : FullAuditedEntity, IMultiTenant public string Category { get; set; } public decimal Amount { get; set; } public Guid? CurrencyId { get; set; } - public Currency Currency { get; set; } public DateTime RequestDate { get; set; } public string Description { get; set; } diff --git a/api/src/Kurs.Platform.Domain/Entities/Tenant/Hr/JobPosition.cs b/api/src/Kurs.Platform.Domain/Entities/Tenant/Hr/JobPosition.cs index 9cba39bc..495f6d5e 100644 --- a/api/src/Kurs.Platform.Domain/Entities/Tenant/Hr/JobPosition.cs +++ b/api/src/Kurs.Platform.Domain/Entities/Tenant/Hr/JobPosition.cs @@ -17,7 +17,6 @@ public class JobPosition : FullAuditedEntity, IMultiTenant public decimal MinSalary { get; set; } public decimal MaxSalary { get; set; } public Guid? CurrencyId { get; set; } - public Currency Currency { get; set; } public string RequiredSkills { get; set; } public string Responsibilities { get; set; } public string Qualifications { get; set; } diff --git a/api/src/Kurs.Platform.EntityFrameworkCore/EntityFrameworkCore/PlatformDbContext.cs b/api/src/Kurs.Platform.EntityFrameworkCore/EntityFrameworkCore/PlatformDbContext.cs index b2918b00..d4ab0c20 100644 --- a/api/src/Kurs.Platform.EntityFrameworkCore/EntityFrameworkCore/PlatformDbContext.cs +++ b/api/src/Kurs.Platform.EntityFrameworkCore/EntityFrameworkCore/PlatformDbContext.cs @@ -831,7 +831,6 @@ public class PlatformDbContext : b.Property(x => x.Code).IsRequired().HasMaxLength(8); b.Property(x => x.Name).IsRequired().HasMaxLength(128); b.Property(x => x.GroupName).HasMaxLength(128); - b.Property(x => x.CurrencyCode).HasMaxLength(8); b.Property(x => x.TaxLabel).HasMaxLength(64); b.HasIndex(x => x.Code).IsUnique(); @@ -1647,19 +1646,12 @@ public class PlatformDbContext : b.Property(x => x.Company).HasMaxLength(256); b.Property(x => x.BankId).IsRequired(); - b.Property(x => x.CurrencyId); b.HasOne(x => x.Bank) .WithMany() .HasPrincipalKey(x => x.Id) .HasForeignKey(x => x.BankId) .OnDelete(DeleteBehavior.Restrict); - - b.HasOne(x => x.Currency) - .WithMany() - .HasPrincipalKey(x => x.Id) - .HasForeignKey(x => x.CurrencyId) - .OnDelete(DeleteBehavior.Restrict); }); builder.Entity(b => diff --git a/api/src/Kurs.Platform.EntityFrameworkCore/Migrations/20251030104555_Initial.Designer.cs b/api/src/Kurs.Platform.EntityFrameworkCore/Migrations/20251030134034_Initial.Designer.cs similarity index 99% rename from api/src/Kurs.Platform.EntityFrameworkCore/Migrations/20251030104555_Initial.Designer.cs rename to api/src/Kurs.Platform.EntityFrameworkCore/Migrations/20251030134034_Initial.Designer.cs index fdc5ab0c..52409666 100644 --- a/api/src/Kurs.Platform.EntityFrameworkCore/Migrations/20251030104555_Initial.Designer.cs +++ b/api/src/Kurs.Platform.EntityFrameworkCore/Migrations/20251030134034_Initial.Designer.cs @@ -13,7 +13,7 @@ using Volo.Abp.EntityFrameworkCore; namespace Kurs.Platform.Migrations { [DbContext(typeof(PlatformDbContext))] - [Migration("20251030104555_Initial")] + [Migration("20251030134034_Initial")] partial class Initial { /// @@ -1259,8 +1259,6 @@ namespace Kurs.Platform.Migrations b.HasIndex("BankId"); - b.HasIndex("CurrencyId"); - b.ToTable("T_Acc_BankAccount", (string)null); }); @@ -2545,8 +2543,6 @@ namespace Kurs.Platform.Migrations b.HasKey("Id"); - b.HasIndex("CurrencyId"); - b.HasIndex("DepartmentId"); b.HasIndex("ParentCostCenterId"); @@ -2574,9 +2570,8 @@ namespace Kurs.Platform.Migrations .HasColumnType("uniqueidentifier") .HasColumnName("CreatorId"); - b.Property("CurrencyCode") - .HasMaxLength(8) - .HasColumnType("nvarchar(8)"); + b.Property("CurrencyId") + .HasColumnType("uniqueidentifier"); b.Property("DeleterId") .HasColumnType("uniqueidentifier") @@ -3650,8 +3645,6 @@ namespace Kurs.Platform.Migrations b.HasIndex("BankAccountId"); - b.HasIndex("CurrencyId"); - b.HasIndex("DepartmentId"); b.HasIndex("EmploymentTypeId"); @@ -4102,8 +4095,6 @@ namespace Kurs.Platform.Migrations b.HasIndex("ApproverId"); - b.HasIndex("CurrencyId"); - b.HasIndex("EmployeeId"); b.ToTable("T_Hr_Expense", (string)null); @@ -4401,8 +4392,6 @@ namespace Kurs.Platform.Migrations b.HasKey("Id"); - b.HasIndex("CurrencyId"); - b.HasIndex("DepartmentId"); b.ToTable("T_Hr_JobPosition", (string)null); @@ -11629,14 +11618,7 @@ namespace Kurs.Platform.Migrations .OnDelete(DeleteBehavior.Restrict) .IsRequired(); - b.HasOne("Kurs.Platform.Entities.Currency", "Currency") - .WithMany() - .HasForeignKey("CurrencyId") - .OnDelete(DeleteBehavior.Restrict); - b.Navigation("Bank"); - - b.Navigation("Currency"); }); modelBuilder.Entity("Kurs.Platform.Entities.BlogPost", b => @@ -11733,10 +11715,6 @@ namespace Kurs.Platform.Migrations modelBuilder.Entity("Kurs.Platform.Entities.CostCenter", b => { - b.HasOne("Kurs.Platform.Entities.Currency", "Currency") - .WithMany() - .HasForeignKey("CurrencyId"); - b.HasOne("Kurs.Platform.Entities.Department", "Department") .WithMany() .HasForeignKey("DepartmentId"); @@ -11749,8 +11727,6 @@ namespace Kurs.Platform.Migrations .WithMany() .HasForeignKey("ResponsibleEmployeeId"); - b.Navigation("Currency"); - b.Navigation("Department"); b.Navigation("ParentCostCenter"); @@ -11820,10 +11796,6 @@ namespace Kurs.Platform.Migrations .HasForeignKey("BankAccountId") .OnDelete(DeleteBehavior.Restrict); - b.HasOne("Kurs.Platform.Entities.Currency", "Currency") - .WithMany() - .HasForeignKey("CurrencyId"); - b.HasOne("Kurs.Platform.Entities.Department", "Department") .WithMany() .HasForeignKey("DepartmentId"); @@ -11844,8 +11816,6 @@ namespace Kurs.Platform.Migrations b.Navigation("BankAccount"); - b.Navigation("Currency"); - b.Navigation("Department"); b.Navigation("EmploymentType"); @@ -11910,10 +11880,6 @@ namespace Kurs.Platform.Migrations .WithMany() .HasForeignKey("ApproverId"); - b.HasOne("Kurs.Platform.Entities.Currency", "Currency") - .WithMany() - .HasForeignKey("CurrencyId"); - b.HasOne("Kurs.Platform.Entities.Employee", "Employee") .WithMany("ExpenseRequests") .HasForeignKey("EmployeeId") @@ -11922,23 +11888,15 @@ namespace Kurs.Platform.Migrations b.Navigation("Approver"); - b.Navigation("Currency"); - b.Navigation("Employee"); }); modelBuilder.Entity("Kurs.Platform.Entities.JobPosition", b => { - b.HasOne("Kurs.Platform.Entities.Currency", "Currency") - .WithMany() - .HasForeignKey("CurrencyId"); - b.HasOne("Kurs.Platform.Entities.Department", "Department") .WithMany() .HasForeignKey("DepartmentId"); - b.Navigation("Currency"); - b.Navigation("Department"); }); diff --git a/api/src/Kurs.Platform.EntityFrameworkCore/Migrations/20251030104555_Initial.cs b/api/src/Kurs.Platform.EntityFrameworkCore/Migrations/20251030134034_Initial.cs similarity index 99% rename from api/src/Kurs.Platform.EntityFrameworkCore/Migrations/20251030104555_Initial.cs rename to api/src/Kurs.Platform.EntityFrameworkCore/Migrations/20251030134034_Initial.cs index cd64fce2..5a190f0d 100644 --- a/api/src/Kurs.Platform.EntityFrameworkCore/Migrations/20251030104555_Initial.cs +++ b/api/src/Kurs.Platform.EntityFrameworkCore/Migrations/20251030134034_Initial.cs @@ -2817,7 +2817,7 @@ namespace Kurs.Platform.Migrations Code = table.Column(type: "nvarchar(8)", maxLength: 8, nullable: false), Name = table.Column(type: "nvarchar(128)", maxLength: 128, nullable: false), GroupName = table.Column(type: "nvarchar(128)", maxLength: 128, nullable: true), - CurrencyCode = table.Column(type: "nvarchar(8)", maxLength: 8, nullable: true), + CurrencyId = table.Column(type: "uniqueidentifier", nullable: true), PhoneCode = table.Column(type: "int", nullable: false), TaxLabel = table.Column(type: "nvarchar(64)", maxLength: 64, nullable: true), ZipRequired = table.Column(type: "bit", nullable: false), @@ -3020,12 +3020,6 @@ namespace Kurs.Platform.Migrations constraints: table => { table.PrimaryKey("PK_T_Acc_BankAccount", x => x.Id); - table.ForeignKey( - name: "FK_T_Acc_BankAccount_P_Sas_Currency_CurrencyId", - column: x => x.CurrencyId, - principalTable: "P_Sas_Currency", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); table.ForeignKey( name: "FK_T_Acc_BankAccount_T_Acc_Bank_BankId", column: x => x.BankId, @@ -3938,11 +3932,6 @@ namespace Kurs.Platform.Migrations constraints: table => { table.PrimaryKey("PK_T_Hr_CostCenter", x => x.Id); - table.ForeignKey( - name: "FK_T_Hr_CostCenter_P_Sas_Currency_CurrencyId", - column: x => x.CurrencyId, - principalTable: "P_Sas_Currency", - principalColumn: "Id"); table.ForeignKey( name: "FK_T_Hr_CostCenter_T_Hr_CostCenter_ParentCostCenterId", column: x => x.ParentCostCenterId, @@ -4018,11 +4007,6 @@ namespace Kurs.Platform.Migrations constraints: table => { table.PrimaryKey("PK_T_Hr_JobPosition", x => x.Id); - table.ForeignKey( - name: "FK_T_Hr_JobPosition_P_Sas_Currency_CurrencyId", - column: x => x.CurrencyId, - principalTable: "P_Sas_Currency", - principalColumn: "Id"); table.ForeignKey( name: "FK_T_Hr_JobPosition_T_Hr_Department_DepartmentId", column: x => x.DepartmentId, @@ -4120,11 +4104,6 @@ namespace Kurs.Platform.Migrations constraints: table => { table.PrimaryKey("PK_T_Hr_Employee", x => x.Id); - table.ForeignKey( - name: "FK_T_Hr_Employee_P_Sas_Currency_CurrencyId", - column: x => x.CurrencyId, - principalTable: "P_Sas_Currency", - principalColumn: "Id"); table.ForeignKey( name: "FK_T_Hr_Employee_T_Acc_BankAccount_BankAccountId", column: x => x.BankAccountId, @@ -4187,11 +4166,6 @@ namespace Kurs.Platform.Migrations constraints: table => { table.PrimaryKey("PK_T_Hr_Expense", x => x.Id); - table.ForeignKey( - name: "FK_T_Hr_Expense_P_Sas_Currency_CurrencyId", - column: x => x.CurrencyId, - principalTable: "P_Sas_Currency", - principalColumn: "Id"); table.ForeignKey( name: "FK_T_Hr_Expense_T_Hr_Employee_ApproverId", column: x => x.ApproverId, @@ -5195,11 +5169,6 @@ namespace Kurs.Platform.Migrations table: "T_Acc_BankAccount", column: "BankId"); - migrationBuilder.CreateIndex( - name: "IX_T_Acc_BankAccount_CurrencyId", - table: "T_Acc_BankAccount", - column: "CurrencyId"); - migrationBuilder.CreateIndex( name: "IX_T_Adm_BlogCategory_Slug", table: "T_Adm_BlogCategory", @@ -5312,11 +5281,6 @@ namespace Kurs.Platform.Migrations table: "T_Crd_QuestionOption", column: "QuestionId"); - migrationBuilder.CreateIndex( - name: "IX_T_Hr_CostCenter_CurrencyId", - table: "T_Hr_CostCenter", - column: "CurrencyId"); - migrationBuilder.CreateIndex( name: "IX_T_Hr_CostCenter_DepartmentId", table: "T_Hr_CostCenter", @@ -5357,11 +5321,6 @@ namespace Kurs.Platform.Migrations table: "T_Hr_Employee", column: "BankAccountId"); - migrationBuilder.CreateIndex( - name: "IX_T_Hr_Employee_CurrencyId", - table: "T_Hr_Employee", - column: "CurrencyId"); - migrationBuilder.CreateIndex( name: "IX_T_Hr_Employee_DepartmentId", table: "T_Hr_Employee", @@ -5387,21 +5346,11 @@ namespace Kurs.Platform.Migrations table: "T_Hr_Expense", column: "ApproverId"); - migrationBuilder.CreateIndex( - name: "IX_T_Hr_Expense_CurrencyId", - table: "T_Hr_Expense", - column: "CurrencyId"); - migrationBuilder.CreateIndex( name: "IX_T_Hr_Expense_EmployeeId", table: "T_Hr_Expense", column: "EmployeeId"); - migrationBuilder.CreateIndex( - name: "IX_T_Hr_JobPosition_CurrencyId", - table: "T_Hr_JobPosition", - column: "CurrencyId"); - migrationBuilder.CreateIndex( name: "IX_T_Hr_JobPosition_DepartmentId", table: "T_Hr_JobPosition", @@ -5615,22 +5564,6 @@ namespace Kurs.Platform.Migrations /// protected override void Down(MigrationBuilder migrationBuilder) { - migrationBuilder.DropForeignKey( - name: "FK_T_Acc_BankAccount_P_Sas_Currency_CurrencyId", - table: "T_Acc_BankAccount"); - - migrationBuilder.DropForeignKey( - name: "FK_T_Hr_CostCenter_P_Sas_Currency_CurrencyId", - table: "T_Hr_CostCenter"); - - migrationBuilder.DropForeignKey( - name: "FK_T_Hr_Employee_P_Sas_Currency_CurrencyId", - table: "T_Hr_Employee"); - - migrationBuilder.DropForeignKey( - name: "FK_T_Hr_JobPosition_P_Sas_Currency_CurrencyId", - table: "T_Hr_JobPosition"); - migrationBuilder.DropForeignKey( name: "FK_T_Acc_BankAccount_T_Acc_Bank_BankId", table: "T_Acc_BankAccount"); @@ -5767,6 +5700,9 @@ namespace Kurs.Platform.Migrations migrationBuilder.DropTable( name: "P_Sas_ContactTitle"); + migrationBuilder.DropTable( + name: "P_Sas_Currency"); + migrationBuilder.DropTable( name: "P_Sas_DataSource"); @@ -6124,9 +6060,6 @@ namespace Kurs.Platform.Migrations migrationBuilder.DropTable( name: "P_Sas_CountryGroup"); - migrationBuilder.DropTable( - name: "P_Sas_Currency"); - migrationBuilder.DropTable( name: "T_Acc_Bank"); diff --git a/api/src/Kurs.Platform.EntityFrameworkCore/Migrations/PlatformDbContextModelSnapshot.cs b/api/src/Kurs.Platform.EntityFrameworkCore/Migrations/PlatformDbContextModelSnapshot.cs index fa95f359..c7cc445d 100644 --- a/api/src/Kurs.Platform.EntityFrameworkCore/Migrations/PlatformDbContextModelSnapshot.cs +++ b/api/src/Kurs.Platform.EntityFrameworkCore/Migrations/PlatformDbContextModelSnapshot.cs @@ -1256,8 +1256,6 @@ namespace Kurs.Platform.Migrations b.HasIndex("BankId"); - b.HasIndex("CurrencyId"); - b.ToTable("T_Acc_BankAccount", (string)null); }); @@ -2542,8 +2540,6 @@ namespace Kurs.Platform.Migrations b.HasKey("Id"); - b.HasIndex("CurrencyId"); - b.HasIndex("DepartmentId"); b.HasIndex("ParentCostCenterId"); @@ -2571,9 +2567,8 @@ namespace Kurs.Platform.Migrations .HasColumnType("uniqueidentifier") .HasColumnName("CreatorId"); - b.Property("CurrencyCode") - .HasMaxLength(8) - .HasColumnType("nvarchar(8)"); + b.Property("CurrencyId") + .HasColumnType("uniqueidentifier"); b.Property("DeleterId") .HasColumnType("uniqueidentifier") @@ -3647,8 +3642,6 @@ namespace Kurs.Platform.Migrations b.HasIndex("BankAccountId"); - b.HasIndex("CurrencyId"); - b.HasIndex("DepartmentId"); b.HasIndex("EmploymentTypeId"); @@ -4099,8 +4092,6 @@ namespace Kurs.Platform.Migrations b.HasIndex("ApproverId"); - b.HasIndex("CurrencyId"); - b.HasIndex("EmployeeId"); b.ToTable("T_Hr_Expense", (string)null); @@ -4398,8 +4389,6 @@ namespace Kurs.Platform.Migrations b.HasKey("Id"); - b.HasIndex("CurrencyId"); - b.HasIndex("DepartmentId"); b.ToTable("T_Hr_JobPosition", (string)null); @@ -11626,14 +11615,7 @@ namespace Kurs.Platform.Migrations .OnDelete(DeleteBehavior.Restrict) .IsRequired(); - b.HasOne("Kurs.Platform.Entities.Currency", "Currency") - .WithMany() - .HasForeignKey("CurrencyId") - .OnDelete(DeleteBehavior.Restrict); - b.Navigation("Bank"); - - b.Navigation("Currency"); }); modelBuilder.Entity("Kurs.Platform.Entities.BlogPost", b => @@ -11730,10 +11712,6 @@ namespace Kurs.Platform.Migrations modelBuilder.Entity("Kurs.Platform.Entities.CostCenter", b => { - b.HasOne("Kurs.Platform.Entities.Currency", "Currency") - .WithMany() - .HasForeignKey("CurrencyId"); - b.HasOne("Kurs.Platform.Entities.Department", "Department") .WithMany() .HasForeignKey("DepartmentId"); @@ -11746,8 +11724,6 @@ namespace Kurs.Platform.Migrations .WithMany() .HasForeignKey("ResponsibleEmployeeId"); - b.Navigation("Currency"); - b.Navigation("Department"); b.Navigation("ParentCostCenter"); @@ -11817,10 +11793,6 @@ namespace Kurs.Platform.Migrations .HasForeignKey("BankAccountId") .OnDelete(DeleteBehavior.Restrict); - b.HasOne("Kurs.Platform.Entities.Currency", "Currency") - .WithMany() - .HasForeignKey("CurrencyId"); - b.HasOne("Kurs.Platform.Entities.Department", "Department") .WithMany() .HasForeignKey("DepartmentId"); @@ -11841,8 +11813,6 @@ namespace Kurs.Platform.Migrations b.Navigation("BankAccount"); - b.Navigation("Currency"); - b.Navigation("Department"); b.Navigation("EmploymentType"); @@ -11907,10 +11877,6 @@ namespace Kurs.Platform.Migrations .WithMany() .HasForeignKey("ApproverId"); - b.HasOne("Kurs.Platform.Entities.Currency", "Currency") - .WithMany() - .HasForeignKey("CurrencyId"); - b.HasOne("Kurs.Platform.Entities.Employee", "Employee") .WithMany("ExpenseRequests") .HasForeignKey("EmployeeId") @@ -11919,23 +11885,15 @@ namespace Kurs.Platform.Migrations b.Navigation("Approver"); - b.Navigation("Currency"); - b.Navigation("Employee"); }); modelBuilder.Entity("Kurs.Platform.Entities.JobPosition", b => { - b.HasOne("Kurs.Platform.Entities.Currency", "Currency") - .WithMany() - .HasForeignKey("CurrencyId"); - b.HasOne("Kurs.Platform.Entities.Department", "Department") .WithMany() .HasForeignKey("DepartmentId"); - b.Navigation("Currency"); - b.Navigation("Department"); }); diff --git a/api/src/Kurs.Platform.EntityFrameworkCore/Tenants/TenantDatabaseViewCreator.cs b/api/src/Kurs.Platform.EntityFrameworkCore/Tenants/TenantDatabaseViewCreator.cs new file mode 100644 index 00000000..ec77f119 --- /dev/null +++ b/api/src/Kurs.Platform.EntityFrameworkCore/Tenants/TenantDatabaseViewCreator.cs @@ -0,0 +1,105 @@ +using System; +using System.Threading.Tasks; +using Kurs.Platform.EntityFrameworkCore; +using Kurs.Platform.Enums; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using Volo.Abp.Data; +using Volo.Abp.DependencyInjection; +using Volo.Abp.EntityFrameworkCore; + +namespace Kurs.Platform.Data.Seeds; + +/// +/// Database view seeder for creating or updating database views +/// +public class DatabaseViewSeeder : IDataSeedContributor, ITransientDependency +{ + private readonly IDbContextProvider _dbContextProvider; + private readonly ILogger _logger; + + public DatabaseViewSeeder( + IDbContextProvider dbContextProvider, + ILogger logger) + { + _dbContextProvider = dbContextProvider; + _logger = logger; + } + + public async Task SeedAsync(DataSeedContext context) + { + try + { + var dbContext = await _dbContextProvider.GetDbContextAsync(); + + _logger.LogInformation("Starting database view seeding..."); + + await CreateOrUpdateMaterialGroupView(dbContext, nameof(TableNameEnum.MaterialGroup)); + + _logger.LogInformation("Database view seeding completed successfully."); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error occurred while seeding database views."); + throw; + } + } + + /// + /// Creates or updates the V_T_Scp_MaterialGroup view + /// + private async Task CreateOrUpdateMaterialGroupView(PlatformDbContext dbContext, string tableName) + { + string fullTableName = TableNameResolver.GetFullTableName(tableName); + string fullViewName = TableNameResolver.GetFullViewName(tableName); + + try + { + var createViewSql = $@" + IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[{fullViewName}]')) + DROP VIEW [dbo].[{fullViewName}]; + + EXEC(' + CREATE VIEW [dbo].[{fullViewName}] + AS + SELECT TOP 100 PERCENT + lvl5.TenantId, + lvl5.Id, + lvl5.ParentGroupId, + lvl5.Code, + lvl5.Name, + lvl5.Description, + lvl5.IsActive, + lvl5.CreatorId, + lvl5.CreationTime, + lvl5.LastModificationTime, + lvl5.IsDeleted, + COALESCE(lvl1.Name, '''') + + CASE WHEN lvl1.Name IS NOT NULL THEN N'' -> '' ELSE N'''' END + + COALESCE(lvl2.Name, '''') + + CASE WHEN lvl2.Name IS NOT NULL THEN N'' -> '' ELSE N'''' END + + COALESCE(lvl3.Name, '''') + + CASE WHEN lvl3.Name IS NOT NULL THEN N'' -> '' ELSE N'''' END + + COALESCE(lvl4.Name, '''') + + CASE WHEN lvl4.Name IS NOT NULL THEN N'' -> '' ELSE N'''' END + + lvl5.Name AS HierarchyPath + FROM T_Scp_MaterialGroup lvl5 + LEFT JOIN {fullTableName} lvl4 ON lvl5.ParentGroupId = lvl4.Id + LEFT JOIN {fullTableName} lvl3 ON lvl4.ParentGroupId = lvl3.Id + LEFT JOIN {fullTableName} lvl2 ON lvl3.ParentGroupId = lvl2.Id + LEFT JOIN {fullTableName} lvl1 ON lvl2.ParentGroupId = lvl1.Id + ORDER BY HierarchyPath + ') + "; + + await dbContext.Database.ExecuteSqlRawAsync(createViewSql); + + _logger.LogInformation($"Created/Updated {fullViewName} view successfully."); + } + catch (Exception ex) + { + _logger.LogError(ex, $"Error occurred while creating {fullViewName} view."); + throw; + } + } +}