Skip to content

Commit 1aca252

Browse files
authored
Merge pull request #22 from umbraco/feature/zapier-trigger-data
Update trigger data
2 parents 1bdc542 + 6efb4ce commit 1aca252

File tree

5 files changed

+95
-41
lines changed

5 files changed

+95
-41
lines changed

src/Umbraco.Forms.Integrations.Automation.Zapier/Components/NewFormSubmittedComponent.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class NewFormSubmittedComposer : ComponentComposer<NewFormSubmittedCompon
1717

1818
public class NewFormSubmittedComponent : IComponent
1919
{
20-
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
20+
private readonly UmbUrlHelper _umbUrlHelper;
2121

2222
private readonly IRecordStorage _recordStorage;
2323

@@ -27,12 +27,14 @@ public class NewFormSubmittedComponent : IComponent
2727

2828
private readonly ILogger _logger;
2929

30-
public NewFormSubmittedComponent(IUmbracoContextAccessor umbracoContextAccessor, IRecordStorage recordStorage,
30+
public NewFormSubmittedComponent(
31+
UmbUrlHelper umbUrlHelper,
32+
IRecordStorage recordStorage,
3133
ZapierService zapierService,
3234
ZapierFormSubscriptionHookService zapierFormSubscriptionHookService,
3335
ILogger logger)
3436
{
35-
_umbracoContextAccessor = umbracoContextAccessor;
37+
_umbUrlHelper = umbUrlHelper;
3638

3739
_recordStorage = recordStorage;
3840

@@ -56,13 +58,9 @@ private void RecordStorage_RecordInserting(object sender, Core.RecordEventArgs e
5658
{
5759
var triggerHelper = new TriggerHelper(_zapierService);
5860

59-
UmbracoContext umbracoContext = _umbracoContextAccessor.UmbracoContext;
60-
var umbracoPageId = e.Record.UmbracoPageId;
61-
var pageUrl = umbracoContext.UrlProvider.GetUrl(umbracoPageId, UrlMode.Absolute);
62-
6361
if (_zapierFormSubscriptionHookService.TryGetById(e.Form.Id.ToString(), out var subscriptionHooks))
6462
{
65-
var content = e.Form.ToFormDictionary(e.Record, pageUrl);
63+
var content = e.Form.ToFormDictionary(e.Record, _umbUrlHelper.GetPageUrl(e.Record.UmbracoPageId));
6664

6765
foreach (var subscriptionHook in subscriptionHooks)
6866
{

src/Umbraco.Forms.Integrations.Automation.Zapier/Components/NewFormSubmittedNotification.cs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ namespace Umbraco.Forms.Integrations.Automation.Zapier.Components
1616
{
1717
public class NewFormSubmittedNotification : INotificationHandler<RecordCreatingNotification>
1818
{
19-
private readonly IUmbracoHelperAccessor _umbracoHelperAccessor;
20-
21-
private readonly IPublishedUrlProvider _publishedUrlProvider;
19+
private readonly UmbUrlHelper _umbUrlHelper;
2220

2321
private readonly IFormService _formService;
2422

@@ -29,15 +27,12 @@ public class NewFormSubmittedNotification : INotificationHandler<RecordCreatingN
2927
private readonly ILogger<NewFormSubmittedNotification> _logger;
3028

3129
public NewFormSubmittedNotification(
32-
IUmbracoHelperAccessor umbracoHelperAccessor,
33-
IPublishedUrlProvider publishedUrlProvider,
30+
UmbUrlHelper umbUrlHelper,
3431
IFormService formService,
3532
ZapierService zapierService, ZapierFormSubscriptionHookService zapierFormSubscriptionHookService,
3633
ILogger<NewFormSubmittedNotification> logger)
3734
{
38-
_umbracoHelperAccessor = umbracoHelperAccessor;
39-
40-
_publishedUrlProvider = publishedUrlProvider;
35+
_umbUrlHelper = umbUrlHelper;
4136

4237
_formService = formService;
4338

@@ -58,17 +53,7 @@ public void Handle(RecordCreatingNotification notification)
5853

5954
if (_zapierFormSubscriptionHookService.TryGetById(form.Id.ToString(), out var subscriptionHooks))
6055
{
61-
string pageUrl = string.Empty;
62-
if (_umbracoHelperAccessor.TryGetUmbracoHelper(out UmbracoHelper umbracoHelper))
63-
{
64-
IPublishedContent publishedContent = umbracoHelper.Content(notificationSavedEntity.UmbracoPageId);
65-
if (publishedContent != null)
66-
{
67-
pageUrl = publishedContent.Url(_publishedUrlProvider, mode: UrlMode.Absolute);
68-
}
69-
}
70-
71-
var content = form.ToFormDictionary(notificationSavedEntity, pageUrl);
56+
var content = form.ToFormDictionary(notificationSavedEntity, _umbUrlHelper.GetPageUrl(notificationSavedEntity.UmbracoPageId));
7257

7358
foreach (var subscriptionHook in subscriptionHooks)
7459
{

src/Umbraco.Forms.Integrations.Automation.Zapier/Controllers/FormPollingController.cs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.Linq;
4-
5-
using Umbraco.Forms.Integrations.Automation.Zapier.Models.Dtos;
3+
using Umbraco.Forms.Core.Data.Storage;
64
using Umbraco.Forms.Integrations.Automation.Zapier.Extensions;
5+
using Umbraco.Forms.Integrations.Automation.Zapier.Helpers;
76
using Umbraco.Forms.Integrations.Automation.Zapier.Services;
87

98
#if NETCOREAPP
@@ -22,32 +21,42 @@ public class FormPollingController : ZapierFormAuthorizedApiController
2221
{
2322
private readonly ZapierFormService _zapierFormService;
2423

24+
private readonly IRecordStorage _recordStorage;
25+
26+
private readonly UmbUrlHelper _umbUrlHelper;
27+
2528
#if NETCOREAPP
26-
public FormPollingController(IOptions<ZapierSettings> options, ZapierFormService zapierFormService, IUserValidationService userValidationService)
29+
public FormPollingController(IOptions<ZapierSettings> options, ZapierFormService zapierFormService, IRecordStorage recordStorage,
30+
UmbUrlHelper umbUrlHelper,
31+
IUserValidationService userValidationService)
2732
: base(options, userValidationService)
2833
#else
29-
public FormPollingController(ZapierFormService zapierFormService, IUserValidationService userValidationService)
34+
public FormPollingController(ZapierFormService zapierFormService, IRecordStorage recordStorage,
35+
UmbUrlHelper umbUrlHelper,
36+
IUserValidationService userValidationService)
3037
: base(userValidationService)
3138
#endif
3239
{
3340
_zapierFormService = zapierFormService;
34-
}
3541

36-
[Obsolete("Used only for Umbraco Zapier app v1.0.0. For updated versions use GetFormById")]
37-
public IEnumerable<FormDto> GetSampleForm()
38-
{
39-
if (!IsUserValid()) return null;
42+
_recordStorage = recordStorage;
4043

41-
return _zapierFormService.GetAll().Take(1);
44+
_umbUrlHelper = umbUrlHelper;
4245
}
4346

4447
public List<Dictionary<string, string>> GetFormPropertiesById(string id)
4548
{
4649
if (!IsUserValid()) return new List<Dictionary<string, string>>();
4750

4851
var form = _zapierFormService.GetById(id);
49-
return form != null
50-
? new List<Dictionary<string, string>> { form.ToFormDictionary() }
52+
53+
var latestFormRecord = _recordStorage.GetAllRecords(form)
54+
.OrderByDescending(p => p.Created)
55+
.FirstOrDefault();
56+
57+
return form != null && latestFormRecord != null
58+
? new List<Dictionary<string, string>>
59+
{form.ToFormDictionary(latestFormRecord, _umbUrlHelper.GetPageUrl(latestFormRecord.UmbracoPageId))}
5160
: new List<Dictionary<string, string>>();
5261
}
5362
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#if NETCOREAPP
2+
using Umbraco.Cms.Core.Models.PublishedContent;
3+
using Umbraco.Cms.Core.Routing;
4+
using Umbraco.Cms.Web.Common;
5+
using Umbraco.Extensions;
6+
#else
7+
using Umbraco.Core.Models.PublishedContent;
8+
using Umbraco.Web;
9+
#endif
10+
11+
namespace Umbraco.Forms.Integrations.Automation.Zapier.Helpers
12+
{
13+
public class UmbUrlHelper
14+
{
15+
#if NETCOREAPP
16+
private readonly IUmbracoHelperAccessor _umbracoHelperAccessor;
17+
18+
private readonly IPublishedUrlProvider _publishedUrlProvider;
19+
20+
public UmbUrlHelper(IUmbracoHelperAccessor umbracoHelperAccessor, IPublishedUrlProvider publishedUrlProvider)
21+
{
22+
_umbracoHelperAccessor = umbracoHelperAccessor;
23+
24+
_publishedUrlProvider = publishedUrlProvider;
25+
}
26+
#else
27+
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
28+
29+
public UmbUrlHelper(IUmbracoContextAccessor umbracoContextAccessor)
30+
{
31+
_umbracoContextAccessor = umbracoContextAccessor;
32+
}
33+
#endif
34+
35+
public string GetPageUrl(int umbracoPageId)
36+
{
37+
#if NETCOREAPP
38+
string pageUrl = string.Empty;
39+
if (_umbracoHelperAccessor.TryGetUmbracoHelper(out UmbracoHelper umbracoHelper))
40+
{
41+
IPublishedContent publishedContent = umbracoHelper.Content(umbracoPageId);
42+
if (publishedContent != null)
43+
{
44+
pageUrl = publishedContent.Url(_publishedUrlProvider, mode: UrlMode.Absolute);
45+
}
46+
}
47+
#else
48+
UmbracoContext umbracoContext = _umbracoContextAccessor.UmbracoContext;
49+
50+
var pageUrl = umbracoContext.UrlProvider.GetUrl(umbracoPageId, UrlMode.Absolute);
51+
#endif
52+
53+
return pageUrl;
54+
}
55+
}
56+
}

src/Umbraco.Forms.Integrations.Automation.Zapier/ZapierFormsComposer.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
using Umbraco.Cms.Core.Composing;
55
using Umbraco.Cms.Core.DependencyInjection;
66
using Umbraco.Forms.Integrations.Automation.Zapier.Configuration;
7+
using Umbraco.Forms.Integrations.Automation.Zapier.Helpers;
78
#else
89
using CSharpTest.Net.Collections;
910
using Umbraco.Core;
1011
using Umbraco.Core.Composing;
12+
using Umbraco.Forms.Integrations.Automation.Zapier.Helpers;
1113
#endif
1214

1315
using Umbraco.Forms.Integrations.Automation.Zapier.Services;
@@ -30,6 +32,8 @@ public void Compose(IUmbracoBuilder builder)
3032
builder.Services.AddSingleton<ZapierService>();
3133

3234
builder.Services.AddScoped<IUserValidationService, UserValidationService>();
35+
36+
builder.Services.AddSingleton<UmbUrlHelper>();
3337
}
3438
#else
3539
public void Compose(Composition composition)
@@ -41,6 +45,8 @@ public void Compose(Composition composition)
4145
composition.Register<ZapierService>(Lifetime.Singleton);
4246

4347
composition.Register<IUserValidationService, UserValidationService>(Lifetime.Scope);
48+
49+
composition.Register<UmbUrlHelper>(Lifetime.Singleton);
4450
}
4551
#endif
4652
}

0 commit comments

Comments
 (0)