HRM-10/HRM.UI/BackgroundJobs/Calendar/CalendarMessages.cs
Mohaimen Hasan 838b8d4480 first commit
2026-11-03 13:06:00 +06:00

206 lines
10 KiB
C#

using HRM.BO;
using System;
using System.Net;
using System.Text;
namespace HRM.UI.BackgroundJobs.Calendar
{
/// <summary>A notification in its two forms: the short in-app (bell) text and the e-mail.</summary>
public sealed class CalendarMessage
{
public string Header { get; set; }
public string BellText { get; set; }
public string MailSubject { get; set; }
public string MailBody { get; set; }
}
/// <summary>
/// Wording of every calendar notification. Text the users typed (title, location, remarks...) is HTML-encoded
/// before it goes into an e-mail. Messages to one attendee show that person's own title and (one-off items) own
/// dates, the same as their calendar and their Outlook invitation.
/// </summary>
public static class CalendarMessages
{
/// <param name="start">Start of the occurrence (already the person's own start for a one-off item).</param>
public static CalendarMessage Reminder(CalendarItem item, CalendarItemAttendee to, DateTime start, DateTime end, string calendarLink)
{
string kind = KindOf(item);
string title = TitleFor(item, to);
return Build(item, title, "Reminder: " + title,
kind + " " + When(item, start, end),
"Reminder: " + title + " - " + When(item, start, end),
"This is a reminder for the " + kind.ToLowerInvariant() + " below.",
start, end, calendarLink);
}
public static CalendarMessage Invited(CalendarItem item, CalendarItemAttendee to, string calendarLink)
{
bool isTask = item.ItemType == EnumCalendarItemType.Task;
string title = TitleFor(item, to);
(DateTime start, DateTime end) = DatesFor(item, to);
string header = isTask ? "New task: " + title : "Invitation: " + title;
string intro = isTask
? (item.OrganizerName ?? "Someone") + " assigned you a task."
: (item.OrganizerName ?? "Someone") + " invited you to an event.";
return Build(item, title, header, When(item, start, end), header, intro, start, end, calendarLink);
}
public static CalendarMessage Updated(CalendarItem item, CalendarItemAttendee to, string calendarLink)
{
string title = TitleFor(item, to);
(DateTime start, DateTime end) = DatesFor(item, to);
string header = KindOf(item) + " updated: " + title;
return Build(item, title, header, "Please check the new details.", header,
(item.OrganizerName ?? "The organizer") + " changed this " + KindOf(item).ToLowerInvariant() + ". Please check the details below.",
start, end, calendarLink);
}
public static CalendarMessage Removed(CalendarItem item, CalendarItemAttendee to, string calendarLink)
{
string title = TitleFor(item, to);
(DateTime start, DateTime end) = DatesFor(item, to);
string header = "Removed from: " + title;
return Build(item, title, header, "You are no longer part of this " + KindOf(item).ToLowerInvariant() + ".", header,
(item.OrganizerName ?? "The organizer") + " removed you from this " + KindOf(item).ToLowerInvariant() + ".",
start, end, calendarLink);
}
public static CalendarMessage Cancelled(CalendarItem item, CalendarItemAttendee to, string calendarLink)
{
string title = TitleFor(item, to);
(DateTime start, DateTime end) = DatesFor(item, to);
string header = KindOf(item) + " cancelled: " + title;
return Build(item, title, header, "This " + KindOf(item).ToLowerInvariant() + " was cancelled.", header,
(item.OrganizerName ?? "The organizer") + " cancelled this " + KindOf(item).ToLowerInvariant() + ".",
start, end, calendarLink);
}
/// <summary>The organizer's own copy of the Outlook invitation (new or changed item).</summary>
public static CalendarMessage OrganizerCopy(CalendarItem item, CalendarItemAttendee to, bool isUpdate, string calendarLink)
{
string kind = KindOf(item).ToLowerInvariant();
string title = TitleFor(item, to);
(DateTime start, DateTime end) = DatesFor(item, to);
string header = (isUpdate ? KindOf(item) + " updated: " : KindOf(item) + " added: ") + title;
string intro = isUpdate
? "Your " + kind + " was updated in your Outlook calendar too."
: "Your " + kind + " was added to your Outlook calendar too.";
return Build(item, title, header, intro, header, intro, start, end, calendarLink);
}
public static CalendarMessage Responded(CalendarItem item, CalendarItemAttendee attendee, string calendarLink)
{
string answer = attendee.ResponseStatus switch
{
EnumResponseStatus.Accepted => "is going",
EnumResponseStatus.Declined => "is not going",
EnumResponseStatus.Tentative => "might go",
_ => "has not answered yet"
};
string text = attendee.Name + " " + answer + ".";
return Build(item, item.Title, "Answer: " + item.Title, text, attendee.Name + " " + answer + ": " + item.Title, text,
item.StartDateTime, item.EndDateTime, calendarLink);
}
public static CalendarMessage TaskUpdated(CalendarItem item, CalendarItemAttendee attendee, string calendarLink)
{
string status = attendee.TaskStatus switch
{
EnumCalendarTaskStatus.Done => "Done",
EnumCalendarTaskStatus.InProgress => "In progress",
_ => "Not started"
};
string text = attendee.Name + " set the task to " + status + "."
+ (string.IsNullOrWhiteSpace(attendee.Remarks) ? string.Empty : " Remarks: " + attendee.Remarks);
return Build(item, item.Title, "Task progress: " + item.Title, text, "Task " + status.ToLowerInvariant() + ": " + item.Title, text,
item.StartDateTime, item.EndDateTime, calendarLink);
}
#region Helpers
/// <summary>The person's own title when the organizer gave them one, else the item's.</summary>
private static string TitleFor(CalendarItem item, CalendarItemAttendee to)
{
return string.IsNullOrWhiteSpace(to?.TitleOverride) ? item.Title : to.TitleOverride;
}
/// <summary>The person's own dates on a one-off item (a repeating item always uses the series' dates).</summary>
private static (DateTime Start, DateTime End) DatesFor(CalendarItem item, CalendarItemAttendee to)
{
if (to == null || !string.IsNullOrWhiteSpace(item.RecurrenceRule))
return (item.StartDateTime, item.EndDateTime);
DateTime start = to.FromOverride ?? item.StartDateTime;
DateTime end = to.ToOverride ?? item.EndDateTime;
return (start, end < start ? start : end);
}
private static string KindOf(CalendarItem item)
{
return item.ItemType == EnumCalendarItemType.Task ? "Task" : "Event";
}
/// <summary>"Thu, 15 Oct 2026, 10:00 AM - 11:00 AM" or "Thu, 15 Oct 2026 (entire day)".</summary>
public static string When(CalendarItem item, DateTime start, DateTime end)
{
if (item.IsAllDay)
{
string first = start.ToString("ddd, dd MMM yyyy");
return start.Date == end.Date
? first + " (entire day)"
: first + " - " + end.ToString("ddd, dd MMM yyyy") + " (entire day)";
}
return start.Date == end.Date
? start.ToString("ddd, dd MMM yyyy, h:mm tt") + " - " + end.ToString("h:mm tt")
: start.ToString("ddd, dd MMM yyyy, h:mm tt") + " - " + end.ToString("ddd, dd MMM yyyy, h:mm tt");
}
/// <param name="title">The title shown in the e-mail (the recipient's own title, where they have one).</param>
private static CalendarMessage Build(CalendarItem item, string title, string header, string bellText, string subject, string intro,
DateTime start, DateTime end, string calendarLink)
{
StringBuilder body = new StringBuilder();
body.Append("<p>").Append(Encode(intro)).Append("</p>");
body.Append("<table cellpadding=\"6\" style=\"border-collapse:collapse;font-family:Segoe UI,Arial,sans-serif;font-size:14px\">");
Row(body, item.ItemType == EnumCalendarItemType.Task ? "Task" : "Event", title);
Row(body, "When", When(item, start, end));
if (!string.IsNullOrWhiteSpace(item.Location))
Row(body, "Location", item.Location);
if (!string.IsNullOrWhiteSpace(item.OrganizerName))
Row(body, "Organizer", item.OrganizerName);
if (!string.IsNullOrWhiteSpace(item.Description))
Row(body, "Details", item.Description);
body.Append("</table>");
if (!string.IsNullOrWhiteSpace(calendarLink))
body.Append("<p><a href=\"").Append(Encode(calendarLink)).Append("\">Open my calendar</a></p>");
// EmployeeNotification's column sizes are not documented: keep the bell texts short to be safe.
return new CalendarMessage
{
Header = Truncate(header, 100),
BellText = Truncate(bellText, 250),
MailSubject = Truncate(subject, 250),
MailBody = body.ToString()
};
}
private static void Row(StringBuilder body, string label, string value)
{
body.Append("<tr><td style=\"color:#52527A;font-weight:600;vertical-align:top\">").Append(Encode(label))
.Append("</td><td style=\"white-space:pre-line\">").Append(Encode(value)).Append("</td></tr>");
}
private static string Encode(string value)
{
return WebUtility.HtmlEncode(value ?? string.Empty);
}
private static string Truncate(string value, int max)
{
value ??= string.Empty;
return value.Length <= max ? value : value.Substring(0, max - 1) + "…";
}
#endregion
}
}