Added new API to get employee list
This commit is contained in:
parent
38e1f7a0fc
commit
162d735d16
@ -120,6 +120,46 @@ namespace MarcoBMS.Services.Controllers
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpGet("list/project/{projectId}")]
|
||||||
|
public async Task<IActionResult> GetEmployeesByProjectAsync(Guid projectId, [FromQuery] string searchString)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Get the currently logged-in employee information
|
||||||
|
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
||||||
|
|
||||||
|
// Check if the logged-in employee has permission for the requested project
|
||||||
|
var hasProjectPermission = await _permission.HasProjectPermission(loggedInEmployee, projectId);
|
||||||
|
if (!hasProjectPermission)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("User {EmployeeId} attempts to get employees for project {ProjectId} without permission", loggedInEmployee.Id, projectId);
|
||||||
|
return StatusCode(403, ApiResponse<object>.ErrorResponse("Access denied", "User does not have access to view the employees for this project", 403));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch employees allocated to the project matching the search criteria
|
||||||
|
var employees = await _context.ProjectAllocations
|
||||||
|
.AsNoTracking() // Improves performance by disabling change tracking for read-only query
|
||||||
|
.Include(pa => pa.Employee)
|
||||||
|
.ThenInclude(e => e!.JobRole)
|
||||||
|
.Where(pa => pa.ProjectId == projectId && pa.Employee != null &&
|
||||||
|
(pa.Employee.FirstName + " " + pa.Employee.LastName).Contains(searchString))
|
||||||
|
.Select(pa => pa.Employee!)
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
_logger.LogInfo("Employees fetched for project {ProjectId} by user {EmployeeId}. Count: {Count}", projectId, loggedInEmployee.Id, employees.Count);
|
||||||
|
|
||||||
|
// Return the employee list wrapped in a successful API response
|
||||||
|
return Ok(ApiResponse<object>.SuccessResponse(employees, "Employee list fetched successfully", 200));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// Log the exception and return a 500 status code with error message
|
||||||
|
_logger.LogError(ex, "Error occurred while fetching employees for project {ProjectId}", projectId);
|
||||||
|
return StatusCode(500, ApiResponse<object>.ErrorResponse("Internal server error", "An unexpected error occurred", 500));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
[HttpGet("list/{projectId?}")]
|
[HttpGet("list/{projectId?}")]
|
||||||
public async Task<IActionResult> GetEmployeesByProjectAsync(Guid? projectId, [FromQuery] bool showInactive = false)
|
public async Task<IActionResult> GetEmployeesByProjectAsync(Guid? projectId, [FromQuery] bool showInactive = false)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user