From 7555b73f020f4d82b4ffe6ce24ba02a66b02fd08 Mon Sep 17 00:00:00 2001 From: "ashutosh.nehete" Date: Wed, 6 Aug 2025 14:51:55 +0530 Subject: [PATCH] Added an API to get task by employeeId --- .../Controllers/ProjectController.cs | 19 +++ Marco.Pms.Services/Service/ProjectServices.cs | 135 ++++++++++++++++-- .../ServiceInterfaces/IProjectServices.cs | 1 + 3 files changed, 141 insertions(+), 14 deletions(-) diff --git a/Marco.Pms.Services/Controllers/ProjectController.cs b/Marco.Pms.Services/Controllers/ProjectController.cs index 0520414..436dff9 100644 --- a/Marco.Pms.Services/Controllers/ProjectController.cs +++ b/Marco.Pms.Services/Controllers/ProjectController.cs @@ -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 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.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 diff --git a/Marco.Pms.Services/Service/ProjectServices.cs b/Marco.Pms.Services/Service/ProjectServices.cs index c92adbe..f3299fa 100644 --- a/Marco.Pms.Services/Service/ProjectServices.cs +++ b/Marco.Pms.Services/Service/ProjectServices.cs @@ -897,23 +897,53 @@ namespace Marco.Pms.Services.Service public async Task> 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.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.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.ErrorResponse( + "An error occurred while fetching project assignments.", + 500); + } } + #endregion #region =================================================================== Project InfraStructure Get APIs =================================================================== @@ -1044,6 +1074,83 @@ namespace Marco.Pms.Services.Service } } + /// + /// Retrieves tasks assigned to a specific employee within a date range for a tenant. + /// + /// The ID of the employee to filter tasks. + /// The start date to filter task assignments. + /// The end date to filter task assignments. + /// The tenant ID to filter tasks. + /// The employee requesting the data (for authorization/logging). + /// An ApiResponse containing the task details. + public async Task> 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.SuccessResponse(response, "Task fetched successfully", 200); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error while fetching tasks for EmployeeId: {EmployeeId}", employeeId); + return ApiResponse.ErrorResponse("An error occurred while fetching the tasks.", 500); + } + } + + #endregion #region =================================================================== Project Infrastructre Manage APIs =================================================================== diff --git a/Marco.Pms.Services/Service/ServiceInterfaces/IProjectServices.cs b/Marco.Pms.Services/Service/ServiceInterfaces/IProjectServices.cs index e76df7f..a1f78f8 100644 --- a/Marco.Pms.Services/Service/ServiceInterfaces/IProjectServices.cs +++ b/Marco.Pms.Services/Service/ServiceInterfaces/IProjectServices.cs @@ -24,6 +24,7 @@ namespace Marco.Pms.Services.Service.ServiceInterfaces Task> GetInfraDetailsAsync(Guid projectId, Guid tenantId, Employee loggedInEmployee); Task> GetWorkItemsAsync(Guid workAreaId, Guid tenantId, Employee loggedInEmployee); + Task> GetTasksByEmployeeAsync(Guid employeeId, DateTime fromDate, DateTime toDate, Guid tenantId, Employee loggedInEmployee); Task ManageProjectInfraAsync(List infraDtos, Guid tenantId, Employee loggedInEmployee); Task>> CreateProjectTaskAsync(List workItemDtos, Guid tenantId, Employee loggedInEmployee); Task DeleteProjectTaskAsync(Guid id, Guid tenantId, Employee loggedInEmployee);