-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Core + persistence logic for sms notification summaries #GPCActive (#416
) * Core logic for supporting sms notification summaries * Repository logic for supporting sms notification summaries
- Loading branch information
Showing
21 changed files
with
559 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/Altinn.Notifications.Core/Models/Notification/SmsNotificationSummary.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
namespace Altinn.Notifications.Core.Models.Notification | ||
{ | ||
/// <summary> | ||
/// An implementation of <see cref="INotificationSummary{TClass}"/> for sms notifications"/> | ||
/// </summary> | ||
public class SmsNotificationSummary : INotificationSummary<SmsNotificationWithResult> | ||
{ | ||
/// <inheritdoc/> | ||
public Guid OrderId { get; set; } | ||
|
||
/// <inheritdoc/> | ||
public string? SendersReference { get; set; } | ||
|
||
/// <inheritdoc/> | ||
public int Generated { get; internal set; } | ||
|
||
/// <inheritdoc/> | ||
public int Succeeded { get; internal set; } | ||
|
||
/// <inheritdoc/> | ||
public List<SmsNotificationWithResult> Notifications { get; set; } = new List<SmsNotificationWithResult>(); | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="SmsNotificationSummary"/> class. | ||
/// </summary> | ||
public SmsNotificationSummary(Guid orderId) | ||
{ | ||
OrderId = orderId; | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/Altinn.Notifications.Core/Models/Notification/SmsNotificationWithResult.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using Altinn.Notifications.Core.Enums; | ||
using Altinn.Notifications.Core.Models.Recipients; | ||
|
||
namespace Altinn.Notifications.Core.Models.Notification | ||
{ | ||
/// <summary> | ||
/// An implementation of <see cref="INotificationWithResult{TClass, T}"/> for sms notifications"/> | ||
/// Using the <see cref="SmsRecipient"/> as recipient type and the <see cref="SmsNotificationResultType"/> as result type | ||
/// </summary> | ||
public class SmsNotificationWithResult : INotificationWithResult<SmsRecipient, SmsNotificationResultType> | ||
{ | ||
/// <inheritdoc/> | ||
public Guid Id { get; } | ||
|
||
/// <inheritdoc/> | ||
public bool Succeeded { get; internal set; } | ||
|
||
/// <inheritdoc/> | ||
public SmsRecipient Recipient { get; } | ||
|
||
/// <inheritdoc/> | ||
public NotificationResult<SmsNotificationResultType> ResultStatus { get; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="SmsNotificationWithResult"/> class. | ||
/// </summary> | ||
public SmsNotificationWithResult(Guid id, SmsRecipient recipient, NotificationResult<SmsNotificationResultType> result) | ||
{ | ||
Id = id; | ||
Recipient = recipient; | ||
ResultStatus = result; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="SmsNotificationWithResult"/> class. | ||
/// </summary> | ||
internal SmsNotificationWithResult(Guid id, bool succeeded, SmsRecipient recipient, NotificationResult<SmsNotificationResultType> result) | ||
{ | ||
Id = id; | ||
Succeeded = succeeded; | ||
Recipient = recipient; | ||
ResultStatus = result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/Altinn.Notifications.Core/Services/Interfaces/ISmsNotificationSummaryService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Altinn.Notifications.Core.Models.Notification; | ||
using Altinn.Notifications.Core.Shared; | ||
|
||
namespace Altinn.Notifications.Core.Services.Interfaces | ||
{ | ||
/// <summary> | ||
/// Interface describing the sms notification summary service | ||
/// </summary> | ||
public interface ISmsNotificationSummaryService | ||
{ | ||
/// <summary> | ||
/// Gets a summary of all the generated sms notifications for the provided order id | ||
/// </summary> | ||
/// <param name="orderId">The order id to find notifications for</param> | ||
/// <param name="creator">The creator of the order</param> | ||
public Task<Result<SmsNotificationSummary, ServiceError>> GetSummary(Guid orderId, string creator); | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
src/Altinn.Notifications.Core/Services/SmsNotificationSummaryService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using Altinn.Notifications.Core.Enums; | ||
using Altinn.Notifications.Core.Models.Notification; | ||
using Altinn.Notifications.Core.Persistence; | ||
using Altinn.Notifications.Core.Services.Interfaces; | ||
using Altinn.Notifications.Core.Shared; | ||
|
||
namespace Altinn.Notifications.Core.Services | ||
{ | ||
/// <summary> | ||
/// Implementation of <see cref="ISmsNotificationSummaryService"/> | ||
/// </summary> | ||
public class SmsNotificationSummaryService : ISmsNotificationSummaryService | ||
{ | ||
private readonly INotificationSummaryRepository _summaryRepository; | ||
private readonly static Dictionary<SmsNotificationResultType, string> _smsResultDescriptions = new() | ||
{ | ||
{ SmsNotificationResultType.New, "The SMS has been created, but has not been picked up for processing yet." }, | ||
{ SmsNotificationResultType.Sending, "The SMS is being processed and will be attempted sent shortly." }, | ||
{ SmsNotificationResultType.Accepted, "The SMS has been accepted by the gateway service and will be sent shortly." }, | ||
{ SmsNotificationResultType.Failed, "The SMS was not sent due to an unspecified failure." }, | ||
{ SmsNotificationResultType.Failed_RecipientNotIdentified, "The SMS was not sent because the recipient's SMS address was not found." }, | ||
{ SmsNotificationResultType.Failed_InvalidRecipient, "The SMS was not sent because the recipient number was invalid." } | ||
}; | ||
|
||
private readonly static List<SmsNotificationResultType> _successResults = new() | ||
{ | ||
SmsNotificationResultType.Accepted | ||
}; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="SmsNotificationSummaryService"/> class. | ||
/// </summary> | ||
public SmsNotificationSummaryService(INotificationSummaryRepository summaryRepository) | ||
{ | ||
_summaryRepository = summaryRepository; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async Task<Result<SmsNotificationSummary, ServiceError>> GetSummary(Guid orderId, string creator) | ||
{ | ||
SmsNotificationSummary? summary = await _summaryRepository.GetSmsSummary(orderId, creator); | ||
|
||
if (summary == null) | ||
{ | ||
return new ServiceError(404); | ||
} | ||
|
||
if (summary.Notifications.Count != 0) | ||
{ | ||
ProcessNotificationResults(summary); | ||
} | ||
|
||
return summary; | ||
} | ||
|
||
/// <summary> | ||
/// Processes the notification results setting counts and descriptions | ||
/// </summary> | ||
internal static void ProcessNotificationResults(SmsNotificationSummary summary) | ||
{ | ||
summary.Generated = summary.Notifications.Count; | ||
|
||
foreach (SmsNotificationWithResult notification in summary.Notifications) | ||
{ | ||
NotificationResult<SmsNotificationResultType> resultStatus = notification.ResultStatus; | ||
if (IsSuccessResult(resultStatus.Result)) | ||
{ | ||
notification.Succeeded = true; | ||
++summary.Succeeded; | ||
} | ||
|
||
resultStatus.SetResultDescription(GetResultDescription(resultStatus.Result)); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Checks if the <see cref="SmsNotificationResultType"/> is a success result | ||
/// </summary> | ||
internal static bool IsSuccessResult(SmsNotificationResultType result) | ||
{ | ||
return _successResults.Contains(result); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the English description of the <see cref="SmsNotificationResultType"/>" | ||
/// </summary> | ||
internal static string GetResultDescription(SmsNotificationResultType result) | ||
{ | ||
return _smsResultDescriptions[result]; | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Altinn.Notifications.Persistence/Migration/v0.21/01-setup-functions.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
CREATE OR REPLACE FUNCTION notifications.getsmssummary( | ||
_alternateorderid uuid, | ||
_creatorname text) | ||
RETURNS TABLE( | ||
sendersreference text, | ||
alternateid uuid, | ||
recipientid text, | ||
mobilenumber text, | ||
result smsnotificationresulttype, | ||
resulttime timestamptz) | ||
LANGUAGE 'plpgsql' | ||
AS $BODY$ | ||
|
||
BEGIN | ||
RETURN QUERY | ||
SELECT o.sendersreference, n.alternateid, n.recipientid, n.mobilenumber, n.result, n.resulttime | ||
FROM notifications.smsnotifications n | ||
LEFT JOIN notifications.orders o ON n._orderid = o._id | ||
WHERE o.alternateid = _alternateorderid | ||
and o.creatorname = _creatorname; | ||
IF NOT FOUND THEN | ||
RETURN QUERY | ||
SELECT o.sendersreference, NULL::uuid, NULL::text, NULL::text, NULL::smsnotificationresulttype, NULL::timestamptz | ||
FROM notifications.orders o | ||
WHERE o.alternateid = _alternateorderid | ||
and o.creatorname = _creatorname; | ||
END IF; | ||
END; | ||
$BODY$; |
Oops, something went wrong.