using FastEndpoints; using FrameworkBLL.SchedulerTaskGenerator; using FrameworkDAL.CustomCode.SchedulerTaskGenerator; using FrameworkDAL.DTO.SchedulerTaskGenerator; using GB5Shared.DTO.Framework.Login; using GB5Shared.DTO.Framework.ResponseStandard; using GB5Shared.FastEndPoint; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using static GB5Shared.GB5Constant.Constant; namespace FrameworkSL.Endpoints.SchedulerTaskGenerator { /// /// Returns all scheduler jobs that are due for execution at the current IST time. /// Called internally by the scheduler engine to determine what to dispatch next. /// public sealed class GetSchedulerTasksEndpoint : BaseEndpoint> { private readonly ISchedulerTaskServiceBLL _schedulerService; private readonly ILoginContextProvider _loginProvider; public GetSchedulerTasksEndpoint( ISchedulerTaskServiceBLL schedulerService, ILoginContextProvider loginProvider) { _schedulerService = schedulerService ?? throw new ArgumentNullException(nameof(schedulerService)); _loginProvider = loginProvider ?? throw new ArgumentNullException(nameof(loginProvider)); } public override void Configure() { Get("/Scheduler/GetSchedulerTasks"); AllowAnonymous(); // Internal scheduler call — no user auth required } public sealed record GetSchedulerTasksParameters( [property: FromHeader] string Login ); protected override async Task> ExecuteAsync( GetSchedulerTasksParameters req, LoginDTO loginDTO, CancellationToken ct) { try { // ✅ Null-safe: guard loginDTO itself before accessing .DatabaseName if (string.IsNullOrWhiteSpace(loginDTO?.DatabaseName)) loginDTO = _loginProvider.GetLogin(); // Compute current IST time at the SL layer before passing to BLL var nowIst = TimeZoneInfo.ConvertTimeBySystemTimeZoneId( DateTime.UtcNow, "India Standard Time"); // Load all jobs that are ready to execute at this moment var tasks = await _schedulerService.LoadExecutableTasksAsync( loginDTO, nowIst, ct); return await GB5Shared.ResponseStandard.Response.CreateSuccessResponse( tasks.ToList() as object, CacheKeyLevel.USER_LEVEL, loginDTO ); } catch (Exception ex) { // ✅ Consistent generic type: matches the declared return type return await GB5Shared.ResponseStandard.Response.CreateExceptionError( ex, CacheKeyLevel.USER_LEVEL, loginDTO, ex.Message, 500 ); } } } }