Added an API to get task by employeeId
This commit is contained in:
parent
9edc0e645e
commit
7555b73f02
@ -355,6 +355,25 @@ namespace MarcoBMS.Services.Controllers
|
||||
var response = await _projectServices.GetWorkItemsAsync(workAreaId, tenantId, loggedInEmployee);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
[HttpGet("tasks-employee/{employeeId}")]
|
||||
public async Task<IActionResult> GetTasksByEmployee(Guid employeeId, [FromQuery] DateTime? fromDate, DateTime? toDate)
|
||||
{
|
||||
// --- Step 1: Input Validation ---
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
var errors = ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList();
|
||||
_logger.LogWarning("Get Work Items by employeeId called with invalid model state \n Errors: {Errors}", string.Join(", ", errors));
|
||||
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid request data provided.", errors, 400));
|
||||
}
|
||||
|
||||
if (!toDate.HasValue) toDate = DateTime.UtcNow;
|
||||
if (!fromDate.HasValue) fromDate = toDate.Value.AddDays(-7);
|
||||
|
||||
// --- Step 2: Prepare data without I/O ---
|
||||
Employee loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
||||
var response = await _projectServices.GetTasksByEmployeeAsync(employeeId, fromDate.Value, toDate.Value, tenantId, loggedInEmployee);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
@ -897,23 +897,53 @@ namespace Marco.Pms.Services.Service
|
||||
|
||||
public async Task<ApiResponse<object>> GetProjectByEmployeeBasicAsync(Guid employeeId, Guid tenantId, Employee loggedInEmployee)
|
||||
{
|
||||
var projectAllocation = await _context.ProjectAllocations
|
||||
.Include(pa => pa.Project)
|
||||
.Include(pa => pa.Employee).ThenInclude(e => e!.JobRole)
|
||||
.Where(pa => pa.EmployeeId == employeeId && pa.TenantId == tenantId && pa.Project != null && pa.Employee != null)
|
||||
.Select(pa => new
|
||||
{
|
||||
ProjectName = pa.Project!.Name,
|
||||
ProjectShortName = pa.Project.ShortName,
|
||||
AssignedDate = pa.AllocationDate,
|
||||
RemovedDate = pa.ReAllocationDate,
|
||||
Designation = pa.Employee!.JobRole!.Name
|
||||
})
|
||||
.ToListAsync();
|
||||
// Log the start of the method execution with key input parameters
|
||||
_logger.LogInfo("Fetching projects for EmployeeId: {EmployeeId}, TenantId: {TenantId} by User: {UserId}",
|
||||
employeeId, tenantId, loggedInEmployee.Id);
|
||||
|
||||
return ApiResponse<object>.SuccessResponse(projectAllocation, $"{projectAllocation.Count} records provided employee assigned to projects fetched", 200);
|
||||
try
|
||||
{
|
||||
// Retrieve project allocations linked to the specified employee and tenant
|
||||
var projectAllocation = await _context.ProjectAllocations
|
||||
.AsNoTracking() // Optimization: no tracking since entities are not updated
|
||||
.Include(pa => pa.Project) // Include related Project data
|
||||
.Include(pa => pa.Employee).ThenInclude(e => e!.JobRole) // Include related Employee and their JobRole
|
||||
.Where(pa => pa.EmployeeId == employeeId
|
||||
&& pa.TenantId == tenantId
|
||||
&& pa.Project != null
|
||||
&& pa.Employee != null)
|
||||
.Select(pa => new
|
||||
{
|
||||
ProjectName = pa.Project!.Name,
|
||||
ProjectShortName = pa.Project.ShortName,
|
||||
AssignedDate = pa.AllocationDate,
|
||||
RemovedDate = pa.ReAllocationDate,
|
||||
Designation = pa.Employee!.JobRole!.Name
|
||||
})
|
||||
.ToListAsync();
|
||||
|
||||
// Log successful retrieval including count of records
|
||||
_logger.LogInfo("Successfully fetched {Count} projects for EmployeeId: {EmployeeId}",
|
||||
projectAllocation.Count, employeeId);
|
||||
|
||||
return ApiResponse<object>.SuccessResponse(
|
||||
projectAllocation,
|
||||
$"{projectAllocation.Count} project assignments fetched for employee.",
|
||||
200);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Log the exception with stack trace for debugging
|
||||
_logger.LogError(ex, "Error occurred while fetching projects for EmployeeId: {EmployeeId}, TenantId: {TenantId}",
|
||||
employeeId, tenantId);
|
||||
|
||||
return ApiResponse<object>.ErrorResponse(
|
||||
"An error occurred while fetching project assignments.",
|
||||
500);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region =================================================================== Project InfraStructure Get APIs ===================================================================
|
||||
@ -1044,6 +1074,83 @@ namespace Marco.Pms.Services.Service
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves tasks assigned to a specific employee within a date range for a tenant.
|
||||
/// </summary>
|
||||
/// <param name="employeeId">The ID of the employee to filter tasks.</param>
|
||||
/// <param name="fromDate">The start date to filter task assignments.</param>
|
||||
/// <param name="toDate">The end date to filter task assignments.</param>
|
||||
/// <param name="tenantId">The tenant ID to filter tasks.</param>
|
||||
/// <param name="loggedInEmployee">The employee requesting the data (for authorization/logging).</param>
|
||||
/// <returns>An ApiResponse containing the task details.</returns>
|
||||
public async Task<ApiResponse<object>> GetTasksByEmployeeAsync(Guid employeeId, DateTime fromDate, DateTime toDate, Guid tenantId, Employee loggedInEmployee)
|
||||
{
|
||||
_logger.LogInfo("Fetching tasks for EmployeeId: {EmployeeId} from {FromDate} to {ToDate} for TenantId: {TenantId}",
|
||||
employeeId, fromDate, toDate, tenantId);
|
||||
|
||||
try
|
||||
{
|
||||
// Query TaskMembers with related necessary fields in one projection to minimize DB calls and data size
|
||||
var taskData = await _context.TaskMembers
|
||||
.Where(tm => tm.EmployeeId == employeeId &&
|
||||
tm.TenantId == tenantId &&
|
||||
tm.TaskAllocation != null &&
|
||||
tm.TaskAllocation.AssignmentDate.Date >= fromDate.Date &&
|
||||
tm.TaskAllocation.AssignmentDate.Date <= toDate.Date)
|
||||
.Select(tm => new
|
||||
{
|
||||
AssignmentDate = tm.TaskAllocation!.AssignmentDate,
|
||||
PlannedTask = tm.TaskAllocation.PlannedTask,
|
||||
CompletedTask = tm.TaskAllocation.CompletedTask,
|
||||
ProjectId = tm.TaskAllocation.WorkItem!.WorkArea!.Floor!.Building!.ProjectId,
|
||||
BuildingName = tm.TaskAllocation.WorkItem.WorkArea.Floor.Building!.Name,
|
||||
FloorName = tm.TaskAllocation.WorkItem.WorkArea.Floor.FloorName,
|
||||
AreaName = tm.TaskAllocation.WorkItem.WorkArea.AreaName,
|
||||
ActivityName = tm.TaskAllocation.WorkItem.ActivityMaster!.ActivityName,
|
||||
ActivityUnit = tm.TaskAllocation.WorkItem.ActivityMaster.UnitOfMeasurement
|
||||
})
|
||||
.OrderByDescending(t => t.AssignmentDate)
|
||||
.ToListAsync();
|
||||
|
||||
_logger.LogInfo("Retrieved {TaskCount} tasks for EmployeeId: {EmployeeId}", taskData.Count, employeeId);
|
||||
|
||||
// Extract distinct project IDs to fetch project details efficiently
|
||||
var distinctProjectIds = taskData.Select(t => t.ProjectId).Distinct().ToList();
|
||||
|
||||
var projects = await _context.Projects
|
||||
.Where(p => distinctProjectIds.Contains(p.Id))
|
||||
.Select(p => new { p.Id, p.Name })
|
||||
.ToListAsync();
|
||||
|
||||
// Prepare the response
|
||||
var response = taskData.Select(t =>
|
||||
{
|
||||
var project = projects.FirstOrDefault(p => p.Id == t.ProjectId);
|
||||
|
||||
return new
|
||||
{
|
||||
ProjectName = project?.Name ?? "Unknown Project",
|
||||
t.AssignmentDate,
|
||||
t.PlannedTask,
|
||||
t.CompletedTask,
|
||||
Location = $"{t.BuildingName} > {t.FloorName} > {t.AreaName}",
|
||||
ActivityName = t.ActivityName,
|
||||
ActivityUnit = t.ActivityUnit
|
||||
};
|
||||
}).ToList();
|
||||
|
||||
_logger.LogInfo("Successfully prepared task response for EmployeeId: {EmployeeId}", employeeId);
|
||||
|
||||
return ApiResponse<object>.SuccessResponse(response, "Task fetched successfully", 200);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error while fetching tasks for EmployeeId: {EmployeeId}", employeeId);
|
||||
return ApiResponse<object>.ErrorResponse("An error occurred while fetching the tasks.", 500);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region =================================================================== Project Infrastructre Manage APIs ===================================================================
|
||||
|
@ -24,6 +24,7 @@ namespace Marco.Pms.Services.Service.ServiceInterfaces
|
||||
|
||||
Task<ApiResponse<object>> GetInfraDetailsAsync(Guid projectId, Guid tenantId, Employee loggedInEmployee);
|
||||
Task<ApiResponse<object>> GetWorkItemsAsync(Guid workAreaId, Guid tenantId, Employee loggedInEmployee);
|
||||
Task<ApiResponse<object>> GetTasksByEmployeeAsync(Guid employeeId, DateTime fromDate, DateTime toDate, Guid tenantId, Employee loggedInEmployee);
|
||||
Task<ServiceResponse> ManageProjectInfraAsync(List<InfraDto> infraDtos, Guid tenantId, Employee loggedInEmployee);
|
||||
Task<ApiResponse<List<WorkItemVM>>> CreateProjectTaskAsync(List<WorkItemDto> workItemDtos, Guid tenantId, Employee loggedInEmployee);
|
||||
Task<ServiceResponse> DeleteProjectTaskAsync(Guid id, Guid tenantId, Employee loggedInEmployee);
|
||||
|
Loading…
x
Reference in New Issue
Block a user