using Fitness_core_BE.Helpers; using Fitness_core_BE.Interfaces; using Fitness_core_BE.Interfaces.Services; namespace Fitness_core_BE.Services { public class AttendanceService : IAttendanceService { private readonly IAttendanceRepository _attendanceRepo; private readonly UserRoleHelper _userRoleHelper; private readonly IAccessDeviceService _accessDeviceService; public AttendanceService( IAttendanceRepository attendanceRepo, UserRoleHelper userRoleHelper, IAccessDeviceService accessDeviceService) { _attendanceRepo = attendanceRepo; _userRoleHelper = userRoleHelper; _accessDeviceService = accessDeviceService; } public async Task GetAllAttendanceRecords(object payload, HttpContext httpContext) { var data = DeserializePayload.Deserialize(payload); Guid userId = DataExtractor.GetUserIdFromContext(httpContext); Guid clientId = await _userRoleHelper.GetClientId(userId); //await _accessDeviceService.GetAttendance(clientId); return await _attendanceRepo.GetLatestPerPersonAsync(clientId); } public async Task GetAttendanceCounts(object payload, HttpContext httpContext) { var data = DeserializePayload.Deserialize(payload); Guid userId = DataExtractor.GetUserIdFromContext(httpContext); Guid clientId = await _userRoleHelper.GetClientId(userId); //await _accessDeviceService.GetAttendance(clientId); var fromDate = DataExtractor.GetOptionalDateTime(data, "fromDate"); var toDate = DataExtractor.GetOptionalDateTime(data, "toDate"); var now = DateTime.UtcNow; DateTime resolvedFrom; DateTime resolvedTo; if (fromDate == null && toDate == null) { resolvedFrom = new DateTime(now.Year, now.Month, 1); resolvedTo = now.Date.AddDays(1).AddTicks(-1); } else if (fromDate != null && toDate == null) { resolvedFrom = fromDate.Value; resolvedTo = now.Date.AddDays(1).AddTicks(-1); } else { resolvedFrom = fromDate!.Value; resolvedTo = toDate!.Value.Date.AddDays(1).AddTicks(-1); } return await _attendanceRepo.GetAttendanceCountsAsync(clientId, resolvedFrom, resolvedTo); } public async Task GetTodayAttendance(object payload, HttpContext httpContext) { var data = DeserializePayload.Deserialize(payload); Guid userId = DataExtractor.GetUserIdFromContext(httpContext); Guid clientId = await _userRoleHelper.GetClientId(userId); //await _accessDeviceService.GetAttendance(clientId); return await _attendanceRepo.GetTodayAttendanceAsync(clientId); } } }