This repository was archived by the owner on Nov 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzapier.js
More file actions
245 lines (226 loc) · 8.6 KB
/
Copy pathzapier.js
File metadata and controls
245 lines (226 loc) · 8.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
var Zap = {
apsis_send_transactional_email_copy_pre_write: function(bundle) {
// Working
var ActionFields = bundle.action_fields;
var Data = {};
var DemDataFields = [];
for(var prop in ActionFields){
if(ActionFields.hasOwnProperty(prop)){
if(prop === "Key"){
var Keys = ActionFields[prop];
var index = 0;
for (var pr in Keys) {
if(DemDataFields.length-1 < index){
throw new ErrorException('The number of Demographic Data fields can not be greater than the number of Values.');
}
DemDataFields[pr].Key = Keys[pr];
index++;
}
}
else if(prop === "Value"){
var Values = ActionFields[prop];
for (var p in Values) {
var NewObj = {Value: Values[p], Key: ""};
DemDataFields.push(NewObj);
}
}
else{
Data[prop] = ActionFields[prop];
}
}
}
if(DemDataFields.length > 0){
for(var i = 0; i < DemDataFields.length; i++){
if(DemDataFields[i].Key === ""){
throw new ErrorException('The number of Values can not be greater than the number of Demographic Data fields.');
}
}
Data.DemDataFields = DemDataFields;
}
bundle.request.data = JSON.stringify(Data);
return bundle.request;
},
remove_subscriber_from_optoutall_post_write: function(bundle) {
// Working
var Response = JSON.parse(bundle.response.content);
if(Response.Message === 'Subscriber e-mail address does not exist on the Opt-out all list')
{
bundle.response.status_code = 200;
}
console.log(Response.Message);
return bundle.response;
},
remove_subscriber_from_optoutall_pre_write: function(bundle) {
// Working
bundle.request.method = 'DELETE';
var Data = bundle.action_fields_full.email;
bundle.request.data = JSON.stringify(Data);
return bundle.request;
},
apsis_new_mailinglist_subscriber_post_poll: function(bundle) {
// Working
var Data = JSON.parse(bundle.response.content);
if(Data.Code != 1) {
throw new ErrorException('Unable to retrieve the subscribers on the mailinglist');
}
else {
var Pages = parseInt(Data.Result.TotalPages, 10);
console.log(typeof(Pages));
if(Pages > 1){
var Request = {
'method' : 'GET',
'url' : 'https://se.api.anpdm.com:8443/v1/mailinglists/'+bundle.trigger_fields.MailinlistID+'/subscribers/'+Pages+'/100',
'params' : {},
'headers' : {
'accept' :'application/json',
'content-type' : 'application/json; charset=utf-8'
},
'auth' : bundle.request.auth,
'data' : null
};
var Response = JSON.parse(z.request(Request));
Data = Response.content;
}
bundle.response.content = Subscribers;
return bundle.response;
}
},
apsis_new_mailinglist_subscriber_pre_poll: function(bundle) {
// Working
return bundle.request;
},
apsis_update_event_attendee_pre_write: function(bundle) {
// Working
bundle.request.method = 'PUT';
var Data = bundle.action_fields_full.Status ;
bundle.request.data = JSON.stringify(Data);
return bundle.request;
},
apsis_find_attendee_pre_search: function(bundle) {
// Working
var Data = { EventId : bundle.search_fields.eventId };
bundle.request.method = 'POST';
bundle.request.data = JSON.stringify(Data);
return bundle.request;
},
apsis_get_event_attendee_pre_poll: function(bundle) {
// Working
var Data = {
EventId : bundle.trigger_fields.EventId,
AttendeeStatus : bundle.trigger_fields.Status
};
bundle.request.method = 'POST';
bundle.request.data = JSON.stringify(Data);
return bundle.request;
},
apsis_get_active_events_post_poll: function(bundle) {
// Working
var Response = JSON.parse(bundle.response.content);
var Result = Response.Result;
if(Result.length > 1){
throw new ErrorException('There are no active Events available on your APSIS account.');
}
var Events = [];
for(var e in Result){
var NewEvent = {Id : Result[e].Id, Name: Result[e].Name};
Events.push(NewEvent);
}
bundle.response.content = Events;
return bundle.response;
},
apsis_get_active_events_pre_poll: function(bundle) {
// Working
var Data = {ExcludeDisabled: "true"};
bundle.request.method = 'POST';
bundle.request.data = JSON.stringify(Data);
return bundle.request;
},
apsis_create_subscriber_pre_write: function(bundle) {
// Working
var ActionFields = bundle.action_fields;
var Data = {};
var DemDataFields = [];
for(var prop in ActionFields){
if(ActionFields.hasOwnProperty(prop)){
if(prop === "Key"){
var Keys = ActionFields[prop];
var index = 0;
for (var pr in Keys) {
if(DemDataFields.length-1 < index){
throw new ErrorException('The number of Demographic Data fields can not be greater than the number of Values.');
}
DemDataFields[pr].Key = Keys[pr];
index++;
}
}
else if(prop === "Value"){
var Values = ActionFields[prop];
for (var p in Values) {
var NewObj = {Value: Values[p], Key: ""};
DemDataFields.push(NewObj);
}
}
else{
Data[prop] = ActionFields[prop];
}
}
}
if(DemDataFields.length > 0){
for(var i = 0; i < DemDataFields.length; i++){
if(DemDataFields[i].Key === ""){
throw new ErrorException('The number of Values can not be greater than the number of Demographic Data fields.');
}
}
Data.DemDataFields = DemDataFields;
}
bundle.request.data = JSON.stringify(Data);
return bundle.request;
},
apsis_get_demographic_data_post_poll: function(bundle) {
// Working
var ResponseObj = JSON.parse(bundle.response.content);
var ResultObj = ResponseObj.Result.Demographics;
var NewObj = [];
ResultObj.forEach(function(result, index){
if(result.Key !== ""){
NewObj.push(result);}
});
bundle.response.content = NewObj;
return bundle.response;
},
apsis_get_subscriber_id_pre_write: function(bundle) {
// Working
var EmailData = bundle.action_fields_full.Email;
bundle.request.data = "\""+EmailData+"\"";
console.log(bundle.request.data);
return bundle.request;
},
apsis_unsubscribe_pre_write: function(bundle) {
// Working
var JsonObj = {};
JsonObj.MailinglistId = 0;
JsonObj.Reason = "Moved to OptOutAll via Zapier";
JsonObj.SendQueueId = 0;
JsonObj.SubscriberId = bundle.action_fields_full.SubscriberId;
var JsonArray = [];
JsonArray.push(JsonObj);
var JsonData = JSON.stringify(JsonArray);
bundle.request.data = JsonData;
return bundle.request;
},
apsis_incoming_sms_post_poll: function(bundle) {
// Working
var smskeyword = bundle.trigger_fields.Keyword;
var results = JSON.parse(bundle.response.content, function(k,v){
if(k === 'SmsResponse'){
return v.replace(smskeyword + " ",'');
}
return v;
});
return results;
},
apsis_get_mailinglist_pre_poll: function(bundle) {
bundle.request.method = 'POST';
return bundle.request;
}
};