using FastEndpoints;
using GB5Shared.DTO.Framework.Login;
using GB5Shared.DTO.Framework.ResponseStandard;
using GB5Shared.FastEndPoint;
using GB5Shared.QueryExecutor;
using static GB5Shared.GB5Constant.Constant;
namespace FrameworkSL.Endpoints.CommonReport
{
///
/// Returns the list of async report jobs submitted by the current user.
/// Used by the Angular "My Reports" inbox to display pending/completed jobs
/// with download links and status indicators.
///
public class GetMyReports
: BaseEndpoint>
{
private readonly IQueryExecutor _queryExecutor;
public GetMyReports(IQueryExecutor queryExecutor) => _queryExecutor = queryExecutor;
public override void Configure()
{
Get("/CommonReport/GetMyReports");
AllowAnonymous();
}
public record GetMyReportsParameters(
[property: FromHeader] string Login,
[property: QueryParam] int? PageNumber,
[property: QueryParam] int? PageSize
);
protected override string? GetCacheKey(GetMyReportsParameters req, LoginDTO login) => null;
protected override async Task> ExecuteAsync(
GetMyReportsParameters req, LoginDTO loginDTO, CancellationToken ct)
{
int page = req.PageNumber ?? 1;
int size = req.PageSize ?? 20;
var jobs = await _queryExecutor
.QueryAsync(
loginDTO,
GetMyReportsQB.GET_MY_REPORTS,
new
{
SubmittedById = loginDTO.UserId,
ClientId = loginDTO.ClientId,
Offset = (page - 1) * size,
PageSize = size
},
cancellationToken: ct)
.ConfigureAwait(false);
return await GB5Shared.ResponseStandard.Response
.CreateSuccessResponse(
(object)jobs,
CacheKeyLevel.NOT_REQUIRED,
loginDTO)
.ConfigureAwait(false);
}
private sealed class MyReportJobDTO
{
public int SysJobId { get; init; }
public string JobType { get; init; } = string.Empty;
public byte RunStatus { get; init; }
public string? ResultLocation { get; init; }
public DateTime SubmittedOn { get; init; }
public DateTime? CompletedOn { get; init; }
public string? Parameters { get; init; }
}
private static class GetMyReportsQB
{
public const string GET_MY_REPORTS = @"
SELECT
SYSJOBID AS SysJobId,
SYSJOBTYPE AS JobType,
RUNSTATUS AS RunStatus,
RESULTLOCATION AS ResultLocation,
CREATEDON AS SubmittedOn,
COMPLETEDON AS CompletedOn,
PARAMETERS AS Parameters
FROM TSYSJOB
WHERE SUBMITTEDBYID = @SubmittedById
AND SYSJOBTENANTID = @ClientId
ORDER BY SYSJOBID DESC
OFFSET @Offset ROWS FETCH NEXT @PageSize ROWS ONLY;";
}
}
}