Skip to content

Commit

Permalink
Ensuring reference type is only updated with contact point once
Browse files Browse the repository at this point in the history
  • Loading branch information
acn-sbuad committed Mar 18, 2024
1 parent b5053a6 commit c60e19b
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 77 deletions.
23 changes: 9 additions & 14 deletions src/Altinn.Notifications.Core/Services/ContactPointService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,25 @@ public ContactPointService(IProfileClient profile)
}

/// <inheritdoc/>
public async Task<List<Recipient>> GetEmailContactPoints(List<Recipient> recipients)
public async Task AddEmailContactPoints(List<Recipient> recipients)
{
return await AugmentRecipients(
await AugmentRecipients(
recipients,
(recipient, userContactPoints) =>
{
recipient.IsReserved = userContactPoints.IsReserved;

recipient.AddressInfo.Add(new EmailAddressPoint(userContactPoints.Email));

return recipient;
});
}

/// <inheritdoc/>
public async Task<List<Recipient>> GetSmsContactPoints(List<Recipient> recipients)
public async Task AddSmsContactPoints(List<Recipient> recipients)
{
return await AugmentRecipients(
await AugmentRecipients(
recipients,
(recipient, userContactPoints) =>
{
if (userContactPoints.IsReserved)
{
recipient.IsReserved = userContactPoints.IsReserved;
}

recipient.AddressInfo.Add(new SmsAddressPoint(userContactPoints.MobileNumber));

return recipient;
});
}
Expand All @@ -72,7 +63,11 @@ private async Task<List<Recipient>> AugmentRecipients(List<Recipient> recipients
UserContactPoints? userContactPoints = userContactPointsList!
.Find(u => u.NationalIdentityNumber == recipient.NationalIdentityNumber);

augmentedRecipients.Add(createContactPoint(recipient, userContactPoints!));
if (userContactPoints != null)
{
recipient.IsReserved = userContactPoints.IsReserved;
augmentedRecipients.Add(createContactPoint(recipient, userContactPoints));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,11 @@ public async Task ProcessOrder(NotificationOrder order)
var recipients = order.Recipients;
var recipientsWithoutEmail = recipients.Where(r => !r.AddressInfo.Exists(ap => ap.AddressType == AddressType.Email)).ToList();

List<Recipient> agumentedRecipients = await _contactPointService.GetEmailContactPoints(recipientsWithoutEmail);
await _contactPointService.AddEmailContactPoints(recipientsWithoutEmail);

var augmentedRecipientDictionary = agumentedRecipients.ToDictionary(ar => $"{ar.NationalIdentityNumber}-{ar.OrganisationNumber}");

foreach (Recipient originalRecipient in recipients)
foreach (Recipient recipient in recipients)
{
if (augmentedRecipientDictionary.TryGetValue($"{originalRecipient.NationalIdentityNumber}-{originalRecipient.OrganisationNumber}", out Recipient? augmentedRecipient))
{
originalRecipient.AddressInfo.AddRange(augmentedRecipient!.AddressInfo);
}

await _emailService.CreateNotification(order.Id, order.RequestedSendTime, originalRecipient, order.IgnoreReservation);
await _emailService.CreateNotification(order.Id, order.RequestedSendTime, recipient, order.IgnoreReservation);
}
}

Expand All @@ -57,18 +50,12 @@ public async Task ProcessOrderRetry(NotificationOrder order)
var recipients = order.Recipients;
var recipientsWithoutEmail = recipients.Where(r => !r.AddressInfo.Exists(ap => ap.AddressType == AddressType.Email)).ToList();

List<Recipient> agumentedRecipients = await _contactPointService.GetEmailContactPoints(recipientsWithoutEmail);
var augmentedRecipientDictionary = agumentedRecipients.ToDictionary(ar => $"{ar.NationalIdentityNumber}-{ar.OrganisationNumber}");
await _contactPointService.AddEmailContactPoints(recipientsWithoutEmail);

List<EmailRecipient> emailRecipients = await _emailNotificationRepository.GetRecipients(order.Id);

foreach (Recipient recipient in order.Recipients)
{
if (augmentedRecipientDictionary.TryGetValue($"{recipient.NationalIdentityNumber}-{recipient.OrganisationNumber}", out Recipient? augmentedRecipient))
{
recipient.AddressInfo.AddRange(augmentedRecipient!.AddressInfo);
}

EmailAddressPoint? addressPoint = recipient.AddressInfo.Find(a => a.AddressType == AddressType.Email) as EmailAddressPoint;

if (!emailRecipients.Exists(er =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ namespace Altinn.Notifications.Core.Services.Interfaces
public interface IContactPointService
{
/// <summary>
/// Retrieves email contact points for recipients based on their national identity number or organisation number
/// Looks up and adds the email contact points for recipients based on their national identity number or organisation number
/// </summary>
/// <param name="recipients">List of recipients to retrieve contact points for</param>
/// <returns>The list of recipients augumented with email address points where available</returns>
public Task<List<Recipient>> GetEmailContactPoints(List<Recipient> recipients);
/// <remarks>Implementation alters the recipient reference object directly</remarks>
public Task AddEmailContactPoints(List<Recipient> recipients);

/// <summary>
/// Retrieves SMS contact points for recipients based on their national identity number or organisation number
/// Looks up and adds the SMS contact points for recipients based on their national identity number or organisation number
/// </summary>
/// <param name="recipients">List of recipients to retrieve contact points for</param>
/// <returns>The list of recipients augumented with SMS address points where available</returns>
public Task<List<Recipient>> GetSmsContactPoints(List<Recipient> recipients);
/// <remarks>Implementation alters the recipient reference object directly</remarks>
public Task AddSmsContactPoints(List<Recipient> recipients);

/// <summary>
/// Retrieves the availabililty of contact points for the provided recipient based on their national identity number or organisation number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,13 @@ public async Task ProcessOrder(NotificationOrder order)
{
var recipients = order.Recipients;
var recipientsWithoutMobileNumber = recipients.Where(r => !r.AddressInfo.Exists(ap => ap.AddressType == AddressType.Sms)).ToList();
await _contactPointService.AddSmsContactPoints(recipientsWithoutMobileNumber);

List<Recipient> agumentedRecipients = await _contactPointService.GetSmsContactPoints(recipientsWithoutMobileNumber);

var augmentedRecipientDictionary = agumentedRecipients.ToDictionary(ar => $"{ar.NationalIdentityNumber}-{ar.OrganisationNumber}");
int smsCount = GetSmsCountForOrder(order);

foreach (Recipient originalRecipient in recipients)
foreach (Recipient recipient in recipients)
{
if (augmentedRecipientDictionary.TryGetValue($"{originalRecipient.NationalIdentityNumber}-{originalRecipient.OrganisationNumber}", out Recipient? augmentedRecipient))
{
originalRecipient.AddressInfo.AddRange(augmentedRecipient!.AddressInfo);
}

await _smsService.CreateNotification(order.Id, order.RequestedSendTime, originalRecipient, smsCount, order.IgnoreReservation);
await _smsService.CreateNotification(order.Id, order.RequestedSendTime, recipient, smsCount, order.IgnoreReservation);
}
}

Expand All @@ -59,19 +52,13 @@ public async Task ProcessOrderRetry(NotificationOrder order)
var recipients = order.Recipients;
var recipientsWithoutMobileNumber = recipients.Where(r => !r.AddressInfo.Exists(ap => ap.AddressType == AddressType.Sms)).ToList();

List<Recipient> agumentedRecipients = await _contactPointService.GetSmsContactPoints(recipientsWithoutMobileNumber);
var augmentedRecipientDictionary = agumentedRecipients.ToDictionary(ar => $"{ar.NationalIdentityNumber}-{ar.OrganisationNumber}");
await _contactPointService.AddSmsContactPoints(recipientsWithoutMobileNumber);

int smsCount = GetSmsCountForOrder(order);
List<SmsRecipient> smsRecipients = await _smsNotificationRepository.GetRecipients(order.Id);

foreach (Recipient recipient in order.Recipients)
{
if (augmentedRecipientDictionary.TryGetValue($"{recipient.NationalIdentityNumber}-{recipient.OrganisationNumber}", out Recipient? augmentedRecipient))
{
recipient.AddressInfo.AddRange(augmentedRecipient!.AddressInfo);
}

SmsAddressPoint? addressPoint = recipient.AddressInfo.Find(a => a.AddressType == AddressType.Sms) as SmsAddressPoint;

if (!smsRecipients.Exists(sr =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public async Task GetSmsContactPoints_NationalIdentityNumberAvailable_ProfileSer
var service = GetTestService(profileClient: profileClientMock.Object);

// Act
List<Recipient> actual = await service.GetSmsContactPoints(input);
await service.AddSmsContactPoints(input);

// Assert
Assert.Equivalent(expectedOutput, actual);
Assert.Equivalent(expectedOutput, input);
}

[Fact]
Expand Down Expand Up @@ -77,10 +77,10 @@ public async Task GetEmailContactPoints_NationalIdentityNumberAvailable_ProfileS
var service = GetTestService(profileClient: profileClientMock.Object);

// Act
List<Recipient> actual = await service.GetEmailContactPoints(input);
await service.AddEmailContactPoints(input);

// Assert
Assert.Equivalent(expectedOutput, actual);
Assert.Equivalent(expectedOutput, input);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,24 +140,19 @@ public async Task ProcessOrder_RecipientMissingEmail_ContactPointServiceCalled()
s => s.CreateNotification(
It.IsAny<Guid>(),
It.IsAny<DateTime>(),
It.Is<Recipient>(r => (r.NationalIdentityNumber == "123456" && r.AddressInfo.Count == 1)),
It.Is<Recipient>(r => r.NationalIdentityNumber == "123456"),
It.IsAny<bool>()));

var contactPointServiceMock = new Mock<IContactPointService>();
contactPointServiceMock.Setup(c => c.GetEmailContactPoints(It.Is<List<Recipient>>(r => r.Count == 1)))
.ReturnsAsync((List<Recipient> r) =>
{
Recipient augumentedRecipient = new() { AddressInfo = [new EmailAddressPoint("[email protected]")], NationalIdentityNumber = r[0].NationalIdentityNumber };
return new List<Recipient>() { augumentedRecipient };
});
contactPointServiceMock.Setup(c => c.AddEmailContactPoints(It.Is<List<Recipient>>(r => r.Count == 1)));

var service = GetTestService(emailService: notificationServiceMock.Object, contactPointService: contactPointServiceMock.Object);

// Act
await service.ProcessOrder(order);

// Assert
contactPointServiceMock.Verify(c => c.GetEmailContactPoints(It.Is<List<Recipient>>(r => r.Count == 1)), Times.Once);
contactPointServiceMock.Verify(c => c.AddEmailContactPoints(It.Is<List<Recipient>>(r => r.Count == 1)), Times.Once);
notificationServiceMock.VerifyAll();
}

Expand Down Expand Up @@ -219,9 +214,7 @@ private static EmailOrderProcessingService GetTestService(
{
var contactPointServiceMock = new Mock<IContactPointService>();
contactPointServiceMock
.Setup(e => e.GetEmailContactPoints(It.IsAny<List<Recipient>>()))
.ReturnsAsync(
(List<Recipient> recipients) => recipients);
.Setup(e => e.AddEmailContactPoints(It.IsAny<List<Recipient>>()));
contactPointService = contactPointServiceMock.Object;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,17 @@ public async Task ProcessOrder_RecipientMissingMobileNumber_ContactPointServiceC
s => s.CreateNotification(
It.IsAny<Guid>(),
It.IsAny<DateTime>(),
It.Is<Recipient>(r => (r.NationalIdentityNumber == "123456" && r.AddressInfo.Count == 1)),
It.Is<Recipient>(r => r.NationalIdentityNumber == "123456"),
It.IsAny<int>(),
It.IsAny<bool>()));

var contactPointServiceMock = new Mock<IContactPointService>();
contactPointServiceMock.Setup(c => c.GetSmsContactPoints(It.Is<List<Recipient>>(r => r.Count == 1)))
.ReturnsAsync((List<Recipient> r) =>
contactPointServiceMock.Setup(c => c.AddSmsContactPoints(It.Is<List<Recipient>>(r => r.Count == 1)))
.Callback<List<Recipient>>(r =>
{
Recipient augumentedRecipient = new() { AddressInfo = [new SmsAddressPoint("+4712345678")], NationalIdentityNumber = r[0].NationalIdentityNumber };
return new List<Recipient>() { augumentedRecipient };
r.Clear();
r.Add(augumentedRecipient);
});

var service = GetTestService(smsService: notificationServiceMock.Object, contactPointService: contactPointServiceMock.Object);
Expand All @@ -132,7 +133,7 @@ public async Task ProcessOrder_RecipientMissingMobileNumber_ContactPointServiceC
await service.ProcessOrder(order);

// Assert
contactPointServiceMock.Verify(c => c.GetSmsContactPoints(It.Is<List<Recipient>>(r => r.Count == 1)), Times.Once);
contactPointServiceMock.Verify(c => c.AddSmsContactPoints(It.Is<List<Recipient>>(r => r.Count == 1)), Times.Once);
notificationServiceMock.VerifyAll();
}

Expand Down Expand Up @@ -211,10 +212,7 @@ private static SmsOrderProcessingService GetTestService(
if (contactPointService == null)
{
var contactPointServiceMock = new Mock<IContactPointService>();
contactPointServiceMock
.Setup(e => e.GetSmsContactPoints(It.IsAny<List<Recipient>>()))
.ReturnsAsync(
(List<Recipient> recipients) => recipients);
contactPointServiceMock.Setup(e => e.AddSmsContactPoints(It.IsAny<List<Recipient>>()));

contactPointService = contactPointServiceMock.Object;
}
Expand Down

0 comments on commit c60e19b

Please sign in to comment.