Migrated database changes after refactoring the Notification model

This commit is contained in:
Khwezi Mngoma
2026-05-10 15:27:26 +02:00
parent 394429677e
commit e8e9a85c57
20 changed files with 244 additions and 777 deletions
@@ -0,0 +1,61 @@
using LiteCharms.Features.Email.Commands;
using LiteCharms.Models.Configuraton.Email;
namespace LiteCharms.Features.Email.Commands.Handlers;
public class SendEmailCommandHandler(IOptions<SmtpSettings> smtpOptions) : IRequestHandler<SendEmailCommand, Result>
{
public async ValueTask<Result> Handle(SendEmailCommand request, CancellationToken cancellationToken)
{
try
{
var settings = smtpOptions.Value;
if(settings == null)
return Result.Fail(new Error("SMTP settings are not configured."));
if(settings.Credentials == null)
return Result.Fail(new Error("SMTP credentials are not configured."));
if(string.IsNullOrWhiteSpace(settings?.Credentials.Username) || string.IsNullOrWhiteSpace(settings.Credentials.Password))
return Result.Fail(new Error("SMTP credentials are incomplete."));
if(string.IsNullOrWhiteSpace(settings.Host) || settings.Port == 0)
return Result.Fail(new Error("SMTP host and port must be configured."));
var message = new MimeMessage();
message.From.Add(new MailboxAddress(request.SenderName, request.From!));
message.To.Add(new MailboxAddress(request.RecipientName, request.To!));
message.Subject = request.Subject!;
var bodyBuilder = new BodyBuilder();
if(request.Attachment?.Length > 0 && !string.IsNullOrEmpty(request.AttachmentFileName))
bodyBuilder.Attachments.Add(request.AttachmentFileName!, request.Attachment!, cancellationToken);
if (!request.IsHtml) bodyBuilder.TextBody = request.Message;
if (request.IsHtml) bodyBuilder.HtmlBody = request.Message;
message.Body = bodyBuilder.ToMessageBody();
using var client = new SmtpClient();
await client.ConnectAsync(settings.Host!, settings.Port, settings.UseSsl, cancellationToken);
await client.AuthenticateAsync(settings.Credentials!.Username!, settings.Credentials.Password!, cancellationToken);
var response = await client.SendAsync(message, cancellationToken);
bool emailSent = response.Contains("OK", StringComparison.InvariantCultureIgnoreCase);
await client.DisconnectAsync(true, cancellationToken);
return emailSent
? Result.Ok()
: Result.Fail(new Error("Failed to send email. SMTP response: " + response));
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
}
@@ -0,0 +1,79 @@
namespace LiteCharms.Features.Email.Commands;
public class SendEmailCommand : IRequest<Result>
{
public string? From { get; set; }
public string? SenderName { get; set; }
public string? To { get; set; }
public string? RecipientName { get; set; }
public string? Subject { get; set; }
public string? Message { get; set; }
public bool IsHtml { get; set; }
public Stream? Attachment { get; set; }
public string? AttachmentFileName { get; set; }
private SendEmailCommand(string from, string senderName, string to, string recipientName, string subject, string message, bool isHtml = false, Stream? attachment = null, string? attachmentFileName = null)
{
From = from;
To = to;
Subject = subject;
Message = message;
IsHtml = isHtml;
Attachment = attachment;
AttachmentFileName = attachmentFileName;
SenderName = senderName;
RecipientName = recipientName;
}
public static SendEmailCommand Create(string from, string senderName, string to, string recipientName, string subject, string message, bool isHtml = false, Stream? attachment = null, string? attachmentFileName = null)
{
if (string.IsNullOrWhiteSpace(from))
throw new ArgumentException("From address is required.");
if (string.IsNullOrWhiteSpace(senderName))
throw new ArgumentException("Sender name is required.");
if (!string.IsNullOrWhiteSpace(senderName) && senderName?.Length > 255)
throw new ArgumentException("Sender name cannot exceed 255 characters.");
if (string.IsNullOrWhiteSpace(to))
throw new ArgumentException("To address is required.");
if (string.IsNullOrWhiteSpace(recipientName))
throw new ArgumentException("Recipient name is required.");
if (!string.IsNullOrWhiteSpace(recipientName) && recipientName?.Length > 255)
throw new ArgumentException("Recipient name cannot exceed 255 characters.");
if (string.IsNullOrWhiteSpace(subject))
throw new ArgumentException("Subject is required.");
if (!string.IsNullOrWhiteSpace(subject) && subject?.Length > 2048)
throw new ArgumentException("Subject cannot exceed 2048 characters.");
if (string.IsNullOrWhiteSpace(message))
throw new ArgumentException("Message is required.");
if (message.Length > 10485760)
throw new ArgumentException("Message cannot exceed 10 MB.");
if (attachment != null && string.IsNullOrWhiteSpace(attachmentFileName))
throw new ArgumentException("Attachment file name must be provided when an attachment is included.");
if (attachment is not null && attachment.Length > 10485760)
throw new ArgumentException("Attachment cannot exceed 10 MB.");
if (!string.IsNullOrWhiteSpace(attachmentFileName) && attachmentFileName.Length > 255)
throw new ArgumentException("Attachment file name cannot exceed 255 characters.");
return new(from, senderName!, to, recipientName!, subject!, message, isHtml, attachment, attachmentFileName);
}
}