Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
acn-sbuad committed Mar 6, 2024
2 parents 60e2a53 + f6d14f4 commit 7e4c447
Show file tree
Hide file tree
Showing 31 changed files with 391 additions and 95 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Altinn.Notifications.Core.Enums;
using Altinn.Notifications.Core.Models.Recipients;

namespace Altinn.Notifications.Core.Models.Notification;

Expand All @@ -20,14 +21,9 @@ public class EmailNotification : INotification<EmailNotificationResultType>
public NotificationChannel NotificationChannel { get; } = NotificationChannel.Email;

/// <summary>
/// Get the id of the recipient of the email notification
/// Gets the recipient of the notification
/// </summary>
public string? RecipientId { get; internal set; }

/// <summary>
/// Get or sets the to address of the email notification
/// </summary>
public string ToAddress { get; internal set; } = string.Empty;
public EmailRecipient Recipient { get; internal set; } = new();

/// <summary>
/// Get or sets the send result of the notification
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Altinn.Notifications.Core.Enums;
using Altinn.Notifications.Core.Models.Recipients;

namespace Altinn.Notifications.Core.Models.Notification;

Expand All @@ -20,14 +21,9 @@ public class SmsNotification : INotification<SmsNotificationResultType>
public NotificationChannel NotificationChannel { get; } = NotificationChannel.Sms;

/// <summary>
/// Get the id of the recipient of the sms notification
/// Get the recipient of the notification
/// </summary>
public string? RecipientId { get; internal set; }

/// <summary>
/// Get or sets the mobilenumber of the sms notification
/// </summary>
public string RecipientNumber { get; internal set; } = string.Empty;
public SmsRecipient Recipient { get; internal set; } = new();

