Skip to content

Commit

Permalink
Extended method for setting default value when empty
Browse files Browse the repository at this point in the history
  • Loading branch information
khanrn committed Jan 16, 2024
1 parent 4762334 commit 9e40f09
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Altinn.Notifications.Core/Services/OrderRequestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class OrderRequestService : IOrderRequestService
private readonly IGuidService _guid;
private readonly IDateTimeService _dateTime;
private readonly string _defaultEmailFromAddress;
private readonly string _defaultSenderNumber;

/// <summary>
/// Initializes a new instance of the <see cref="OrderRequestService"/> class.
Expand All @@ -28,6 +29,7 @@ public OrderRequestService(IOrderRepository repository, IGuidService guid, IDate
_guid = guid;
_dateTime = dateTime;
_defaultEmailFromAddress = config.Value.DefaultEmailFromAddress;
_defaultSenderNumber = config.Value.DefaultSenderNumber;
}

/// <inheritdoc/>
Expand All @@ -36,7 +38,7 @@ public OrderRequestService(IOrderRepository repository, IGuidService guid, IDate
Guid orderId = _guid.NewGuid();
DateTime created = _dateTime.UtcNow();

var templates = SetFromAddressIfNotDefined(orderRequest.Templates);
var templates = SetFromAddressOrSenderNumberIfNotDefined(orderRequest.Templates);

var order = new NotificationOrder(
orderId,
Expand All @@ -53,13 +55,18 @@ public OrderRequestService(IOrderRepository repository, IGuidService guid, IDate
return (savedOrder, null);
}

private List<INotificationTemplate> SetFromAddressIfNotDefined(List<INotificationTemplate> templates)
private List<INotificationTemplate> SetFromAddressOrSenderNumberIfNotDefined(List<INotificationTemplate> templates)
{
foreach (var template in templates.OfType<EmailTemplate>().Where(template => string.IsNullOrEmpty(template.FromAddress)))
{
template.FromAddress = _defaultEmailFromAddress;
}

foreach (var template in templates.OfType<SmsTemplate>().Where(template => string.IsNullOrEmpty(template.SenderNumber)))
{
template.SenderNumber = _defaultSenderNumber;
}

return templates;
}
}

0 comments on commit 9e40f09

Please sign in to comment.