-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Service logic for creating new sms notification #GCPActive (#402)
* SmsNotificationServie create notification * Update service to handle empty adresses * set lower idle connection for integrationtests * added unit test and migration script * order processing retry implemented * fixed code smell * Seperated tets for order processing * added test for repository method * Seperate services for order processing per channel * added unit tests * unifying use of switch and if --------- Co-authored-by: Stephanie Buadu <47737608+acn-sbuad@users.noreply.github.com> Co-authored-by: acn-sbuad <stephanie.buadu@avanade.com>
- Loading branch information
1 parent
c2f5125
commit f26e059
Showing
22 changed files
with
852 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/Altinn.Notifications.Core/Models/Recipients/SmsRecipient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace Altinn.Notifications.Core.Models.Recipients; | ||
|
||
/// <summary> | ||
/// Class representing an sms recipient | ||
/// </summary> | ||
public class SmsRecipient | ||
{ | ||
/// <summary> | ||
/// Gets or sets the recipient id | ||
/// </summary> | ||
public string? RecipientId { get; set; } = null; | ||
|
||
/// <summary> | ||
/// Gets or sets the mobile number | ||
/// </summary> | ||
public string MobileNumber { get; set; } = string.Empty; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/Altinn.Notifications.Core/Services/EmailOrderProcessingService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using Altinn.Notifications.Core.Enums; | ||
using Altinn.Notifications.Core.Models; | ||
using Altinn.Notifications.Core.Models.Address; | ||
using Altinn.Notifications.Core.Models.Orders; | ||
using Altinn.Notifications.Core.Models.Recipients; | ||
using Altinn.Notifications.Core.Persistence; | ||
using Altinn.Notifications.Core.Services.Interfaces; | ||
|
||
namespace Altinn.Notifications.Core.Services; | ||
|
||
/// <summary> | ||
/// Implementation of the <see cref="IEmailOrderProcessingService"/> | ||
/// </summary> | ||
public class EmailOrderProcessingService : IEmailOrderProcessingService | ||
{ | ||
private readonly IEmailNotificationRepository _emailNotificationRepository; | ||
private readonly IEmailNotificationService _emailService; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="OrderProcessingService"/> class. | ||
/// </summary> | ||
public EmailOrderProcessingService( | ||
IEmailNotificationRepository emailNotificationRepository, | ||
IEmailNotificationService emailService) | ||
{ | ||
_emailNotificationRepository = emailNotificationRepository; | ||
_emailService = emailService; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async Task ProcessOrder(NotificationOrder order) | ||
{ | ||
foreach (Recipient recipient in order.Recipients) | ||
{ | ||
await _emailService.CreateNotification(order.Id, order.RequestedSendTime, recipient); | ||
} | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async Task ProcessOrderRetry(NotificationOrder order) | ||
{ | ||
List<EmailRecipient> emailRecipients = await _emailNotificationRepository.GetRecipients(order.Id); | ||
foreach (Recipient recipient in order.Recipients) | ||
{ | ||
EmailAddressPoint? addressPoint = recipient.AddressInfo.Find(a => a.AddressType == AddressType.Email) as EmailAddressPoint; | ||
|
||
if (!emailRecipients.Exists(er => | ||
er.RecipientId == (string.IsNullOrEmpty(recipient.RecipientId) ? null : recipient.RecipientId) | ||
&& er.ToAddress.Equals(addressPoint?.EmailAddress))) | ||
{ | ||
await _emailService.CreateNotification(order.Id, order.RequestedSendTime, recipient); | ||
} | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/Altinn.Notifications.Core/Services/Interfaces/IEmailOrderProcessingService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Altinn.Notifications.Core.Models.Orders; | ||
|
||
namespace Altinn.Notifications.Core.Services.Interfaces; | ||
|
||
/// <summary> | ||
/// Interface for the order processing service speficic to email orders | ||
/// </summary> | ||
public interface IEmailOrderProcessingService | ||
{ | ||
/// <summary> | ||
/// Processes a notification order | ||
/// </summary> | ||
public Task ProcessOrder(NotificationOrder order); | ||
|
||
/// <summary> | ||
/// Retry processing of an order | ||
/// </summary> | ||
public Task ProcessOrderRetry(NotificationOrder order); | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Altinn.Notifications.Core/Services/Interfaces/ISmsNotificationService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Altinn.Notifications.Core.Models; | ||
|
||
namespace Altinn.Notifications.Core.Services.Interfaces; | ||
|
||
/// <summary> | ||
/// Interface for sms notification service | ||
/// </summary> | ||
public interface ISmsNotificationService | ||
{ | ||
/// <summary> | ||
/// Creates a new sms notification based on the provided orderId and recipient | ||
/// </summary> | ||
public Task CreateNotification(Guid orderId, DateTime requestedSendTime, Recipient recipient); | ||
} |
19 changes: 19 additions & 0 deletions
19
src/Altinn.Notifications.Core/Services/Interfaces/ISmsOrderProcessingService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Altinn.Notifications.Core.Models.Orders; | ||
|
||
namespace Altinn.Notifications.Core.Services.Interfaces; | ||
|
||
/// <summary> | ||
/// Interface for the order processing service speficic to sms orders | ||
/// </summary> | ||
public interface ISmsOrderProcessingService | ||
{ | ||
/// <summary> | ||
/// Processes a notification order | ||
/// </summary> | ||
public Task ProcessOrder(NotificationOrder order); | ||
|
||
/// <summary> | ||
/// Retry processing of an order | ||
/// </summary> | ||
public Task ProcessOrderRetry(NotificationOrder order); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/Altinn.Notifications.Core/Services/SmsNotificationService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using Altinn.Notifications.Core.Enums; | ||
using Altinn.Notifications.Core.Models; | ||
using Altinn.Notifications.Core.Models.Address; | ||
using Altinn.Notifications.Core.Models.Notification; | ||
using Altinn.Notifications.Core.Persistence; | ||
using Altinn.Notifications.Core.Services.Interfaces; | ||
|
||
namespace Altinn.Notifications.Core.Services; | ||
|
||
/// <summary> | ||
/// Implementation of <see cref="ISmsNotificationService"/> | ||
/// </summary> | ||
public class SmsNotificationService : ISmsNotificationService | ||
{ | ||
private readonly IGuidService _guid; | ||
private readonly IDateTimeService _dateTime; | ||
private readonly ISmsNotificationRepository _repository; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="SmsNotificationService"/> class. | ||
/// </summary> | ||
public SmsNotificationService( | ||
IGuidService guid, | ||
IDateTimeService dateTime, | ||
ISmsNotificationRepository repository) | ||
{ | ||
_guid = guid; | ||
_dateTime = dateTime; | ||
_repository = repository; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async Task CreateNotification(Guid orderId, DateTime requestedSendTime, Recipient recipient) | ||
{ | ||
SmsAddressPoint? addressPoint = recipient.AddressInfo.Find(a => a.AddressType == AddressType.Sms) as SmsAddressPoint; | ||
|
||
if (!string.IsNullOrEmpty(addressPoint?.MobileNumber)) | ||
{ | ||
await CreateNotificationForRecipient(orderId, requestedSendTime, recipient.RecipientId, addressPoint!.MobileNumber, SmsNotificationResultType.New); | ||
} | ||
else | ||
{ | ||
await CreateNotificationForRecipient(orderId, requestedSendTime, recipient.RecipientId, string.Empty, SmsNotificationResultType.Failed_RecipientNotIdentified); | ||
} | ||
} | ||
|
||
private async Task CreateNotificationForRecipient(Guid orderId, DateTime requestedSendTime, string recipientId, string recipientNumber, SmsNotificationResultType type) | ||
{ | ||
var smsNotification = new SmsNotification() | ||
{ | ||
Id = _guid.NewGuid(), | ||
OrderId = orderId, | ||
RequestedSendTime = requestedSendTime, | ||
RecipientNumber = recipientNumber, | ||
RecipientId = string.IsNullOrEmpty(recipientId) ? null : recipientId, | ||
SendResult = new(type, _dateTime.UtcNow()) | ||
}; | ||
|
||
await _repository.AddNotification(smsNotification, requestedSendTime.AddHours(1)); | ||
} | ||
} |
Oops, something went wrong.