/// <inheritdoc/>
public NotificationResult<SmsNotificationResultType> SendResult { get; internal set; } = new(SmsNotificationResultType.New, DateTime.UtcNow);
Expand Down
20 changes: 9 additions & 11 deletions src/Altinn.Notifications.Core/Models/Recipient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,27 @@ namespace Altinn.Notifications.Core.Models;
public class Recipient
{
/// <summary>
/// Gets the recipient id
/// Gets the recipient's organisation number
/// </summary>
public string RecipientId { get; set; } = string.Empty;
public string? OrganisationNumber { get; set; } = null;

/// <summary>
/// Gets a list of address points for the recipient
/// Gets the recipient's national identity number
/// </summary>
public List<IAddressPoint> AddressInfo { get; set; } = new List<IAddressPoint>();
public string? NationalIdentityNumber { get; set; } = null;

/// <summary>
/// Initializes a new instance of the <see cref="Recipient"/> class.
/// Gets a list of address points for the recipient
/// </summary>
public Recipient(string recipientId, List<IAddressPoint> addressInfo)
{
RecipientId = recipientId;
AddressInfo = addressInfo;
}
public List<IAddressPoint> AddressInfo { get; set; } = new List<IAddressPoint>();

/// <summary>
/// Initializes a new instance of the <see cref="Recipient"/> class.
/// </summary>
public Recipient(List<IAddressPoint> addressInfo)
public Recipient(List<IAddressPoint> addressInfo, string? organisationNumber = null, string? nationalIdentityNumber = null)
{
OrganisationNumber = organisationNumber;
NationalIdentityNumber = nationalIdentityNumber;
AddressInfo = addressInfo;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ namespace Altinn.Notifications.Core.Models.Recipients;
public class EmailRecipient
{
/// <summary>
/// Gets or sets the recipient id
/// Gets or sets the recipient's organisation number
/// </summary>
public string? RecipientId { get; set; } = null;
public string? OrganisationNumber { get; set; } = null;

/// <summary>
/// Gets or sets the recipient's national identity number
/// </summary>
public string? NationalIdentityNumber { get; set; } = null;

/// <summary>
/// Gets or sets the toaddress
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ namespace Altinn.Notifications.Core.Models.Recipients;
public class SmsRecipient
{
/// <summary>
/// Gets or sets the recipient id
/// Gets or sets the recipient's organisation number
/// </summary>
public string? RecipientId { get; set; } = null;
public string? OrganisationNumber { get; set; } = null;

/// <summary>
/// Gets or sets the recipient's national identity number
/// </summary>
public string? NationalIdentityNumber { get; set; } = null;

/// <summary>
/// Gets or sets the mobile number
Expand Down
17 changes: 12 additions & 5 deletions src/Altinn.Notifications.Core/Services/EmailNotificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Altinn.Notifications.Core.Models;
using Altinn.Notifications.Core.Models.Address;
using Altinn.Notifications.Core.Models.Notification;
using Altinn.Notifications.Core.Models.Recipients;
using Altinn.Notifications.Core.Persistence;
using Altinn.Notifications.Core.Services.Interfaces;

Expand Down Expand Up @@ -44,13 +45,20 @@ public async Task CreateNotification(Guid orderId, DateTime requestedSendTime, R
{
EmailAddressPoint? addressPoint = recipient.AddressInfo.Find(a => a.AddressType == AddressType.Email) as EmailAddressPoint;

EmailRecipient emailRecipient = new()
{
OrganisationNumber = recipient.OrganisationNumber,
NationalIdentityNumber = recipient.NationalIdentityNumber,
ToAddress = addressPoint?.EmailAddress ?? string.Empty
};

if (!string.IsNullOrEmpty(addressPoint?.EmailAddress))
{
await CreateNotificationForRecipient(orderId, requestedSendTime, recipient.RecipientId, addressPoint.EmailAddress, EmailNotificationResultType.New);
await CreateNotificationForRecipient(orderId, requestedSendTime, emailRecipient, EmailNotificationResultType.New);
}
else
{
await CreateNotificationForRecipient(orderId, requestedSendTime, recipient.RecipientId, string.Empty, EmailNotificationResultType.Failed_RecipientNotIdentified);
await CreateNotificationForRecipient(orderId, requestedSendTime, emailRecipient, EmailNotificationResultType.Failed_RecipientNotIdentified);
}
}

Expand Down Expand Up @@ -81,15 +89,14 @@ public async Task UpdateSendStatus(EmailSendOperationResult sendOperationResult)
await _repository.UpdateSendStatus(sendOperationResult.NotificationId, (EmailNotificationResultType)sendOperationResult.SendResult!, sendOperationResult.OperationId);
}

private async Task CreateNotificationForRecipient(Guid orderId, DateTime requestedSendTime, string recipientId, string toAddress, EmailNotificationResultType result)
private async Task CreateNotificationForRecipient(Guid orderId, DateTime requestedSendTime, EmailRecipient recipient, EmailNotificationResultType result)
{
var emailNotification = new EmailNotification()
{
Id = _guid.NewGuid(),
OrderId = orderId,
RequestedSendTime = requestedSendTime,
ToAddress = toAddress,
RecipientId = string.IsNullOrEmpty(recipientId) ? null : recipientId,
Recipient = recipient,
SendResult = new(result, _dateTime.UtcNow())
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ public async Task ProcessOrderRetry(NotificationOrder order)
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)))
er.NationalIdentityNumber == recipient.NationalIdentityNumber
&& er.OrganisationNumber == recipient.OrganisationNumber
&& er.ToAddress == addressPoint?.EmailAddress))
{
await _emailService.CreateNotification(order.Id, order.RequestedSendTime, recipient);
}
Expand Down
17 changes: 12 additions & 5 deletions src/Altinn.Notifications.Core/Services/SmsNotificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Altinn.Notifications.Core.Models;
using Altinn.Notifications.Core.Models.Address;
using Altinn.Notifications.Core.Models.Notification;
using Altinn.Notifications.Core.Models.Recipients;
using Altinn.Notifications.Core.Persistence;
using Altinn.Notifications.Core.Services.Interfaces;

Expand Down Expand Up @@ -44,13 +45,20 @@ public async Task CreateNotification(Guid orderId, DateTime requestedSendTime, R
{
SmsAddressPoint? addressPoint = recipient.AddressInfo.Find(a => a.AddressType == AddressType.Sms) as SmsAddressPoint;

SmsRecipient smsRecipient = new()
{
OrganisationNumber = recipient.OrganisationNumber,
NationalIdentityNumber = recipient.NationalIdentityNumber,
MobileNumber = addressPoint?.MobileNumber ?? string.Empty
};

if (!string.IsNullOrEmpty(addressPoint?.MobileNumber))
{
await CreateNotificationForRecipient(orderId, requestedSendTime, recipient.RecipientId, addressPoint!.MobileNumber, SmsNotificationResultType.New, smsCount);
await CreateNotificationForRecipient(orderId, requestedSendTime, smsRecipient, SmsNotificationResultType.New, smsCount);
}
else
{
await CreateNotificationForRecipient(orderId, requestedSendTime, recipient.RecipientId, string.Empty, SmsNotificationResultType.Failed_RecipientNotIdentified);
await CreateNotificationForRecipient(orderId, requestedSendTime, smsRecipient, SmsNotificationResultType.Failed_RecipientNotIdentified);
}
}

Expand All @@ -75,15 +83,14 @@ public async Task UpdateSendStatus(SmsSendOperationResult sendOperationResult)
await _repository.UpdateSendStatus(sendOperationResult.NotificationId, sendOperationResult.SendResult, sendOperationResult.GatewayReference);
}

private async Task CreateNotificationForRecipient(Guid orderId, DateTime requestedSendTime, string recipientId, string recipientNumber, SmsNotificationResultType type, int smsCount = 0)
private async Task CreateNotificationForRecipient(Guid orderId, DateTime requestedSendTime, SmsRecipient recipient, SmsNotificationResultType type, int smsCount = 0)
{
var smsNotification = new SmsNotification()
{
Id = _guid.NewGuid(),
OrderId = orderId,
RequestedSendTime = requestedSendTime,
RecipientNumber = recipientNumber,
RecipientId = string.IsNullOrEmpty(recipientId) ? null : recipientId,
Recipient = recipient,
SendResult = new(type, _dateTime.UtcNow())
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ public async Task ProcessOrderRetry(NotificationOrder order)
SmsAddressPoint? addressPoint = recipient.AddressInfo.Find(a => a.AddressType == AddressType.Sms) as SmsAddressPoint;

if (!smsRecipients.Exists(sr =>
sr.RecipientId == (string.IsNullOrEmpty(recipient.RecipientId) ? null : recipient.RecipientId)
&& sr.MobileNumber.Equals(addressPoint?.MobileNumber)))
sr.NationalIdentityNumber == recipient.NationalIdentityNumber
&& sr.OrganisationNumber == recipient.OrganisationNumber
&& sr.MobileNumber == addressPoint?.MobileNumber))
{
await _smsService.CreateNotification(order.Id, order.RequestedSendTime, recipient, smsCount);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- Modify table emailnotifications: Remove the recipientid column
ALTER TABLE notifications.emailnotifications
DROP COLUMN IF EXISTS recipientid;

-- Modify table smsnotifications: Add new columns recipientorgno and recipientnin
ALTER TABLE notifications.emailnotifications
ADD COLUMN IF NOT EXISTS recipientorgno text,
ADD COLUMN IF NOT EXISTS recipientnin text;


-- Modify table smsnotifications: Remove the recipientid column
ALTER TABLE notifications.smsnotifications
DROP COLUMN IF EXISTS recipientid;

-- Modify table smsnotifications: Add new columns recipientorgno and recipientnin
ALTER TABLE notifications.smsnotifications
ADD COLUMN IF NOT EXISTS recipientorgno text,
ADD COLUMN IF NOT EXISTS recipientnin text;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TYPE public.emailnotificationresulttype ADD VALUE IF NOT EXISTS 'Failed_RecipientReserved';

ALTER TYPE public.smsnotificationresulttype ADD VALUE IF NOT EXISTS 'Failed_Recipientreserved';
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
drop procedure if exists notifications.insertemailnotification(
IN _orderid uuid,
IN _alternateid uuid,
IN _recipientid text,
IN _toaddress text,
IN _result text,
IN _resulttime timestamp with time zone,
IN _expirytime timestamp with time zone);

drop procedure if exists notifications.insertsmsnotification(
IN _orderid uuid,
IN _alternateid uuid,
IN _recipientid text,
IN _mobilenumber text,
IN _result text,
IN _resulttime timestamp with time zone,
IN _expirytime timestamp with time zone);


CREATE OR REPLACE PROCEDURE notifications.insertemailnotification(
_orderid uuid,
_alternateid uuid,
_recipientorgno TEXT,
_recipientnin TEXT,
_toaddress TEXT,
_result text,
_resulttime timestamptz,
_expirytime timestamptz)
LANGUAGE 'plpgsql'
AS $BODY$
DECLARE
__orderid BIGINT := (SELECT _id from notifications.orders
where alternateid = _orderid);
BEGIN

INSERT INTO notifications.emailnotifications(
_orderid,
alternateid,
recipientorgno,
recipientnin,
toaddress, result,
resulttime,
expirytime)
VALUES (
__orderid,
_alternateid,
_recipientorgno,
_recipientnin,
_toaddress,
_result::emailnotificationresulttype,
_resulttime,
_expirytime);
END;
$BODY$;


CREATE OR REPLACE PROCEDURE notifications.insertsmsnotification(
_orderid uuid,
_alternateid uuid,
_recipientorgno TEXT,
_recipientnin TEXT,
_mobilenumber TEXT,
_result text,
_resulttime timestamptz,
_expirytime timestamptz)
LANGUAGE 'plpgsql'
AS $BODY$
DECLARE
__orderid BIGINT := (SELECT _id from notifications.orders
where alternateid = _orderid);
BEGIN

INSERT INTO notifications.smsnotifications(
_orderid,
alternateid,
recipientorgno,
recipientnin,
mobilenumber,
result,
resulttime,
expirytime)
VALUES (
__orderid,
_alternateid,
_recipientorgno,
_recipientnin,
_mobilenumber,
_result::smsnotificationresulttype,
_resulttime,
_expirytime);
END;
$BODY$;
Loading

0 comments on commit 7e4c447

Please sign in to comment.