395 lines
14 KiB
C#
395 lines
14 KiB
C#
|
|
using Ease.Core.Model;
|
||
|
|
using Ease.Core.Utility;
|
||
|
|
using Hangfire;
|
||
|
|
using HRM.BO;
|
||
|
|
using HRM.UI.BackgroundJobs.Calendar;
|
||
|
|
using Microsoft.AspNetCore.Authorization;
|
||
|
|
using Microsoft.AspNetCore.Http;
|
||
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
using System;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using System.Linq;
|
||
|
|
|
||
|
|
namespace HRM.UI.Controllers
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// HRM Calendar. The service checks every permission itself (organizer, attendee, task-assign rights),
|
||
|
|
/// so these endpoints only pass the logged-in user through.
|
||
|
|
/// </summary>
|
||
|
|
[ApiController]
|
||
|
|
[Route("api/Calendar")]
|
||
|
|
[Authorize]
|
||
|
|
public class CalendarController : Controller
|
||
|
|
{
|
||
|
|
#region Declaration
|
||
|
|
private readonly ICalendarItemService _calendarItemService;
|
||
|
|
private readonly IBackgroundJobClient _backgroundJobs;
|
||
|
|
|
||
|
|
public CalendarController(ICalendarItemService calendarItemService, IBackgroundJobClient backgroundJobs)
|
||
|
|
{
|
||
|
|
this._calendarItemService = calendarItemService;
|
||
|
|
this._backgroundJobs = backgroundJobs;
|
||
|
|
}
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Entries to draw between fromDate (inclusive) and toDate (exclusive).
|
||
|
|
/// layers is an optional comma-separated list of EnumCalendarLayer values; empty means all layers.
|
||
|
|
/// </summary>
|
||
|
|
[HttpGet]
|
||
|
|
[Route("getFeed")]
|
||
|
|
public ActionResult GetFeed(DateTime fromDate, DateTime toDate, string layers)
|
||
|
|
{
|
||
|
|
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
|
||
|
|
List<CalendarFeedItem> feed;
|
||
|
|
try
|
||
|
|
{
|
||
|
|
feed = _calendarItemService.GetFeed(currentUser, fromDate, toDate, ParseLayers(layers));
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
return Fail(ex);
|
||
|
|
}
|
||
|
|
|
||
|
|
return Ok(feed);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>The past and next few days (1 to 31, default 7) for the sidebar, plus the open-task count.</summary>
|
||
|
|
[HttpGet]
|
||
|
|
[Route("getUpcoming")]
|
||
|
|
public ActionResult GetUpcoming(int days = 7)
|
||
|
|
{
|
||
|
|
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
|
||
|
|
CalendarUpcoming upcoming;
|
||
|
|
try
|
||
|
|
{
|
||
|
|
upcoming = _calendarItemService.GetUpcoming(currentUser, days);
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
return Fail(ex);
|
||
|
|
}
|
||
|
|
|
||
|
|
return Ok(upcoming);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>What the logged-in user may do (create events, assign tasks, see birthdays), from the Calendar settings.</summary>
|
||
|
|
[HttpGet]
|
||
|
|
[Route("getOptions")]
|
||
|
|
public ActionResult GetOptions()
|
||
|
|
{
|
||
|
|
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
|
||
|
|
CalendarOptions options;
|
||
|
|
try
|
||
|
|
{
|
||
|
|
options = _calendarItemService.GetOptions(currentUser);
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
return Fail(ex);
|
||
|
|
}
|
||
|
|
|
||
|
|
return Ok(options);
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpGet]
|
||
|
|
[Route("getItem/{id}")]
|
||
|
|
public ActionResult GetItem(int id)
|
||
|
|
{
|
||
|
|
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
|
||
|
|
CalendarItem item;
|
||
|
|
try
|
||
|
|
{
|
||
|
|
item = _calendarItemService.Get(currentUser, id);
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
return Fail(ex);
|
||
|
|
}
|
||
|
|
|
||
|
|
return Ok(item);
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpPost]
|
||
|
|
[Route("saveItem")]
|
||
|
|
public ActionResult SaveItem(CalendarItem item)
|
||
|
|
{
|
||
|
|
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
|
||
|
|
CalendarSaveResult result;
|
||
|
|
// A new item invites everyone; an edit (including changing part of a series) tells people what changed.
|
||
|
|
EnumCalendarChange change = item != null && item.IsNew ? EnumCalendarChange.Created : EnumCalendarChange.Updated;
|
||
|
|
try
|
||
|
|
{
|
||
|
|
result = _calendarItemService.Save(currentUser, item);
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
return Fail(ex);
|
||
|
|
}
|
||
|
|
|
||
|
|
int id = result.CalendarItemID;
|
||
|
|
// a schedule job tells nobody and goes to no Outlook: the tasks it creates do, when it runs (a schedule email
|
||
|
|
// likewise: only its e-mails reach people)
|
||
|
|
if (item.ItemType == EnumCalendarItemType.ScheduleJob || item.ItemType == EnumCalendarItemType.ScheduleEmail)
|
||
|
|
return Ok(id);
|
||
|
|
|
||
|
|
bool detailsChanged = result.DetailsChanged;
|
||
|
|
QueueJob(() => _backgroundJobs.Enqueue<CalendarJobs>(j => j.NotifyChange(id, change, detailsChanged)));
|
||
|
|
QueueJob(() => _backgroundJobs.Enqueue<CalendarJobs>(j => j.SyncOutlook(id)));
|
||
|
|
// Changing part of a series also changes the series (a skipped day, or an earlier end), so its Outlook entry
|
||
|
|
// needs updating too; its people hear about the change from the new item.
|
||
|
|
if (result.SeriesID.HasValue && result.SeriesID.Value != id)
|
||
|
|
{
|
||
|
|
int seriesID = result.SeriesID.Value;
|
||
|
|
QueueJob(() => _backgroundJobs.Enqueue<CalendarJobs>(j => j.SyncOutlook(seriesID)));
|
||
|
|
}
|
||
|
|
return Ok(id);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Cancels the item for everyone; with occurrenceStart, only that occurrence of a repeating item
|
||
|
|
/// (scope ThisOccurrence, the default) or that occurrence and every later one (scope ThisAndFollowing).
|
||
|
|
/// </summary>
|
||
|
|
[HttpPost]
|
||
|
|
[Route("cancelItem/{id}")]
|
||
|
|
public ActionResult CancelItem(int id, DateTime? occurrenceStart, EnumCalendarEditScope? scope)
|
||
|
|
{
|
||
|
|
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
|
||
|
|
EnumCalendarChange change;
|
||
|
|
try
|
||
|
|
{
|
||
|
|
change = _calendarItemService.Cancel(currentUser, id, occurrenceStart, scope);
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
return Fail(ex);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Losing some dates is a change to the series; cancelling everything is a cancellation.
|
||
|
|
QueueJob(() => _backgroundJobs.Enqueue<CalendarJobs>(j => j.NotifyChange(id, change, true)));
|
||
|
|
QueueJob(() => _backgroundJobs.Enqueue<CalendarJobs>(j => j.SyncOutlook(id)));
|
||
|
|
return Ok();
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpPost]
|
||
|
|
[Route("respond")]
|
||
|
|
public ActionResult Respond(CalendarRespondRequest request)
|
||
|
|
{
|
||
|
|
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
|
||
|
|
int attendeeID;
|
||
|
|
try
|
||
|
|
{
|
||
|
|
if (request == null)
|
||
|
|
return BadRequest("Nothing to save.");
|
||
|
|
attendeeID = _calendarItemService.Respond(currentUser, request.CalendarItemID, request.ResponseStatus);
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
return Fail(ex);
|
||
|
|
}
|
||
|
|
|
||
|
|
int itemID = request.CalendarItemID;
|
||
|
|
QueueJob(() => _backgroundJobs.Enqueue<CalendarJobs>(j => j.NotifyOrganizer(itemID, attendeeID, EnumCalendarChange.Responded)));
|
||
|
|
return Ok();
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpPost]
|
||
|
|
[Route("updateTaskStatus")]
|
||
|
|
public ActionResult UpdateTaskStatus(CalendarTaskStatusRequest request)
|
||
|
|
{
|
||
|
|
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
|
||
|
|
int attendeeID;
|
||
|
|
try
|
||
|
|
{
|
||
|
|
if (request == null)
|
||
|
|
return BadRequest("Nothing to save.");
|
||
|
|
attendeeID = _calendarItemService.UpdateTaskStatus(currentUser, request.CalendarItemID, request.TaskStatus, request.Remarks);
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
return Fail(ex);
|
||
|
|
}
|
||
|
|
|
||
|
|
int itemID = request.CalendarItemID;
|
||
|
|
QueueJob(() => _backgroundJobs.Enqueue<CalendarJobs>(j => j.NotifyOrganizer(itemID, attendeeID, EnumCalendarChange.TaskUpdated)));
|
||
|
|
return Ok();
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpPost]
|
||
|
|
[Route("markRead/{id}")]
|
||
|
|
public ActionResult MarkRead(int id)
|
||
|
|
{
|
||
|
|
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
|
||
|
|
try
|
||
|
|
{
|
||
|
|
_calendarItemService.MarkRead(currentUser, id);
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
return Fail(ex);
|
||
|
|
}
|
||
|
|
|
||
|
|
return Ok();
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpGet]
|
||
|
|
[Route("getMyTasks")]
|
||
|
|
public ActionResult GetMyTasks(EnumCalendarTaskStatus? taskStatus)
|
||
|
|
{
|
||
|
|
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
|
||
|
|
List<CalendarItem> items;
|
||
|
|
try
|
||
|
|
{
|
||
|
|
items = _calendarItemService.GetMyTasks(currentUser, taskStatus);
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
return Fail(ex);
|
||
|
|
}
|
||
|
|
|
||
|
|
return Ok(items);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// HR: "test run" of a schedule job or schedule email from the form (saved or not): who would get it now, the next
|
||
|
|
/// runs and, for a schedule email, the e-mail as its first recipient would get it.
|
||
|
|
/// </summary>
|
||
|
|
[HttpPost]
|
||
|
|
[Route("previewScheduleJob")]
|
||
|
|
public ActionResult PreviewScheduleJob(CalendarItem job)
|
||
|
|
{
|
||
|
|
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
|
||
|
|
CalendarJobPreview preview;
|
||
|
|
try
|
||
|
|
{
|
||
|
|
preview = _calendarItemService.PreviewScheduleJob(currentUser, job);
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
return Fail(ex);
|
||
|
|
}
|
||
|
|
|
||
|
|
return Ok(preview);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>HR: runs a saved schedule job at once; its assignees are told and get the Outlook invitation as usual.</summary>
|
||
|
|
[HttpPost]
|
||
|
|
[Route("runScheduleJobNow/{id}")]
|
||
|
|
public ActionResult RunScheduleJobNow(int id)
|
||
|
|
{
|
||
|
|
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
|
||
|
|
CalendarJobRunResult result;
|
||
|
|
try
|
||
|
|
{
|
||
|
|
result = _calendarItemService.RunScheduleJobNow(currentUser, id);
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
return Fail(ex);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (result.Created)
|
||
|
|
{
|
||
|
|
int taskID = result.TaskID;
|
||
|
|
QueueJob(() => _backgroundJobs.Enqueue<CalendarJobs>(j => j.NotifyChange(taskID, EnumCalendarChange.Created, true)));
|
||
|
|
QueueJob(() => _backgroundJobs.Enqueue<CalendarJobs>(j => j.SyncOutlook(taskID)));
|
||
|
|
}
|
||
|
|
return Ok(result);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// HR: sends a saved schedule email at once. Its e-mail batch is created here (so the answer can say how many it
|
||
|
|
/// goes to) and sent by a Hangfire job, like a run that falls due; Mail Manager's Mail Log History follows it.
|
||
|
|
/// </summary>
|
||
|
|
[HttpPost]
|
||
|
|
[Route("runScheduleEmailNow/{id}")]
|
||
|
|
public ActionResult RunScheduleEmailNow(int id)
|
||
|
|
{
|
||
|
|
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
|
||
|
|
CalendarEmailRunResult result;
|
||
|
|
try
|
||
|
|
{
|
||
|
|
result = _calendarItemService.RunScheduleEmailNow(currentUser, id);
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
return Fail(ex);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (result.Created)
|
||
|
|
{
|
||
|
|
DateTime runAt = result.RunAt;
|
||
|
|
QueueJob(() => _backgroundJobs.Enqueue<CalendarJobs>(j => j.SendScheduleEmail(id, runAt)));
|
||
|
|
}
|
||
|
|
return Ok(result);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Active admin-side user accounts that can be added as participants (admin logins only).</summary>
|
||
|
|
[HttpGet]
|
||
|
|
[Route("getActiveUsers")]
|
||
|
|
public ActionResult GetActiveUsers()
|
||
|
|
{
|
||
|
|
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
|
||
|
|
List<CalendarUserOption> users;
|
||
|
|
try
|
||
|
|
{
|
||
|
|
users = _calendarItemService.GetActiveUsers(currentUser);
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
return Fail(ex);
|
||
|
|
}
|
||
|
|
|
||
|
|
return Ok(users);
|
||
|
|
}
|
||
|
|
|
||
|
|
#region Helpers
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Queues a notification job. The user's change is already saved, so a problem here is only logged:
|
||
|
|
/// it must never turn a successful save into an error.
|
||
|
|
/// </summary>
|
||
|
|
private static void QueueJob(Action enqueue)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
enqueue();
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
ExceptionLog.Write(ex);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private static List<EnumCalendarLayer> ParseLayers(string layers)
|
||
|
|
{
|
||
|
|
List<EnumCalendarLayer> result = new List<EnumCalendarLayer>();
|
||
|
|
if (string.IsNullOrWhiteSpace(layers))
|
||
|
|
return result;
|
||
|
|
|
||
|
|
foreach (string part in layers.Split(',', StringSplitOptions.RemoveEmptyEntries))
|
||
|
|
{
|
||
|
|
if (int.TryParse(part.Trim(), out int value) && Enum.IsDefined(typeof(EnumCalendarLayer), value))
|
||
|
|
result.Add((EnumCalendarLayer)value);
|
||
|
|
}
|
||
|
|
return result.Distinct().ToList();
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Bad input becomes 400 and a missing permission 403, each with a message the screen can show;
|
||
|
|
/// anything else stays a 500 like the rest of the API.
|
||
|
|
/// </summary>
|
||
|
|
private ActionResult Fail(Exception ex)
|
||
|
|
{
|
||
|
|
Exception cause = ex is ServiceException && ex.InnerException != null ? ex.InnerException : ex;
|
||
|
|
if (cause is UnauthorizedAccessException)
|
||
|
|
return StatusCode(StatusCodes.Status403Forbidden, cause.Message);
|
||
|
|
if (cause is ArgumentException)
|
||
|
|
return BadRequest(cause.Message);
|
||
|
|
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
|
||
|
|
}
|
||
|
|
|
||
|
|
#endregion
|
||
|
|
}
|
||
|
|
}
|