Skip to content

Commit 437406d

Browse files
committed
order cancellation implemented
1 parent f72b021 commit 437406d

File tree

16 files changed

+1099
-43
lines changed

16 files changed

+1099
-43
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace Altinn.Notifications.Core.Enums
2+
{
3+
/// <summary>
4+
/// Enum for the different types of errors that can occur when cancelling an order
5+
/// </summary>
6+
public enum CancellationError
7+
{
8+
/// <summary>
9+
/// Order was not found
10+
/// </summary>
11+
OrderNotFound,
12+
13+
/// <summary>
14+
/// Order was found but processing had already started
15+
/// </summary>
16+
CancellationProhibited
17+
}
18+
}

src/Altinn.Notifications.Core/Enums/OrderProcessingStatus.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public enum OrderProcessingStatus
99
Registered,
1010
Processing,
1111
Completed,
12-
SendConditionNotMet
12+
SendConditionNotMet,
13+
Cancelled
1314
}
1415
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member

src/Altinn.Notifications.Core/Persistence/IOrderRepository.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Altinn.Notifications.Core.Enums;
22
using Altinn.Notifications.Core.Models.Orders;
3+
using Altinn.Notifications.Core.Shared;
34

45
namespace Altinn.Notifications.Core.Persistence;
56

@@ -49,4 +50,12 @@ public interface IOrderRepository
4950
/// <param name="creator">The short name of the order creator</param>
5051
/// <returns>A list of notification orders</returns>
5152
public Task<List<NotificationOrder>> GetOrdersBySendersReference(string sendersReference, string creator);
53+
54+
/// <summary>
55+
/// Cancels the order corresponding to the provided id within the provided creator scope if processing has not started yet
56+
/// </summary>
57+
/// <param name="id">The order id</param>
58+
/// <param name="creator">The short name of the order creator</param>
59+
/// <returns>If successful the cancelled notification order with status info. If error a cancellation error type.</returns>
60+
public Task<Result<NotificationOrderWithStatus, CancellationError>> CancelOrder(Guid id, string creator);
5261
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Altinn.Notifications.Core.Enums;
2+
using Altinn.Notifications.Core.Models.Orders;
3+
using Altinn.Notifications.Core.Persistence;
4+
using Altinn.Notifications.Core.Services.Interfaces;
5+
using Altinn.Notifications.Core.Shared;
6+
7+
namespace Altinn.Notifications.Core.Services
8+
{
9+
/// <summary>
10+
/// Implementation of the <see cref="ICancelOrderService"/> interface.
11+
/// </summary>
12+
public class CancelOrderService : ICancelOrderService
13+
{
14+
private readonly IOrderRepository _repository;
15+
16+
/// <summary>
17+
/// Initializes a new instance of the <see cref="CancelOrderService"/> class.
18+
/// </summary>
19+
/// <param name="repository">The repository</param>
20+
public CancelOrderService(IOrderRepository repository)
21+
{
22+
_repository = repository;
23+
}
24+
25+
/// <inheritdoc/>
26+
public async Task<Result<NotificationOrderWithStatus, CancellationError>> CancelOrder(Guid id, string creator)
27+
{
28+
var result = await _repository.CancelOrder(id, creator);
29+
30+
return result.Match<Result<NotificationOrderWithStatus, CancellationError>>(
31+
order =>
32+
{
33+
order.ProcessingStatus.StatusDescription = GetOrderService.GetStatusDescription(order.ProcessingStatus.Status);
34+
return order;
35+
},
36+
error =>
37+
{
38+
return error;
39+
});
40+
}
41+
}
42+
}

src/Altinn.Notifications.Core/Services/GetOrderService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public class GetOrderService : IGetOrderService
1717
{ OrderProcessingStatus.Registered, "Order has been registered and is awaiting requested send time before processing." },
1818
{ OrderProcessingStatus.Processing, "Order processing is ongoing. Notifications are being generated." },
1919
{ OrderProcessingStatus.Completed, "Order processing is completed. All notifications have been generated." },
20-
{ OrderProcessingStatus.SendConditionNotMet, "Order processing was stopped due to send condition not being met." }
20+
{ OrderProcessingStatus.SendConditionNotMet, "Order processing was stopped due to send condition not being met." },
21+
{ OrderProcessingStatus.Cancelled, "Order processing was stopped due to order being cancelled." }
2122
};
2223

