Skip to content

Commit

Permalink
option to update sms send status with only gateway ref (#444)
Browse files Browse the repository at this point in the history
* update sms operation based on gatewayref only

* added unit test

* added unit tests

* simplified test
  • Loading branch information
acn-sbuad authored Feb 20, 2024
1 parent 7834f2e commit 43206c5
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class SmsSendOperationResult
/// <summary>
/// The notification id
/// </summary>
public Guid NotificationId { get; set; }
public Guid? NotificationId { get; set; }

/// <summary>
/// The reference to the delivery in sms gateway
Expand Down Expand Up @@ -59,7 +59,7 @@ public static bool TryParse(string input, out SmsSendOperationResult value)
parsedOutput = Deserialize(input!);

value = parsedOutput!;
return value.NotificationId != Guid.Empty;
return value.NotificationId != Guid.Empty || value.GatewayReference != string.Empty;
}
catch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ public interface ISmsNotificationRepository
/// <summary>
/// Sets result status of an sms notification and update operation id
/// </summary>
public Task UpdateSendStatus(Guid notificationId, SmsNotificationResultType result, string? gatewayReference = null);
public Task UpdateSendStatus(Guid? notificationId, SmsNotificationResultType result, string? gatewayReference = null);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ public class SmsNotificationRepository : ISmsNotificationRepository
private const string _getSmsNotificationsSql = "select * from notifications.getsms_statusnew_updatestatus()";
private const string _getSmsRecipients = "select * from notifications.getsmsrecipients($1)"; // (_orderid)

private const string _updateSmsNotificationStatus =
private const string _updateSmsNotificationStatus =
@"UPDATE notifications.smsnotifications
SET result = $1::smsnotificationresulttype, resulttime = now(), gatewayreference = $2
WHERE alternateid = $3;"; // (_result, _gatewayreference, _alternateid, )
SET result = $1::smsnotificationresulttype,
resulttime = now(),
gatewayreference = $2
WHERE alternateid = $3 OR gatewayreference = $2;"; // (_result, _gatewayreference, _alternateid)

/// <summary>
/// Initializes a new instance of the <see cref="SmsNotificationRepository"/> class.
Expand Down Expand Up @@ -109,13 +111,13 @@ public async Task<List<Sms>> GetNewNotifications()
}

/// <inheritdoc/>
public async Task UpdateSendStatus(Guid notificationId, SmsNotificationResultType result, string? gatewayReference = null)
public async Task UpdateSendStatus(Guid? notificationId, SmsNotificationResultType result, string? gatewayReference = null)
{
await using NpgsqlCommand pgcom = _dataSource.CreateCommand(_updateSmsNotificationStatus);
using TelemetryTracker tracker = new(_telemetryClient, pgcom);
pgcom.Parameters.AddWithValue(NpgsqlDbType.Text, result.ToString());
pgcom.Parameters.AddWithValue(NpgsqlDbType.Text, gatewayReference ?? (object)DBNull.Value);
pgcom.Parameters.AddWithValue(NpgsqlDbType.Uuid, notificationId);
pgcom.Parameters.AddWithValue(NpgsqlDbType.Uuid, notificationId ?? (object)DBNull.Value);

await pgcom.ExecuteNonQueryAsync();
tracker.Track();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public async Task GetRecipients()
}

[Fact]
public async Task UpdateSendStatus_WithGatewayRef()
public async Task UpdateSendStatus_WithNotificationId_WithGatewayRef()
{
// Arrange
(NotificationOrder order, SmsNotification smsNotification) = await PostgreUtil.PopulateDBWithOrderAndSmsNotification();
Expand Down Expand Up @@ -136,7 +136,41 @@ FROM notifications.smsnotifications sms
}

[Fact]
public async Task UpdateSendStatus_WithoutGatewayRef()
public async Task UpdateSendStatus_WithoutNotificationId_WithGatewayRef()
{
// Arrange
(NotificationOrder order, SmsNotification smsNotification) = await PostgreUtil.PopulateDBWithOrderAndSmsNotification();
_orderIdsToDelete.Add(order.Id);

SmsNotificationRepository repo = (SmsNotificationRepository)ServiceUtil
.GetServices(new List<Type>() { typeof(ISmsNotificationRepository) })
.First(i => i.GetType() == typeof(SmsNotificationRepository));

string gatewayReference = Guid.NewGuid().ToString();

string setGateqwaySql = $@"Update notifications.smsnotifications
SET gatewayreference = '{gatewayReference}'
WHERE alternateid = '{smsNotification.Id}'";

await PostgreUtil.RunSql(setGateqwaySql);

// Act
await repo.UpdateSendStatus(null, SmsNotificationResultType.Accepted, gatewayReference);

// Assert
string sql = $@"SELECT count(1)
FROM notifications.smsnotifications sms
WHERE sms.alternateid = '{smsNotification.Id}'
AND sms.result = '{SmsNotificationResultType.Accepted}'
AND sms.gatewayreference = '{gatewayReference}'";

int actualCount = await PostgreUtil.RunSqlReturnOutput<int>(sql);

Assert.Equal(1, actualCount);
}

[Fact]
public async Task UpdateSendStatus_WithNotificationId_WithoutGatewayRef()
{
// Arrange
(NotificationOrder order, SmsNotification smsNotification) = await PostgreUtil.PopulateDBWithOrderAndSmsNotification();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Altinn.Notifications.Core.Models.Notification;

using Xunit;

namespace Altinn.Notifications.Tests.Notifications.Core.TestingModels
{
public class SmsSendOperationResultTests
{
[Fact]
public void TryParse_ValidInput_ReturnsTrue()
{
// Arrange
string input = "{\"notificationId\":\"d3b3f3e3-3e3b-3b3b-3b3b-3b3b3b3b3b3b\",\"gatewayReference\":\"123456789\",\"sendResult\":1}";

// Act
bool result = SmsSendOperationResult.TryParse(input, out _);

// Assert
Assert.True(result);
}

[Fact]
public void TryParse_ValidInput_NoNotificationId_ReturnsTrue()
{
// Arrange
string input = "{\"gatewayReference\":\"123456789\",\"sendResult\":1}";

// Act
bool result = SmsSendOperationResult.TryParse(input, out _);

// Assert
Assert.True(result);
}
}
}

0 comments on commit 43206c5

Please sign in to comment.