-
Notifications
You must be signed in to change notification settings - Fork 1
/
operations.js
405 lines (362 loc) · 13.5 KB
/
operations.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
var request = require('request');
var ObjectId = require('mongodb').ObjectID;
// These are variables that are set when the `uploadToMailChimp` operation is
// called.
var apiKey, apiRoot3, apiRoot2;
// Constants used below:
var pollInterval = 2000;
var maxMsToFollowBatchSubscribeStatus = 5 * 60 * 1000; // 5 minutes
/**
* pad
*
* @name pad
* @function
* @param {String} x A string with the length 1 or 2.
* @return {String} `x` if it has the length 2, or `'0' + x` otherwise.
*/
function pad(x) {
return x.length === 2 ? x : ('0' + x);
}
/**
* getYYYYMMDDDateString
* Returns the current date as a string in the format YYYY-MM-DD.
*
* @name getYYYYMMDDDateString
* @function
* @return {String} The current date in the format YYYY-MM-DD.
*/
function getYYYYMMDDDateString() {
var d = new Date();
var yyyy = d.getFullYear().toString();
var mm = (d.getMonth() + 1).toString();
var dd = d.getDate().toString();
return yyyy + '.' + pad(mm) + '.' + pad(dd);
}
/**
* createList
* Creates a new MailChimp list with the given name. The given name must be a
* non-empty string, otherwise an error is given to the callback instead of
* creating the list.
*
* @name createList
* @function
* @param {String} listName The name of the new MailChimp list.
* @param {Function} callback The function to call after creating the list. The
* first argument is an error or null, the second argument is the list ID of the
* new list.
* @return {undefined}
*/
function createList(listName, callback) {
if (typeof listName !== 'string') {
return callback('The list name must be a string.');
}
listName = listName.trim();
if (listName.length === 0) {
return callback('The list name must not be empty.');
}
var options = {
url: apiRoot3 + '/lists',
method: 'POST',
json: {
name: listName,
contact: {
company: 'Brigels Resort AG',
address1: 'Via Plaun Rueun 44',
city: 'Brigels',
state: 'Graubünden',
zip: '7165',
country: 'CH',
phone: '+41 81 920 14 00'
},
permission_reminder: 'You signed up for updates on our website.',
campaign_defaults: {
from_name: 'Pradas Resort',
from_email: 'info@pradasresort.ch',
subject: '',
language: 'DE'
},
email_type_option: false
},
auth: {
user: 'any_string',
pass: apiKey
}
};
request(options, function (error, response, body) {
if (error || response.statusCode !== 200) {
return callback('Failed to create MailChimp list: ' + (error ||
JSON.stringify(body) || 'Unknown error'));
}
var listId = body.id;
// the FNAME and LNAME are already created for a new list
addMergeField(listId, 'GENDER', 'Gender', 'text', true, function (err) {
if (err) { return callback(err); }
callback(null, listId);
});
});
}
/**
* addMergeField
* Creates a new merge field for an existing MailChimp list.
*
* @name addMergeField
* @function
* @param {String} listId The ID of the list to add the merge field.
* @param {String} tag The tag of the merge field.
* @param {String} name The name of the merge field.
* @param {String} type The type of the merge field.
* @param {String} isPublic Is this merge field visible in the sunbscribe form?
* @param {String} listId The ID of the list to add the merge field.
* @param {Function} callback The function to call after creating the list merge field.
* @return {undefined}
*/
function addMergeField(listId, tag, name, type, isPublic, callback) {
var options = {
url: apiRoot3 + '/lists/' + listId + '/merge-fields',
method: 'POST',
json: {
tag: tag,
name: name,
type: type,
public: isPublic
},
auth: {
user: 'any_string',
pass: apiKey
}
};
request(options, function (error, response, body) {
if (error || response.statusCode !== 200) {
return callback('Failed to add merge field "' + name + '" to the MailChimp list "' + listId + '": ' + (error || JSON.stringify(body) || 'Unknown error'));
}
callback(null);
});
}
/**
* addWebhook
* Creates a new MailChimp list with the given name. The given name must be a
* non-empty string, otherwise an error is given to the callback instead of
* creating the list.
*
* @name addWebhook
* @function
* @param {String} listId The ID of the list to add the webhook.
* @param {Function} callback The function to call after creating the list webhook.
* @return {undefined}
*/
function addWebhook(listId, callback) {
if (typeof listId !== 'string') {
return callback('The list ID must be a string.');
}
var options = {
url: apiRoot2 + '/lists/webhook-add',
method: 'POST',
json: {
apikey: apiKey,
id: listId,
url: 'http://pradascrm.jillix.net/@/api/mailchimp/processWebhookRequest',
actions: {
unsubscribe: true,
subscribe: false,
profile: false,
cleaned: false,
upemail: false,
campaign: false
}
}
};
request(options, function (error, response, body) {
if (error || response.statusCode !== 200) {
return callback('Failed to create webhook for MailChimp list "' + listId + '": ' + (error ||
JSON.stringify(body) || 'Unknown error'));
}
var webhookId = body.id;
callback(null, webhookId);
});
}
/**
* batchListSubscribe
* Subscribes one or more email addresses to a MailChimp list identified by its
* ID.
*
* @name batchListSubscribe
* @function
* @param {String} listId The ID of the MailChimp list.
* @param {Array} emails An array of strings representing the email addresses.
* @param {Function} callback The function to call after doing the batch
* subscribe request. The first and only argument is an error or null.
* @param {Function} finishedCallback The function to call after the batch
* subscribe operation finishes. The first and only argument is an error or
* null. The progress of the batch operation is followed (through polling the
* server each 2 seconds which are the current value of the `pollInterval`
* configuration variable at the beginning of this file, for maximum 5 minutes
* which are the current value of the `maxMsToFollowBatchSubscribeStatus`
* configuration variable at the beginning of this file) only if this callback
* is given.
* @return {undefined}
*/
function batchListSubscribe(listId, subscribers, callback, finishedCallback) {
var ops = [];
for (var i = 0; i < subscribers.length; i++) {
subscribers[i].name = subscribers[i].name || {};
ops.push({
method: 'POST',
path: '/lists/' + listId + '/members',
operation_id: new Date().toString() + ' ' + (i + 1),
body: JSON.stringify({
status: 'subscribed',
email_address: subscribers[i].email,
merge_fields: {
FNAME: subscribers[i].name.first,
LNAME: subscribers[i].name.last,
GENDER: subscribers[i].gender
}
})
});
}
var options = {
url: apiRoot3 + '/batches',
method: 'POST',
json: {
operations: ops
},
auth: {
user: 'any_string',
pass: apiKey
}
};
request(options, function (error, response, body) {
var msPassed = 0;
// This function polls the MailChimp server to find whether the batch
// operation is finished or not. When it is finished, the callback is
// called.
function checkResponseBody(responseBody) {
if (responseBody.status !== 'finished') {
if (msPassed > maxMsToFollowBatchSubscribeStatus) {
return finishedCallback('Timeout');
}
setTimeout(function () {
msPassed += pollInterval;
request({
url: apiRoot3 + '/batches/' + body.id,
method: 'GET',
auth: {
user: 'any_string',
pass: apiKey
}
}, function (err, resp, b) {
if (err || resp.statusCode !== 200) {
return finishedCallback('Batch subscribe operation failed: ' +
(err || JSON.stringify(b) || 'Unknown error'));
}
b = JSON.parse(b);
checkResponseBody(b);
});
}, pollInterval);
} else {
if (responseBody.errored_operations > 0) {
return finishedCallback('Errors for ' +
responseBody.errored_operations +
' email addresses');
}
finishedCallback(null);
}
}
if (error || response.statusCode !== 200) {
return callback('Batch subscribe operation request failed: ' +
(error || JSON.stringify(body) || 'Unknown error'));
}
callback(null);
if (typeof finishedCallback === 'function') {
checkResponseBody(body);
}
});
}
/**
* addEmailsToNewList
* Subscribes one or more email addresses to a new MailChimp list. This is a
* composition of the `createList` and `batchListSubscribe` functions.
*
* From my tests in 13rd of January 2016, 4-5 minutes must pass until the new
* subscribers are seen in the newly created list in the MailChimp user
* interface after the batch operation is finished.
* - For 1 email: 10s until the list creation and the batch subscribe operations
* are done. After that another 4m55s until the MailChimp UI shows the new
* emails in the list.
* - For 5 emails: 10s and 4m38s.
* - For 50 emails: 14s and 4m43s.
* - For 1000 emails: 48s and 4m16s.
*
* @name addEmailsToNewList
* @function
* @param {String} listName The name of the new MailChimp list.
* @param {Array} emails An array of strings representing the email addresses.
* @param {Function} callback The function to call after doing the batch
* subscribe request. The first and only argument is an error or null.
* @param {Function} finishedCallback The function to call after the batch
* subscribe operation finishes. The first and only argument is an error or
* null. The progress of the batch operation is followed (through polling the
* server each 2 seconds which are the current value of the `pollInterval`
* configuration variable at the beginning of this file, for maximum 5 minutes
* which are the current value of the `maxMsToFollowBatchSubscribeStatus`
* configuration variable at the beginning of this file) only if this callback
* is given.
* @return {undefined}
*/
function addEmailsToNewList(listName, emails, callback, finishedCallback) {
if (emails.length === 0) {
return callback('No emails given');
}
createList(listName, function (err, listId) {
if (err) { return callback(err); }
addWebhook(listId, function (err, webhookId) {
if (err) { return callback(err); }
batchListSubscribe(listId, emails, callback, finishedCallback);
});
});
}
/**
* uploadToMailChimp
* The actual operation that uploads to MailChimp the email addresses from the
* `pradas_clients` template filtered by a query to a new MailChimp list. The
* `link.data` object should have the properties `query` (an object with which
* to filter the emails from the database) and `apiKey` (a string with the
* MailChimp API key with which the list creation and the batch subscribe
* operation should be done).
*
* @name uploadToMailChimp
* @function
* @param {Object} link The link object representing the current request.
* @return {undefined}
*/
function uploadToMailChimp(link) {
apiKey = link.params.apiKey;
var dc = apiKey.split('-')[1];
apiRoot3 = 'https://' + dc + '.api.mailchimp.com/3.0';
apiRoot2 = 'https://' + dc + '.api.mailchimp.com/2.0';
// Build the request
var customRequest = {
role: link.session.crudRole,
templateId: ObjectId('5651cc754d9707687fbc7591'), // pradas_clients
query: link.data.query,
options: {},
noCursor: true
};
// Emit the request to read the emails of the users inside the
// `pradas_clients` template
M.emit('crud.read', customRequest, function (err, data) {
if (err) { link.send(500, err); return; }
// If the second callback is given, the progress of the batch subscribe
// operation is followed for as long as 5 minutes
addEmailsToNewList(link.data.listName, data, function (err) {
if (err) { return link.send(500, err); }
link.send(200);
}, function (err) {
// TODO: send an email with Mandrill to the user doing the upload to
// MailChimp regarding the status of the batch operation when it is
// done or in maximum 5 minutes, telling the user the details of the
// batch results.
});
});
}
exports.uploadToMailChimp = uploadToMailChimp;