Skip to content

Commit

Permalink
calculating sms count on notification creation and returning exact nu…
Browse files Browse the repository at this point in the history
…mber for metrics
  • Loading branch information
acn-sbuad committed Mar 6, 2024
1 parent 7e4c447 commit 5d53a41
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

CREATE OR REPLACE PROCEDURE notifications.insertsmsnotification(
_orderid uuid,
_alternateid uuid,
_recipientorgno TEXT,
_recipientnin TEXT,
_mobilenumber TEXT,
_result text,
_smscount integer,
_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,
smscount,
resulttime,
expirytime)
VALUES (
__orderid,
_alternateid,
_recipientorgno,
_recipientnin,
_mobilenumber,
_result::smsnotificationresulttype,
_smscount,
_resulttime,
_expirytime);
END;
$BODY$;
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
CREATE OR REPLACE FUNCTION notifications.getmetrics(
month_input int,
year_input int
)
RETURNS TABLE (
org text,
placed_orders bigint,
sent_emails bigint,
succeeded_emails bigint,
sent_sms bigint,
succeeded_sms bigint
)
AS $$
BEGIN
RETURN QUERY
SELECT
o.creatorname,
COUNT(DISTINCT o._id) AS placed_orders,
SUM(CASE WHEN e._id IS NOT NULL THEN 1 ELSE 0 END) AS sent_emails,
SUM(CASE WHEN e.result = 'Succeeded' THEN 1 ELSE 0 END) AS succeeded_emails,
SUM(CASE WHEN s._id IS NOT NULL THEN s.smscount ELSE 0 END) AS sent_sms,
SUM(CASE WHEN s.result = 'Accepted' THEN 1 ELSE 0 END) AS succeeded_sms
FROM notifications.orders o
LEFT JOIN notifications.emailnotifications e ON o._id = e._orderid
LEFT JOIN notifications.smsnotifications s ON o._id = s._orderid
WHERE EXTRACT(MONTH FROM o.requestedsendtime) = month_input
AND EXTRACT(YEAR FROM o.requestedsendtime) = year_input
GROUP BY o.creatorname;
END;
$$ LANGUAGE plpgsql;
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class SmsNotificationRepository : ISmsNotificationRepository
private readonly NpgsqlDataSource _dataSource;
private readonly TelemetryClient? _telemetryClient;

private const string _insertSmsNotificationSql = "call notifications.insertsmsnotification($1, $2, $3, $4, $5, $6, $7, $8)"; // (__orderid, _alternateid, _recipientorgno, _recipientnin, _mobilenumber, _result, _resulttime, _expirytime)
private const string _insertSmsNotificationSql = "call notifications.insertsmsnotification($1, $2, $3, $4, $5, $6, $7, $8, $9)"; // (__orderid, _alternateid, _recipientorgno, _recipientnin, _mobilenumber, _result, _smscount, _resulttime, _expirytime)
private const string _getSmsNotificationsSql = "select * from notifications.getsms_statusnew_updatestatus()";
private const string _getSmsRecipients = "select * from notifications.getsmsrecipients_v2($1)"; // (_orderid)

Expand Down Expand Up @@ -55,6 +55,7 @@ public async Task AddNotification(SmsNotification notification, DateTime expiry,
pgcom.Parameters.AddWithValue(NpgsqlDbType.Text, (object)DBNull.Value); // recipientnin
pgcom.Parameters.AddWithValue(NpgsqlDbType.Text, notification.Recipient.MobileNumber);
pgcom.Parameters.AddWithValue(NpgsqlDbType.Text, notification.SendResult.Result.ToString());
pgcom.Parameters.AddWithValue(NpgsqlDbType.Integer, smsCount);
pgcom.Parameters.AddWithValue(NpgsqlDbType.TimestampTz, notification.SendResult.ResultTime);
pgcom.Parameters.AddWithValue(NpgsqlDbType.TimestampTz, expiry);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public async Task AddNotification()
}
};

await repo.AddNotification(smsNotification, DateTime.UtcNow);
await repo.AddNotification(smsNotification, DateTime.UtcNow, 1);

// Assert
string sql = $@"SELECT count(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public static async Task<NotificationOrder> PopulateDBWithOrderAndEmailNotificat
}

