Skip to content

Commit 9914c26

Browse files
committed
Code cleanup. Include Standard Fields mapping. Add mapping service.
1 parent ae6f7f8 commit 9914c26

File tree

10 files changed

+156
-68
lines changed

10 files changed

+156
-68
lines changed

src/Umbraco.Forms.Integrations.Automation.Zapier/App_Plugins/UmbracoForms.Integrations/Automation/Zapier/fields-mapping.html

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/Umbraco.Forms.Integrations.Automation.Zapier/App_Plugins/UmbracoForms.Integrations/Automation/Zapier/main.controller.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/Umbraco.Forms.Integrations.Automation.Zapier/App_Plugins/UmbracoForms.Integrations/Automation/Zapier/package.manifest

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/Umbraco.Forms.Integrations.Automation.Zapier/Models/Mapping.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+

2+
using System.Collections.Generic;
3+
using Umbraco.Forms.Core;
4+
5+
namespace Umbraco.Forms.Integrations.Automation.Zapier.Services
6+
{
7+
public interface IMappingService
8+
{
9+
IMappingService IncludeFieldsMappings(string mappings, RecordEventArgs e);
10+
11+
IMappingService IncludeStandardFieldsMappings(string mappings, RecordEventArgs e);
12+
13+
Dictionary<string, string> Map();
14+
}
15+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
using Newtonsoft.Json;
6+
7+
using Umbraco.Core.Models.PublishedContent;
8+
using Umbraco.Forms.Core;
9+
using Umbraco.Forms.Core.Providers.Models;
10+
using Umbraco.Web;
11+
12+
namespace Umbraco.Forms.Integrations.Automation.Zapier.Services
13+
{
14+
public class MappingService: IMappingService
15+
{
16+
private readonly Dictionary<string, string> _content;
17+
18+
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
19+
20+
public MappingService(IUmbracoContextAccessor umbracoContextAccessor)
21+
{
22+
_umbracoContextAccessor = umbracoContextAccessor;
23+
24+
_content = new Dictionary<string, string>();
25+
}
26+
27+
public IMappingService IncludeFieldsMappings(string mappings, RecordEventArgs e)
28+
{
29+
var mappingsList = JsonConvert.DeserializeObject<List<FieldMapping>>(mappings);
30+
if (mappingsList.Any())
31+
{
32+
foreach (var mapping in mappingsList)
33+
{
34+
var fieldRecord = e.Record.RecordFields[Guid.Parse(mapping.Value)];
35+
_content.Add(mapping.Alias, string.IsNullOrEmpty(mapping.StaticValue) ? fieldRecord.ValuesAsString() : mapping.StaticValue);
36+
}
37+
}
38+
39+
return this;
40+
}
41+
42+
public IMappingService IncludeStandardFieldsMappings(string mappings, RecordEventArgs e)
43+
{
44+
if (!string.IsNullOrEmpty(mappings))
45+
{
46+
var standardFieldMappings =
47+
JsonConvert.DeserializeObject<IEnumerable<StandardFieldMapping>>(mappings,
48+
FormsJsonSerializerSettings.Default).ToList();
49+
50+
foreach (StandardFieldMapping fieldMapping in standardFieldMappings.Where(x => x.Include))
51+
{
52+
switch (fieldMapping.Field)
53+
{
54+
case StandardField.FormId:
55+
_content.Add(fieldMapping.KeyName, e.Form.Id.ToString());
56+
break;
57+
case StandardField.FormName:
58+
_content.Add(fieldMapping.KeyName, e.Form.Name);
59+
break;
60+
case StandardField.PageUrl:
61+
UmbracoContext umbracoContext = _umbracoContextAccessor.UmbracoContext;
62+
var pageUrl = umbracoContext.UrlProvider.GetUrl(e.Record.UmbracoPageId, UrlMode.Absolute);
63+
_content.Add(fieldMapping.KeyName, pageUrl);
64+
break;
65+
case StandardField.SubmissionDate:
66+
_content.Add(fieldMapping.KeyName, e.Record.Created.ToString());
67+
break;
68+
default:
69+
throw new InvalidOperationException(
70+
$"The field '{fieldMapping.Field}' is not supported for including in the collection.");
71+
}
72+
}
73+
}
74+
75+
return this;
76+
}
77+
78+
public Dictionary<string, string> Map() => _content;
79+
}
80+
}

src/Umbraco.Forms.Integrations.Automation.Zapier/Umbraco.Forms.Integrations.Automation.Zapier.csproj

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,4 @@
2222
<PackageReference Include="UmbracoForms.Core" Version="8.11.0" />
2323
</ItemGroup>
2424

25-
<ItemGroup>
26-
<Content Include="App_Plugins\UmbracoForms.Integrations\Automation\Zapier\**\*.*">
27-
<Pack>true</Pack>
28-
<PackagePath>App_Plugins\UmbracoForms.Integrations\Automation\Zapier\</PackagePath>
29-
</Content>
30-
</ItemGroup>
31-
3225
</Project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Umbraco.Core;
2+
using Umbraco.Core.Composing;
3+
using Umbraco.Forms.Integrations.Automation.Zapier.Services;
4+
5+
namespace Umbraco.Forms.Integrations.Automation.Zapier
6+
{
7+
public class ZapierComposer : IUserComposer
8+
{
9+
public void Compose(Composition composition)
10+
{
11+
composition.Register<IMappingService, MappingService>(Lifetime.Singleton);
12+
}
13+
}
14+
}

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
43
using System.Net.Http;
5-
using Newtonsoft.Json;
4+
65
using Umbraco.Forms.Core;
76
using Umbraco.Forms.Core.Enums;
87
using Umbraco.Forms.Core.Persistence.Dtos;
9-
using Umbraco.Forms.Integrations.Automation.Zapier.Models;
8+
using Umbraco.Forms.Integrations.Automation.Zapier.Services;
109
using Umbraco.Forms.Integrations.Automation.Zapier.Validators;
1110

1211
namespace Umbraco.Forms.Integrations.Automation.Zapier
1312
{
1413
public class ZapierWorkflow : WorkflowType
1514
{
16-
public ZapierWorkflow()
15+
private readonly IMappingService _mappingService;
16+
17+
public ZapierWorkflow(IMappingService mappingService)
1718
{
19+
_mappingService = mappingService;
20+
1821
Name = "Trigger Zap";
1922
Id = new Guid("d05b95e5-86f8-4c31-99b8-4ec7fc62a787");
2023
Description = "Automation workflow for triggering Zaps in Zapier.";
@@ -26,6 +29,11 @@ public ZapierWorkflow()
2629
View = "FieldMapper")]
2730
public string Mappings { get; set; }
2831

32+
[Core.Attributes.Setting("Standard Fields Mappings",
33+
Description = "Please map fields of the form against Zap ones",
34+
View = "StandardFieldMapper")]
35+
public string StandardFieldsMappings { get; set; }
36+
2937
[Core.Attributes.Setting("WebHook Uri",
3038
Description = "Zapier WebHook URL",
3139
View = "TextField")]
@@ -41,17 +49,10 @@ public override WorkflowExecutionStatus Execute(Record record, RecordEventArgs e
4149
{
4250
try
4351
{
44-
var content = new Dictionary<string, string>();
45-
46-
var mappings = JsonConvert.DeserializeObject<List<Mapping>>(Mappings);
47-
if (mappings.Any())
48-
{
49-
foreach (var mapping in mappings)
50-
{
51-
var fieldRecord = record.RecordFields[Guid.Parse(mapping.Value)];
52-
content.Add(mapping.Alias, string.IsNullOrEmpty(mapping.StaticValue) ? fieldRecord.ValuesAsString() : mapping.StaticValue);
53-
}
54-
}
52+
var content = _mappingService
53+
.IncludeFieldsMappings(Mappings, e)
54+
.IncludeStandardFieldsMappings(StandardFieldsMappings, e)
55+
.Map();
5556

5657
var result = ClientFactory().PostAsync(WebHookUri, new FormUrlEncodedContent(content)).Result;
5758

@@ -74,5 +75,7 @@ public override List<Exception> ValidateSettings()
7475

7576
return exceptions;
7677
}
78+
79+
7780
}
7881
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<umbPackage>
3+
<info>
4+
<package>
5+
<name>Umbraco.Forms.Integrations.Automation.Zapier</name>
6+
<version>1.1.1</version>
7+
<iconUrl></iconUrl>
8+
<licence url="https://opensource.org/licenses/MIT">MIT</licence>
9+
<url>https://github.com/umbraco/Umbraco.Forms.Integrations</url>
10+
<requirements type="strict">
11+
<major>8</major>
12+
<minor>1</minor>
13+
<patch>0</patch>
14+
</requirements>
15+
</package>
16+
<author>
17+
<name>Umbraco HQ</name>
18+
<website>https://github.com/umbraco/Umbraco.Forms.Integrations</website>
19+
</author>
20+
<contributors>
21+
<contributor>Adrian Cojocariu</contributor>
22+
</contributors>
23+
<readme><![CDATA[An extension for Umbraco Forms adding support for calling Zapier triggers.]]></readme>
24+
</info>
25+
<files>
26+
<folder path="App_Plugins/UmbracoForms.Integrations/Automation/Zapier" orgPath="App_Plugins/UmbracoForms.Integrations/Automation/Zapier" />
27+
<file path="bin/release/net472/Umbraco.Forms.Integrations.Automation.Zapier.dll" orgPath="bin/Umbraco.Forms.Integrations.Automation.Zapier.dll" />
28+
</files>
29+
</umbPackage>

0 commit comments

Comments
 (0)