using HRM.BO;
using HRM.UI.Realtime.Jobs;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace HRM.UI.Controllers.Realtime
{
///
/// Reading background jobs over plain HTTP. The browser uses it to catch up (page reload, another tab) and as a
/// fallback when no SignalR connection can be made; live changes come over the hub.
///
[ApiController]
[Route("api/jobs")]
[Authorize]
public class JobsController : ControllerBase
{
private readonly IJobRunner _jobRunner;
public JobsController(IJobRunner jobRunner)
{
_jobRunner = jobRunner;
}
/// One job's current state.
[HttpGet("{runId}")]
public ActionResult Get(string runId)
{
CurrentUser user = CurrentUser.GetCurrentUser(HttpContext.User);
JobAccess access = _jobRunner.TryGet(runId, user.UserID, user.PayrollTypeID, out JobSnapshot snapshot);
if (access == JobAccess.NotFound)
return NotFound("The job could not be found. The application may have restarted.");
if (access == JobAccess.Forbidden)
return Forbid();
return Ok(snapshot);
}
}
}