From 39bfb51f53013727fba74f975c541fdcbc334064 Mon Sep 17 00:00:00 2001 From: Stephanie Buadu <47737608+acn-sbuad@users.noreply.github.com> Date: Thu, 25 Jan 2024 13:48:37 +0100 Subject: [PATCH] #337 persisting sms texts as part of order creation #GCPActive (#385) * #337 persisting sms texts as part of order creation #GCPActive * added unit test * fixed namespace for interfaces * removed unused dep * added cleanup of testdata #GCPActive * Fixed code smells * fixed pr comments --- .../{Interfaces => }/IKafkaProducer.cs | 2 +- .../IEmailNotificationRepository.cs | 2 +- .../INotificationSummaryRepository.cs | 2 +- .../IOrderRepository.cs | 2 +- .../IResourceLimitRepository.cs | 2 +- .../Services/EmailNotificationService.cs | 4 +- .../Services/GetOrderService.cs | 2 +- .../Services/NotificationSummaryService.cs | 2 +- .../NotificationsEmailServiceUpdateService.cs | 2 +- .../Services/OrderProcessingService.cs | 4 +- .../Services/OrderRequestService.cs | 2 +- .../Extensions/ServiceCollectionExtensions.cs | 2 +- .../Kafka/Consumers/EmailStatusConsumer.cs | 2 +- .../Kafka/Consumers/PastDueOrdersConsumer.cs | 2 +- .../Kafka/Producers/KafkaProducer.cs | 2 +- .../Extensions/ServiceCollectionExtensions.cs | 2 +- .../Migration/v0.15/01-setup-tables.sql | 8 + .../Repository/EmailNotificationRepository.cs | 2 +- .../NotificationSummaryRepository.cs | 2 +- .../Repository/OrderRepository.cs | 77 +++++----- .../Repository/ResourceLimitRepository.cs | 3 +- .../KafkaHealthCheckTests.cs | 2 +- .../FunctionTests.cs | 2 +- .../OrderRepositoryTests.cs | 144 ++++++++++++++++++ .../Utils/KafkaUtil.cs | 2 +- .../Utils/PostgreUtil.cs | 2 +- .../EmailNotificationServiceTests.cs | 4 +- .../TestingServices/GetOrderServiceTests.cs | 2 +- .../OrderProcessingServiceTests.cs | 4 +- .../OrderRequestServiceTests.cs | 2 +- .../OrderRepositoryTests.cs | 53 ------- 31 files changed, 224 insertions(+), 121 deletions(-) rename src/Altinn.Notifications.Core/Integrations/{Interfaces => }/IKafkaProducer.cs (86%) rename src/Altinn.Notifications.Core/{Repository/Interfaces => Persistence}/IEmailNotificationRepository.cs (95%) rename src/Altinn.Notifications.Core/{Repository/Interfaces => Persistence}/INotificationSummaryRepository.cs (89%) rename src/Altinn.Notifications.Core/{Repository/Interfaces => Persistence}/IOrderRepository.cs (97%) rename src/Altinn.Notifications.Core/{Repository/Interfaces => Persistence}/IResourceLimitRepository.cs (89%) create mode 100644 src/Altinn.Notifications.Persistence/Migration/v0.15/01-setup-tables.sql create mode 100644 test/Altinn.Notifications.IntegrationTests/Notifications.Persistence/OrderRepositoryTests.cs delete mode 100644 test/Altinn.Notifications.Tests/Notifications.Persistence/TestingRepositories/OrderRepositoryTests.cs diff --git a/src/Altinn.Notifications.Core/Integrations/Interfaces/IKafkaProducer.cs b/src/Altinn.Notifications.Core/Integrations/IKafkaProducer.cs similarity index 86% rename from src/Altinn.Notifications.Core/Integrations/Interfaces/IKafkaProducer.cs rename to src/Altinn.Notifications.Core/Integrations/IKafkaProducer.cs index 498c2d2f..6ab1ed53 100644 --- a/src/Altinn.Notifications.Core/Integrations/Interfaces/IKafkaProducer.cs +++ b/src/Altinn.Notifications.Core/Integrations/IKafkaProducer.cs @@ -1,4 +1,4 @@ -namespace Altinn.Notifications.Core.Integrations.Interfaces; +namespace Altinn.Notifications.Core.Integrations; /// /// Interface for handling all producer actions for Kafka diff --git a/src/Altinn.Notifications.Core/Repository/Interfaces/IEmailNotificationRepository.cs b/src/Altinn.Notifications.Core/Persistence/IEmailNotificationRepository.cs similarity index 95% rename from src/Altinn.Notifications.Core/Repository/Interfaces/IEmailNotificationRepository.cs rename to src/Altinn.Notifications.Core/Persistence/IEmailNotificationRepository.cs index 3fbb0780..bed06396 100644 --- a/src/Altinn.Notifications.Core/Repository/Interfaces/IEmailNotificationRepository.cs +++ b/src/Altinn.Notifications.Core/Persistence/IEmailNotificationRepository.cs @@ -3,7 +3,7 @@ using Altinn.Notifications.Core.Models.Notification; using Altinn.Notifications.Core.Models.Recipients; -namespace Altinn.Notifications.Core.Repository.Interfaces; +namespace Altinn.Notifications.Core.Persistence; /// /// Interface describing all repository operations related to an email notification diff --git a/src/Altinn.Notifications.Core/Repository/Interfaces/INotificationSummaryRepository.cs b/src/Altinn.Notifications.Core/Persistence/INotificationSummaryRepository.cs similarity index 89% rename from src/Altinn.Notifications.Core/Repository/Interfaces/INotificationSummaryRepository.cs rename to src/Altinn.Notifications.Core/Persistence/INotificationSummaryRepository.cs index c1ef4cdb..728dfa86 100644 --- a/src/Altinn.Notifications.Core/Repository/Interfaces/INotificationSummaryRepository.cs +++ b/src/Altinn.Notifications.Core/Persistence/INotificationSummaryRepository.cs @@ -1,6 +1,6 @@ using Altinn.Notifications.Core.Models.Notification; -namespace Altinn.Notifications.Core.Repository.Interfaces; +namespace Altinn.Notifications.Core.Persistence; /// /// Interface describing all repository operations related to notification summaries diff --git a/src/Altinn.Notifications.Core/Repository/Interfaces/IOrderRepository.cs b/src/Altinn.Notifications.Core/Persistence/IOrderRepository.cs similarity index 97% rename from src/Altinn.Notifications.Core/Repository/Interfaces/IOrderRepository.cs rename to src/Altinn.Notifications.Core/Persistence/IOrderRepository.cs index c3fbe9e3..0b317c79 100644 --- a/src/Altinn.Notifications.Core/Repository/Interfaces/IOrderRepository.cs +++ b/src/Altinn.Notifications.Core/Persistence/IOrderRepository.cs @@ -1,7 +1,7 @@ using Altinn.Notifications.Core.Enums; using Altinn.Notifications.Core.Models.Orders; -namespace Altinn.Notifications.Core.Repository.Interfaces; +namespace Altinn.Notifications.Core.Persistence; /// /// Interface describing all repository actions for notification orders diff --git a/src/Altinn.Notifications.Core/Repository/Interfaces/IResourceLimitRepository.cs b/src/Altinn.Notifications.Core/Persistence/IResourceLimitRepository.cs similarity index 89% rename from src/Altinn.Notifications.Core/Repository/Interfaces/IResourceLimitRepository.cs rename to src/Altinn.Notifications.Core/Persistence/IResourceLimitRepository.cs index e53d98bf..fddd9f82 100644 --- a/src/Altinn.Notifications.Core/Repository/Interfaces/IResourceLimitRepository.cs +++ b/src/Altinn.Notifications.Core/Persistence/IResourceLimitRepository.cs @@ -1,4 +1,4 @@ -namespace Altinn.Notifications.Core.Repository.Interfaces +namespace Altinn.Notifications.Core.Persistence { /// /// Interface for handling actions towards the resource limits table diff --git a/src/Altinn.Notifications.Core/Services/EmailNotificationService.cs b/src/Altinn.Notifications.Core/Services/EmailNotificationService.cs index 9386824c..41fa9fae 100644 --- a/src/Altinn.Notifications.Core/Services/EmailNotificationService.cs +++ b/src/Altinn.Notifications.Core/Services/EmailNotificationService.cs @@ -1,10 +1,10 @@ using Altinn.Notifications.Core.Configuration; using Altinn.Notifications.Core.Enums; -using Altinn.Notifications.Core.Integrations.Interfaces; +using Altinn.Notifications.Core.Integrations; using Altinn.Notifications.Core.Models; using Altinn.Notifications.Core.Models.Address; using Altinn.Notifications.Core.Models.Notification; -using Altinn.Notifications.Core.Repository.Interfaces; +using Altinn.Notifications.Core.Persistence; using Altinn.Notifications.Core.Services.Interfaces; using Microsoft.Extensions.Options; diff --git a/src/Altinn.Notifications.Core/Services/GetOrderService.cs b/src/Altinn.Notifications.Core/Services/GetOrderService.cs index c9a1d035..3fc65754 100644 --- a/src/Altinn.Notifications.Core/Services/GetOrderService.cs +++ b/src/Altinn.Notifications.Core/Services/GetOrderService.cs @@ -1,7 +1,7 @@ using Altinn.Notifications.Core.Enums; using Altinn.Notifications.Core.Models; using Altinn.Notifications.Core.Models.Orders; -using Altinn.Notifications.Core.Repository.Interfaces; +using Altinn.Notifications.Core.Persistence; using Altinn.Notifications.Core.Services.Interfaces; namespace Altinn.Notifications.Core.Services; diff --git a/src/Altinn.Notifications.Core/Services/NotificationSummaryService.cs b/src/Altinn.Notifications.Core/Services/NotificationSummaryService.cs index 3ead7772..82dec78a 100644 --- a/src/Altinn.Notifications.Core/Services/NotificationSummaryService.cs +++ b/src/Altinn.Notifications.Core/Services/NotificationSummaryService.cs @@ -1,7 +1,7 @@ using Altinn.Notifications.Core.Enums; using Altinn.Notifications.Core.Models; using Altinn.Notifications.Core.Models.Notification; -using Altinn.Notifications.Core.Repository.Interfaces; +using Altinn.Notifications.Core.Persistence; using Altinn.Notifications.Core.Services.Interfaces; namespace Altinn.Notifications.Core.Services diff --git a/src/Altinn.Notifications.Core/Services/NotificationsEmailServiceUpdateService.cs b/src/Altinn.Notifications.Core/Services/NotificationsEmailServiceUpdateService.cs index 94f8fcbb..8d19f2cb 100644 --- a/src/Altinn.Notifications.Core/Services/NotificationsEmailServiceUpdateService.cs +++ b/src/Altinn.Notifications.Core/Services/NotificationsEmailServiceUpdateService.cs @@ -2,7 +2,7 @@ using Altinn.Notifications.Core.Enums; using Altinn.Notifications.Core.Models.AltinnServiceUpdate; -using Altinn.Notifications.Core.Repository.Interfaces; +using Altinn.Notifications.Core.Persistence; using Altinn.Notifications.Core.Services.Interfaces; using Microsoft.Extensions.Logging; diff --git a/src/Altinn.Notifications.Core/Services/OrderProcessingService.cs b/src/Altinn.Notifications.Core/Services/OrderProcessingService.cs index 35aabe93..0d02aaa9 100644 --- a/src/Altinn.Notifications.Core/Services/OrderProcessingService.cs +++ b/src/Altinn.Notifications.Core/Services/OrderProcessingService.cs @@ -1,12 +1,12 @@ using System.Diagnostics; using Altinn.Notifications.Core.Configuration; using Altinn.Notifications.Core.Enums; -using Altinn.Notifications.Core.Integrations.Interfaces; +using Altinn.Notifications.Core.Integrations; 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.Repository.Interfaces; +using Altinn.Notifications.Core.Persistence; using Altinn.Notifications.Core.Services.Interfaces; using Microsoft.Extensions.Options; diff --git a/src/Altinn.Notifications.Core/Services/OrderRequestService.cs b/src/Altinn.Notifications.Core/Services/OrderRequestService.cs index a772e65e..12ebf309 100644 --- a/src/Altinn.Notifications.Core/Services/OrderRequestService.cs +++ b/src/Altinn.Notifications.Core/Services/OrderRequestService.cs @@ -2,7 +2,7 @@ using Altinn.Notifications.Core.Models; using Altinn.Notifications.Core.Models.NotificationTemplate; using Altinn.Notifications.Core.Models.Orders; -using Altinn.Notifications.Core.Repository.Interfaces; +using Altinn.Notifications.Core.Persistence; using Altinn.Notifications.Core.Services.Interfaces; using Microsoft.Extensions.Options; diff --git a/src/Altinn.Notifications.Integrations/Extensions/ServiceCollectionExtensions.cs b/src/Altinn.Notifications.Integrations/Extensions/ServiceCollectionExtensions.cs index 56e6c721..428fc902 100644 --- a/src/Altinn.Notifications.Integrations/Extensions/ServiceCollectionExtensions.cs +++ b/src/Altinn.Notifications.Integrations/Extensions/ServiceCollectionExtensions.cs @@ -1,4 +1,4 @@ -using Altinn.Notifications.Core.Integrations.Interfaces; +using Altinn.Notifications.Core.Integrations; using Altinn.Notifications.Integrations.Configuration; using Altinn.Notifications.Integrations.Health; using Altinn.Notifications.Integrations.Kafka.Consumers; diff --git a/src/Altinn.Notifications.Integrations/Kafka/Consumers/EmailStatusConsumer.cs b/src/Altinn.Notifications.Integrations/Kafka/Consumers/EmailStatusConsumer.cs index 2dc38c95..511a8432 100644 --- a/src/Altinn.Notifications.Integrations/Kafka/Consumers/EmailStatusConsumer.cs +++ b/src/Altinn.Notifications.Integrations/Kafka/Consumers/EmailStatusConsumer.cs @@ -1,4 +1,4 @@ -using Altinn.Notifications.Core.Integrations.Interfaces; +using Altinn.Notifications.Core.Integrations; using Altinn.Notifications.Core.Models.Notification; using Altinn.Notifications.Core.Services.Interfaces; using Altinn.Notifications.Integrations.Configuration; diff --git a/src/Altinn.Notifications.Integrations/Kafka/Consumers/PastDueOrdersConsumer.cs b/src/Altinn.Notifications.Integrations/Kafka/Consumers/PastDueOrdersConsumer.cs index 4b160e7e..f9ed87ba 100644 --- a/src/Altinn.Notifications.Integrations/Kafka/Consumers/PastDueOrdersConsumer.cs +++ b/src/Altinn.Notifications.Integrations/Kafka/Consumers/PastDueOrdersConsumer.cs @@ -1,4 +1,4 @@ -using Altinn.Notifications.Core.Integrations.Interfaces; +using Altinn.Notifications.Core.Integrations; using Altinn.Notifications.Core.Models.Orders; using Altinn.Notifications.Core.Services.Interfaces; using Altinn.Notifications.Integrations.Configuration; diff --git a/src/Altinn.Notifications.Integrations/Kafka/Producers/KafkaProducer.cs b/src/Altinn.Notifications.Integrations/Kafka/Producers/KafkaProducer.cs index 7f7f3da5..852c1bc5 100644 --- a/src/Altinn.Notifications.Integrations/Kafka/Producers/KafkaProducer.cs +++ b/src/Altinn.Notifications.Integrations/Kafka/Producers/KafkaProducer.cs @@ -1,4 +1,4 @@ -using Altinn.Notifications.Core.Integrations.Interfaces; +using Altinn.Notifications.Core.Integrations; using Altinn.Notifications.Integrations.Configuration; using Confluent.Kafka; diff --git a/src/Altinn.Notifications.Persistence/Extensions/ServiceCollectionExtensions.cs b/src/Altinn.Notifications.Persistence/Extensions/ServiceCollectionExtensions.cs index f209516f..13683684 100644 --- a/src/Altinn.Notifications.Persistence/Extensions/ServiceCollectionExtensions.cs +++ b/src/Altinn.Notifications.Persistence/Extensions/ServiceCollectionExtensions.cs @@ -1,4 +1,4 @@ -using Altinn.Notifications.Core.Repository.Interfaces; +using Altinn.Notifications.Core.Persistence; using Altinn.Notifications.Persistence.Configuration; using Altinn.Notifications.Persistence.Health; using Altinn.Notifications.Persistence.Repository; diff --git a/src/Altinn.Notifications.Persistence/Migration/v0.15/01-setup-tables.sql b/src/Altinn.Notifications.Persistence/Migration/v0.15/01-setup-tables.sql new file mode 100644 index 00000000..2b9302ab --- /dev/null +++ b/src/Altinn.Notifications.Persistence/Migration/v0.15/01-setup-tables.sql @@ -0,0 +1,8 @@ +CREATE TABLE IF NOT EXISTS notifications.smstexts( + _id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, + _orderid BIGINT REFERENCES notifications.orders(_id) ON DELETE CASCADE, + sendernumber TEXT NOT NULL, + body TEXT NOT NULL +); + +GRANT SELECT,INSERT,UPDATE,DELETE ON TABLE notifications.smstexts TO platform_notifications; \ No newline at end of file diff --git a/src/Altinn.Notifications.Persistence/Repository/EmailNotificationRepository.cs b/src/Altinn.Notifications.Persistence/Repository/EmailNotificationRepository.cs index 3731ec5c..de2b3077 100644 --- a/src/Altinn.Notifications.Persistence/Repository/EmailNotificationRepository.cs +++ b/src/Altinn.Notifications.Persistence/Repository/EmailNotificationRepository.cs @@ -2,7 +2,7 @@ using Altinn.Notifications.Core.Models; using Altinn.Notifications.Core.Models.Notification; using Altinn.Notifications.Core.Models.Recipients; -using Altinn.Notifications.Core.Repository.Interfaces; +using Altinn.Notifications.Core.Persistence; using Altinn.Notifications.Persistence.Extensions; using Microsoft.ApplicationInsights; using Npgsql; diff --git a/src/Altinn.Notifications.Persistence/Repository/NotificationSummaryRepository.cs b/src/Altinn.Notifications.Persistence/Repository/NotificationSummaryRepository.cs index bf382295..8bd24862 100644 --- a/src/Altinn.Notifications.Persistence/Repository/NotificationSummaryRepository.cs +++ b/src/Altinn.Notifications.Persistence/Repository/NotificationSummaryRepository.cs @@ -1,7 +1,7 @@ using Altinn.Notifications.Core.Enums; using Altinn.Notifications.Core.Models.Notification; using Altinn.Notifications.Core.Models.Recipients; -using Altinn.Notifications.Core.Repository.Interfaces; +using Altinn.Notifications.Core.Persistence; using Altinn.Notifications.Persistence.Extensions; using Microsoft.ApplicationInsights; diff --git a/src/Altinn.Notifications.Persistence/Repository/OrderRepository.cs b/src/Altinn.Notifications.Persistence/Repository/OrderRepository.cs index 822ca37f..f73f27b7 100644 --- a/src/Altinn.Notifications.Persistence/Repository/OrderRepository.cs +++ b/src/Altinn.Notifications.Persistence/Repository/OrderRepository.cs @@ -1,12 +1,16 @@ using System.Data; + using Altinn.Notifications.Core.Enums; using Altinn.Notifications.Core.Models; using Altinn.Notifications.Core.Models.NotificationTemplate; using Altinn.Notifications.Core.Models.Orders; -using Altinn.Notifications.Core.Repository.Interfaces; +using Altinn.Notifications.Core.Persistence; using Altinn.Notifications.Persistence.Extensions; + using Microsoft.ApplicationInsights; + using Npgsql; + using NpgsqlTypes; namespace Altinn.Notifications.Persistence.Repository; @@ -23,6 +27,7 @@ public class OrderRepository : IOrderRepository private const string _getOrdersBySendersReferenceSql = "select notificationorder from notifications.orders where sendersreference=$1 and creatorname=$2"; private const string _insertOrderSql = "select notifications.insertorder($1, $2, $3, $4, $5, $6)"; // (_alternateid, _creatorname, _sendersreference, _created, _requestedsendtime, _notificationorder) private const string _insertEmailTextSql = "call notifications.insertemailtext($1, $2, $3, $4, $5)"; // (__orderid, _fromaddress, _subject, _body, _contenttype) + private const string _insertSmsTextSql = "insert into notifications.smstexts(_orderid, sendernumber, body) VALUES ($1, $2, $3)"; // __orderid, _sendernumber, _body private const string _setProcessCompleted = "update notifications.orders set processedstatus =$1::orderprocessingstate where alternateid=$2"; private const string _getOrdersPastSendTimeUpdateStatus = "select notifications.getorders_pastsendtime_updatestatus()"; private const string _getOrderIncludeStatus = "select * from notifications.getorder_includestatus($1, $2)"; // _alternateid, creator @@ -92,12 +97,13 @@ public async Task Create(NotificationOrder order) { long dbOrderId = await InsertOrder(order); - EmailTemplate? emailTemplate = ExtractTemplates(order); - if (emailTemplate != null) - { - await InsertEmailText(dbOrderId, emailTemplate.FromAddress, emailTemplate.Subject, emailTemplate.Body, emailTemplate.ContentType.ToString()); - } + EmailTemplate? emailTemplate = order.Templates.Find(t => t.Type == NotificationTemplateType.Email) as EmailTemplate; + Task emailInsertTask = InsertEmailTextAsync(dbOrderId, emailTemplate); + SmsTemplate? smsTemplate = order.Templates.Find(t => t.Type == NotificationTemplateType.Sms) as SmsTemplate; + Task smsInsertTask = InsertSmsTextAsync(dbOrderId, smsTemplate); + + await Task.WhenAll(emailInsertTask, smsInsertTask); await transaction.CommitAsync(); } catch (Exception) @@ -179,26 +185,6 @@ public async Task> GetPastDueOrdersAndSetProcessingState return order; } - /// - /// Extracts relevant templates from a notification order - /// - internal static EmailTemplate? ExtractTemplates(NotificationOrder order) - { - EmailTemplate? emailTemplate = null; - - foreach (INotificationTemplate template in order.Templates) - { - switch (template.Type) - { - case Core.Enums.NotificationTemplateType.Email: - emailTemplate = template as EmailTemplate; - break; - } - } - - return emailTemplate; - } - private async Task InsertOrder(NotificationOrder order) { await using NpgsqlCommand pgcom = _dataSource.CreateCommand(_insertOrderSql); @@ -219,18 +205,37 @@ private async Task InsertOrder(NotificationOrder order) return orderId; } - private async Task InsertEmailText(long dbOrderId, string fromAddress, string subject, string body, string contentType) + private async Task InsertSmsTextAsync(long dbOrderId, SmsTemplate? smsTemplate) { - await using NpgsqlCommand pgcom = _dataSource.CreateCommand(_insertEmailTextSql); - using TelemetryTracker tracker = new(_telemetryClient, pgcom); + if (smsTemplate != null) + { + await using NpgsqlCommand pgcom = _dataSource.CreateCommand(_insertSmsTextSql); + using TelemetryTracker tracker = new(_telemetryClient, pgcom); - pgcom.Parameters.AddWithValue(NpgsqlDbType.Bigint, dbOrderId); - pgcom.Parameters.AddWithValue(NpgsqlDbType.Text, fromAddress); - pgcom.Parameters.AddWithValue(NpgsqlDbType.Text, subject); - pgcom.Parameters.AddWithValue(NpgsqlDbType.Text, body); - pgcom.Parameters.AddWithValue(NpgsqlDbType.Text, contentType); + pgcom.Parameters.AddWithValue(NpgsqlDbType.Bigint, dbOrderId); + pgcom.Parameters.AddWithValue(NpgsqlDbType.Text, smsTemplate.SenderNumber); + pgcom.Parameters.AddWithValue(NpgsqlDbType.Text, smsTemplate.Body); - await pgcom.ExecuteNonQueryAsync(); - tracker.Track(); + await pgcom.ExecuteNonQueryAsync(); + tracker.Track(); + } + } + + private async Task InsertEmailTextAsync(long dbOrderId, EmailTemplate? emailTemplate) + { + if (emailTemplate != null) + { + await using NpgsqlCommand pgcom = _dataSource.CreateCommand(_insertEmailTextSql); + using TelemetryTracker tracker = new(_telemetryClient, pgcom); + + pgcom.Parameters.AddWithValue(NpgsqlDbType.Bigint, dbOrderId); + pgcom.Parameters.AddWithValue(NpgsqlDbType.Text, emailTemplate.FromAddress); + pgcom.Parameters.AddWithValue(NpgsqlDbType.Text, emailTemplate.Subject); + pgcom.Parameters.AddWithValue(NpgsqlDbType.Text, emailTemplate.Body); + pgcom.Parameters.AddWithValue(NpgsqlDbType.Text, emailTemplate.ContentType.ToString()); + + await pgcom.ExecuteNonQueryAsync(); + tracker.Track(); + } } } diff --git a/src/Altinn.Notifications.Persistence/Repository/ResourceLimitRepository.cs b/src/Altinn.Notifications.Persistence/Repository/ResourceLimitRepository.cs index e01808c3..cf58e337 100644 --- a/src/Altinn.Notifications.Persistence/Repository/ResourceLimitRepository.cs +++ b/src/Altinn.Notifications.Persistence/Repository/ResourceLimitRepository.cs @@ -1,5 +1,4 @@ -using Altinn.Notifications.Core.Repository.Interfaces; - +using Altinn.Notifications.Core.Persistence; using Microsoft.ApplicationInsights; using Npgsql; diff --git a/test/Altinn.Notifications.IntegrationTests/Notifications.Integrations/KafkaHealthCheckTests.cs b/test/Altinn.Notifications.IntegrationTests/Notifications.Integrations/KafkaHealthCheckTests.cs index 035ef1df..815d59f8 100644 --- a/test/Altinn.Notifications.IntegrationTests/Notifications.Integrations/KafkaHealthCheckTests.cs +++ b/test/Altinn.Notifications.IntegrationTests/Notifications.Integrations/KafkaHealthCheckTests.cs @@ -1,4 +1,4 @@ -using Altinn.Notifications.Core.Integrations.Interfaces; +using Altinn.Notifications.Core.Integrations; using Altinn.Notifications.Integrations.Configuration; using Altinn.Notifications.Integrations.Health; using Altinn.Notifications.Integrations.Kafka.Producers; diff --git a/test/Altinn.Notifications.IntegrationTests/Notifications.Persistence/FunctionTests.cs b/test/Altinn.Notifications.IntegrationTests/Notifications.Persistence/FunctionTests.cs index db795b5b..6fd8a794 100644 --- a/test/Altinn.Notifications.IntegrationTests/Notifications.Persistence/FunctionTests.cs +++ b/test/Altinn.Notifications.IntegrationTests/Notifications.Persistence/FunctionTests.cs @@ -1,4 +1,4 @@ -using Altinn.Notifications.Core.Repository.Interfaces; +using Altinn.Notifications.Core.Persistence; using Altinn.Notifications.IntegrationTests.Utils; using Altinn.Notifications.Persistence.Repository; diff --git a/test/Altinn.Notifications.IntegrationTests/Notifications.Persistence/OrderRepositoryTests.cs b/test/Altinn.Notifications.IntegrationTests/Notifications.Persistence/OrderRepositoryTests.cs new file mode 100644 index 00000000..39c2b39b --- /dev/null +++ b/test/Altinn.Notifications.IntegrationTests/Notifications.Persistence/OrderRepositoryTests.cs @@ -0,0 +1,144 @@ +using Altinn.Notifications.Core.Models.NotificationTemplate; +using Altinn.Notifications.Core.Models.Orders; +using Altinn.Notifications.Core.Persistence; +using Altinn.Notifications.IntegrationTests.Utils; +using Altinn.Notifications.Persistence.Repository; + +using Xunit; + +namespace Altinn.Notifications.IntegrationTests.Notifications.Persistence +{ + public class OrderRepositoryTests : IAsyncLifetime + { + private List orderIdsToDelete; + + public OrderRepositoryTests() + { + orderIdsToDelete = new List(); + } + + public async Task InitializeAsync() + { + await Task.CompletedTask; + } + + public async Task DisposeAsync() + { + string deleteSql = $@"DELETE from notifications.orders o where o.alternateid in ('{string.Join("','", orderIdsToDelete)}')"; + await PostgreUtil.RunSql(deleteSql); + } + + [Fact] + public async Task Create_OrderWithSmsTemplate_SmsTextsPersisted() + { + // Arrange + OrderRepository repo = (OrderRepository)ServiceUtil + .GetServices(new List() { typeof(IOrderRepository) }) + .First(i => i.GetType() == typeof(OrderRepository)); + + NotificationOrder order = new() + { + Id = Guid.NewGuid(), + Created = DateTime.UtcNow, + Creator = new("test"), + Templates = new List() + { + new SmsTemplate("Altinn", "This is the body") + } + }; + + orderIdsToDelete.Add(order.Id); + + // Act + await repo.Create(order); + + // Assert + string sql = $@"SELECT count(1) + FROM notifications.smstexts as st + JOIN notifications.orders o ON st._orderid = o._id + WHERE o.alternateid = '{order.Id}'"; + + int actualCount = await PostgreUtil.RunSqlReturnOutput(sql); + + Assert.Equal(1, actualCount); + } + + [Fact] + public async Task Create_OrderWithEmailTemplate_EmailTextsPersisted() + { + // Arrange + OrderRepository repo = (OrderRepository)ServiceUtil + .GetServices(new List() { typeof(IOrderRepository) }) + .First(i => i.GetType() == typeof(OrderRepository)); + + NotificationOrder order = new() + { + Id = Guid.NewGuid(), + Created = DateTime.UtcNow, + Creator = new("test"), + Templates = new List() + { + new EmailTemplate("noreply@altinn.no", "Subject", "Body", Core.Enums.EmailContentType.Plain) + } + }; + + orderIdsToDelete.Add(order.Id); + + // Act + await repo.Create(order); + + // Assert + string sql = $@"SELECT count(1) + FROM notifications.emailtexts as et + JOIN notifications.orders o ON et._orderid = o._id + WHERE o.alternateid = '{order.Id}'"; + + int actualCount = await PostgreUtil.RunSqlReturnOutput(sql); + + Assert.Equal(1, actualCount); + } + + [Fact] + public async Task Create_OrderWithEmailAndSmsTemplate_BothTextsPersisted() + { + // Arrange + OrderRepository repo = (OrderRepository)ServiceUtil + .GetServices(new List() { typeof(IOrderRepository) }) + .First(i => i.GetType() == typeof(OrderRepository)); + + NotificationOrder order = new() + { + Id = Guid.NewGuid(), + Created = DateTime.UtcNow, + Creator = new("test"), + Templates = new List() + { + new EmailTemplate("noreply@altinn.no", "Subject", "Body", Core.Enums.EmailContentType.Plain), + new SmsTemplate("Altinn", "This is the body") + } + }; + + orderIdsToDelete.Add(order.Id); + + // Act + await repo.Create(order); + + // Assert + string emailSql = $@"SELECT count(1) + FROM notifications.emailtexts as et + JOIN notifications.orders o ON et._orderid = o._id + WHERE o.alternateid = '{order.Id}'"; + + string smsSql = $@"SELECT count(1) + FROM notifications.smstexts as st + JOIN notifications.orders o ON st._orderid = o._id + WHERE o.alternateid = '{order.Id}'"; + + int emailTextCount = await PostgreUtil.RunSqlReturnOutput(emailSql); + int smsTextCound = await PostgreUtil.RunSqlReturnOutput(smsSql); + + Assert.Equal(1, emailTextCount); + Assert.Equal(1, smsTextCound); + } + } +} diff --git a/test/Altinn.Notifications.IntegrationTests/Utils/KafkaUtil.cs b/test/Altinn.Notifications.IntegrationTests/Utils/KafkaUtil.cs index 4e8026a6..e7a14dc4 100644 --- a/test/Altinn.Notifications.IntegrationTests/Utils/KafkaUtil.cs +++ b/test/Altinn.Notifications.IntegrationTests/Utils/KafkaUtil.cs @@ -1,4 +1,4 @@ -using Altinn.Notifications.Core.Integrations.Interfaces; +using Altinn.Notifications.Core.Integrations; using Altinn.Notifications.Integrations.Kafka.Producers; using Confluent.Kafka; diff --git a/test/Altinn.Notifications.IntegrationTests/Utils/PostgreUtil.cs b/test/Altinn.Notifications.IntegrationTests/Utils/PostgreUtil.cs index 3b12fb20..e591a986 100644 --- a/test/Altinn.Notifications.IntegrationTests/Utils/PostgreUtil.cs +++ b/test/Altinn.Notifications.IntegrationTests/Utils/PostgreUtil.cs @@ -1,6 +1,6 @@ using Altinn.Notifications.Core.Models.Notification; using Altinn.Notifications.Core.Models.Orders; -using Altinn.Notifications.Core.Repository.Interfaces; +using Altinn.Notifications.Core.Persistence; using Altinn.Notifications.Persistence.Extensions; using Altinn.Notifications.Persistence.Repository; diff --git a/test/Altinn.Notifications.Tests/Notifications.Core/TestingServices/EmailNotificationServiceTests.cs b/test/Altinn.Notifications.Tests/Notifications.Core/TestingServices/EmailNotificationServiceTests.cs index 505084a1..69df0b12 100644 --- a/test/Altinn.Notifications.Tests/Notifications.Core/TestingServices/EmailNotificationServiceTests.cs +++ b/test/Altinn.Notifications.Tests/Notifications.Core/TestingServices/EmailNotificationServiceTests.cs @@ -4,11 +4,11 @@ using Altinn.Notifications.Core.Configuration; using Altinn.Notifications.Core.Enums; -using Altinn.Notifications.Core.Integrations.Interfaces; +using Altinn.Notifications.Core.Integrations; using Altinn.Notifications.Core.Models; using Altinn.Notifications.Core.Models.Address; using Altinn.Notifications.Core.Models.Notification; -using Altinn.Notifications.Core.Repository.Interfaces; +using Altinn.Notifications.Core.Persistence; using Altinn.Notifications.Core.Services; using Altinn.Notifications.Core.Services.Interfaces; diff --git a/test/Altinn.Notifications.Tests/Notifications.Core/TestingServices/GetOrderServiceTests.cs b/test/Altinn.Notifications.Tests/Notifications.Core/TestingServices/GetOrderServiceTests.cs index aaedf3a5..6e9a248a 100644 --- a/test/Altinn.Notifications.Tests/Notifications.Core/TestingServices/GetOrderServiceTests.cs +++ b/test/Altinn.Notifications.Tests/Notifications.Core/TestingServices/GetOrderServiceTests.cs @@ -4,7 +4,7 @@ using Altinn.Notifications.Core.Enums; using Altinn.Notifications.Core.Models.Orders; -using Altinn.Notifications.Core.Repository.Interfaces; +using Altinn.Notifications.Core.Persistence; using Altinn.Notifications.Core.Services; using Moq; diff --git a/test/Altinn.Notifications.Tests/Notifications.Core/TestingServices/OrderProcessingServiceTests.cs b/test/Altinn.Notifications.Tests/Notifications.Core/TestingServices/OrderProcessingServiceTests.cs index c21719fa..68ca7c46 100644 --- a/test/Altinn.Notifications.Tests/Notifications.Core/TestingServices/OrderProcessingServiceTests.cs +++ b/test/Altinn.Notifications.Tests/Notifications.Core/TestingServices/OrderProcessingServiceTests.cs @@ -4,12 +4,12 @@ using Altinn.Notifications.Core.Configuration; using Altinn.Notifications.Core.Enums; -using Altinn.Notifications.Core.Integrations.Interfaces; +using Altinn.Notifications.Core.Integrations; 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.Repository.Interfaces; +using Altinn.Notifications.Core.Persistence; using Altinn.Notifications.Core.Services; using Altinn.Notifications.Core.Services.Interfaces; using Altinn.Notifications.Integrations.Configuration; diff --git a/test/Altinn.Notifications.Tests/Notifications.Core/TestingServices/OrderRequestServiceTests.cs b/test/Altinn.Notifications.Tests/Notifications.Core/TestingServices/OrderRequestServiceTests.cs index 281e399d..5fa0be7a 100644 --- a/test/Altinn.Notifications.Tests/Notifications.Core/TestingServices/OrderRequestServiceTests.cs +++ b/test/Altinn.Notifications.Tests/Notifications.Core/TestingServices/OrderRequestServiceTests.cs @@ -6,7 +6,7 @@ using Altinn.Notifications.Core.Models; using Altinn.Notifications.Core.Models.NotificationTemplate; using Altinn.Notifications.Core.Models.Orders; -using Altinn.Notifications.Core.Repository.Interfaces; +using Altinn.Notifications.Core.Persistence; using Altinn.Notifications.Core.Services; using Altinn.Notifications.Core.Services.Interfaces; diff --git a/test/Altinn.Notifications.Tests/Notifications.Persistence/TestingRepositories/OrderRepositoryTests.cs b/test/Altinn.Notifications.Tests/Notifications.Persistence/TestingRepositories/OrderRepositoryTests.cs deleted file mode 100644 index c458953b..00000000 --- a/test/Altinn.Notifications.Tests/Notifications.Persistence/TestingRepositories/OrderRepositoryTests.cs +++ /dev/null @@ -1,53 +0,0 @@ -using Altinn.Notifications.Core.Enums; -using Altinn.Notifications.Core.Models.NotificationTemplate; -using Altinn.Notifications.Core.Models.Orders; -using Altinn.Notifications.Persistence.Repository; - -using Xunit; - -namespace Altinn.Notifications.Tests.Notifications.Persistence.TestingRepositories; - -public class OrderRepositoryTests -{ - [Fact] - public void ExtractTemplates_EmailTemplate() - { - // Arrange - EmailTemplate expected = new() - { - Body = "body", - ContentType = EmailContentType.Plain, - FromAddress = "from@domain.com", - Subject = "subject", - Type = NotificationTemplateType.Email - }; - - NotificationOrder input = new() - { - Templates = new() - { - new EmailTemplate("from@domain.com", "subject", "body", EmailContentType.Plain) - } - }; - - // Act - EmailTemplate? actual = OrderRepository.ExtractTemplates(input); - - // Assert - Assert.NotNull(actual); - Assert.Equivalent(expected, actual); - } - - [Fact] - public void ExtractTemplates_EmptyListOfTemplates_NullForAllTemplates() - { - // Arrange - NotificationOrder input = new(); - - // Act - EmailTemplate? actualEmailTemplate = OrderRepository.ExtractTemplates(input); - - // Assert - Assert.Null(actualEmailTemplate); - } -}