From a2a6ebadea37027ce5d8eb02ce2847d545eb240f Mon Sep 17 00:00:00 2001 From: Stephanie Buadu <47737608+acn-sbuad@users.noreply.github.com> Date: Tue, 28 May 2024 13:59:34 +0200 Subject: [PATCH] only copy recipient if lookup is required (#528) --- .../Services/OrderRequestService.cs | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/Altinn.Notifications.Core/Services/OrderRequestService.cs b/src/Altinn.Notifications.Core/Services/OrderRequestService.cs index ba0b79e2..ac3ba2ba 100644 --- a/src/Altinn.Notifications.Core/Services/OrderRequestService.cs +++ b/src/Altinn.Notifications.Core/Services/OrderRequestService.cs @@ -47,9 +47,7 @@ public async Task 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); @@ -74,19 +72,19 @@ public async Task RegisterNotificationOrder(No }; } - private async Task GetRecipientLookupResult(List recipients, NotificationChannel channel, string? resourceId) + private async Task GetRecipientLookupResult(List originalRecipients, NotificationChannel channel, string? resourceId) { List 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()); } } @@ -104,12 +102,12 @@ public async Task 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)) @@ -120,7 +118,7 @@ public async Task RegisterNotificationOrder(No int recipientsWeCannotReach = lookupResult.MissingContact.Union(lookupResult.IsReserved).ToList().Count; - if (recipientsWeCannotReach == recipients.Count) + if (recipientsWeCannotReach == recipientsWithoutContactPoint.Count) { lookupResult.Status = RecipientLookupStatus.Failed; }