diff --git a/Marco.Pms.Services/Controllers/AttendanceController.cs b/Marco.Pms.Services/Controllers/AttendanceController.cs index 8065719..b2b6b58 100644 --- a/Marco.Pms.Services/Controllers/AttendanceController.cs +++ b/Marco.Pms.Services/Controllers/AttendanceController.cs @@ -80,31 +80,41 @@ namespace MarcoBMS.Services.Controllers } [HttpGet("log/employee/{employeeId}")] - public async Task GetAttendanceLogByEmployeeId(Guid employeeId, [FromQuery] string? dateFrom = null, [FromQuery] string? dateTo = null) + public async Task GetAttendanceLogByEmployeeId(Guid employeeId, [FromQuery] DateTime? dateFrom = null, [FromQuery] DateTime? dateTo = null) { - DateTime fromDate = new DateTime(); - DateTime toDate = new DateTime(); - - if (dateFrom != null && DateTime.TryParse(dateFrom, out fromDate) == false) - { - _logger.LogWarning("User sent Invalid from Date while featching attendance logs"); - return BadRequest(ApiResponse.ErrorResponse("Invalid Date", "Invalid Date", 400)); - } - if (dateTo != null && DateTime.TryParse(dateTo, out toDate) == false) - { - _logger.LogWarning("User sent Invalid to Date while featching attendance logs"); - return BadRequest(ApiResponse.ErrorResponse("Invalid Date", "Invalid Date", 400)); - } - if (employeeId == Guid.Empty) { _logger.LogWarning("The employee Id sent by user is empty"); return BadRequest(ApiResponse.ErrorResponse("Employee ID is required and must not be Empty.", "Employee ID is required and must not be empty.", 400)); } - List attendances = await _context.Attendes - .Where(c => c.EmployeeId == employeeId && c.TenantId == tenantId && c.AttendanceDate.Date >= fromDate && c.AttendanceDate.Date <= toDate).ToListAsync(); - Employee? employee = await _context.Employees.Include(e => e.JobRole).FirstOrDefaultAsync(e => e.Id == employeeId && e.TenantId == tenantId && e.IsActive); + Employee? employee = await _context.Employees.Include(e => e.JobRole).FirstOrDefaultAsync(e => e.Id == employeeId && e.TenantId == tenantId); + if (employee == null) + { + _logger.LogWarning("Employee {EmployeeId} not found", employeeId); + return NotFound(ApiResponse.ErrorResponse("Employee not found", "Employee not found in database", 404)); + } + + if (!dateFrom.HasValue) + { + dateFrom = DateTime.UtcNow; + } + if (!dateTo.HasValue) + { + var days = 0 - 7; + dateTo = dateFrom.Value.AddDays(days); + } + + List attendances = await _context.Attendes + .Include(a => a.RequestedBy) + .ThenInclude(e => e!.JobRole) + .Include(a => a.RequestedBy) + .ThenInclude(e => e!.JobRole) + .Where(c => c.EmployeeId == employeeId && c.TenantId == tenantId && c.AttendanceDate.Date >= dateFrom && c.AttendanceDate.Date <= dateTo).ToListAsync(); + + var projectIds = attendances.Select(a => a.ProjectID).Distinct().ToList(); + + var projects = await _context.Projects.Where(p => projectIds.Contains(p.Id) && p.TenantId == tenantId).ToListAsync(); List results = new List(); @@ -119,11 +129,16 @@ namespace MarcoBMS.Services.Controllers FirstName = employee.FirstName, LastName = employee.LastName, ProjectId = attendance.ProjectID, + ProjectName = projects.Where(p => p.Id == attendance.ProjectID).Select(p => p.Name).FirstOrDefault(), CheckInTime = attendance.InTime, CheckOutTime = attendance.OutTime, JobRoleName = employee.JobRole != null ? employee.JobRole.Name : "", Activity = attendance.Activity, - EmployeeAvatar = null + EmployeeAvatar = null, + RequestedAt = attendance.RequestedAt, + RequestedBy = _mapper.Map(attendance.RequestedBy), + ApprovedAt = attendance.ApprovedAt, + Approver = _mapper.Map(attendance.Approver) }; results.Add(result); }