Added the AsNoTracking in GetCurrentEmployeeAsync and get permission Ids helper functions

This commit is contained in:
ashutosh.nehete 2025-09-29 10:18:44 +05:30
parent 27a2610388
commit 31da8a4402
3 changed files with 12 additions and 5 deletions

View File

@ -37,6 +37,7 @@ namespace MarcoBMS.Services.Helpers
// --- Step 1: Define the subquery using the main thread's context ---
// This is safe because the query is not executed yet.
var employeeRoleIdsQuery = _context.EmployeeRoleMappings
.AsNoTracking()
.Where(erm => erm.EmployeeId == EmployeeId && erm.IsEnabled)
.Select(erm => erm.RoleId);
@ -50,6 +51,7 @@ namespace MarcoBMS.Services.Helpers
// Now, re-create and execute the query using this new, isolated context.
var roleIds = await contextForCache.EmployeeRoleMappings
.AsNoTracking()
.Where(erm => erm.EmployeeId == EmployeeId && erm.IsEnabled)
.Select(erm => erm.RoleId)
.ToListAsync();
@ -73,9 +75,12 @@ namespace MarcoBMS.Services.Helpers
var roleIds = await employeeRoleIdsQuery.ToListAsync();
var permissionIds = await _context.RolePermissionMappings
.AsNoTracking()
.Where(rp => roleIds.Contains(rp.ApplicationRoleId)).Select(rp => rp.FeaturePermissionId).ToListAsync();
var permissions = await _context.FeaturePermissions.Include(f => f.Feature)
var permissions = await _context.FeaturePermissions
.AsNoTracking()
.Include(f => f.Feature)
.Where(fp => permissionIds.Contains(fp.Id))
.Distinct()
.ToListAsync();

View File

@ -36,7 +36,7 @@ namespace MarcoBMS.Services.Helpers
var tenantId = _httpContextAccessor.HttpContext?.User.FindFirst("TenantId")?.Value;
if (tenantId != null)
{
return await _context.Tenants.FirstOrDefaultAsync(t => t.Id == Guid.Parse(tenantId));
return await _context.Tenants.AsNoTracking().FirstOrDefaultAsync(t => t.Id == Guid.Parse(tenantId));
}
return null;
}
@ -54,7 +54,7 @@ namespace MarcoBMS.Services.Helpers
{
var user = await GetCurrentUserAsync();
if (user == null) return new Employee { };
var Employee = await _context.Employees.Include(e => e.JobRole).FirstOrDefaultAsync(e => e.ApplicationUserId == user.Id && e.IsActive);
var Employee = await _context.Employees.AsNoTracking().Include(e => e.JobRole).FirstOrDefaultAsync(e => e.ApplicationUserId == user.Id && e.IsActive);
return Employee ?? new Employee { };
}

View File

@ -79,6 +79,7 @@ namespace Marco.Pms.Services.Service
{
// Fetch permissions explicitly assigned to this employee in the project.
var projectLevelPermissionIds = await _context.ProjectLevelPermissionMappings
.AsNoTracking()
.Where(pl => pl.ProjectId == projectId.Value && pl.EmployeeId == employeeId)
.Select(pl => pl.PermissionId)
.ToListAsync();
@ -97,6 +98,7 @@ namespace Marco.Pms.Services.Service
// Get all feature permissions under those modules where the user didn't have explicit project-level grants.
var allOverriddenPermissions = await _context.FeaturePermissions
.AsNoTracking()
.Where(fp => projectLevelModuleIds.Contains(fp.FeatureId) &&
!projectLevelPermissionIds.Contains(fp.Id))
.Select(fp => fp.Id)
@ -138,12 +140,12 @@ namespace Marco.Pms.Services.Service
var hasPermission = await HasPermission(PermissionsMaster.ManageProject, employeeId);
if (hasPermission)
{
var projects = await _context.Projects.Where(c => c.TenantId == LoggedInEmployee.TenantId).ToListAsync();
var projects = await _context.Projects.AsNoTracking().Where(c => c.TenantId == LoggedInEmployee.TenantId).ToListAsync();
projectIds = projects.Select(p => p.Id).ToList();
}
else
{
var allocation = await _context.ProjectAllocations.Where(c => c.EmployeeId == employeeId && c.IsActive).ToListAsync();
var allocation = await _context.ProjectAllocations.AsNoTracking().Where(c => c.EmployeeId == employeeId && c.IsActive).ToListAsync();
if (!allocation.Any())
{
return false;