refactor(notification-outbox): extract EmailAddressValidator helper, drop orphaned using
This commit is contained in:
@@ -91,7 +91,7 @@ public sealed class EmailNotificationDeliveryAdapter : INotificationDeliveryAdap
|
||||
|
||||
// A malformed sender or recipient address cannot be fixed by retrying —
|
||||
// surface it as a permanent failure (mirrors NS-008).
|
||||
var addressError = NotificationDeliveryService.ValidateAddresses(
|
||||
var addressError = EmailAddressValidator.ValidateAddresses(
|
||||
smtpConfig.FromAddress, recipients);
|
||||
if (addressError != null)
|
||||
{
|
||||
|
||||
38
src/ScadaLink.NotificationService/EmailAddressValidator.cs
Normal file
38
src/ScadaLink.NotificationService/EmailAddressValidator.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using MimeKit;
|
||||
using ScadaLink.Commons.Entities.Notifications;
|
||||
|
||||
namespace ScadaLink.NotificationService;
|
||||
|
||||
/// <summary>
|
||||
/// NS-008: Validates the sender and recipient email addresses before an SMTP
|
||||
/// delivery is attempted, so a malformed address surfaces as a clean error
|
||||
/// string rather than a <c>ParseException</c> escaping the delivery path.
|
||||
/// <para>
|
||||
/// Public so the central Notification Outbox's <c>EmailNotificationDeliveryAdapter</c>
|
||||
/// can share this exact pre-send validation rather than carry a divergent copy.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public static class EmailAddressValidator
|
||||
{
|
||||
/// <summary>
|
||||
/// Validates the sender and recipient email addresses, returning a
|
||||
/// human-readable error string if any is malformed, or null if all parse.
|
||||
/// </summary>
|
||||
public static string? ValidateAddresses(
|
||||
string fromAddress, IReadOnlyList<NotificationRecipient> recipients)
|
||||
{
|
||||
if (!MailboxAddress.TryParse(fromAddress, out _))
|
||||
{
|
||||
return $"Invalid sender (from) email address: '{fromAddress}'";
|
||||
}
|
||||
|
||||
var invalid = recipients
|
||||
.Where(r => !MailboxAddress.TryParse(r.EmailAddress, out _))
|
||||
.Select(r => r.EmailAddress)
|
||||
.ToList();
|
||||
|
||||
return invalid.Count > 0
|
||||
? $"Invalid recipient email address(es): {string.Join(", ", invalid)}"
|
||||
: null;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
using System.Text.Json;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MimeKit;
|
||||
using ScadaLink.Commons.Entities.Notifications;
|
||||
using ScadaLink.Commons.Interfaces.Repositories;
|
||||
using ScadaLink.Commons.Interfaces.Services;
|
||||
@@ -91,7 +90,7 @@ public class NotificationDeliveryService : INotificationDeliveryService, IDispos
|
||||
// malformed address previously caused MailboxAddress.Parse to throw a
|
||||
// ParseException that escaped SendAsync unhandled; it must instead produce a
|
||||
// clean NotificationResult the calling script can handle.
|
||||
var addressError = ValidateAddresses(smtpConfig.FromAddress, recipients);
|
||||
var addressError = EmailAddressValidator.ValidateAddresses(smtpConfig.FromAddress, recipients);
|
||||
if (addressError != null)
|
||||
{
|
||||
_logger.LogWarning("Notification to list {List} has invalid addresses: {Reason}", listName, addressError);
|
||||
@@ -225,7 +224,7 @@ public class NotificationDeliveryService : INotificationDeliveryService, IDispos
|
||||
}
|
||||
|
||||
// NS-008: a malformed address cannot be fixed by retrying — park it.
|
||||
var addressError = ValidateAddresses(smtpConfig.FromAddress, recipients);
|
||||
var addressError = EmailAddressValidator.ValidateAddresses(smtpConfig.FromAddress, recipients);
|
||||
if (addressError != null)
|
||||
{
|
||||
_logger.LogError(
|
||||
@@ -344,33 +343,6 @@ public class NotificationDeliveryService : INotificationDeliveryService, IDispos
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// NS-008: Validates the sender and recipient email addresses, returning a
|
||||
/// human-readable error string if any is malformed, or null if all parse.
|
||||
/// <para>
|
||||
/// Public and shared: the central Notification Outbox's
|
||||
/// <c>EmailNotificationDeliveryAdapter</c> applies the same pre-send address
|
||||
/// validation, so both delivery paths use this single implementation.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public static string? ValidateAddresses(
|
||||
string fromAddress, IReadOnlyList<NotificationRecipient> recipients)
|
||||
{
|
||||
if (!MailboxAddress.TryParse(fromAddress, out _))
|
||||
{
|
||||
return $"Invalid sender (from) email address: '{fromAddress}'";
|
||||
}
|
||||
|
||||
var invalid = recipients
|
||||
.Where(r => !MailboxAddress.TryParse(r.EmailAddress, out _))
|
||||
.Select(r => r.EmailAddress)
|
||||
.ToList();
|
||||
|
||||
return invalid.Count > 0
|
||||
? $"Invalid recipient email address(es): {string.Join(", ", invalid)}"
|
||||
: null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Delivers an email via SMTP. Throws on failure (transient errors and
|
||||
/// <see cref="SmtpPermanentException"/> propagate; the caller classifies them).
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
using System.Net.Sockets;
|
||||
using MailKit;
|
||||
using MailKit.Net.Smtp;
|
||||
using MailKit.Security;
|
||||
|
||||
namespace ScadaLink.NotificationService.Tests;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user