Skip to content

Commit

Permalink
#337 persisting sms texts as part of order creation #GCPActive (#385)
Browse files Browse the repository at this point in the history
* #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
  • Loading branch information
acn-sbuad authored Jan 25, 2024
1 parent 15763e8 commit 39bfb51
Show file tree
Hide file tree
Showing 31 changed files with 224 additions and 121 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Altinn.Notifications.Core.Integrations.Interfaces;
namespace Altinn.Notifications.Core.Integrations;

/// <summary>
/// Interface for handling all producer actions for Kafka
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// Interface describing all repository operations related to an email notification
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Altinn.Notifications.Core.Models.Notification;

namespace Altinn.Notifications.Core.Repository.Interfaces;
namespace Altinn.Notifications.Core.Persistence;

/// <summary>
/// Interface describing all repository operations related to notification summaries
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Interface describing all repository actions for notification orders
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Altinn.Notifications.Core.Repository.Interfaces
namespace Altinn.Notifications.Core.Persistence
{
/// <summary>
/// Interface for handling actions towards the resource limits table
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Altinn.Notifications.Core/Services/GetOrderService.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Altinn.Notifications.Core.Integrations.Interfaces;
using Altinn.Notifications.Core.Integrations;
using Altinn.Notifications.Integrations.Configuration;

using Confluent.Kafka;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
77 changes: 41 additions & 36 deletions src/Altinn.Notifications.Persistence/Repository/OrderRepository.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -92,12 +97,13 @@ public async Task<NotificationOrder> 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)
Expand Down Expand Up @@ -179,26 +185,6 @@ public async Task<List<NotificationOrder>> GetPastDueOrdersAndSetProcessingState
return order;
}

/// <summary>
/// Extracts relevant templates from a notification order
/// </summary>
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<long> InsertOrder(NotificationOrder order)
{
await using NpgsqlCommand pgcom = _dataSource.CreateCommand(_insertOrderSql);
Expand All @@ -219,18 +205,37 @@ private async Task<long> 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();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Altinn.Notifications.Core.Repository.Interfaces;

using Altinn.Notifications.Core.Persistence;
using Microsoft.ApplicationInsights;

using Npgsql;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Loading

0 comments on commit 39bfb51

Please sign in to comment.