Merge pull request 'Ashutosh_Enhancement#198_Added_IsSystem' (#39) from Ashutosh_Enhancement#198_Added_IsSystem into Issues_May_2W

Reviewed-on: #39
This commit is contained in:
Vikas Nale 2025-05-08 10:22:14 +00:00
commit 141d001673
12 changed files with 2684 additions and 101 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,40 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Marco.Pms.DataAccess.Migrations
{
/// <inheritdoc />
public partial class Added_IsSystem_Flag : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<bool>(
name: "IsSystem",
table: "Employees",
type: "tinyint(1)",
nullable: false,
defaultValue: false);
migrationBuilder.AddColumn<bool>(
name: "IsSystem",
table: "ApplicationRoles",
type: "tinyint(1)",
nullable: false,
defaultValue: false);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "IsSystem",
table: "Employees");
migrationBuilder.DropColumn(
name: "IsSystem",
table: "ApplicationRoles");
}
}
}

View File

@ -342,6 +342,9 @@ namespace Marco.Pms.DataAccess.Migrations
b.Property<bool>("IsActive") b.Property<bool>("IsActive")
.HasColumnType("tinyint(1)"); .HasColumnType("tinyint(1)");
b.Property<bool>("IsSystem")
.HasColumnType("tinyint(1)");
b.Property<Guid?>("JobRoleId") b.Property<Guid?>("JobRoleId")
.HasColumnType("char(36)"); .HasColumnType("char(36)");
@ -468,6 +471,9 @@ namespace Marco.Pms.DataAccess.Migrations
b.Property<string>("Description") b.Property<string>("Description")
.HasColumnType("longtext"); .HasColumnType("longtext");
b.Property<bool>("IsSystem")
.HasColumnType("tinyint(1)");
b.Property<string>("Role") b.Property<string>("Role")
.HasColumnType("longtext"); .HasColumnType("longtext");

View File

@ -39,6 +39,8 @@ namespace Marco.Pms.Model.Employees
public bool IsActive { get; set; } = true; public bool IsActive { get; set; } = true;
public bool IsSystem { get; set; } = false;
public Guid RoleId { get; set; } public Guid RoleId { get; set; }
//[ForeignKey(nameof(RoleId))] //[ForeignKey(nameof(RoleId))]
//public EmployeeRole EmployeeRole { get; set; } //public EmployeeRole EmployeeRole { get; set; }

View File

@ -5,6 +5,7 @@
public Guid Id { get; set; } public Guid Id { get; set; }
public string? Role { get; set; } public string? Role { get; set; }
public string? Description { get; set; } public string? Description { get; set; }
public bool IsSystem { get; set; } = false;
public Guid TenantId { get; set; } public Guid TenantId { get; set; }

View File

@ -12,6 +12,7 @@ namespace Marco.Pms.Model.Mapper
{ {
Id = model.Id, Id = model.Id,
Role = model.Role, Role = model.Role,
IsSystem = model.IsSystem,
Description = model.Description, Description = model.Description,
//FeaturePermission = model.FeaturePermissions //FeaturePermission = model.FeaturePermissions
}; };

View File

@ -29,6 +29,7 @@ namespace Marco.Pms.Model.Mapper
PhoneNumber = model.PhoneNumber, PhoneNumber = model.PhoneNumber,
Photo = model.Photo, Photo = model.Photo,
IsActive = model.IsActive, IsActive = model.IsActive,
IsSystem = model.IsSystem,
JoiningDate = model.JoiningDate JoiningDate = model.JoiningDate
}; };
} }

View File

@ -5,6 +5,7 @@
public Guid Id { get; set; } public Guid Id { get; set; }
public string? Role { get; set; } public string? Role { get; set; }
public string? Description { get; set; } public string? Description { get; set; }
public bool IsSystem { get; set; }
public ICollection<FeaturePermissionVM>? FeaturePermission { get; set; } public ICollection<FeaturePermissionVM>? FeaturePermission { get; set; }
} }
} }

View File

