using Marco.Pms.Model.Activities; using Marco.Pms.Model.AttendanceModule; using Marco.Pms.Model.Authentication; using Marco.Pms.Model.Employees; using Marco.Pms.Model.Entitlements; using Marco.Pms.Model.Projects; using Marco.Pms.Utility; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; namespace Marco.Pms.DataAccess.Data { public class ApplicationDbContext : IdentityDbContext { private readonly IHttpContextAccessor _httpContextAccessor; public ApplicationDbContext(DbContextOptions options, IHttpContextAccessor httpContextAccessor) : base(options) { _httpContextAccessor = httpContextAccessor; } public DbSet RefreshTokens { get; set; } public DbSet Tenants { get; set; } public DbSet ApplicationUsers { get; set; } public DbSet ActivityMasters { get; set; } public DbSet Projects { get; set; } public DbSet ProjectAllocations { get; set; } public DbSet StatusMasters { get; set; } public DbSet Buildings { get; set; } public DbSet Floor { get; set; } public DbSet WorkAreas { get; set; } public DbSet WorkItems { get; set; } public DbSet WorkItemMapping { get; set; } public DbSet WorkShifts { get; set; } public DbSet TaskAllocations { get; set; } // public DbSet Attendances { get; set; } public DbSet Attendes { get; set; } public DbSet AttendanceLogs { get; set; } // public DbSet AttendLogs { get; set; } public DbSet Employees { get; set; } public DbSet EmployeeRoleMappings { get; set; } public DbSet Modules { get; set; } public DbSet Features { get; set; } public DbSet FeaturePermissions { get; set; } public DbSet ApplicationRoles { get; set; } public DbSet JobRoles { get; set; } public DbSet RolePermissionMappings { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); ManageApplicationStructure(modelBuilder); modelBuilder.Entity().HasData( new ApplicationRole { Id = new Guid("2c8d0808-c421-11ef-9b93-0242ac110002"), Role = "Admin", Description = "", TenantId = 1 }, new ApplicationRole { Id = new Guid("62e0918d-c421-11ef-9b93-0242ac110002"), Role = "Welder", Description = "", TenantId = 1 }, new ApplicationRole { Id = new Guid("68823f1f-c421-11ef-9b93-0242ac110002"), Role = "Helper", Description = "", TenantId = 1 }, new ApplicationRole { Id = new Guid("6d3a7c72-c421-11ef-9b93-0242ac110002"), Role = "Site Engineer", Description = "", TenantId = 1 } , new ApplicationRole { Id = new Guid("6d3aad72-c421-11ef-9b93-0242ac110002"), Role = "Project Manager", Description = "", TenantId = 1 } ); modelBuilder.Entity(entity => { entity.HasKey(e => e.Id); entity.HasOne(e => e.User) .WithMany() .HasForeignKey(e => e.UserId) .OnDelete(DeleteBehavior.Cascade); }); modelBuilder.Entity().HasData( new Tenant { Id = 1, Name = "MarcoBMS", ContactName = "Admin", ContactNumber = "123456789", Description = "", DomainName = "www.marcobms.org", OnBoardingDate = DateTime.MinValue } ); modelBuilder.Entity().HasData( new StatusMaster { Id = 1, Status = "Active", TenantId = 1 }, new StatusMaster { Id = 2, Status = "In Progress", TenantId = 1 }, new StatusMaster { Id = 3, Status = "On Hold", TenantId = 1 }, new StatusMaster { Id = 4, Status = "Completed", TenantId = 1 } ); modelBuilder.Entity().HasData( new Project { Id = 1, Name = "Project 1", ProjectAddress = "Project 1 Address", ContactPerson = "Project 1 Contact Person", ProjectStatusId = 1, TenantId = 1 }, new Project { Id = 2, Name = "Project 2", ProjectAddress = "Project 2 Address", ContactPerson = "Project 2 Contact Person", ProjectStatusId = 2, TenantId = 1 }, new Project { Id = 3, Name = "Project 3", ProjectAddress = "Project 3 Address", ContactPerson = "Project 3 Contact Person", ProjectStatusId = 3, TenantId = 1 }); var tenantId = _httpContextAccessor.HttpContext?.Items["TenantId"]?.ToString(); modelBuilder.Entity() .HasData( new ActivityMaster { Id = 1, ActivityName = "Core Cutting", UnitOfMeasurement = UnitOfMeasurement.Number.ToString(), TenantId = 1 }, new ActivityMaster { Id = 2, ActivityName = "Fabrication", UnitOfMeasurement = UnitOfMeasurement.Meter.ToString(), TenantId = 1 }, new ActivityMaster { Id = 3, ActivityName = "Lifting", UnitOfMeasurement = UnitOfMeasurement.Meter.ToString(), TenantId = 1 }, new ActivityMaster { Id = 4, ActivityName = "Hanging", UnitOfMeasurement = UnitOfMeasurement.Meter.ToString(), TenantId = 1 }, new ActivityMaster { Id = 5, ActivityName = "Tapping", UnitOfMeasurement = UnitOfMeasurement.Meter.ToString(), TenantId = 1 }, new ActivityMaster { Id = 6, ActivityName = "Welding", UnitOfMeasurement = UnitOfMeasurement.Meter.ToString(), TenantId = 1 }, new ActivityMaster { Id = 7, ActivityName = "Testing", UnitOfMeasurement = UnitOfMeasurement.Area.ToString(), TenantId = 1 }, new ActivityMaster { Id = 8, ActivityName = "Painting", UnitOfMeasurement = UnitOfMeasurement.Meter.ToString(), TenantId = 1 }, new ActivityMaster { Id = 9, ActivityName = "Marking Area", UnitOfMeasurement = UnitOfMeasurement.Meter.ToString(), TenantId = 1 }, new ActivityMaster { Id = 10, ActivityName = "Drilling", UnitOfMeasurement = UnitOfMeasurement.Number.ToString(), TenantId = 1 }, new ActivityMaster { Id = 11, ActivityName = "MS Support Fabrication", UnitOfMeasurement = UnitOfMeasurement.Number.ToString(), TenantId = 1 }, new ActivityMaster { Id = 12, ActivityName = "MS Support Hanging", UnitOfMeasurement = UnitOfMeasurement.Number.ToString(), TenantId = 1 }, new ActivityMaster { Id = 13, ActivityName = "Hydrant Volve", UnitOfMeasurement = UnitOfMeasurement.Number.ToString(), TenantId = 1 }, new ActivityMaster { Id = 14, ActivityName = "Sprinkler Installation", UnitOfMeasurement = UnitOfMeasurement.Number.ToString(), TenantId = 1 } ); } private static void ManageApplicationStructure(ModelBuilder modelBuilder) { // Configure ApplicationRole to Tenant relationship (if Tenant exists) modelBuilder.Entity() .HasOne() // No navigation property in ApplicationRole .WithMany() .HasForeignKey(ar => ar.TenantId); // Configure the relationship between ApplicationRole and FeaturePermission via a join table modelBuilder.Entity(entity => { entity.HasKey(rfp => new { rfp.ApplicationRoleId, rfp.FeaturePermissionId }); entity.HasOne() .WithMany() .HasForeignKey(rfp => rfp.ApplicationRoleId); entity.HasOne() .WithMany() .HasForeignKey(rfp => rfp.FeaturePermissionId); }); modelBuilder.Entity().HasData(new Module { Id = 1, Name = "Project", Description = "Project Module", Key = "b04da7e9-0406-409c-ac7f-b97256e6ea02" }, new Module { Id = 2, Name = "Employee", Description = "Employee Module", Key = "0971c7fb-6ce1-458a-ae3f-8d3205893637" }, new Module { Id = 3, Name = "Masters", Description = "Masters Module", Key = "504ec132-e6a9-422f-8f85-050602cfce05" }); modelBuilder.Entity().HasData( new Feature { Id = new Guid("53176ebf-c75d-42e5-839f-4508ffac3def"), Description = "Manage Project", Name = "Manage Project", ModuleId = 1, IsActive = true }, new Feature { Id = new Guid("9666de86-d7c7-4d3d-acaa-fcd6d6b81f3c"), Description = "Manage Infra", Name = "Manage Infra", ModuleId = 1, IsActive = true }, new Feature { Id = new Guid("9d4b5489-2079-40b9-bd77-6e1bf90bc19f"), Description = "Manage Tasks", Name = "Manage Tasks", ModuleId = 1, IsActive = true }, new Feature { Id = new Guid("39e66f81-efc6-446c-95bd-46bff6cfb606"), Description = "Assign and Update Tasks Progress", Name = "Assign and Update Tasks Progress", ModuleId = 1, IsActive = true }, new Feature { Id = new Guid("81ab8a87-8ccd-4015-a917-0627cee6a100"), Description = "Manage Employee", Name = "Manage Employee", ModuleId = 2, IsActive = true }, new Feature { Id = new Guid("52c9cf54-1eb2-44d2-81bb-524cf29c0a94"), Description = "Attendance", Name = "Attendance", ModuleId = 2, IsActive = true }, new Feature { Id = new Guid("be3b3afc-6ccf-4566-b9b6-aafcb65546be"), Description = "Global Masters", Name = "Global Masters", ModuleId = 3, IsActive = true }, new Feature { Id = new Guid("660131a4-788c-4739-a082-cbbf7879cbf2"), Description = "Tenant Masters", Name = "Tenant Masters", ModuleId = 3, IsActive = true } ); modelBuilder.Entity().HasData( new FeaturePermission { Id = new Guid("6ea44136-987e-44ba-9e5d-1cf8f5837ebc"), FeatureId = new Guid("53176ebf-c75d-42e5-839f-4508ffac3def"), IsEnabled = true, Name = "View Project" , Description=""}, new FeaturePermission { Id = new Guid("172fc9b6-755b-4f62-ab26-55c34a330614"), FeatureId = new Guid("53176ebf-c75d-42e5-839f-4508ffac3def"), IsEnabled = true, Name = "Manage Project", Description = "" }, new FeaturePermission { Id = new Guid("b94802ce-0689-4643-9e1d-11c86950c35b"), FeatureId = new Guid("53176ebf-c75d-42e5-839f-4508ffac3def"), IsEnabled = true, Name = "Manage Team", Description = "" }, new FeaturePermission { Id = new Guid("c7b68e33-72f0-474f-bd96-77636427ecc8"), FeatureId = new Guid("9666de86-d7c7-4d3d-acaa-fcd6d6b81f3c"), IsEnabled = true, Name = "View Project Infra", Description = "" }, new FeaturePermission { Id = new Guid("f2aee20a-b754-4537-8166-f9507b44585b"), FeatureId = new Guid("9666de86-d7c7-4d3d-acaa-fcd6d6b81f3c"), IsEnabled = true, Name = "Manage Project Infra", Description = "" }, new FeaturePermission { Id = new Guid("9fcc5f87-25e3-4846-90ac-67a71ab92e3c"), FeatureId = new Guid("9d4b5489-2079-40b9-bd77-6e1bf90bc19f"), IsEnabled = true, Name = "View Task", Description = "" }, new FeaturePermission { Id = new Guid("08752f33-3b29-4816-b76b-ea8a968ed3c5"), FeatureId = new Guid("9d4b5489-2079-40b9-bd77-6e1bf90bc19f"), IsEnabled = true, Name = "Manage Task", Description = "" }, new FeaturePermission { Id = new Guid("d135a4b0-4f9a-4903-ab9c-4843839ebdee"), FeatureId = new Guid("39e66f81-efc6-446c-95bd-46bff6cfb606"), IsEnabled = true, Name = "Assign Task and Report Progress", Description = "" }, new FeaturePermission { Id = new Guid("ed99ecd4-1bed-42e1-b7b3-d64c04493823"), FeatureId = new Guid("39e66f81-efc6-446c-95bd-46bff6cfb606"), IsEnabled = true, Name = "Approve Task", Description = "" }, new FeaturePermission { Id = new Guid("b82d2b7e-0d52-45f3-997b-c008ea460e7f"), FeatureId = new Guid("81ab8a87-8ccd-4015-a917-0627cee6a100"), IsEnabled = true, Name = "View Employee", Description = "" }, new FeaturePermission { Id = new Guid("a97d366a-c2bb-448d-be93-402bd2324566"), FeatureId = new Guid("81ab8a87-8ccd-4015-a917-0627cee6a100"), IsEnabled = true, Name = "Manage Employee", Description = "" }, new FeaturePermission { Id = new Guid("fbd213e0-0250-46f1-9f5f-4b2a1e6e76a3"), FeatureId = new Guid("81ab8a87-8ccd-4015-a917-0627cee6a100"), IsEnabled = true, Name = "Assign To Project", Description = "" }, new FeaturePermission { Id = new Guid("915e6bff-65f6-4e3f-aea8-3fd217d3ea9e"), FeatureId = new Guid("52c9cf54-1eb2-44d2-81bb-524cf29c0a94"), IsEnabled = true, Name = "Perform Attendance ", Description = "" }, new FeaturePermission { Id = new Guid("57802c4a-00aa-4a1f-a048-fd2f70dd44b6"), FeatureId = new Guid("52c9cf54-1eb2-44d2-81bb-524cf29c0a94"), IsEnabled = true, Name = "Regularize Attendance", Description = "" }, new FeaturePermission { Id = new Guid("5ffbafe0-7ab0-48b1-bb50-c1bf76b65f9d"), FeatureId = new Guid("be3b3afc-6ccf-4566-b9b6-aafcb65546be"), IsEnabled = true, Name = "View Masters", Description = "" }, new FeaturePermission { Id = new Guid("588a8824-f924-4955-82d8-fc51956cf323"), FeatureId = new Guid("be3b3afc-6ccf-4566-b9b6-aafcb65546be"), IsEnabled = true, Name = "Manage Masters", Description = "" }, new FeaturePermission { Id = new Guid("cb8ec407-46d4-4467-930c-69127cda6dec"), FeatureId = new Guid("660131a4-788c-4739-a082-cbbf7879cbf2"), IsEnabled = true, Name = "View Masters", Description = "" }, new FeaturePermission { Id = new Guid("6b1a6d97-a951-4de5-9b19-709bac7c4f18"), FeatureId = new Guid("660131a4-788c-4739-a082-cbbf7879cbf2"), IsEnabled = true, Name = "Manage Masters", Description = "" } ); } } }