Files
monorepo/LCU.API.Operations.IncentiveAPI/LCU.API.Internal.Operations.IncentiveApi/Services/MicroApiServices/NotificationApiService.cs
T
2026-03-17 09:53:09 -05:00

313 lines
11 KiB
C#

using LCU.API.Internal.Operations.IncentiveApi.Configuration;
using LCU.API.Internal.Operations.IncentiveApi.DataAccess;
using LCU.Library.Internal.IncentiveApiModels.Entities;
using LCU.API.Internal.Operations.IncentiveApi.Models.Dayforce;
using LCU.API.Internal.Operations.IncentiveApi.Models.GoalDispersal;
using LCU.API.Internal.Operations.IncentiveApiLib.Services;
using LCU.API.Internal.NotificationsApiLib.Models;
namespace LCU.API.Internal.Operations.IncentiveApi.Services.MicroApiServices;
public class NotificationApiService : INotificationApiService
{
private readonly EmailRecipientsConfiguration _emailRecipientsConfig;
private readonly INotificationApiDao _notificationsDao;
private readonly ApiConfiguration _incentiveApiConfig;
public NotificationApiService(EmailRecipientsConfiguration emailRecipientsConfig, ApiConfiguration incentiveApiConfig, INotificationApiDao notificationsDao,
IUserService userService)
{
_emailRecipientsConfig = emailRecipientsConfig;
_notificationsDao = notificationsDao;
_incentiveApiConfig = incentiveApiConfig;
}
public async Task SendMonthStartGoalDispersalReceipt(Dictionary<string, GoalDispersalReceipt> receipts, List<string> recipients)
{
try
{
#region Body HTML Boilerplate
string body =
@"<style>
.report {
border:1px solid #C0C0C0;
border-collapse:collapse;
padding:5px;
}
.report th {
border:1px solid #C0C0C0;
padding:5px;
background:#F0F0F0;
}
.report td {
border:1px solid #C0C0C0;
padding:5px;
}
</style>
<table class='report'>
<tbody>
<tr>
<td><h3>Department</h3></td>
<td><h3>Strategy</h3></td>
<td><h3>Alerts</h3></td>
</tr>";
#endregion
foreach (var (departmentName, receipt) in receipts)
{
if (receipt == null)
{
body += $@"
<tr>
<td><b>{departmentName}</b></td>
<td>&nbsp;<i>FAILED</i></td>
<td>&nbsp;No Receipt Data, Failed to Run Dispersal.</td>
</tr>";
}
else if (receipt.GoalDispersalErrors.Any())
{
body += $@"
<tr>
<td><b>{departmentName}</b></td>
<td>&nbsp;<i>{receipt.Strategy}</i></td>
<td>&nbsp;{FormatReceiptItems(receipt.GoalDispersalErrors)}</td>
</tr>";
}
}
body += @"</tbody> </table>";
var emailRequest = new EmailRequest
{
Recipient = recipients,
Subject = "Month Start Personnel Dispersal Report",
Body = body
};
await _notificationsDao.SendMessageAsync(emailRequest);
}
catch (Exception ex)
{
Log.Error(ex, "Failed to Send Month Start Dispersal Report");
}
}
public async Task SendMissingRoleNotification(IEnumerable<AppUser> usersMissingRole, List<string> recipients)
{
try
{
#region Body HTML Boilerplate
string body =
@"<style>
.report {
border:1px solid #C0C0C0;
border-collapse:collapse;
padding:5px;
}
.report th {
border:1px solid #C0C0C0;
padding:5px;
background:#F0F0F0;
}
.report td {
border:1px solid #C0C0C0;
padding:5px;
}
</style>
<table class='report'>
<tbody>
<tr>
<td><h3>Name</h3></td>
<td><h3>Employee ID</h3></td>
<td><h3>Department</h3></td>
<td><h3>Missing Role</h3></td>
</tr>";
#endregion
foreach (var user in usersMissingRole)
{
body += $@"
<tr>
<td>{user.FullName}</td>
<td>{user.EmployeeId}</td>
<td>{user.HomeDepartment}</td>
<td>{user.BaseRole}</td>
</tr>";
}
body += @"</tbody> </table>";
var emailRequest = new EmailRequest
{
Recipient = recipients,
Subject = $"Users missing Dayforce role",
Body = body
};
await _notificationsDao.SendMessageAsync(emailRequest);
}
catch (Exception ex)
{
Log.Error(ex, "Failed to send missing role notification");
}
}
public async Task SendTerminatedEmployeeNotification(IEnumerable<TerminatedEmployeeReceipt> terminated, List<string> recipients)
{
try
{
#region Body HTML Boilerplate
string body =
@"<style>
.report {
border:1px solid #C0C0C0;
border-collapse:collapse;
padding:5px;
}
.report th {
border:1px solid #C0C0C0;
padding:5px;
background:#F0F0F0;
}
.report td {
border:1px solid #C0C0C0;
padding:5px;
}
</style>
<table class='report'>
<tbody>
<tr>
<td><h3>Name</h3></td>
<td><h3>Employee ID</h3></td>
<td><h3>Department</h3></td>
<td><h3>Terminated</h3></td>
<td><h3>Manager</h3></td>
<td><h3>Prev Month Payout</h3></td>
<td><h3>This Month Payout</h3></td>
</tr>";
#endregion
foreach (var receipt in terminated)
{
var manager = receipt.Manager != null ? receipt.Manager.FullName : "Not Found";
body += $@"
<tr>
<td>{receipt.Terminated.FullName}</td>
<td>{receipt.Terminated.EmployeeId}</td>
<td>{receipt.Terminated.HomeDepartment}</td>
<td>{receipt.Terminated.Terminated.Value:MM/dd/yyyy}</td>
<td>{manager}</td>
<td>${receipt.LastMonthPayout.PayoutTotal}.00</td>
<td>${receipt.ThisMonthPayout.PayoutTotal}.00</td>
</tr>";
}
body += @"</tbody> </table>";
var emailRequest = new EmailRequest
{
Recipient = recipients,
Subject = $"Users terminated in Dayforce",
Body = body
};
await _notificationsDao.SendMessageAsync(emailRequest);
}
catch (Exception ex)
{
Log.Error(ex, "Failed to send terminated employee notification");
}
}
private string FormatReceiptItems(List<GoalDispersalReceiptItem> receiptErrors)
{
string orderedList = @"<ol>";
foreach (var error in receiptErrors)
{
orderedList += $@"<li> <b>{error.EmployeeName}</b> - {error.EmployeeId}: {error.Message}<br><br></li>";
}
orderedList += @"</ol>";
return orderedList;
}
public async Task SendTransferRequestInitiatedNotification(ActivityTransferAction transferAction,
List<string> recipients)
{
try
{
#region Body HTML Boilerplate
string body =
@"<style>
.report {
border:1px solid #C0C0C0;
border-collapse:collapse;
padding:5px;
}
.report th {
border:1px solid #C0C0C0;
padding:5px;
background:#F0F0F0;
}
.report td {
border:1px solid #C0C0C0;
padding:5px;
}
</style>";
#endregion
body += @"
<p>A transfer request has been initiated for the following activity:</p>
<table class='report'>
<tbody>
<tr>
<td><h3>Product</h3></td>
<td><h3>Activity Type</h3></td>
<td><h3>Requested by</h3></td>
<td><h3>Transfer to Employee</h3></td>
<td><h3>Transfer to Department</h3></td>
</tr>";
body += $@"
<tr>
<td>{transferAction.RequestedActivity.Product}</td>
<td>{transferAction.RequestedActivity.Type}</td>
<td>{transferAction.CreatedBy}</td>
<td>{transferAction.NewActivityEmployeeId}</td>
<td>{transferAction.NewActivityDepartmentId}</td>
</tr>
</tbody>
</table>";
var webAppUrl = $@"{_incentiveApiConfig.IncentiveManagerClientUrl}/activity/{transferAction.RequestedActivityId}";
body += $@"
<p>
The request can be approved at <a href='{webAppUrl}'>{webAppUrl}</a>.
</p>";
if (_emailRecipientsConfig.OverrideRecipients)
{
body += $"<p>The original recipients have been overriden in this environment: {string.Join(", ", recipients)}</p>";
recipients = _emailRecipientsConfig.TransferRequestInitiatedNotification;
}
var emailRequest = new EmailRequest
{
Recipient = recipients,
Subject = "Activity transfer request initiated",
Body = body
};
await _notificationsDao.SendMessageAsync(emailRequest);
}
catch (Exception ex)
{
Log.Error(ex, "Failed to send transfer request initiated notification");
}
}
}