@ -28,6 +28,7 @@
public string? ApplicationUserId { get; set; } public string? ApplicationUserId { get; set; }
public Guid? JobRoleId { get; set; } public Guid? JobRoleId { get; set; }
public bool IsSystem { get; set; }
public string? JobRole { get; set; } public string? JobRole { get; set; }
} }

View File

@ -85,50 +85,7 @@ namespace MarcoBMS.Services.Controllers
} }
} }
[HttpPost]
[Route("roles")]
public async Task<IActionResult> ManageRoles([FromBody] List<EmployeeRoleDot> employeeRoleDots)
{
if (!ModelState.IsValid)
{
var errors = ModelState.Values
.SelectMany(v => v.Errors)
.Select(e => e.ErrorMessage)
.ToList();
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid data", errors, 400));
}
Guid TenantId = GetTenantId();
try
{
foreach (EmployeeRoleDot role in employeeRoleDots)
{
EmployeeRoleMapping mapping = role.ToEmployeeRoleMappingFromEmployeeRoleDot(TenantId);
var existingItem = await _context.EmployeeRoleMappings.AsNoTracking().SingleOrDefaultAsync(c => c.Id == mapping.Id);
if (existingItem == null)
{
if (role.IsEnabled == true)
{
_context.EmployeeRoleMappings.Add(mapping);
}
}
else if (role.IsEnabled == false)
{
_context.EmployeeRoleMappings.Remove(existingItem);
}
}
await _context.SaveChangesAsync();
}
catch (Exception ex)
{
return BadRequest(ApiResponse<object>.ErrorResponse(ex.Message, ex, 400));
}
return Ok(ApiResponse<object>.SuccessResponse("success", "Roles modified.", 200));
}
[HttpGet] [HttpGet]
[Route("list/{projectid?}")] [Route("list/{projectid?}")]
@ -323,8 +280,16 @@ namespace MarcoBMS.Services.Controllers
public async Task<IActionResult> SuspendEmployee(Guid id) public async Task<IActionResult> SuspendEmployee(Guid id)
{ {
Guid tenantId = _userHelper.GetTenantId(); Guid tenantId = _userHelper.GetTenantId();
Employee? employee = await _context.Employees.FirstOrDefaultAsync(e => e.Id == id && e.TenantId == tenantId); var LoggedEmployee = await _userHelper.GetCurrentEmployeeAsync();
Employee? employee = await _context.Employees.FirstOrDefaultAsync(e => e.Id == id && e.IsActive && e.TenantId == tenantId);
if (employee != null) if (employee != null)
{
if (employee.IsSystem)
{
_logger.LogWarning("Employee with ID {LoggedEmployeeId} tries to suspend system-defined employee with ID {EmployeeId}", LoggedEmployee.Id, employee.Id);
return BadRequest(ApiResponse<object>.ErrorResponse("System-defined employees cannot be suspended.", "System-defined employees cannot be suspended.", 400));
}
else
{ {
var assignedToTasks = await _context.TaskMembers.Where(t => t.EmployeeId == employee.Id).ToListAsync(); var assignedToTasks = await _context.TaskMembers.Where(t => t.EmployeeId == employee.Id).ToListAsync();
if (assignedToTasks.Count != 0) if (assignedToTasks.Count != 0)
@ -384,6 +349,7 @@ namespace MarcoBMS.Services.Controllers
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
_logger.LogInfo("Employee with ID {EmployeId} Deleted successfully", employee.Id); _logger.LogInfo("Employee with ID {EmployeId} Deleted successfully", employee.Id);
} }
}
else else
{ {
_logger.LogError("Employee with ID {EmploueeId} not found in database", id); _logger.LogError("Employee with ID {EmploueeId} not found in database", id);

View File

@ -1,11 +1,13 @@
using System.Data; using System.Data;
using Marco.Pms.DataAccess.Data; using Marco.Pms.DataAccess.Data;
using Marco.Pms.Model.Dtos.Employees;
using Marco.Pms.Model.Dtos.Roles; using Marco.Pms.Model.Dtos.Roles;
using Marco.Pms.Model.Entitlements; using Marco.Pms.Model.Entitlements;
using Marco.Pms.Model.Mapper; using Marco.Pms.Model.Mapper;
using Marco.Pms.Model.Utilities; using Marco.Pms.Model.Utilities;
using Marco.Pms.Model.ViewModels; using Marco.Pms.Model.ViewModels;
using MarcoBMS.Services.Helpers; using MarcoBMS.Services.Helpers;
using MarcoBMS.Services.Service;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@ -21,16 +23,16 @@ namespace MarcoBMS.Services.Controllers
private readonly ApplicationDbContext _context; private readonly ApplicationDbContext _context;
private readonly RolesHelper _rolesHelper; private readonly RolesHelper _rolesHelper;
private readonly UserHelper _userHelper; private readonly UserHelper _userHelper;
private readonly UserManager<ApplicationUser> _userManager; private readonly UserManager<ApplicationUser> _userManager;
private readonly ILoggingService _logger;
public RolesController(UserManager<ApplicationUser> userManager, ApplicationDbContext context, RolesHelper rolesHelper, UserHelper userHelper) public RolesController(UserManager<ApplicationUser> userManager, ApplicationDbContext context, RolesHelper rolesHelper, UserHelper userHelper, ILoggingService logger)
{ {
_context = context; _context = context;
_userManager = userManager; _userManager = userManager;
_rolesHelper = rolesHelper; _rolesHelper = rolesHelper;
_userHelper = userHelper; _userHelper = userHelper;
_logger = logger;
} }
private Guid GetTenantId() private Guid GetTenantId()
@ -99,7 +101,30 @@ namespace MarcoBMS.Services.Controllers
return BadRequest(ApiResponse<object>.ErrorResponse(ex.Message, ex, 400)); return BadRequest(ApiResponse<object>.ErrorResponse(ex.Message, ex, 400));
} }
} }
[HttpDelete("jobrole/{id}")]
public async Task<IActionResult> DeleteJobRole(Guid id)
{
Guid tenantId = GetTenantId();
var LoggedEmployee = await _userHelper.GetCurrentEmployeeAsync();
var jobRole = await _context.JobRoles.FirstOrDefaultAsync(r => r.Id == id && r.TenantId == tenantId);
if (jobRole != null)
{
var employee = await _context.Employees.Where(e => e.JobRoleId == jobRole.Id).ToListAsync();
if (employee.Any())
{
_logger.LogWarning("Employee with ID {LoggedEmployeeId} tries to delete the job role with ID {JobRoleId} which is assigned to one or more employees", LoggedEmployee.Id, jobRole.Id);
return BadRequest(ApiResponse<object>.ErrorResponse("Cannot delete the job role because it is assigned to one or more employees.", "Cannot delete the job role because it is assigned to one or more employees.", 400));
}
_context.JobRoles.Remove(jobRole);
await _context.SaveChangesAsync();
_logger.LogInfo("Employee with ID {LoggedEmployeeId} deleted the job role with ID {JobRoleId}", LoggedEmployee.Id, jobRole.Id);
}
else
{
_logger.LogWarning("Job role with ID {JobRoleId} not found in database", id);
}
return Ok(ApiResponse<object>.SuccessResponse(new { }, "Job role deleted successfully", 200));
}
[HttpGet] [HttpGet]
public async Task<IActionResult> GetAllRoles() public async Task<IActionResult> GetAllRoles()
@ -137,6 +162,7 @@ namespace MarcoBMS.Services.Controllers
{ {
Id = item.Id, Id = item.Id,
Role = item.Role, Role = item.Role,
IsSystem = item.IsSystem,
Description = item.Description, Description = item.Description,
FeaturePermission = [] FeaturePermission = []
}; };
@ -225,17 +251,21 @@ namespace MarcoBMS.Services.Controllers
try try
{ {
Guid TenantId = GetTenantId(); Guid TenantId = GetTenantId();
var LoggedEmployee = await _userHelper.GetCurrentEmployeeAsync();
var existingRole = await _context.ApplicationRoles.AsNoTracking().FirstOrDefaultAsync(r => r.Id == id);
if (existingRole != null && existingRole.IsSystem)
{
_logger.LogWarning("Employee with ID {LoggedEmployeeId} tries to update System-defined application roles {AppcationRoleId}", LoggedEmployee.Id, existingRole.Id);
return BadRequest(ApiResponse<object>.ErrorResponse("System-defined roles cannot be updated", "System-defined roles cannot be updated", 400));
}
ApplicationRole role = updateRoleDto.ToApplicationRoleFromUpdateDto(TenantId); ApplicationRole role = updateRoleDto.ToApplicationRoleFromUpdateDto(TenantId);
if (role.TenantId != TenantId) if (role.TenantId != TenantId)
return Unauthorized(ApiResponse<object>.ErrorResponse("You don't have any authority to update role", "You don't have any authority to update role", 401)); return Unauthorized(ApiResponse<object>.ErrorResponse("You don't have any authority to update role", "You don't have any authority to update role", 401));
if (existingRole != null)
var projectModel = _context.ApplicationRoles.Update(role);
if (projectModel == null)
{ {
return NotFound(ApiResponse<object>.ErrorResponse("Project not found", "Project not found", 404)); _context.ApplicationRoles.Update(role);
await _context.SaveChangesAsync();
} }
bool modified = false; bool modified = false;
@ -265,7 +295,13 @@ namespace MarcoBMS.Services.Controllers
} }
catch (Exception ex) catch (Exception ex)
{ {
return BadRequest(ApiResponse<object>.ErrorResponse(ex.Message, ex, 400)); var response = new
{
message = ex.Message,
detail = ex.StackTrace,
statusCode = StatusCodes.Status500InternalServerError
};
return BadRequest(ApiResponse<object>.ErrorResponse(ex.Message, response, 400));
} }
} }
@ -303,11 +339,101 @@ namespace MarcoBMS.Services.Controllers
{ {
Id = role.Id, Id = role.Id,
Role = role.Role, Role = role.Role,
IsSystem = role.IsSystem,
FeaturePermission = featurePermissions FeaturePermission = featurePermissions
}; };
return Ok(ApiResponse<object>.SuccessResponse(vm, "Roles Perimssions fetched successfully.", 200)); return Ok(ApiResponse<object>.SuccessResponse(vm, "Roles Perimssions fetched successfully.", 200));
} }
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteApplicationRole(Guid id)
{
Guid tenantId = GetTenantId();
var LoggedEmployee = await _userHelper.GetCurrentEmployeeAsync();
var role = await _context.ApplicationRoles.AsNoTracking().FirstOrDefaultAsync(r => r.Id == id && r.TenantId == tenantId);
if (role != null)
{
if (role.IsSystem)
{
_logger.LogInfo("Employee with ID {LoggedEmployeeId} tries to delete system-defined application role with ID {ApplicationRoleId}", LoggedEmployee.Id, role.Id);
return BadRequest(ApiResponse<object>.ErrorResponse("This role cannot be deleted because it is system-defined.", "This role cannot be deleted because it is system-defined.", 400));
}
var employeeRoleMapping = await _context.EmployeeRoleMappings.Where(erm => erm.RoleId == role.Id).ToListAsync();
if (employeeRoleMapping.Count != 0)
{
_logger.LogInfo("Employee with ID {LoggedEmployeeId} tries to delete application role with ID {ApplicationRoleId} with is assigned to an employee", LoggedEmployee.Id, role.Id);
return BadRequest(ApiResponse<object>.ErrorResponse("This role cannot be deleted because it is currently assigned to employees.", "This role cannot be deleted because it is currently assigned to employees.", 400));
}
_context.ApplicationRoles.Remove(role);
var rolePermissionMapping = await _context.RolePermissionMappings.Where(r => r.ApplicationRoleId == role.Id).ToListAsync();
if (rolePermissionMapping.Count != 0)
{
_context.RolePermissionMappings.RemoveRange(rolePermissionMapping);
_logger.LogInfo("All permissions assigned to the application role with ID {ApplicationRoleId} have been removed.", role.Id);
}
await _context.SaveChangesAsync();
_logger.LogInfo("Employee with ID {LoggedEmployeeId} deleted application role with ID {ApplicationRoleId}", LoggedEmployee.Id, role.Id);
}
else
{
_logger.LogWarning("Application role with ID {ApplicationRoleId} not found in database", id);
}
return Ok(ApiResponse<object>.SuccessResponse(new { }, "Application role is deleted successfully", 200));
}
[HttpPost]
[Route("assign-roles")]
public async Task<IActionResult> ManageRoles([FromBody] List<EmployeeRoleDot> employeeRoleDots)
{
if (!ModelState.IsValid)
{
var errors = ModelState.Values
.SelectMany(v => v.Errors)
.Select(e => e.ErrorMessage)
.ToList();
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid data", errors, 400));
}
var employeesIds = employeeRoleDots.Select(e => e.EmployeeId).Distinct().ToList();
var LoggedEmployee = await _userHelper.GetCurrentEmployeeAsync();
var employees = await _context.Employees.Where(e => employeesIds.Contains(e.Id)).ToListAsync();
Guid TenantId = GetTenantId();
try
{
foreach (EmployeeRoleDot role in employeeRoleDots)
{
var employee = employees.Find(e => e.Id == role.EmployeeId && e.IsSystem);
if (employee != null)
{
_logger.LogWarning("Employee with ID {LoggedEmployeeId} tries to assign or remove the application role to System-defined employee with ID {EmployeeId}", LoggedEmployee.Id, employee.Id);
return BadRequest(ApiResponse<object>.ErrorResponse("System-defined employee cannot have application roles assigned or removed.", "System-defined employee cannot have application roles assigned or removed.", 400));
}
EmployeeRoleMapping mapping = role.ToEmployeeRoleMappingFromEmployeeRoleDot(TenantId);
var existingItem = await _context.EmployeeRoleMappings.AsNoTracking().SingleOrDefaultAsync(c => c.Id == mapping.Id);
if (existingItem == null)
{
if (role.IsEnabled == true)
{
_context.EmployeeRoleMappings.Add(mapping);
}
}
else if (role.IsEnabled == false)
{
_context.EmployeeRoleMappings.Remove(existingItem);
}
}
await _context.SaveChangesAsync();
}
catch (Exception ex)
{
return BadRequest(ApiResponse<object>.ErrorResponse(ex.Message, ex, 400));
}
return Ok(ApiResponse<object>.SuccessResponse("success", "Roles modified.", 200));
}
} }
} }

