using FastEndpoints; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.DTO.Framework.ResponseStandard; using GB5Shared.FastEndPoint; using PayRollBLL.DailyAttendance; using PayRollDAL.DTO.DailyAttendance; using static GB5Shared.GB5Constant.Constant; namespace PayRollSL.EndPoints.DailyAttendance { public class LoadDailyAttendance : BaseEndpoint> { private readonly IDailyAttendanceBLL _dailyAttendanceBLL; public LoadDailyAttendance(IDailyAttendanceBLL dailyAttendanceBLL) { _dailyAttendanceBLL = dailyAttendanceBLL; } public override void Configure() { Post("/DailyAttendance/LoadDailyAttendance"); AllowAnonymous(); } public record LoadDailyAttendanceParameters( [property: FromHeader] string Login, [property: QueryParam] int FirstNumber, [property: QueryParam] int MaxResult, [property: QueryParam] int UpdateDailyAttendance, [property: FromBody] DailyAttendanceFilterDTO DailyAttendanceFilterDTO ); protected override async Task> ExecuteAsync(LoadDailyAttendanceParameters req, LoginDTO loginDTO, CancellationToken ct) { try { if (req.UpdateDailyAttendance == 0) { // GB4 parity: GET path — fetch data and total count in parallel var dataTask = _dailyAttendanceBLL.GetDailyAttendance(req.FirstNumber, req.MaxResult, req.UpdateDailyAttendance, req.DailyAttendanceFilterDTO, loginDTO); var countTask = _dailyAttendanceBLL.GetDailyAttendanceCount(req.DailyAttendanceFilterDTO, loginDTO); await System.Threading.Tasks.Task.WhenAll(dataTask, countTask); var response = await GB5Shared.ResponseStandard.Response.CreateSuccessResponse(dataTask.Result, CacheKeyLevel.NOT_REQUIRED, loginDTO); response.Total = countTask.Result; return response; } else { if (req.DailyAttendanceFilterDTO.IsProcessAsynch == 0) { // GB4 parity: fire-and-forget — return immediately, process in background _ = Task.Run(async () => { try { await _dailyAttendanceBLL.UpdateDailyAttendanceFromPunch(req.DailyAttendanceFilterDTO, loginDTO); } catch { /* background errors swallowed; captured via distributed tracing */ } }); return await GB5Shared.ResponseStandard.Response.CreateSuccessResponse( $"Request accepted. After process mail will be sent to {loginDTO.UserPrimaryMailId}", CacheKeyLevel.NOT_REQUIRED, loginDTO); } else { // IsProcessAsynch == 1: synchronous — block until all days processed await _dailyAttendanceBLL.UpdateDailyAttendanceFromPunch(req.DailyAttendanceFilterDTO, loginDTO); return await GB5Shared.ResponseStandard.Response.CreateSuccessResponse("Daily Attendance Updated Successfully", CacheKeyLevel.NOT_REQUIRED, loginDTO); } } } catch (Exception ex) { return await GB5Shared.ResponseStandard.Response.CreateExceptionError(ex, CacheKeyLevel.NOT_REQUIRED, loginDTO, ex.Message, 500); } } } }