From a1e2bf6ae04ef07aa18607563af9cfea692b99a4 Mon Sep 17 00:00:00 2001 From: Mohd Ariff Date: Wed, 18 Dec 2024 14:37:58 +0800 Subject: [PATCH] Update --- Controllers/API/AdminAPI.cs | 56 ++++++- Controllers/API/IdentityAPI.cs | 35 ++-- DBContext/IdentityDBContext.cs | 4 +- ...cs => 20241218060528_Initiate.Designer.cs} | 22 +-- ...Initiate.cs => 20241218060528_Initiate.cs} | 8 +- Migrations/IdentityDBContextModelSnapshot.cs | 20 +-- .../20241206071642_Initiate.Designer.cs | 2 +- .../InventoryDBContextModelSnapshot.cs | 2 +- Models/UserModel.cs | 2 +- Views/Admin/CompDeptAdmin.cshtml | 18 ++- Views/Admin/UserAdmin.cshtml | 153 ++++++++++++++---- Views/Identity/ComDeptAssignment.cshtml | 42 +++-- Views/Shared/_Layout.cshtml | 5 + 13 files changed, 269 insertions(+), 100 deletions(-) rename Migrations/{20241206015840_Initiate.Designer.cs => 20241218060528_Initiate.Designer.cs} (95%) rename Migrations/{20241206015840_Initiate.cs => 20241218060528_Initiate.cs} (95%) diff --git a/Controllers/API/AdminAPI.cs b/Controllers/API/AdminAPI.cs index cf48d8c..206d35f 100644 --- a/Controllers/API/AdminAPI.cs +++ b/Controllers/API/AdminAPI.cs @@ -21,13 +21,15 @@ namespace PSTW_CentralSystem.Controllers.API private readonly IdentityDBContext _identityDbContext; private readonly UserManager _userManager; private readonly SignInManager _signInManager; + private readonly RoleManager _roleManager; - public AdminAPI(ILogger logger, IdentityDBContext authDbContext, UserManager userManager, SignInManager signInManager) + public AdminAPI(ILogger logger, IdentityDBContext authDbContext, UserManager userManager, SignInManager signInManager, RoleManager roleManager) { _logger = logger; _identityDbContext = authDbContext; _userManager = userManager; _signInManager = signInManager; + _roleManager = roleManager; } [HttpPost("GetClassAndMethodInformation")] @@ -86,6 +88,7 @@ namespace PSTW_CentralSystem.Controllers.API // Return the list as JSON return Json(controllerAndMethodList); } + [HttpPost("GetUserList")] public async Task GetUserList() { @@ -98,7 +101,7 @@ namespace PSTW_CentralSystem.Controllers.API // Fetch all users excluding those with roles SuperAdmin or SystemAdmin var allUsers = await _identityDbContext.Users - .Include(u => u.Department) + .Include(u => u.Department.Company) .ToListAsync(); if (userRole == null || userRole.Count == 0) @@ -113,16 +116,17 @@ namespace PSTW_CentralSystem.Controllers.API } } else - { - userInfo = await _identityDbContext.Users.Include(u => u.Department).ToListAsync(); + { + userInfo = allUsers; } var userList = userInfo.Select(u => new { id = u.Id, email = u.NormalizedEmail, company = u.Department?.Company?.CompanyName, - department = u.Department, - role = _userManager.GetRolesAsync(u).Result + department = u.Department?.DepartmentName, + role = _userManager.GetRolesAsync(u).Result, + status = u.UserInfoStatus, }).ToList(); return Ok(new { UserInfo = userList }); } @@ -132,6 +136,43 @@ namespace PSTW_CentralSystem.Controllers.API } } + [HttpPatch("UpdateUserStatusAndRole/{id}")] + public async Task UpdateUserStatusAndRole(int id, [FromBody] string role) + { + try + { + var user = await _identityDbContext.Users.FindAsync(id); + + if (user == null) + { + return NotFound(new { message = "User not found" }); + } + + var existingUserRoles = await _userManager.GetRolesAsync(user); + if (existingUserRoles != null && existingUserRoles.Count > 0) { + await _userManager.RemoveFromRolesAsync(user, existingUserRoles); + } + + await _userManager.AddToRoleAsync(user, role); + + user.UserInfoStatus = 1; + await _identityDbContext.SaveChangesAsync(); + + return Ok(new { message = "User updated successfully" }); + } + catch (Exception ex) + { + return StatusCode(500, $"An error occurred: {ex.Message}"); + } + } + + [HttpPost("GetRoleList")] + public async Task GetRoleList() + { + var roles = await _roleManager.Roles.Select(r => new { r.Id, r.Name }).Where(r => r.Name != "SuperAdmin" && r.Name != "SystemAdmin").ToListAsync(); + return Json(roles); + } + [HttpPost("GetDepartmentWithCompanyList")] public async Task GetDepartmentWithCompanyList() { @@ -160,12 +201,13 @@ namespace PSTW_CentralSystem.Controllers.API DepartmentId = departmentList!.DepartmentId, DepartmentName = departmentList.DepartmentName, CompanyId = departmentList.CompanyId, - CompanyName = companyList?.CompanyName + CompanyName = companyList?.CompanyName, }; // Return the constructed list as JSON return departmentWithCompany; } + [HttpPost("AddCompanyDepartment")] public async Task AddCompanyDepartment([FromBody] UpdateDepartmentCompany departmentCompanyDetails) { diff --git a/Controllers/API/IdentityAPI.cs b/Controllers/API/IdentityAPI.cs index 0334965..dc9204f 100644 --- a/Controllers/API/IdentityAPI.cs +++ b/Controllers/API/IdentityAPI.cs @@ -62,11 +62,6 @@ namespace PSTW_CentralSystem.Controllers.API return StatusCode(500, $"An error occurred: {ex.Message}"); } } - public class LdapLoginCredential - { - public required string username { get; set; } - public required string password { get; set; } - } [HttpPost("LdapLogin")] public async Task LdapLogin([FromBody] LdapLoginCredential ldapLoginInfo) @@ -140,7 +135,7 @@ namespace PSTW_CentralSystem.Controllers.API await _signInManager.SignInAsync(existUser, false); - if (existUser.UserStatus == null || existUser.UserStatus != 0) + if (existUser.UserInfoStatus == null || existUser.UserInfoStatus != 0) { return Ok(new { RedirectUrl = Url.Action("ComDeptAssignment", "Identity") }); } @@ -190,10 +185,23 @@ namespace PSTW_CentralSystem.Controllers.API #endregion Department - [HttpPost("UserComptDeptAssignment")] - public async Task UserComptDeptAssignment([FromBody] UserModel userModel) + [HttpPost("UserComptDeptAssignment/{id}")] + public async Task UserComptDeptAssignment([FromBody] UserDeptAssignment userDeptAssignment, int id) { - return Ok(); + var deptId = userDeptAssignment.departmentId; + if (deptId <= 0) + { + return BadRequest(new { message = "Invalid department ID provided." }); + } + var user = await _userManager.FindByIdAsync(id.ToString()); + if(user == null) + { + return NotFound( new { message = $"Unable to load user with ID '{id}'." }); + } + user.UserInfoStatus = 0; + user.departmentId = deptId; + await _userManager.UpdateAsync(user); + return Ok( new { message = "User updated successfully", RedirectUrl = Url.Action("Index", "Home") }); } public async Task doUserExists(string username) @@ -202,6 +210,15 @@ namespace PSTW_CentralSystem.Controllers.API return user != null ? user : null; } + public class LdapLoginCredential + { + public required string username { get; set; } + public required string password { get; set; } + } + public class UserDeptAssignment() + { + public required int departmentId { get; set; } + } class userLdapInfo() { public required bool Authenticated { get; set; } diff --git a/DBContext/IdentityDBContext.cs b/DBContext/IdentityDBContext.cs index e9598ea..821e617 100644 --- a/DBContext/IdentityDBContext.cs +++ b/DBContext/IdentityDBContext.cs @@ -52,7 +52,7 @@ namespace PSTW_CentralSystem.DBContext NormalizedEmail = "ADMIN@PSTW.COM.MY", SecurityStamp = Guid.NewGuid().ToString(), EmailConfirmed = true, - UserStatus = 1, + UserInfoStatus = 1, }; var systemAdmin = new UserModel { @@ -64,7 +64,7 @@ namespace PSTW_CentralSystem.DBContext NormalizedEmail = "SYSADMIN@PSTW.COM.MY", SecurityStamp = Guid.NewGuid().ToString(), EmailConfirmed = true, - UserStatus = 1, + UserInfoStatus = 1, }; // Hash the password diff --git a/Migrations/20241206015840_Initiate.Designer.cs b/Migrations/20241218060528_Initiate.Designer.cs similarity index 95% rename from Migrations/20241206015840_Initiate.Designer.cs rename to Migrations/20241218060528_Initiate.Designer.cs index d9bccc1..8467a4a 100644 --- a/Migrations/20241206015840_Initiate.Designer.cs +++ b/Migrations/20241218060528_Initiate.Designer.cs @@ -12,7 +12,7 @@ using PSTW_CentralSystem.DBContext; namespace PSTW_CentralSystem.Migrations { [DbContext(typeof(IdentityDBContext))] - [Migration("20241206015840_Initiate")] + [Migration("20241218060528_Initiate")] partial class Initiate { /// @@ -329,13 +329,13 @@ namespace PSTW_CentralSystem.Migrations b.Property("TwoFactorEnabled") .HasColumnType("tinyint(1)"); + b.Property("UserInfoStatus") + .HasColumnType("int"); + b.Property("UserName") .HasMaxLength(256) .HasColumnType("varchar(256)"); - b.Property("UserStatus") - .HasColumnType("int"); - b.Property("departmentId") .HasColumnType("int"); @@ -357,34 +357,36 @@ namespace PSTW_CentralSystem.Migrations { Id = 1, AccessFailedCount = 0, - ConcurrencyStamp = "b58597b6-2835-4ff6-b437-11ec7a213434", + ConcurrencyStamp = "f89f9499-14a5-4bba-a003-5bbb0ef1bb12", Email = "admin@pstw.com.my", EmailConfirmed = true, FullName = "MAAdmin", LockoutEnabled = false, NormalizedEmail = "ADMIN@PSTW.COM.MY", NormalizedUserName = "ADMIN@PSTW.COM.MY", - PasswordHash = "AQAAAAIAAYagAAAAECA03al9kGFTKlmmTls3wiH4NV7HlL76680Qx6lR7d77LHJwIN6/Wt1MBCP9TE1qfg==", + PasswordHash = "AQAAAAIAAYagAAAAEDue4k8/8FwBvdJbbgBDLH+ibzmThXls6CmbJd99AdlrbPZrOWvWxlkv7cwVsPSM9g==", PhoneNumberConfirmed = false, - SecurityStamp = "0f649a27-6566-436c-bc27-d1a6b8e7f846", + SecurityStamp = "d5684375-c368-409a-82e1-1e44fa05de60", TwoFactorEnabled = false, + UserInfoStatus = 1, UserName = "admin@pstw.com.my" }, new { Id = 2, AccessFailedCount = 0, - ConcurrencyStamp = "cf9424fc-8afc-4f59-a0a6-34574676273c", + ConcurrencyStamp = "d19f378c-eef5-4cf7-8ec6-c6b3904e4749", Email = "sysadmin@pstw.com.my", EmailConfirmed = true, FullName = "SysAdmin", LockoutEnabled = false, NormalizedEmail = "SYSADMIN@PSTW.COM.MY", NormalizedUserName = "SYSADMIN@PSTW.COM.MY", - PasswordHash = "AQAAAAIAAYagAAAAEMkwXv250FjOjdLEAY2a/aEF3g3iu9xCVORV/MH37kVcj8vgJez+LlfJtjklaschLg==", + PasswordHash = "AQAAAAIAAYagAAAAEDme/kiOHre+s0r9XvpwSr5dZIoWIbSJhI5B19mCjH4ZFoBBlF6Pay9WYJ2jcVZgfw==", PhoneNumberConfirmed = false, - SecurityStamp = "8efe9683-78fb-404d-96a9-b8b7302c03b2", + SecurityStamp = "7875eac4-323d-43cb-a083-940e52877171", TwoFactorEnabled = false, + UserInfoStatus = 1, UserName = "sysadmin@pstw.com.my" }); }); diff --git a/Migrations/20241206015840_Initiate.cs b/Migrations/20241218060528_Initiate.cs similarity index 95% rename from Migrations/20241206015840_Initiate.cs rename to Migrations/20241218060528_Initiate.cs index 8499d27..8b7fa5b 100644 --- a/Migrations/20241206015840_Initiate.cs +++ b/Migrations/20241218060528_Initiate.cs @@ -131,7 +131,7 @@ namespace PSTW_CentralSystem.Migrations .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn), FullName = table.Column(type: "longtext", nullable: true) .Annotation("MySql:CharSet", "utf8mb4"), - UserStatus = table.Column(type: "int", nullable: true), + UserInfoStatus = table.Column(type: "int", nullable: true), departmentId = table.Column(type: "int", nullable: true), UserName = table.Column(type: "varchar(256)", maxLength: 256, nullable: true) .Annotation("MySql:CharSet", "utf8mb4"), @@ -277,11 +277,11 @@ namespace PSTW_CentralSystem.Migrations migrationBuilder.InsertData( table: "AspNetUsers", - columns: new[] { "Id", "AccessFailedCount", "ConcurrencyStamp", "Email", "EmailConfirmed", "FullName", "LockoutEnabled", "LockoutEnd", "NormalizedEmail", "NormalizedUserName", "PasswordHash", "PhoneNumber", "PhoneNumberConfirmed", "SecurityStamp", "TwoFactorEnabled", "UserName", "UserStatus", "departmentId" }, + columns: new[] { "Id", "AccessFailedCount", "ConcurrencyStamp", "Email", "EmailConfirmed", "FullName", "LockoutEnabled", "LockoutEnd", "NormalizedEmail", "NormalizedUserName", "PasswordHash", "PhoneNumber", "PhoneNumberConfirmed", "SecurityStamp", "TwoFactorEnabled", "UserInfoStatus", "UserName", "departmentId" }, values: new object[,] { - { 1, 0, "b58597b6-2835-4ff6-b437-11ec7a213434", "admin@pstw.com.my", true, "MAAdmin", false, null, "ADMIN@PSTW.COM.MY", "ADMIN@PSTW.COM.MY", "AQAAAAIAAYagAAAAECA03al9kGFTKlmmTls3wiH4NV7HlL76680Qx6lR7d77LHJwIN6/Wt1MBCP9TE1qfg==", null, false, "0f649a27-6566-436c-bc27-d1a6b8e7f846", false, "admin@pstw.com.my", null, null }, - { 2, 0, "cf9424fc-8afc-4f59-a0a6-34574676273c", "sysadmin@pstw.com.my", true, "SysAdmin", false, null, "SYSADMIN@PSTW.COM.MY", "SYSADMIN@PSTW.COM.MY", "AQAAAAIAAYagAAAAEMkwXv250FjOjdLEAY2a/aEF3g3iu9xCVORV/MH37kVcj8vgJez+LlfJtjklaschLg==", null, false, "8efe9683-78fb-404d-96a9-b8b7302c03b2", false, "sysadmin@pstw.com.my", null, null } + { 1, 0, "f89f9499-14a5-4bba-a003-5bbb0ef1bb12", "admin@pstw.com.my", true, "MAAdmin", false, null, "ADMIN@PSTW.COM.MY", "ADMIN@PSTW.COM.MY", "AQAAAAIAAYagAAAAEDue4k8/8FwBvdJbbgBDLH+ibzmThXls6CmbJd99AdlrbPZrOWvWxlkv7cwVsPSM9g==", null, false, "d5684375-c368-409a-82e1-1e44fa05de60", false, 1, "admin@pstw.com.my", null }, + { 2, 0, "d19f378c-eef5-4cf7-8ec6-c6b3904e4749", "sysadmin@pstw.com.my", true, "SysAdmin", false, null, "SYSADMIN@PSTW.COM.MY", "SYSADMIN@PSTW.COM.MY", "AQAAAAIAAYagAAAAEDme/kiOHre+s0r9XvpwSr5dZIoWIbSJhI5B19mCjH4ZFoBBlF6Pay9WYJ2jcVZgfw==", null, false, "7875eac4-323d-43cb-a083-940e52877171", false, 1, "sysadmin@pstw.com.my", null } }); migrationBuilder.InsertData( diff --git a/Migrations/IdentityDBContextModelSnapshot.cs b/Migrations/IdentityDBContextModelSnapshot.cs index 976a39f..e17e091 100644 --- a/Migrations/IdentityDBContextModelSnapshot.cs +++ b/Migrations/IdentityDBContextModelSnapshot.cs @@ -326,13 +326,13 @@ namespace PSTW_CentralSystem.Migrations b.Property("TwoFactorEnabled") .HasColumnType("tinyint(1)"); + b.Property("UserInfoStatus") + .HasColumnType("int"); + b.Property("UserName") .HasMaxLength(256) .HasColumnType("varchar(256)"); - b.Property("UserStatus") - .HasColumnType("int"); - b.Property("departmentId") .HasColumnType("int"); @@ -354,34 +354,36 @@ namespace PSTW_CentralSystem.Migrations { Id = 1, AccessFailedCount = 0, - ConcurrencyStamp = "b58597b6-2835-4ff6-b437-11ec7a213434", + ConcurrencyStamp = "f89f9499-14a5-4bba-a003-5bbb0ef1bb12", Email = "admin@pstw.com.my", EmailConfirmed = true, FullName = "MAAdmin", LockoutEnabled = false, NormalizedEmail = "ADMIN@PSTW.COM.MY", NormalizedUserName = "ADMIN@PSTW.COM.MY", - PasswordHash = "AQAAAAIAAYagAAAAECA03al9kGFTKlmmTls3wiH4NV7HlL76680Qx6lR7d77LHJwIN6/Wt1MBCP9TE1qfg==", + PasswordHash = "AQAAAAIAAYagAAAAEDue4k8/8FwBvdJbbgBDLH+ibzmThXls6CmbJd99AdlrbPZrOWvWxlkv7cwVsPSM9g==", PhoneNumberConfirmed = false, - SecurityStamp = "0f649a27-6566-436c-bc27-d1a6b8e7f846", + SecurityStamp = "d5684375-c368-409a-82e1-1e44fa05de60", TwoFactorEnabled = false, + UserInfoStatus = 1, UserName = "admin@pstw.com.my" }, new { Id = 2, AccessFailedCount = 0, - ConcurrencyStamp = "cf9424fc-8afc-4f59-a0a6-34574676273c", + ConcurrencyStamp = "d19f378c-eef5-4cf7-8ec6-c6b3904e4749", Email = "sysadmin@pstw.com.my", EmailConfirmed = true, FullName = "SysAdmin", LockoutEnabled = false, NormalizedEmail = "SYSADMIN@PSTW.COM.MY", NormalizedUserName = "SYSADMIN@PSTW.COM.MY", - PasswordHash = "AQAAAAIAAYagAAAAEMkwXv250FjOjdLEAY2a/aEF3g3iu9xCVORV/MH37kVcj8vgJez+LlfJtjklaschLg==", + PasswordHash = "AQAAAAIAAYagAAAAEDme/kiOHre+s0r9XvpwSr5dZIoWIbSJhI5B19mCjH4ZFoBBlF6Pay9WYJ2jcVZgfw==", PhoneNumberConfirmed = false, - SecurityStamp = "8efe9683-78fb-404d-96a9-b8b7302c03b2", + SecurityStamp = "7875eac4-323d-43cb-a083-940e52877171", TwoFactorEnabled = false, + UserInfoStatus = 1, UserName = "sysadmin@pstw.com.my" }); }); diff --git a/Migrations/InventoryDB/20241206071642_Initiate.Designer.cs b/Migrations/InventoryDB/20241206071642_Initiate.Designer.cs index b6af9a5..288c958 100644 --- a/Migrations/InventoryDB/20241206071642_Initiate.Designer.cs +++ b/Migrations/InventoryDB/20241206071642_Initiate.Designer.cs @@ -300,7 +300,7 @@ namespace PSTW_CentralSystem.Migrations.InventoryDB b.Property("UserName") .HasColumnType("longtext"); - b.Property("UserStatus") + b.Property("UserInfoStatus") .HasColumnType("int"); b.Property("departmentId") diff --git a/Migrations/InventoryDB/InventoryDBContextModelSnapshot.cs b/Migrations/InventoryDB/InventoryDBContextModelSnapshot.cs index fc29eea..34a483e 100644 --- a/Migrations/InventoryDB/InventoryDBContextModelSnapshot.cs +++ b/Migrations/InventoryDB/InventoryDBContextModelSnapshot.cs @@ -297,7 +297,7 @@ namespace PSTW_CentralSystem.Migrations.InventoryDB b.Property("UserName") .HasColumnType("longtext"); - b.Property("UserStatus") + b.Property("UserInfoStatus") .HasColumnType("int"); b.Property("departmentId") diff --git a/Models/UserModel.cs b/Models/UserModel.cs index 886321e..b032721 100644 --- a/Models/UserModel.cs +++ b/Models/UserModel.cs @@ -8,7 +8,7 @@ namespace PSTW_CentralSystem.Models { // Add custom properties public string? FullName { get; set; } - public int? UserStatus { get; set; } + public int? UserInfoStatus { get; set; } public int? departmentId { get; set; } [ForeignKey("departmentId")] public virtual DepartmentModel? Department { get; set; } diff --git a/Views/Admin/CompDeptAdmin.cshtml b/Views/Admin/CompDeptAdmin.cshtml index 5b69477..9575c56 100644 --- a/Views/Admin/CompDeptAdmin.cshtml +++ b/Views/Admin/CompDeptAdmin.cshtml @@ -150,6 +150,7 @@ departmentName: '', departmentCode: '', }], + compDeptDatatable: null, }; }, watch: { @@ -173,6 +174,9 @@ .then(data => { console.log(data) this.compDeptList = data ? data : []; + if (this.compDeptDatatable != null) { + this.compDeptDatatable.clear().destroy(); + } this.initiateTable(); }) .catch(error => { @@ -231,9 +235,14 @@ }); }); }); - this.itemDatatable = $('#userDatatable').DataTable({ + this.compDeptDatatable = $('#userDatatable').DataTable({ "data": flatData, "columns": [ + { + "title": "Company Name", + "data": "companyName", + "visible": false, + }, { "title": "Department Name", "data": "departmentName", @@ -255,6 +264,7 @@ "dataSrc": "companyName" // Group rows by the companyName column }, responsive: true, + ordering: false, }) // Attach click event listener to the delete buttons @@ -306,6 +316,9 @@ alert(error.message); } finally { + await new Promise(resolve => { + $('#loadingModal').on('shown.bs.modal', resolve); + }); $('#loadingModal').modal('hide'); } }, @@ -327,13 +340,10 @@ $(function () { app.mount('#app'); - // Attach a click event listener to elements with the class 'btn-success'. $('#addCompDeptBtn').on('click', function () { - // Show the modal with the ID 'addManufacturerModal'. $('#registerCompDept').modal('show'); }); $('.closeModal').on('click', function () { - // Show the modal with the ID 'addManufacturerModal'. $('.modal').modal('hide'); }); }); diff --git a/Views/Admin/UserAdmin.cshtml b/Views/Admin/UserAdmin.cshtml index 58e4148..9051bd5 100644 --- a/Views/Admin/UserAdmin.cshtml +++ b/Views/Admin/UserAdmin.cshtml @@ -14,10 +14,10 @@
-