await orderRepo.Create(order);
await notificationRepo.AddNotification(smsNotification, DateTime.UtcNow.AddDays(1));
await notificationRepo.AddNotification(smsNotification, DateTime.UtcNow.AddDays(1), 1);

return (order, smsNotification);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public async Task CreateNotifications_NewSmsNotification_RepositoryCalledOnce()
var service = GetTestService(repo: repoMock.Object);

// Act
await service.CreateNotification(Guid.NewGuid(), DateTime.UtcNow, new Recipient(new List<IAddressPoint>() { new SmsAddressPoint("999999999") }, nationalIdentityNumber: "enduser-nin"));
await service.CreateNotification(Guid.NewGuid(), DateTime.UtcNow, new Recipient(new List<IAddressPoint>() { new SmsAddressPoint("999999999") }, nationalIdentityNumber: "enduser-nin"), It.IsAny<int>());

// Assert
repoMock.Verify(r => r.AddNotification(It.IsAny<SmsNotification>(), It.IsAny<DateTime>()), Times.Once);
repoMock.Verify(r => r.AddNotification(It.IsAny<SmsNotification>(), It.IsAny<DateTime>(), It.IsAny<int>()), Times.Once);
}

[Fact]
Expand All @@ -72,15 +72,15 @@ public async Task CreateNotification_RecipientNumberIsDefined_ResultNew()
};

var repoMock = new Mock<ISmsNotificationRepository>();
repoMock.Setup(r => r.AddNotification(It.Is<SmsNotification>(e => AssertUtils.AreEquivalent(expected, e)), It.Is<DateTime>(d => d == expectedExpiry)));
repoMock.Setup(r => r.AddNotification(It.Is<SmsNotification>(e => AssertUtils.AreEquivalent(expected, e)), It.Is<DateTime>(d => d == expectedExpiry), It.IsAny<int>()));

var service = GetTestService(repo: repoMock.Object, guidOutput: id, dateTimeOutput: dateTimeOutput);

// Act
await service.CreateNotification(orderId, requestedSendTime, new Recipient(new List<IAddressPoint>() { new SmsAddressPoint("+4799999999") }));
await service.CreateNotification(orderId, requestedSendTime, new Recipient(new List<IAddressPoint>() { new SmsAddressPoint("+4799999999") }), 1);

// Assert
repoMock.Verify(r => r.AddNotification(It.Is<SmsNotification>(e => AssertUtils.AreEquivalent(expected, e)), It.Is<DateTime>(d => d == expectedExpiry)), Times.Once);
repoMock.Verify(r => r.AddNotification(It.Is<SmsNotification>(e => AssertUtils.AreEquivalent(expected, e)), It.Is<DateTime>(d => d == expectedExpiry), It.IsAny<int>()), Times.Once);
}

[Fact]
Expand All @@ -102,12 +102,12 @@ public async Task CreateNotification_RecipientNumberMissing_ResultFailedRecipien
};

var repoMock = new Mock<ISmsNotificationRepository>();
repoMock.Setup(r => r.AddNotification(It.Is<SmsNotification>(e => AssertUtils.AreEquivalent(expected, e)), It.Is<DateTime>(d => d == expectedExpiry)));
repoMock.Setup(r => r.AddNotification(It.Is<SmsNotification>(e => AssertUtils.AreEquivalent(expected, e)), It.Is<DateTime>(d => d == expectedExpiry), It.IsAny<int>()));

var service = GetTestService(repo: repoMock.Object, guidOutput: id, dateTimeOutput: dateTimeOutput);

// Act
await service.CreateNotification(orderId, requestedSendTime, new Recipient(new List<IAddressPoint>()));
await service.CreateNotification(orderId, requestedSendTime, new Recipient(new List<IAddressPoint>()), It.IsAny<int>());

// Assert
repoMock.Verify();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Altinn.Notifications.Core.Enums;
using Altinn.Notifications.Core.Models;
using Altinn.Notifications.Core.Models.Address;
using Altinn.Notifications.Core.Models.NotificationTemplate;
using Altinn.Notifications.Core.Models.Orders;
using Altinn.Notifications.Core.Models.Recipients;
using Altinn.Notifications.Core.Persistence;
Expand Down Expand Up @@ -34,13 +35,14 @@ public async Task ProcessOrder_ExpectedInputToService()
Recipients = new List<Recipient>()
{
new Recipient(new List<IAddressPoint>() { new SmsAddressPoint("+4799999999") }, nationalIdentityNumber: "enduser-nin")
}
},
Templates = [new SmsTemplate("Altinn", "this is the body")]
};

