-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmailchimp.js
241 lines (228 loc) · 7.62 KB
/
mailchimp.js
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
var Client = require('node-rest-client').Client;
var client = new Client();
var emitter = require('../core-integration-server-v2/javascripts/emitter');
var apiKey, listId, baseUrl;
var errMsg = '"Connection timeout error" in Mailchimp';
function run(node) {
try {
var nodeType = node.connector.type.toLowerCase();
if(nodeType == 'trigger') {
getStoreData(node);
} else {
postStoreData(node);
}
} catch(e) {
emitter.emit('error', e.message, e.stack, "", node);
}
}
function post(response, node, message) {
node.resData = response;
emitter.emit('success', node, message);
}
function postStoreData(node) {
try {
var method = node.optionType.toLowerCase();
var url = baseUrl + "/3.0/lists/" + listId + "/members";
if(method == 'add') {
createSubscriber(url, node);
} else {
updateSubscriber(url, node);
}
} catch(e) {
emitter.emit('error', e.message, e.stack, "", node);
}
}
function createSubscriber(url, node) {
try {
var reqObj = node.reqData;
var firstName, lastName = '';
if(reqObj.hasOwnProperty("shippingAddress")) {
firstName = reqObj.billingAddress.name
} else {
firstName = reqObj.firstName,
lastName = reqObj.lastName
}
var postData = {
email_address : reqObj.email,
status : "subscribed",
merge_fields : {
FNAME : firstName,
LNAME : lastName
}
};
var args = {
data : postData,
headers : {
"Authorization" : "apikey " + apiKey,
"Accept" : "application/json",
"Content-Type" : "application/json"
}
};
client.post(url, args, function (obj, res) {
try {
var data;
var status = res.statusCode/100;
if (status >= 3) {
data = JSON.parse(obj);
if (data.hasOwnProperty("errors")) {
errMsg = data.errors[0].message;
} else {
var detail = data.detail;
errMsg = detail;
if(detail.includes("already a list member")) {
var index = detail.indexOf('U');
errMsg = detail.substring(0, index);
}
}
emitter.emit('error',errMsg, postData, url, node);
} else {
data = obj;
var msg = "Subscriber created successfully for the email " + reqObj.email;
post(data, node, msg);
}
} catch(e) {
emitter.emit('error', e.message, e.stack, url, node);
}
}).on('error',function(err){
emitter.emit('error', err, postData, url, node);
});
} catch(e) {
emitter.emit('error', e.message, e.stack, "", node);
}
}
function updateSubscriber(url, node) {
try {
var reqObj = node.reqData;
var postData = {
email_address : reqObj.email,
status : "unsubscribed"
};
var args = {
data : postData,
headers : {
"Authorization" : "apikey " + apiKey,
"Accept" : "application/json",
"Content-Type" : "application/json"
}
};
getMemberId(node, function(id) {
url += "/" + id;
client.put(url, args, function(data, res) {
try {
var status = parseInt(res.statusCode/100);
if(status == 2) {
var msg = "Subscriber updated successfully for the email " + reqObj.email;
post(data, node, msg);
}
} catch(e) {
emitter.emit('error', e.message, e.stack, url, node);
}
}).on('error', function(err) {
emitter.emit('error', errMsg, args.data, url, node);
});
});
} catch(e) {
emitter.emit('error', e.message, e.stack, "", node);
}
}
function getMemberId(node, callback) {
try {
var reqObj = node.reqData;
var url = baseUrl + "/3.0/search-members?query=" + reqObj.email;
var args = {
headers : {
"Authorization" : "apikey " + apiKey,
"Accept" : "application/json",
"Content-Type" : "application/json"
}
}
client.get(url, args, function(data, res) {
try {
var status = parseInt(res.statusCode/100);
if(status == 2) {
var members = data.exact_matches.members;
if(members.length == 0) {
createSubscriber(node);
} else {
var id = data.exact_matches.members[0].id;
callback(id);
}
}
} catch(e) {
emitter.emit('error', e.message, e.stack, "", node);
}
});
} catch(e) {
emitter.emit('error', e.message, e.stack, "", node);
}
}
function testApp(callback) {
try {
var url = baseUrl + "/3.0/lists/" + listId + "/members";
var args = {
headers : {
"Authorization" : "apikey " + apiKey,
"Accept" : "application/json",
}
};
var result;
client.get(url, args, function (data, res) {
try {
if(res.statusCode/100 == 2) {
result = {
status :'success',
response: data
};
} else {
var errMsg;
var result = JSON.parse(data);
if(result.hasOwnProperty("errors")) {
errMsg = result.errors[0].message;
} else {
var detail = result.detail;
errMsg = detail;
}
result = {
status :'error',
response : errMsg
};
}
callback(result);
return result;
} catch(e) {
callback({status:"error", response:e.stack});
}
}).on('error',function(err) {
callback({status:"error", response:err});
});
} catch(e) {
callback({status:"error", response:e.stack});
}
}
function test(request, callback) {
try {
var credentials = request.credentials;
apiKey = credentials.apiKey;
listId = credentials.listId;
baseUrl = credentials.url;
testApp(callback);
} catch(e) {
callback({status:"error", response:e.stack});
}
}
function init(node) {
try {
var credentials = node.credentials;
apiKey = credentials.apiKey;
listId = credentials.listId;
baseUrl = credentials.url;
run(node);
} catch(e) {
emitter.emit('error', e.message, e.stack, "", node);
}
}
var Mailchimp = {
init : init,
test : test
};
module.exports = Mailchimp;