351 lines
12 KiB
C#
351 lines
12 KiB
C#
/*
|
|
|-------------------------------------------------------------------------------|
|
|
| Central Email Log |
|
|
| ------------------------------------------------------------------------ |
|
|
| Every outbound mail in the system writes one MasterEmailLog row per send |
|
|
| operation and one MasterEmailLogDetails row per recipient, so that a later |
|
|
| retry can target only the recipients that actually failed. |
|
|
|-------------------------------------------------------------------------------|
|
|
*/
|
|
|
|
using Ease.Core.Model;
|
|
using HRM.BO.Configuration;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace HRM.BO
|
|
{
|
|
#region MasterEmailLog
|
|
|
|
/// <summary>
|
|
/// The header of one send operation: what was sent, to how many people,
|
|
/// how many of them it reached.
|
|
/// </summary>
|
|
public class MasterEmailLog : AuditTrailBase
|
|
{
|
|
#region Constructor
|
|
|
|
public MasterEmailLog()
|
|
{
|
|
EmailType = EnumEmailType.None;
|
|
Description = string.Empty;
|
|
ParameterJson = string.Empty;
|
|
BatchStatus = EnumEmailBatchStatus.Pending;
|
|
PreserveAttachment = true;
|
|
TotalCount = 0;
|
|
SuccessCount = 0;
|
|
FailedCount = 0;
|
|
SkippedCount = 0;
|
|
RetryCount = 0;
|
|
Details = new List<MasterEmailLogDetails>();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Properties
|
|
|
|
public EnumEmailType EmailType { get; set; }
|
|
public string Description { get; set; }
|
|
public int? PayrollTypeID { get; set; }
|
|
|
|
/// <summary>
|
|
/// The inputs this batch was built from, serialised as JSON
|
|
/// (e.g. {"salaryMonth":"2026-03-31"}). Kept so a batch can be
|
|
/// understood - and re-created - long after it ran.
|
|
/// </summary>
|
|
public string ParameterJson { get; set; }
|
|
|
|
public int TotalCount { get; set; }
|
|
public int SuccessCount { get; set; }
|
|
public int FailedCount { get; set; }
|
|
public int SkippedCount { get; set; }
|
|
|
|
public EnumEmailBatchStatus BatchStatus { get; set; }
|
|
public DateTime? StartTime { get; set; }
|
|
public DateTime? EndTime { get; set; }
|
|
public int RetryCount { get; set; }
|
|
public DateTime? LastRetryDate { get; set; }
|
|
|
|
/// <summary>
|
|
/// When true the bytes that were actually mailed are stored on each
|
|
/// detail row, which lets any mail type be retried without knowing how
|
|
/// its document is produced. Turn it off for very large batches.
|
|
/// </summary>
|
|
public bool PreserveAttachment { get; set; }
|
|
|
|
#endregion
|
|
|
|
#region Read-only / display properties
|
|
|
|
/// <summary>Set by the service so grids do not have to map the enum.</summary>
|
|
public string EmailTypeName { get; set; }
|
|
|
|
public string BatchStatusName { get; set; }
|
|
|
|
/// <summary>Who started the batch. Filled in only by the list query.</summary>
|
|
public string CreatedByName { get; set; }
|
|
|
|
public List<MasterEmailLogDetails> Details { get; set; }
|
|
|
|
#endregion
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region MasterEmailLogDetails
|
|
|
|
/// <summary>
|
|
/// One recipient of one send operation, with the outcome for that recipient.
|
|
/// </summary>
|
|
public class MasterEmailLogDetails : AuditTrailBase
|
|
{
|
|
#region Constructor
|
|
|
|
public MasterEmailLogDetails()
|
|
{
|
|
MailFrom = string.Empty;
|
|
MailTo = string.Empty;
|
|
CC = string.Empty;
|
|
Subject = string.Empty;
|
|
Body = string.Empty;
|
|
ErrorMessage = string.Empty;
|
|
SendStatus = EnumEmailSendStatus.Pending;
|
|
AttemptCount = 0;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Properties
|
|
|
|
public int MasterEmailLogID { get; set; }
|
|
|
|
/// <summary>Null when the recipient is not an employee (candidate, external address).</summary>
|
|
public int? EmployeeID { get; set; }
|
|
|
|
/// <summary>Copied at send time so the log still reads correctly if the employee is edited later.</summary>
|
|
public string EmployeeNo { get; set; }
|
|
|
|
public string EmployeeName { get; set; }
|
|
|
|
public string MailFrom { get; set; }
|
|
public string MailTo { get; set; }
|
|
public string CC { get; set; }
|
|
public string Subject { get; set; }
|
|
public string Body { get; set; }
|
|
|
|
/// <summary>Null when this mail type carries no attachment.</summary>
|
|
public string AttachmentName { get; set; }
|
|
|
|
/// <summary>
|
|
/// The exact bytes that were mailed. Null when the batch ran with
|
|
/// PreserveAttachment = false, or when there is no attachment at all.
|
|
/// Not loaded by list queries - use GetAttachment(detailID).
|
|
/// </summary>
|
|
public byte[] AttachmentData { get; set; }
|
|
|
|
public DateTime? SalaryMonth { get; set; }
|
|
|
|
/// <summary>Generic hook for the mail type's own key: TaxParameterID, BonusID, ...</summary>
|
|
public int? ReferenceID { get; set; }
|
|
|
|
public EnumEmailSendStatus SendStatus { get; set; }
|
|
public string ErrorMessage { get; set; }
|
|
public DateTime? SentTime { get; set; }
|
|
public int AttemptCount { get; set; }
|
|
public DateTime? LastRetryDate { get; set; }
|
|
|
|
#endregion
|
|
|
|
#region Read-only / display properties
|
|
|
|
public string SendStatusName { get; set; }
|
|
|
|
public bool HasAttachment { get; set; }
|
|
|
|
#endregion
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Email dispatch request models
|
|
|
|
/// <summary>
|
|
/// One recipient handed to <see cref="IEmailDispatchService"/>. The caller
|
|
/// fills in the addressing and the message; the dispatcher owns everything
|
|
/// after that - logging, sending, error capture and status.
|
|
/// </summary>
|
|
public class EmailRecipient
|
|
{
|
|
public EmailRecipient()
|
|
{
|
|
MailTo = string.Empty;
|
|
CC = string.Empty;
|
|
Subject = string.Empty;
|
|
Body = string.Empty;
|
|
}
|
|
|
|
public int? EmployeeID { get; set; }
|
|
public string EmployeeNo { get; set; }
|
|
public string EmployeeName { get; set; }
|
|
|
|
public string MailTo { get; set; }
|
|
public string CC { get; set; }
|
|
public string Subject { get; set; }
|
|
public string Body { get; set; }
|
|
|
|
public DateTime? SalaryMonth { get; set; }
|
|
public int? ReferenceID { get; set; }
|
|
|
|
/// <summary>
|
|
/// Set by the caller when this recipient must not be attempted at all -
|
|
/// for example "Email address missing". The row is logged as Skipped
|
|
/// with this text instead of being silently dropped.
|
|
/// </summary>
|
|
public string SkipReason { get; set; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// The attachment produced for one recipient. Returning null from the
|
|
/// resolver means "no attachment for this recipient", which is a normal
|
|
/// outcome - not every mail type carries a file.
|
|
/// </summary>
|
|
public class EmailAttachmentInfo
|
|
{
|
|
public EmailAttachmentInfo()
|
|
{
|
|
}
|
|
|
|
public EmailAttachmentInfo(string fileName, byte[] data)
|
|
{
|
|
FileName = fileName;
|
|
Data = data;
|
|
}
|
|
|
|
public string FileName { get; set; }
|
|
public byte[] Data { get; set; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Everything needed to run - and log - one send operation, for any mail type.
|
|
/// </summary>
|
|
public class EmailDispatchRequest
|
|
{
|
|
public EmailDispatchRequest()
|
|
{
|
|
EmailType = EnumEmailType.None;
|
|
Description = string.Empty;
|
|
ParameterJson = string.Empty;
|
|
PreserveAttachment = true;
|
|
DelayBetweenMailsInMs = 500;
|
|
Recipients = new List<EmailRecipient>();
|
|
}
|
|
|
|
public EnumEmailType EmailType { get; set; }
|
|
public string Description { get; set; }
|
|
public int? PayrollTypeID { get; set; }
|
|
public string ParameterJson { get; set; }
|
|
public bool PreserveAttachment { get; set; }
|
|
|
|
/// <summary>Pause between two mails so the SMTP server is not flooded.</summary>
|
|
public int DelayBetweenMailsInMs { get; set; }
|
|
|
|
public int UserID { get; set; }
|
|
|
|
public List<EmailRecipient> Recipients { get; set; }
|
|
|
|
/// <summary>
|
|
/// Produces the attachment for one recipient, or null when there is
|
|
/// none. Called inside the dispatcher's per-recipient try/catch, so a
|
|
/// document that fails to build is recorded against that one employee
|
|
/// and the batch carries on.
|
|
/// </summary>
|
|
public Func<EmailRecipient, EmailAttachmentInfo> AttachmentResolver { get; set; }
|
|
}
|
|
|
|
/// <summary>What a dispatch or retry run ended up doing.</summary>
|
|
public class EmailDispatchResult
|
|
{
|
|
public int MasterEmailLogID { get; set; }
|
|
public int TotalCount { get; set; }
|
|
public int SuccessCount { get; set; }
|
|
public int FailedCount { get; set; }
|
|
public int SkippedCount { get; set; }
|
|
public EnumEmailBatchStatus BatchStatus { get; set; }
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Service interfaces
|
|
|
|
public interface IMasterEmailLogService
|
|
{
|
|
MasterEmailLog Get(int id);
|
|
List<MasterEmailLog> Get(EnumEmailType emailType, int? payrollTypeID, DateTime fromDate, DateTime toDate);
|
|
MasterEmailLog GetWithDetails(int id);
|
|
|
|
List<MasterEmailLogDetails> GetDetails(int masterEmailLogID);
|
|
List<MasterEmailLogDetails> GetDetails(int masterEmailLogID, EnumEmailSendStatus sendStatus);
|
|
List<MasterEmailLogDetails> GetDetailsByEmployee(int employeeID);
|
|
|
|
/// <summary>The stored bytes of one detail row, or null when nothing was kept.</summary>
|
|
byte[] GetAttachment(int masterEmailLogDetailsID);
|
|
|
|
/// <summary>
|
|
/// Writes the header and every recipient row in one transaction, the
|
|
/// details through SqlBulkCopy. Returns the new MasterEmailLogID.
|
|
/// </summary>
|
|
int SaveBatch(MasterEmailLog log, List<MasterEmailLogDetails> details);
|
|
|
|
/// <summary>Stores the outcome of a single recipient, attachment included.</summary>
|
|
void UpdateDetailResult(MasterEmailLogDetails item);
|
|
|
|
/// <summary>
|
|
/// Recounts the detail rows and writes TotalCount/SuccessCount/
|
|
/// FailedCount/SkippedCount plus the resulting batch status.
|
|
/// </summary>
|
|
MasterEmailLog RefreshBatchCounters(int masterEmailLogID, bool markEnded, int? modifiedBy);
|
|
|
|
void MarkRetryStarted(int masterEmailLogID, int modifiedBy);
|
|
|
|
void Delete(int masterEmailLogID);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The single entry point every module should use to send mail to many
|
|
/// recipients. It logs before it sends, never lets one bad recipient stop
|
|
/// the run, and can replay just the failures of an earlier batch.
|
|
/// </summary>
|
|
public interface IEmailDispatchService
|
|
{
|
|
/// <summary>
|
|
/// Writes the header and every recipient row up front, so the batch is
|
|
/// already visible - and already complete - before the first mail goes
|
|
/// out. Returns the new MasterEmailLogID. Fast: one bulk insert.
|
|
/// </summary>
|
|
int CreateBatch(EmailDispatchRequest request);
|
|
|
|
/// <summary>
|
|
/// Runs the send loop for a batch created by <see cref="CreateBatch"/>.
|
|
/// Long-running; callers that do not want to block should start this on
|
|
/// its own thread and let the caller poll the log instead.
|
|
/// </summary>
|
|
EmailDispatchResult ProcessBatch(int masterEmailLogID, EmailDispatchRequest request,
|
|
EmailSettings emailSettings);
|
|
|
|
/// <summary>CreateBatch followed by ProcessBatch, for callers happy to block.</summary>
|
|
EmailDispatchResult Send(EmailDispatchRequest request, EmailSettings emailSettings);
|
|
|
|
/// <summary>
|
|
/// Re-sends only the Failed rows of an existing batch. Uses each row's
|
|
/// stored attachment; when the batch was run without preserving
|
|
/// attachments, <paramref name="attachmentResolver"/> is asked to
|
|
/// rebuild one (it may be null, in which case the mail goes out with
|
|
/// no file).
|
|
/// </summary>
|
|
EmailDispatchResult RetryFailed(int masterEmailLogID, EmailSettings emailSettings, int userID,
|
|
Func<MasterEmailLogDetails, EmailAttachmentInfo> attachmentResolver);
|
|
}
|
|
|
|
#endregion
|
|
}
|