Skip to content

Commit

Permalink
Do not update the SMS order based on the gateway reference. (#707)
Browse files Browse the repository at this point in the history
* Code refactoring
Do not update the SMS order based on the gateway reference.

* Remove the nullable operator and one test

* Code refactoring

* Enhance naming conventions and documentation to more accurately convey their intended purpose.

* Remove an old test

* Add a test to verify that an exception is throw if the identifier of the SMS is invalid

* Check the IDs before deleting them

* Improve the XML documentation.

* Remove an unused directive and improve documentation.

* Improve the documentation

* Code refactoring

* Increase test coverage

* Code refactoring

* Improve the unit tests to test a JSON string that results in a non-null outcome but with an empty Id and a null or empty GatewayReference.

* Improving the documentation

* Improve documentation

* Remove two nullable directives.

* Use lowerCamelCase

* Do not count on the gateway reference

* Rewrite unit tests.

* Copy back two tests

* Remove unused directive and update NuGet packages
  • Loading branch information
Ahmed-Ghanam authored Feb 12, 2025
1 parent c48c2bd commit 21506bc
Show file tree
Hide file tree
Showing 19 changed files with 374 additions and 209 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

<ItemGroup>
<PackageReference Include="libphonenumber-csharp" Version="8.13.54" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.1" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="9.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.2" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="9.0.2" />
</ItemGroup>

<ItemGroup Condition="'$(Configuration)'=='Debug'">
Expand Down
28 changes: 14 additions & 14 deletions src/Altinn.Notifications.Core/Enums/SmsNotificationResultType.cs
Original file line number Diff line number Diff line change
@@ -1,72 +1,72 @@
namespace Altinn.Notifications.Core.Enums;

/// <summary>
/// Enum describing sms notification result types
/// Enum representing the result types for SMS notifications.
/// </summary>
public enum SmsNotificationResultType
{
/// <summary>
/// Default result for new notifications
/// Indicates a new SMS notification.
/// </summary>
New,

/// <summary>
/// Sms notification being sent
/// Indicates that the SMS is currently being sent.
/// </summary>
Sending,

/// <summary>
/// Sms notification sent to service provider
/// Indicates that the SMS has been sent to the service provider.
/// </summary>
Accepted,

/// <summary>
/// Sms notification was successfully delivered to destination.
/// Indicates that the SMS was successfully delivered to the recipient.
/// </summary>
Delivered,

/// <summary>
/// Sms notification send operation failed
/// Indicates that the SMS send operation failed.
/// </summary>
Failed,

/// <summary>
/// Sms notification send operation failed due to invalid recipient
/// Indicates that the SMS send operation failed due to an invalid recipient.
/// </summary>
Failed_InvalidRecipient,

/// <summary>
/// Failed, recipient is reserved in KRR
/// Indicates that the SMS send operation failed because the recipient is reserved due to the contact and reservation register (KRR).
/// </summary>
Failed_RecipientReserved,

/// <summary>
/// Sms notification send operation failed because the receiver number is barred/blocked/not in use.
/// Indicates that the SMS send operation failed because the recipient's number is barred, blocked, or not in use.
/// </summary>
Failed_BarredReceiver,

/// <summary>
/// Sms notification send operation failed because the message has been deleted.
/// Indicates that the SMS send operation failed because the message has been deleted.
/// </summary>
Failed_Deleted,

/// <summary>
/// Sms notification send operation failed because the message validity period has expired.
/// Indicates that the SMS send operation failed because the message validity period has expired.
/// </summary>
Failed_Expired,

/// <summary>
/// Sms notification send operation failed due to the SMS being undeliverable.
/// Indicates that the SMS send operation failed because the SMS was undeliverable.
/// </summary>
Failed_Undelivered,

/// <summary>
/// Recipient mobile number was not identified
/// Indicates that the SMS send operation failed because the recipient's mobile number was not identified.
/// </summary>
Failed_RecipientNotIdentified,

/// <summary>
/// Message was rejected.
/// Indicates that the SMS send operation failed because the message was rejected.
/// </summary>
Failed_Rejected
}
27 changes: 13 additions & 14 deletions src/Altinn.Notifications.Core/Models/Notification/INotification.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
using System;

using Altinn.Notifications.Core.Enums;
using Altinn.Notifications.Core.Enums;

namespace Altinn.Notifications.Core.Models.Notification;

/// <summary>
/// Interface describing a base notification.
/// Defines the contract for a base notification.
/// </summary>
/// <typeparam name="TEnum">The type of the enumeration used to represent the notification status.</typeparam>
public interface INotification<TEnum>
where TEnum : struct, Enum
{
/// <summary>
/// Gets the id of the notification.
/// Gets the unique identifier of the notification.
/// </summary>
public Guid Id { get; }
Guid Id { get; }

/// <summary>
/// Gets the order id of the notification.
/// Gets the unique identifier of the order associated with this notification.
/// </summary>
public Guid OrderId { get; }
Guid OrderId { get; }

/// <summary>
/// Gets the requested send time of the notification.
/// Gets the date and time when the notification is requested to be sent.
/// </summary>
public DateTime RequestedSendTime { get; }
DateTime RequestedSendTime { get; }

/// <summary>
/// Gets the notifiction channel for the notification.
/// Gets the communication channel through which the notification will be sent.
/// </summary>
public NotificationChannel NotificationChannel { get; }
NotificationChannel NotificationChannel { get; }

/// <summary>
/// Gets the send result of the notification.
/// Gets the result of the notification send operation.
/// </summary>
public NotificationResult<TEnum> SendResult { get; }
NotificationResult<TEnum> SendResult { get; }
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
namespace Altinn.Notifications.Core.Models.Notification;

/// <summary>
/// A class represednting a notification result
/// Represents the result of the notification send operation.
/// </summary>
/// <typeparam name="TEnum">The type of the enumeration used to represent the notification send status.</typeparam>
public class NotificationResult<TEnum>
where TEnum : struct, Enum
{
/// <summary>
/// Initializes a new instance of the <see cref="NotificationResult{TEnum}"/> class.
/// </summary>
/// <param name="result">The result of the notification send operation.</param>
/// <param name="resultTime">The date and time when the result was set.</param>
public NotificationResult(TEnum result, DateTime resultTime)
{
ResultTime = resultTime;
Result = result;
ResultTime = resultTime;
}

/// <summary>
/// Sets the result description
/// </summary>
public void SetResultDescription(string? description)
{
ResultDescription = description;
}

/// <summary>
/// Gets the date and time for when the last result was set.
/// Gets the date and time when the result was set.
/// </summary>
public DateTime ResultTime { get; }

/// <summary>
/// Gets the send result of the notification
/// Gets the result of the notification send operation.
/// </summary>
public TEnum Result { get; }

/// <summary>
/// Gets the description of the send result
/// Gets the description of the send result.
/// </summary>
public string? ResultDescription { get; private set; }

/// <summary>
/// Sets the description of the send result.
/// </summary>
/// <param name="description">The description of the send result.</param>
public void SetResultDescription(string? description)
{
ResultDescription = description;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,38 @@
namespace Altinn.Notifications.Core.Models.Notification;

/// <summary>
/// Class describing an sns notification and extends the <see cref="INotification{SmsNotificationResultType}"/>
/// Represents an SMS notification that implements the <see cref="INotification{SmsNotificationResultType}"/> interface.
/// </summary>
public class SmsNotification : INotification<SmsNotificationResultType>
{
/// <inheritdoc/>
/// <summary>
/// Gets the unique identifier of the SMS notification.
/// </summary>
public Guid Id { get; internal set; }

/// <inheritdoc/>
/// <summary>
/// Gets the unique identifier of the order associated with this SMS notification.
/// </summary>
public Guid OrderId { get; internal set; }

/// <inheritdoc/>
/// <summary>
/// Gets the date and time when the SMS notification is requested to be sent.
/// </summary>
public DateTime RequestedSendTime { get; internal set; }

/// <inheritdoc/>
/// <summary>
/// Gets the communication channel through which the SMS notification will be sent.
/// This is always <see cref="NotificationChannel.Sms"/>.
/// </summary>
public NotificationChannel NotificationChannel { get; } = NotificationChannel.Sms;

/// <summary>
/// Get the recipient of the notification
/// Gets the recipient information for the SMS notification.
/// </summary>
public SmsRecipient Recipient { get; internal set; } = new();

/// <inheritdoc/>
/// <summary>
/// Gets the result of the SMS notification send operation.
/// </summary>
public NotificationResult<SmsNotificationResultType> SendResult { get; internal set; } = new(SmsNotificationResultType.New, DateTime.UtcNow);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,67 +5,75 @@
namespace Altinn.Notifications.Core.Models.Notification;

/// <summary>
/// A class representing a sms send operation update object
/// </summary>
/// Represents the result of an SMS send operation.
/// </summary>
public class SmsSendOperationResult
{
/// <summary>
/// The notification id
/// Gets or sets the unique identifier of the SMS notification.
/// </summary>
public Guid? NotificationId { get; set; }
public Guid Id { get; set; }

/// <summary>
/// The reference to the delivery in sms gateway
/// Gets or sets the reference to the delivery in the SMS gateway.
/// </summary>
public string GatewayReference { get; set; } = string.Empty;
public string? GatewayReference { get; set; } = null;

/// <summary>
/// The sms send result
/// Gets or sets the result of the SMS send operation.
/// </summary>
public SmsNotificationResultType SendResult { get; set; }

/// <summary>
/// Json serializes the <see cref="SmsSendOperationResult"/>
/// Serializes the <see cref="SmsSendOperationResult"/> object to a JSON string.
/// </summary>
/// <returns>A JSON string representation of the <see cref="SmsSendOperationResult"/> object.</returns>
public string Serialize()
{
return JsonSerializer.Serialize(this, JsonSerializerOptionsProvider.Options);
}

/// <summary>
/// Deserialize a json string into the <see cref="SmsSendOperationResult"/>
/// Deserializes a JSON string into an <see cref="SmsSendOperationResult"/> object.
/// </summary>
/// <param name="serializedString">The JSON string to deserialize.</param>
/// <returns>An <see cref="SmsSendOperationResult"/> object.</returns>
public static SmsSendOperationResult? Deserialize(string serializedString)
{
return JsonSerializer.Deserialize<SmsSendOperationResult>(
serializedString, JsonSerializerOptionsProvider.Options);
return JsonSerializer.Deserialize<SmsSendOperationResult>(serializedString, JsonSerializerOptionsProvider.Options);
}

/// <summary>
/// Try to parse a json string into a<see cref="SmsSendOperationResult"/>
/// Tries to parse a JSON string into an <see cref="SmsSendOperationResult"/> object.
/// </summary>
public static bool TryParse(string input, out SmsSendOperationResult value)
/// <param name="jsonString">The JSON string to parse.</param>
/// <param name="result">
/// When this method returns, contains the parsed <see cref="SmsSendOperationResult"/> object if the parsing succeeded;
/// otherwise, a new instance of <see cref="SmsSendOperationResult"/>.
/// </param>
/// <returns><c>true</c> if the JSON string was parsed successfully; otherwise, <c>false</c>.</returns>
public static bool TryParse(string jsonString, out SmsSendOperationResult result)
{
SmsSendOperationResult? parsedOutput;
value = new SmsSendOperationResult();
result = new SmsSendOperationResult();

if (string.IsNullOrEmpty(input))
if (string.IsNullOrWhiteSpace(jsonString))
{
return false;
}

try
{
parsedOutput = Deserialize(input!);

value = parsedOutput!;
return value.NotificationId != Guid.Empty || value.GatewayReference != string.Empty;
var parsedResult = Deserialize(jsonString);
if (parsedResult != null)
{
result = parsedResult;
}
}
catch
{
// try parse, we simply return false if fails
// Ignore exceptions and return false
}

return false;
return result.Id != Guid.Empty;
}
}
Loading

0 comments on commit 21506bc

Please sign in to comment.