Compare commits

...

5 Commits

View File

@ -894,6 +894,8 @@ namespace Marco.Pms.Services.Service
return ApiResponse<object>.ErrorResponse("Access Denied.", "You do not have permission to view this project's team.", 403); return ApiResponse<object>.ErrorResponse("Access Denied.", "You do not have permission to view this project's team.", 403);
} }
var jobRoles = await _context.JobRoles.AsNoTracking().Where(jr => jr.TenantId == tenantId).ToListAsync();
// --- Step 3: Execute a Single, Optimized Database Query --- // --- Step 3: Execute a Single, Optimized Database Query ---
// This query projects directly to a new object on the database server, which is highly efficient. // This query projects directly to a new object on the database server, which is highly efficient.
var projectAllocationQuery = _context.ProjectAllocations var projectAllocationQuery = _context.ProjectAllocations
@ -924,7 +926,10 @@ namespace Marco.Pms.Services.Service
} }
var allocations = await projectAllocationQuery var allocations = await projectAllocationQuery
.Where(pa => pa.Service != null) .Where(pa => pa.Service != null && pa.Employee != null)
.ToListAsync();
var response = allocations
.Select(pa => new .Select(pa => new
{ {
// Fields from ProjectAllocation // Fields from ProjectAllocation
@ -947,13 +952,13 @@ namespace Marco.Pms.Services.Service
ServiceName = pa.Service!.Name, ServiceName = pa.Service!.Name,
// Simplified JobRoleId logic: Use the allocation's role if it exists, otherwise fall back to the employee's default role. // Simplified JobRoleId logic: Use the allocation's role if it exists, otherwise fall back to the employee's default role.
JobRoleId = pa.JobRoleId ?? pa.Employee.JobRoleId JobRoleId = pa.JobRoleId ?? pa.Employee.JobRoleId,
}) JobRoleName = jobRoles.Where(jr => jr.Id == (pa.JobRoleId ?? pa.Employee.JobRoleId)).Select(jr => jr.Name).FirstOrDefault() ?? pa.Employee.JobRole!.Name
.ToListAsync(); }).ToList();
_logger.LogInfo("Successfully fetched {AllocationCount} allocations for project {ProjectId}.", allocations.Count, projectId); _logger.LogInfo("Successfully fetched {AllocationCount} allocations for project {ProjectId}.", response.Count, projectId);
return ApiResponse<object>.SuccessResponse(allocations, "Project allocations retrieved successfully.", 200); return ApiResponse<object>.SuccessResponse(response, "Project allocations retrieved successfully.", 200);
} }
catch (Exception ex) catch (Exception ex)
{ {