2324
/// <summary>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Altinn.Notifications.Core.Enums;
2+
using Altinn.Notifications.Core.Models.Orders;
3+
using Altinn.Notifications.Core.Shared;
4+
5+
namespace Altinn.Notifications.Core.Services.Interfaces
6+
{
7+
/// <summary>
8+
/// Interface for operations related to cancelling notification orders
9+
/// </summary>
10+
public interface ICancelOrderService
11+
{
12+
/// <summary>
13+
/// Cancels an order if it has not been processed yet
14+
/// </summary>
15+
/// <param name="id">The order id</param>
16+
/// <param name="creator">The creator of the orders</param>
17+
/// <returns>The cancelled order or a </returns>
18+
public Task<Result<NotificationOrderWithStatus, CancellationError>> CancelOrder(Guid id, string creator);
19+
}
20+
}

src/Altinn.Notifications.Persistence/Altinn.Notifications.Persistence.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
<DbToolsExecuted>true</DbToolsExecuted>
5555
</PropertyGroup>
5656

57-
<Exec Command="$(TargetDir)../../../../DbTools/bin/Debug/$(TargetFramework)/DbTools $(TargetDir)../../../Migration"
58-
Condition="'$(DbToolsExecuted)' != 'true' and (Exists('$(TargetDir)../../../../DbTools/bin/Debug/$(TargetFramework)/DbTools') or Exists('$(TargetDir)../../../../DbTools/bin/Debug/$(TargetFramework)/DbTools.exe'))" />
57+
<Exec Command="$(TargetDir)../../../../DbTools/bin/Debug/$(TargetFramework)/DbTools $(TargetDir)../../../Migration" Condition="'$(DbToolsExecuted)' != 'true' and (Exists('$(TargetDir)../../../../DbTools/bin/Debug/$(TargetFramework)/DbTools') or Exists('$(TargetDir)../../../../DbTools/bin/Debug/$(TargetFramework)/DbTools.exe'))" />
5958
</Target>
6059
</Project>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
CREATE OR REPLACE FUNCTION notifications.cancelorder(
2+
_alternateid uuid,
3+
_creatorname text
4+
)
5+
RETURNS TABLE(
6+
cancelallowed boolean,
7+
alternateid uuid,
8+
creatorname text,
9+
sendersreference text,
10+
created timestamp with time zone,
11+
requestedsendtime timestamp with time zone,
12+
processed timestamp with time zone,
13+
processedstatus orderprocessingstate,
14+
notificationchannel text,
15+
ignorereservation boolean,
16+
resourceid text,
17+
conditionendpoint text,
18+
generatedemailcount bigint,
19+
succeededemailcount bigint,
20+
generatedsmscount bigint,
21+
succeededsmscount bigint
22+
)
23+
LANGUAGE plpgsql
24+
AS $$
25+
DECLARE
26+
order_record RECORD;
27+
BEGIN
28+
-- Retrieve the order and its status
29+
SELECT o.requestedsendtime, o.processedstatus
30+
INTO order_record
31+
FROM notifications.orders o
32+
WHERE o.alternateid = _alternateid AND o.creatorname = _creatorname;
33+
34+
-- If no order is found, return an empty result set
35+
IF NOT FOUND THEN
36+
RETURN;
37+
END IF;
38+
39+
-- Check if order is already cancelled
40+
IF order_record.processedstatus = 'Cancelled' THEN
41+
RETURN QUERY
42+
SELECT TRUE AS cancelallowed,
43+
order_details.*
44+
FROM notifications.getorder_includestatus_v4(_alternateid, _creatorname) AS order_details;
45+
ELSEIF (order_record.requestedsendtime <= NOW() + INTERVAL '5 minutes' or order_record.processedstatus != 'Registered') THEN
46+
RETURN QUERY
47+
SELECT FALSE AS cancelallowed, NULL::uuid, NULL::text, NULL::text, NULL::timestamp with time zone, NULL::timestamp with time zone, NULL::timestamp with time zone, NULL::orderprocessingstate, NULL::text, NULL::boolean, NULL::text, NULL::text, NULL::bigint, NULL::bigint, NULL::bigint, NULL::bigint;
48+
ELSE
49+
-- Cancel the order by updating its status
50+
UPDATE notifications.orders
51+
SET processedstatus = 'Cancelled', processed = NOW()
52+
WHERE notifications.orders.alternateid = _alternateid;
53+
54+
-- Retrieve the updated order details
55+
RETURN QUERY
56+
SELECT TRUE AS cancelallowed,
57+
order_details.*
58+
FROM notifications.getorder_includestatus_v4(_alternateid, _creatorname) AS order_details;
59+
END IF;
60+
END;
61+
$$;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TYPE public.orderprocessingstate ADD VALUE IF NOT EXISTS 'Cancelled';

0 commit comments

Comments
 (0)