1
1
using System ;
2
2
using System . Collections . Generic ;
3
- using System . Linq ;
4
3
using System . Threading ;
5
4
using System . Threading . Tasks ;
6
- using Umbraco . Cms . Core ;
7
5
using Umbraco . Cms . Core . Cache ;
8
6
using Umbraco . Cms . Core . Events ;
9
7
using Umbraco . Cms . Core . Models . PublishedContent ;
12
10
using Umbraco . Cms . Core . Services . Changes ;
13
11
using Umbraco . Cms . Core . Services . Navigation ;
14
12
using Umbraco . Cms . Core . Sync ;
15
- using Umbraco . Cms . Core . Web ;
16
13
using Umbraco . Commerce . Checkout . Web ;
17
14
using Umbraco . Commerce . Core . Api ;
18
15
using Umbraco . Commerce . Core . Models ;
@@ -23,94 +20,81 @@ namespace Umbraco.Commerce.Checkout.Events
23
20
{
24
21
public class SyncZeroValuePaymentProviderContinueUrl : INotificationAsyncHandler < ContentCacheRefresherNotification >
25
22
{
26
- private readonly IUmbracoContextFactory _umbracoContextFactory ;
27
23
private readonly IUmbracoCommerceApi _commerceApi ;
24
+ private readonly INavigationQueryService _navigationQueryService ;
25
+ private readonly IPublishedContentCache _publishedContentCacheService ;
28
26
private readonly IPublishedContentTypeCache _publishedContentTypeCacheService ;
29
- private readonly IDocumentNavigationQueryService _documentNavigationQueryService ;
30
27
31
28
public SyncZeroValuePaymentProviderContinueUrl (
32
- IUmbracoContextFactory umbracoContextFactory ,
33
29
IUmbracoCommerceApi commerceApi ,
34
- IPublishedContentTypeCache publishedContentTypeCacheService ,
35
- IDocumentNavigationQueryService documentNavigationQueryService )
30
+ INavigationQueryService navigationQueryService ,
31
+ IPublishedContentCache publishedContentCacheService ,
32
+ IPublishedContentTypeCache publishedContentTypeCacheService )
36
33
{
37
- _umbracoContextFactory = umbracoContextFactory ;
38
34
_commerceApi = commerceApi ;
35
+ _navigationQueryService = navigationQueryService ;
36
+ _publishedContentCacheService = publishedContentCacheService ;
39
37
_publishedContentTypeCacheService = publishedContentTypeCacheService ;
40
- _documentNavigationQueryService = documentNavigationQueryService ;
41
38
}
42
39
43
- public Task HandleAsync ( ContentCacheRefresherNotification notification , CancellationToken cancellationToken )
40
+ public async Task HandleAsync ( ContentCacheRefresherNotification notification , CancellationToken cancellationToken )
44
41
{
45
42
ArgumentNullException . ThrowIfNull ( notification , nameof ( notification ) ) ;
46
- return HandleAsync ( notification . MessageObject , notification . MessageType ) ;
47
- }
48
43
49
- public async Task HandleAsync ( object messageObject , MessageType messageType )
50
- {
51
- if ( messageType != MessageType . RefreshByPayload )
44
+ if ( notification . MessageType != MessageType . RefreshByPayload )
52
45
{
53
46
throw new NotSupportedException ( ) ;
54
47
}
55
48
56
- if ( messageObject is not ContentCacheRefresher . JsonPayload [ ] payloads )
49
+ if ( notification . MessageObject is not ContentCacheRefresher . JsonPayload [ ] payloads )
57
50
{
58
51
return ;
59
52
}
60
53
61
- using ( UmbracoContextReference ctx = _umbracoContextFactory . EnsureUmbracoContext ( ) )
54
+ foreach ( ContentCacheRefresher . JsonPayload payload in payloads )
62
55
{
63
- foreach ( ContentCacheRefresher . JsonPayload payload in payloads )
56
+ if ( payload . ChangeTypes . HasType ( TreeChangeTypes . RefreshNode ) )
64
57
{
65
- if ( payload . ChangeTypes . HasType ( TreeChangeTypes . RefreshNode ) )
58
+ // Single node refresh
59
+ IPublishedContent ? node = _publishedContentCacheService . GetById ( payload . Id ) ;
60
+ if ( node != null && IsConfirmationPageType ( node ) )
66
61
{
67
- // Single node refresh
68
- IPublishedContent ? node = ctx . UmbracoContext . Content . GetById ( payload . Id ) ;
69
- if ( node != null && IsConfirmationPageType ( node ) )
70
- {
71
- await DoSyncZeroValuePaymentProviderContinueUrlAsync ( node ) ;
72
- }
62
+ await DoSyncZeroValuePaymentProviderContinueUrlAsync ( node ) ;
73
63
}
74
- else if ( payload . ChangeTypes . HasType ( TreeChangeTypes . RefreshBranch ) )
64
+ }
65
+ else if ( payload . ChangeTypes . HasType ( TreeChangeTypes . RefreshBranch ) )
66
+ {
67
+ // Branch refresh
68
+ IPublishedContent ? rootNode = _publishedContentCacheService . GetById ( payload . Id ) ;
69
+ if ( rootNode != null && _navigationQueryService . TryGetDescendantsKeysOfType ( rootNode . Key , UmbracoCommerceCheckoutConstants . ContentTypes . Aliases . CheckoutStepPage , out IEnumerable < Guid > keys ) )
75
70
{
76
- // Branch refresh
77
- IPublishedContent ? rootNode = ctx . UmbracoContext . Content . GetById ( payload . Id ) ;
78
- if ( rootNode != null )
71
+ foreach ( Guid key in keys )
79
72
{
80
- IPublishedContentType checkoutStepPageDocType = _publishedContentTypeCacheService . Get ( PublishedItemType . Content , UmbracoCommerceCheckoutConstants . ContentTypes . Aliases . CheckoutStepPage ) ;
81
- // TODO - Dinh: remove this line
82
- //IPublishedContentType? nodeType = ctx.UmbracoContext.Content.GetContentType(UmbracoCommerceCheckoutConstants.ContentTypes.Aliases.CheckoutStepPage);
83
- if ( checkoutStepPageDocType == null )
84
- {
85
- continue ;
86
- }
87
-
88
- // TODO: fix the obsolete warning. Try using INavigationQueryService
89
- IEnumerable < IPublishedContent > nodes = ctx . UmbracoContext . Content . GetByContentType ( checkoutStepPageDocType ) ;
90
- IEnumerable < IPublishedContent > confimationPages = nodes ? . Where ( x => IsConfirmationPageType ( x ) && x . Path . StartsWith ( rootNode . Path , StringComparison . Ordinal ) ) ?? [ ] ;
91
- foreach ( IPublishedContent ? node in confimationPages )
73
+ IPublishedContent ? node = _publishedContentCacheService . GetById ( key ) ;
74
+ if ( node != null && IsConfirmationPageType ( node ) )
92
75
{
93
76
await DoSyncZeroValuePaymentProviderContinueUrlAsync ( node ) ;
94
77
}
95
78
}
96
79
}
97
- else if ( payload . ChangeTypes . HasType ( TreeChangeTypes . RefreshAll ) )
80
+ }
81
+ else if ( payload . ChangeTypes . HasType ( TreeChangeTypes . RefreshAll ) )
82
+ {
83
+ if ( _navigationQueryService . TryGetRootKeys ( out IEnumerable < Guid > rootKeys ) )
98
84
{
99
- // All refresh
100
- // TODO - Dinh: remove this line
101
- //IPublishedContentType? nodeType = ctx.UmbracoContext.Content.GetContentType(UmbracoCommerceCheckoutConstants.ContentTypes.Aliases.CheckoutStepPage);
102
- IPublishedContentType checkoutStepPageDocType = _publishedContentTypeCacheService . Get ( PublishedItemType . Content , UmbracoCommerceCheckoutConstants . ContentTypes . Aliases . CheckoutStepPage ) ;
103
- if ( checkoutStepPageDocType == null )
85
+ foreach ( Guid rootKey in rootKeys )
104
86
{
105
- continue ;
106
- }
107
-
108
- // TODO: fix the obsolete warning. Try using INavigationQueryService
109
- IEnumerable < IPublishedContent > nodes = ctx . UmbracoContext . Content . GetByContentType ( checkoutStepPageDocType ) ;
110
- IEnumerable < IPublishedContent > confimationPages = nodes ? . Where ( IsConfirmationPageType ) ?? [ ] ;
111
- foreach ( IPublishedContent ? node in confimationPages )
112
- {
113
- await DoSyncZeroValuePaymentProviderContinueUrlAsync ( node ) ;
87
+ if ( _navigationQueryService . TryGetDescendantsKeysOfType ( rootKey , UmbracoCommerceCheckoutConstants . ContentTypes . Aliases . CheckoutStepPage , out IEnumerable < Guid > keys ) )
88
+ {
89
+ foreach ( Guid key in keys )
90
+ {
91
+ IPublishedContent ? node = _publishedContentCacheService . GetById ( key ) ;
92
+ if ( node != null && IsConfirmationPageType ( node ) )
93
+ {
94
+ await DoSyncZeroValuePaymentProviderContinueUrlAsync ( node ) ;
95
+ }
96
+ }
97
+ }
114
98
}
115
99
}
116
100
}
@@ -119,7 +103,7 @@ public async Task HandleAsync(object messageObject, MessageType messageType)
119
103
120
104
private static bool IsConfirmationPageType ( IPublishedContent node )
121
105
{
122
- if ( node == null || node . ContentType == null || ! node . HasProperty ( "uccStepType" ) )
106
+ if ( ! node . HasProperty ( "uccStepType" ) )
123
107
{
124
108
return false ;
125
109
}
@@ -129,13 +113,9 @@ private static bool IsConfirmationPageType(IPublishedContent node)
129
113
130
114
private async Task DoSyncZeroValuePaymentProviderContinueUrlAsync ( IPublishedContent confirmationNode )
131
115
{
132
- if ( confirmationNode == null )
133
- {
134
- return ;
135
- }
136
-
137
116
StoreReadOnly store = confirmationNode . GetStore ( ) ;
138
117
PaymentMethodReadOnly paymentMethod = await _commerceApi . GetPaymentMethodAsync ( store . Id , UmbracoCommerceCheckoutConstants . PaymentMethods . Aliases . ZeroValue ) ;
118
+
139
119
if ( paymentMethod == null )
140
120
{
141
121
return ;
0 commit comments