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 static GB5Shared.GB5Constant.Constant; namespace FrameworkSL.Endpoints.SchedulerTaskGenerator { /// /// Returns full details for a single scheduler job execution by its ID. /// Called internally by the scheduler consumer after picking up a job from the queue. /// public sealed class GetSchedulerTaskDetailsEndpoint : BaseEndpoint> { private readonly ISchedulerTaskServiceBLL _schedulerService; private readonly ILoginContextProvider _loginProvider; public GetSchedulerTaskDetailsEndpoint( ISchedulerTaskServiceBLL schedulerService, ILoginContextProvider loginProvider) { // ✅ Null guards — consistent with other endpoints _schedulerService = schedulerService ?? throw new ArgumentNullException(nameof(schedulerService)); _loginProvider = loginProvider ?? throw new ArgumentNullException(nameof(loginProvider)); } public override void Configure() { Get("/Scheduler/GetSchedulerTaskDetails"); AllowAnonymous(); // Internal scheduler call — no user auth required } public sealed record GetSchedulerTaskDetailsParameters( [property: QueryParam] int JobExecutionID, [property: FromHeader] string Login ); protected override async Task> ExecuteAsync( GetSchedulerTaskDetailsParameters req, LoginDTO loginDTO, CancellationToken ct) { try { // Fallback to ambient login context if header is absent or incomplete if (string.IsNullOrWhiteSpace(loginDTO?.DatabaseName)) loginDTO = _loginProvider.GetLogin(); // ✅ Correctly typed as nullable — BLL interface returns SchedulerTaskDTO? SchedulerTaskDTO? task = await _schedulerService.GetTaskDetailsAsync( req.JobExecutionID, loginDTO, ct // ✅ Forward ct so the DB call respects client cancellation ); // ✅ Explicit null check — return empty/not-found instead of 200 with null payload if (task is null) return await GB5Shared.ResponseStandard.Response.CreateEmptyResponse( CacheKeyLevel.USER_LEVEL, $"No scheduler task found for JobExecutionID {req.JobExecutionID}." ); return await GB5Shared.ResponseStandard.Response.CreateSuccessResponse( task, CacheKeyLevel.USER_LEVEL, loginDTO ); } catch (Exception ex) { return await GB5Shared.ResponseStandard.Response.CreateExceptionError( ex, CacheKeyLevel.USER_LEVEL, loginDTO, ex.Message, 500 ); } } } }