View File

@ -63,6 +63,7 @@ namespace Marco.Pms.Services.Service
{ {
Role = "Super User", Role = "Super User",
Description = "Super User", Description = "Super User",
IsSystem = true,
TenantId = Guid.Parse("b3466e83-7e11-464c-b93a-daf047838b26") TenantId = Guid.Parse("b3466e83-7e11-464c-b93a-daf047838b26")
}; };
if (!await dbContext.ApplicationRoles.Where(a => a.Role == "Super User").AnyAsync()) if (!await dbContext.ApplicationRoles.Where(a => a.Role == "Super User").AnyAsync())
@ -93,6 +94,7 @@ namespace Marco.Pms.Services.Service
PhoneNumber = "9876543210", PhoneNumber = "9876543210",
Photo = null, // GetFileDetails(model.Photo).Result.FileData, Photo = null, // GetFileDetails(model.Photo).Result.FileData,
JobRoleId = jobRole != null ? jobRole.Id : Guid.Empty, JobRoleId = jobRole != null ? jobRole.Id : Guid.Empty,
IsSystem = true,
JoiningDate = Convert.ToDateTime("2000-04-20 10:11:17.588000"), JoiningDate = Convert.ToDateTime("2000-04-20 10:11:17.588000"),
}; };
if ((!await dbContext.Employees.Where(e => e.FirstName == "Admin").AnyAsync()) && (jobRole != null ? jobRole.Id : Guid.Empty) != Guid.Empty) if ((!await dbContext.Employees.Where(e => e.FirstName == "Admin").AnyAsync()) && (jobRole != null ? jobRole.Id : Guid.Empty) != Guid.Empty)