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 receipts, List recipients) { try { #region Body HTML Boilerplate string body = @" "; #endregion foreach (var (departmentName, receipt) in receipts) { if (receipt == null) { body += $@" "; } else if (receipt.GoalDispersalErrors.Any()) { body += $@" "; } } body += @"

Department

Strategy

Alerts

{departmentName}  FAILED  No Receipt Data, Failed to Run Dispersal.
{departmentName}  {receipt.Strategy}  {FormatReceiptItems(receipt.GoalDispersalErrors)}
"; 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 usersMissingRole, List recipients) { try { #region Body HTML Boilerplate string body = @" "; #endregion foreach (var user in usersMissingRole) { body += $@" "; } body += @"

Name

Employee ID

Department

Missing Role

{user.FullName} {user.EmployeeId} {user.HomeDepartment} {user.BaseRole}
"; 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 terminated, List recipients) { try { #region Body HTML Boilerplate string body = @" "; #endregion foreach (var receipt in terminated) { var manager = receipt.Manager != null ? receipt.Manager.FullName : "Not Found"; body += $@" "; } body += @"

Name

Employee ID

Department

Terminated

Manager

Prev Month Payout

This Month Payout

{receipt.Terminated.FullName} {receipt.Terminated.EmployeeId} {receipt.Terminated.HomeDepartment} {receipt.Terminated.Terminated.Value:MM/dd/yyyy} {manager} ${receipt.LastMonthPayout.PayoutTotal}.00 ${receipt.ThisMonthPayout.PayoutTotal}.00
"; 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 receiptErrors) { string orderedList = @"
    "; foreach (var error in receiptErrors) { orderedList += $@"
  1. {error.EmployeeName} - {error.EmployeeId}: {error.Message}

  2. "; } orderedList += @"
"; return orderedList; } public async Task SendTransferRequestInitiatedNotification(ActivityTransferAction transferAction, List recipients) { try { #region Body HTML Boilerplate string body = @""; #endregion body += @"

A transfer request has been initiated for the following activity:

"; body += $@"

Product

Activity Type

Requested by

Transfer to Employee

Transfer to Department

{transferAction.RequestedActivity.Product} {transferAction.RequestedActivity.Type} {transferAction.CreatedBy} {transferAction.NewActivityEmployeeId} {transferAction.NewActivityDepartmentId}
"; var webAppUrl = $@"{_incentiveApiConfig.IncentiveManagerClientUrl}/activity/{transferAction.RequestedActivityId}"; body += $@"

The request can be approved at {webAppUrl}.

"; if (_emailRecipientsConfig.OverrideRecipients) { body += $"

The original recipients have been overriden in this environment: {string.Join(", ", recipients)}

"; 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"); } } }