Completed refactor
This commit is contained in:
@@ -5,7 +5,7 @@ using LiteCharms.Features.Shop;
|
||||
|
||||
namespace LiteCharms.Features.Email;
|
||||
|
||||
public class EmailService(IOptions<SmtpSettings> options) : IEmailService
|
||||
public class EmailService(IOptions<SmtpSettings> options) : IDisposable
|
||||
{
|
||||
private readonly SmtpSettings settings = options.Value;
|
||||
private readonly SmtpClient client = new();
|
||||
@@ -14,7 +14,7 @@ public class EmailService(IOptions<SmtpSettings> options) : IEmailService
|
||||
|
||||
public EmailStatuses Status { get; private set; } = EmailStatuses.Disconnected;
|
||||
|
||||
public async Task<Result<Response>> SendEmailAsync(Message message, CancellationToken cancellationToken = default)
|
||||
public async ValueTask<Result<Response>> SendEmailAsync(Message message, CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var activity = EmailTelemetry.Source.StartActivity("Email Send");
|
||||
activity?.SetTag("email.recipient", message.Recipient?.Address);
|
||||
@@ -100,7 +100,7 @@ public class EmailService(IOptions<SmtpSettings> options) : IEmailService
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Result<Response>> ConnectAsync(CancellationToken cancellationToken = default)
|
||||
public async ValueTask<Result<Response>> ConnectAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var activity = EmailTelemetry.Source.StartActivity("Email Connect");
|
||||
activity?.SetTag("email.smtp.connect", settings.Host);
|
||||
@@ -153,7 +153,7 @@ public class EmailService(IOptions<SmtpSettings> options) : IEmailService
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Result> DisconnectAsync(CancellationToken cancellationToken = default)
|
||||
public async ValueTask<Result> DisconnectAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var activity = EmailTelemetry.Source.StartActivity("Email Disconnect");
|
||||
activity?.SetTag("email.smtp.disconnect", settings.Host);
|
||||
|
||||
@@ -1,22 +1,27 @@
|
||||
using LiteCharms.Features.Notifications.Commands;
|
||||
using LiteCharms.Features.Shop;
|
||||
using LiteCharms.Features.Shop;
|
||||
using LiteCharms.Features.Shop.Notifications;
|
||||
using static LiteCharms.Features.Email.Extensions.Constants;
|
||||
|
||||
namespace LiteCharms.Features.Email.Events.Handlers;
|
||||
|
||||
// TODO: Inject the INotificationService
|
||||
public class SendShopEmailEnquiryEventHandler(ISender mediator) :
|
||||
public class SendShopEmailEnquiryEventHandler(NotificationService notificationService) :
|
||||
INotificationHandler<SendShopEmailEnquiryEvent>
|
||||
{
|
||||
public async ValueTask Handle(SendShopEmailEnquiryEvent notification, CancellationToken cancellationToken)
|
||||
{
|
||||
// TODO: Refactor this to use the NotificationService
|
||||
var command = CreateNotification.Create(NotificationDirection.Outgoing, notification.SenderName!,
|
||||
notification.SenderAddress!, notification.Subject!, notification.Message!, NotificationPlatforms.Email,
|
||||
notification.Priority, ShopEmailFromName, ShopEmailFromAddress, Guid.CreateVersion7().ToString(),
|
||||
CorrelationIdTypes.None, isInternal: true, isHtml: false);
|
||||
|
||||
// TODO: Remove, deprecated
|
||||
await mediator.Send(command, cancellationToken);
|
||||
}
|
||||
public async ValueTask Handle(SendShopEmailEnquiryEvent notification, CancellationToken cancellationToken) =>
|
||||
await notificationService.CreateNotificationAsync(new Shop.Notifications.Models.CreateNotification
|
||||
{
|
||||
CorrelationId = notification.CorrelationId,
|
||||
CorrelationIdType = CorrelationIdTypes.None,
|
||||
Direction = NotificationDirection.Outgoing,
|
||||
IsHtml = false,
|
||||
IsInternal = true,
|
||||
Message = notification.Message,
|
||||
Platform = NotificationPlatforms.Email,
|
||||
Priority = notification.Priority,
|
||||
Subject = notification.Subject!,
|
||||
Sender = notification.SenderName!,
|
||||
SenderAddress = notification.SenderAddress!,
|
||||
Recipient = ShopEmailFromName,
|
||||
RecipientAddress = ShopEmailFromAddress
|
||||
}, cancellationToken);
|
||||
}
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
using LiteCharms.Features.Email.Configuration;
|
||||
|
||||
namespace LiteCharms.Features.Email.Extensions;
|
||||
|
||||
public static class Setup
|
||||
{
|
||||
public static IServiceCollection AddEmailServices(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.Configure<SmtpSettings>(configuration.GetSection("Email"));
|
||||
|
||||
services.AddSingleton<IEmailService, EmailService>();
|
||||
|
||||
services.AddOpenTelemetry()
|
||||
.WithTracing(tracing => tracing.AddSource("LiteCharms.EmailService"))
|
||||
.WithMetrics(metrics => metrics.AddMeter("LiteCharms.EmailService"));
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
using LiteCharms.Features.Email.Models;
|
||||
using LiteCharms.Features.Shop;
|
||||
|
||||
namespace LiteCharms.Features.Email;
|
||||
|
||||
public interface IEmailService : IDisposable
|
||||
{
|
||||
EmailStatuses Status { get; }
|
||||
|
||||
Task<Result<Response>> SendEmailAsync(Message message, CancellationToken cancellationToken = default);
|
||||
|
||||
Task<Result<Response>> ConnectAsync(CancellationToken cancellationToken = default);
|
||||
|
||||
Task<Result> DisconnectAsync(CancellationToken cancellationToken = default);
|
||||
}
|
||||
Reference in New Issue
Block a user