public IEnumerable <string> HasBody(ISenderConfiguration configuration, SenderRequest request) { if (!configuration.AllowEmptyBody && string.IsNullOrWhiteSpace(request.Body)) { yield return("Message body cannot be empty"); } }
public IEnumerable <string> HasSubject(ISenderConfiguration configuration, SenderRequest request) { if (!configuration.AllowEmptySubject && string.IsNullOrWhiteSpace(request.Subject)) { yield return("Message subject cannot be empty"); } }
private EmailMessage(SenderRequest originalRequest, string from, string to, string?subject, string?body) { OriginalRequest = originalRequest; From = from; To = to; Subject = subject; Body = body; }
public async Task Send(SenderRequest request, CancellationToken cancellationToken = default) { var configuration = await _senderConfigurationRepository.Retrieve(request.ConfigurationId) ?? throw new MissingConfigurationException(request.ConfigurationId); _senderRequestValidator.Validate(configuration, request); var from = request.FromEmail ?? configuration.DefaultFromEmail; var to = configuration.ToEmail; var messageBody = GetMessageBody(configuration, request); await Send(EmailMessage.Create(request, from, to, request.Subject, messageBody), cancellationToken); }
public void Validate(ISenderConfiguration configuration, SenderRequest request) { var results = new[] { HasSubject(configuration, request), HasBody(configuration, request), }; var errorMessages = results.SelectMany(m => m); if (errorMessages.Any()) { throw new InvalidSenderRequestException(errorMessages); } }
private string?GetMessageBody(ISenderConfiguration configuration, SenderRequest request) { if (configuration.TemplateType == null) { return(request.Body); } var templateApplicator = _templateApplicators.FirstOrDefault(a => a.Supports(configuration)); if (templateApplicator == null) { throw new NotSupportedException($"Template type {configuration.TemplateType} is not supported"); } return(templateApplicator.Apply(configuration, request)); }
public static EmailMessage Create(SenderRequest originalRequest, string from, string to, string?subject = null, string?body = null) => new EmailMessage(originalRequest, from, to, subject, body);