Implemented an API endpoint that allows the system to record attendance entries (e.g., check-in and check-out) for a specific employee. This helps track employee working hours, presence, and status.

This commit is contained in:
ashutosh.nehete 2025-05-06 10:08:57 +05:30
parent 7e3f0815a0
commit 6d6a2b6015

View File

@ -67,22 +67,56 @@ namespace MarcoBMS.Services.Controllers
return Ok(ApiResponse<object>.SuccessResponse(attendanceLogVMs, System.String.Format("{0} Attendance records fetched successfully", lstAttendance.Count), 200)); return Ok(ApiResponse<object>.SuccessResponse(attendanceLogVMs, System.String.Format("{0} Attendance records fetched successfully", lstAttendance.Count), 200));
} }
[HttpGet("log/employee/{employeeid}")] [HttpGet("log/employee/{employeeId}")]
public async Task<IActionResult> GetAttendanceLogByEmployeeId(Guid employeeid, [FromQuery] string? date = null) public async Task<IActionResult> GetAttendanceLogByEmployeeId(Guid employeeId, [FromQuery] string? dateFrom = null, [FromQuery] string? dateTo = null)
{ {
Guid TenantId = GetTenantId(); Guid TenantId = GetTenantId();
DateOnly forDate = new DateOnly(); DateTime fromDate = new DateTime();
DateTime toDate = new DateTime();
if (date != null && DateOnly.TryParse(date, out forDate) == false) if (dateFrom != null && DateTime.TryParse(dateFrom, out fromDate) == false)
{ {
_logger.LogError("User sent Invalid from Date while featching attendance logs");
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid Date", "Invalid Date", 400));
}
if (dateTo != null && DateTime.TryParse(dateTo, out toDate) == false)
{
_logger.LogError("User sent Invalid to Date while featching attendance logs");
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid Date", "Invalid Date", 400)); return BadRequest(ApiResponse<object>.ErrorResponse("Invalid Date", "Invalid Date", 400));
} }
List<AttendanceLog> lstAttendance = await _context.AttendanceLogs.Where(c => c.EmployeeID == employeeid && c.TenantId == TenantId).ToListAsync();
_logger.LogInfo("{count} Attendance records fetched successfully", lstAttendance.Count); if (employeeId == Guid.Empty)
return Ok(ApiResponse<object>.SuccessResponse(lstAttendance, System.String.Format("{0} Attendance records fetched successfully", lstAttendance.Count), 200)); {
_logger.LogError("The employee Id sent by user is empty");
return BadRequest(ApiResponse<object>.ErrorResponse("Employee ID is required and must not be Empty.", "Employee ID is required and must not be empty.", 400));
}
List<Attendance> attendances = await _context.Attendes.Where(c => c.EmployeeID == employeeId && c.TenantId == TenantId).ToListAsync();
Employee? employee = await _context.Employees.Include(e => e.JobRole).FirstOrDefaultAsync(e => e.Id == employeeId && e.TenantId == TenantId && e.IsActive);
List<EmployeeAttendanceVM> results = new List<EmployeeAttendanceVM>();
if (employee != null)
{
foreach (var attendance in attendances)
{
EmployeeAttendanceVM result = new EmployeeAttendanceVM
{
Id = attendance.Id,
EmployeeId = employee.Id,
FirstName = employee.FirstName,
LastName = employee.LastName,
CheckInTime = attendance.InTime,
CheckOutTime = attendance.OutTime,
JobRoleName = employee.JobRole != null ? employee.JobRole.Name : "",
Activity = attendance.Activity,
EmployeeAvatar = null
};
results.Add(result);
}
}
_logger.LogInfo("{count} Attendance records fetched successfully", results.Count);
return Ok(ApiResponse<object>.SuccessResponse(results, System.String.Format("{0} Attendance records fetched successfully", results.Count), 200));
} }