Skip to content

Commit

Permalink
only copy recipient if lookup is required (#528)
Browse files Browse the repository at this point in the history
  • Loading branch information
acn-sbuad authored May 28, 2024
1 parent c01b6d7 commit a2a6eba
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/Altinn.Notifications.Core/Services/OrderRequestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ public async Task<NotificationOrderRequestResponse> RegisterNotificationOrder(No
Guid orderId = _guid.NewGuid();
DateTime created = _dateTime.UtcNow();

// copying recipients by value to not alter the orderRequest that will be persisted
var copiedRecipents = orderRequest.Recipients.Select(r => r.DeepCopy()).ToList();
var lookupResult = await GetRecipientLookupResult(copiedRecipents, orderRequest.NotificationChannel, orderRequest.ResourceId);
var lookupResult = await GetRecipientLookupResult(orderRequest.Recipients, orderRequest.NotificationChannel, orderRequest.ResourceId);

var templates = SetSenderIfNotDefined(orderRequest.Templates);

Expand All @@ -74,19 +72,19 @@ public async Task<NotificationOrderRequestResponse> RegisterNotificationOrder(No
};
}

private async Task<RecipientLookupResult?> GetRecipientLookupResult(List<Recipient> recipients, NotificationChannel channel, string? resourceId)
private async Task<RecipientLookupResult?> GetRecipientLookupResult(List<Recipient> originalRecipients, NotificationChannel channel, string? resourceId)
{
List<Recipient> recipientsWithoutContactPoint = [];

foreach (var recipient in recipients)
foreach (var recipient in originalRecipients)
{
if (channel == NotificationChannel.Email && !recipient.AddressInfo.Exists(ap => ap.AddressType == AddressType.Email))
{
recipientsWithoutContactPoint.Add(recipient);
recipientsWithoutContactPoint.Add(recipient.DeepCopy());
}
else if (channel == NotificationChannel.Sms && !recipient.AddressInfo.Exists(ap => ap.AddressType == AddressType.Sms))
{
recipientsWithoutContactPoint.Add(recipient);
recipientsWithoutContactPoint.Add(recipient.DeepCopy());
}
}

Expand All @@ -104,12 +102,12 @@ public async Task<NotificationOrderRequestResponse> RegisterNotificationOrder(No
await _contactPointService.AddSmsContactPoints(recipientsWithoutContactPoint, resourceId);
}

var isReserved = recipients.Where(r => r.IsReserved.HasValue && r.IsReserved.Value).Select(r => r.NationalIdentityNumber!).ToList();
var isReserved = recipientsWithoutContactPoint.Where(r => r.IsReserved.HasValue && r.IsReserved.Value).Select(r => r.NationalIdentityNumber!).ToList();

RecipientLookupResult lookupResult = new()
{
IsReserved = isReserved,
MissingContact = recipients
MissingContact = recipientsWithoutContactPoint
.Where(r => channel == NotificationChannel.Email ?
!r.AddressInfo.Exists(ap => ap.AddressType == AddressType.Email) :
!r.AddressInfo.Exists(ap => ap.AddressType == AddressType.Sms))
Expand All @@ -120,7 +118,7 @@ public async Task<NotificationOrderRequestResponse> RegisterNotificationOrder(No

int recipientsWeCannotReach = lookupResult.MissingContact.Union(lookupResult.IsReserved).ToList().Count;

if (recipientsWeCannotReach == recipients.Count)
if (recipientsWeCannotReach == recipientsWithoutContactPoint.Count)
{
lookupResult.Status = RecipientLookupStatus.Failed;
}
Expand Down

0 comments on commit a2a6eba

Please sign in to comment.