Latest Posts

+

User List

-
+
@@ -54,25 +54,51 @@ await Html.RenderPartialAsync("_ValidationScriptsPartial"); } } diff --git a/Views/Identity/ComDeptAssignment.cshtml b/Views/Identity/ComDeptAssignment.cshtml index 21eec61..a5bf1e8 100644 --- a/Views/Identity/ComDeptAssignment.cshtml +++ b/Views/Identity/ComDeptAssignment.cshtml @@ -23,7 +23,7 @@

User ID: @Model?.Id

User Name: @Model?.UserName

-
+
@@ -42,6 +42,11 @@
+
+
+ +
+
@@ -68,7 +73,6 @@ }; }, mounted() { - this.fetchModule(); this.fetchCompanies(); }, computed: { @@ -104,34 +108,25 @@ console.error('Error fetching products:', error); } }, - fetchModule() { - fetch('/AdminAPI/GetUserList', { - method: 'POST' - }) - .then(response => response.json()) - .then(data => { - if (data.length > 0) { - this.userList = data.length ? data : []; - } - this.initiateTable(); - }) - .catch(error => { - console.error('There was a problem with the fetch operation:', error); - }); - }, async updateInfo() { + if (!this.selectedDepartment) { + alert("Please select a valid department."); + return; + } const uid = @Model?.Id; try { // Show the loading modal $('#loadingModal').modal('show'); - + const payload = { + departmentId: this.selectedDepartment, + }; // Perform the fetch request const response = await fetch(`/IdentityAPI/UserComptDeptAssignment/${uid}`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, - body: JSON.stringify(this.selectedDepartment), + body: JSON.stringify(payload), }); // Check if the response is OK @@ -141,15 +136,18 @@ } const data = await response.json(); - this.fetchCompDept(); - $('#registerCompDept').modal('hide'); + window.location.href = data.redirectUrl; alert("Company and department successfully added"); } catch (error) { console.error('Failed to add company and department:', error); - alert(error.message); + const message = error.message || "An unexpected error occurred. Please try again."; + alert(message); } finally { + await new Promise(resolve => { + $('#loadingModal').on('shown.bs.modal', resolve); + }); $('#loadingModal').modal('hide'); } }, diff --git a/Views/Shared/_Layout.cshtml b/Views/Shared/_Layout.cshtml index 25d73ed..4a6e093 100644 --- a/Views/Shared/_Layout.cshtml +++ b/Views/Shared/_Layout.cshtml @@ -73,6 +73,11 @@