1
- using Microsoft . Extensions . Options ;
1
+ using Microsoft . Extensions . Logging ;
2
+ using Microsoft . Extensions . Options ;
2
3
3
4
using System . Text . Json ;
4
5
@@ -19,6 +20,8 @@ public class ActiveCampaignContactsWorkflow : WorkflowType
19
20
20
21
private readonly IContactService _contactService ;
21
22
23
+ private readonly ILogger < ActiveCampaignContactsWorkflow > _logger ;
24
+
22
25
[ Core . Attributes . Setting ( "Account" ,
23
26
Description = "Please select an account" ,
24
27
View = "~/App_Plugins/UmbracoForms.Integrations/Crm/ActiveCampaign/accountpicker.html" ) ]
@@ -34,7 +37,9 @@ public class ActiveCampaignContactsWorkflow : WorkflowType
34
37
View = "~/App_Plugins/UmbracoForms.Integrations/Crm/ActiveCampaign/customfield-mapper.html" ) ]
35
38
public string CustomFieldMappings { get ; set ; }
36
39
37
- public ActiveCampaignContactsWorkflow ( IOptions < ActiveCampaignSettings > options , IAccountService accountService , IContactService contactService )
40
+ public ActiveCampaignContactsWorkflow ( IOptions < ActiveCampaignSettings > options ,
41
+ IAccountService accountService , IContactService contactService ,
42
+ ILogger < ActiveCampaignContactsWorkflow > logger )
38
43
{
39
44
Id = new Guid ( Constants . WorkflowId ) ;
40
45
Name = "ActiveCampaign Contacts Workflow" ;
@@ -46,45 +51,63 @@ public ActiveCampaignContactsWorkflow(IOptions<ActiveCampaignSettings> options,
46
51
_accountService = accountService ;
47
52
48
53
_contactService = contactService ;
54
+
55
+ _logger = logger ;
49
56
}
50
57
51
58
public override WorkflowExecutionStatus Execute ( WorkflowExecutionContext context )
52
59
{
53
- var mappings = JsonSerializer . Deserialize < List < ContactMappingDto > > ( ContactMappings ) ;
60
+ try
61
+ {
62
+ var mappings = JsonSerializer . Deserialize < List < ContactMappingDto > > ( ContactMappings ) ;
54
63
55
- var email = context . Record . RecordFields [ Guid . Parse ( mappings . First ( p => p . ContactField == "email" ) . FormField . Id ) ]
56
- . ValuesAsString ( ) ;
64
+ var email = context . Record . RecordFields [ Guid . Parse ( mappings . First ( p => p . ContactField == "email" ) . FormField . Id ) ]
65
+ . ValuesAsString ( ) ;
57
66
58
- // Check if contact exists.
59
- var contacts = _contactService . Get ( email ) . Result ;
67
+ // Check if contact exists.
68
+ var contacts = _contactService . Get ( email ) . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
60
69
61
- var requestDto = new ContactDetailDto { Contact = Build ( context . Record ) } ;
70
+ var requestDto = new ContactDetailDto { Contact = Build ( context . Record ) } ;
62
71
63
- if ( contacts . Contacts . Count > 0 ) requestDto . Contact . Id = contacts . Contacts . First ( ) . Id ;
72
+ if ( contacts . Contacts . Count > 0 ) requestDto . Contact . Id = contacts . Contacts . First ( ) . Id ;
64
73
65
- // Set contact custom fields.
66
- if ( ! string . IsNullOrEmpty ( CustomFieldMappings ) )
67
- {
68
- var customFieldMappings = JsonSerializer . Deserialize < List < CustomFieldMappingDto > > ( CustomFieldMappings ) ;
74
+ // Set contact custom fields.
75
+ if ( ! string . IsNullOrEmpty ( CustomFieldMappings ) )
76
+ {
77
+ var customFieldMappings = JsonSerializer . Deserialize < List < CustomFieldMappingDto > > ( CustomFieldMappings ) ;
78
+
79
+ requestDto . Contact . FieldValues = customFieldMappings . Select ( p => new CustomFieldValueDto
80
+ {
81
+ Field = p . CustomField . Id ,
82
+ Value = context . Record . RecordFields [ Guid . Parse ( p . FormField . Id ) ] . ValuesAsString ( )
83
+ } ) . ToList ( ) ;
84
+ }
69
85
70
- requestDto . Contact . FieldValues = customFieldMappings . Select ( p => new CustomFieldValueDto
86
+ var contactId = _contactService . CreateOrUpdate ( requestDto , contacts . Contacts . Count > 0 )
87
+ . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
88
+
89
+ if ( string . IsNullOrEmpty ( contactId ) )
71
90
{
72
- Field = p . CustomField . Id ,
73
- Value = context . Record . RecordFields [ Guid . Parse ( p . FormField . Id ) ] . ValuesAsString ( )
74
- } ) . ToList ( ) ;
75
- }
91
+ _logger . LogError ( $ "Failed to create/update contact: { email } ") ;
76
92
77
- var contactId = _contactService . CreateOrUpdate ( requestDto , contacts . Contacts . Count > 0 ) . Result ;
93
+ return WorkflowExecutionStatus . Failed ;
94
+ }
78
95
79
- if ( string . IsNullOrEmpty ( contactId ) ) return WorkflowExecutionStatus . Failed ;
96
+ // Associate contact with account if last one is specified.
97
+ if ( ! string . IsNullOrEmpty ( Account ) )
98
+ {
99
+ var associationResponse = _accountService . CreateAssociation ( int . Parse ( Account ) , int . Parse ( contactId ) )
100
+ . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
101
+ }
80
102
81
- // Associate contact with account if last one is specified.
82
- if ( ! string . IsNullOrEmpty ( Account ) )
83
- {
84
- var associationResponse = _accountService . CreateAssociation ( int . Parse ( Account ) , int . Parse ( contactId ) ) . Result ;
103
+ return WorkflowExecutionStatus . Completed ;
85
104
}
105
+ catch ( Exception ex )
106
+ {
107
+ _logger . LogError ( ex , ex . Message ) ;
86
108
87
- return WorkflowExecutionStatus . Completed ;
109
+ return WorkflowExecutionStatus . Failed ;
110
+ }
88
111
}
89
112
90
113
public override List < Exception > ValidateSettings ( )
0 commit comments