Recipient expectedRecipient = new(new List<IAddressPoint>() { new SmsAddressPoint("+4799999999") }, nationalIdentityNumber: "enduser-nin");

var serviceMock = new Mock<ISmsNotificationService>();
serviceMock.Setup(s => s.CreateNotification(It.IsAny<Guid>(), It.Is<DateTime>(d => d.Equals(requested)), It.Is<Recipient>(r => AssertUtils.AreEquivalent(expectedRecipient, r))));
serviceMock.Setup(s => s.CreateNotification(It.IsAny<Guid>(), It.Is<DateTime>(d => d.Equals(requested)), It.Is<Recipient>(r => AssertUtils.AreEquivalent(expectedRecipient, r)), It.IsAny<int>()));

var service = GetTestService(smsService: serviceMock.Object);

Expand All @@ -63,19 +65,20 @@ public async Task ProcessOrder_ServiceCalledOnceForEachRecipient()
{
new(),
new()
}
},
Templates = [new SmsTemplate("Altinn", "this is the body")]
};

var serviceMock = new Mock<ISmsNotificationService>();
serviceMock.Setup(s => s.CreateNotification(It.IsAny<Guid>(), It.IsAny<DateTime>(), It.IsAny<Recipient>()));
serviceMock.Setup(s => s.CreateNotification(It.IsAny<Guid>(), It.IsAny<DateTime>(), It.IsAny<Recipient>(), It.IsAny<int>()));

var service = GetTestService(smsService: serviceMock.Object);

// Act
await service.ProcessOrder(order);

// Assert
serviceMock.Verify(s => s.CreateNotification(It.IsAny<Guid>(), It.IsAny<DateTime>(), It.IsAny<Recipient>()), Times.Exactly(2));
serviceMock.Verify(s => s.CreateNotification(It.IsAny<Guid>(), It.IsAny<DateTime>(), It.IsAny<Recipient>(), It.IsAny<int>()), Times.Exactly(2));
}

[Fact]
Expand All @@ -92,11 +95,12 @@ public async Task ProcessOrderRetry_ServiceCalledIfRecipientNotInDatabase()
new Recipient(new List<IAddressPoint>() { new SmsAddressPoint("+4799999999") }, nationalIdentityNumber: "enduser-nin"),
new Recipient(new List<IAddressPoint>() { new SmsAddressPoint("+4799999999") }, organisationNumber: "skd-orgNo"),
new Recipient(new List<IAddressPoint>() { new SmsAddressPoint("+4749999999") })
}
},
Templates = [new SmsTemplate("Altinn", "this is the body")]
};

var serviceMock = new Mock<ISmsNotificationService>();
serviceMock.Setup(s => s.CreateNotification(It.IsAny<Guid>(), It.IsAny<DateTime>(), It.IsAny<Recipient>()));
serviceMock.Setup(s => s.CreateNotification(It.IsAny<Guid>(), It.IsAny<DateTime>(), It.IsAny<Recipient>(), It.IsAny<int>()));

var smsRepoMock = new Mock<ISmsNotificationRepository>();
smsRepoMock.Setup(e => e.GetRecipients(It.IsAny<Guid>())).ReturnsAsync(
Expand All @@ -113,7 +117,7 @@ public async Task ProcessOrderRetry_ServiceCalledIfRecipientNotInDatabase()

// Assert
smsRepoMock.Verify(e => e.GetRecipients(It.IsAny<Guid>()), Times.Once);
serviceMock.Verify(s => s.CreateNotification(It.IsAny<Guid>(), It.IsAny<DateTime>(), It.IsAny<Recipient>()), Times.Exactly(2));
serviceMock.Verify(s => s.CreateNotification(It.IsAny<Guid>(), It.IsAny<DateTime>(), It.IsAny<Recipient>(), It.IsAny<int>()), Times.Exactly(2));
}

private static SmsOrderProcessingService GetTestService(
Expand Down

0 comments on commit 5d53a41

Please sign in to comment.