-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbatch.js
57 lines (49 loc) · 1.51 KB
/
batch.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
var itunesconnect = require('./itunesconnect');
var mcapi = require('mailchimp-api');
var _ = require('lodash');
var sync = require('./sync');
function makeConnect() {
return new itunesconnect.Connect(process.env.ITC_USERNAME, process.env.ITC_PASSWORD, {
errorCallback: function(e) {
console.error(e);
},
loginCallback: function() {
console.log('Successfully logged in to iTunes Connect.');
}
});
}
module.exports = {
sync: function(apiKey, listId) {
var mailchimp = new mcapi.Mailchimp(apiKey);
var members = []
function getMembers(page, completed) {
console.log('Getting page %d of subscribers for list %s from MailChimp.', page + 1, listId);
mailchimp.lists.members({
id: listId,
opts: {
start: page,
limit: 100,
}
}, function(body) {
members = members.concat(body.data);
if (members.length < body.total) {
getMembers(page + 1, completed);
} else {
completed();
}
});
}
getMembers(0, function() {
var connect = makeConnect();
_.forEach(members, function(member) {
var email = member.email;
var firstName = member.merges.FNAME;
var lastName = member.merges.LNAME;
var groupNames = _.reduce(member.merges.GROUPINGS, function(result, g) {
return result.concat(_.map(_.filter(g.groups, function(g){ return g.interested; }), function(g){ return g.name; }));
}, []);
sync.addOrUpdate(email, firstName, lastName, groupNames, connect);
});
});
},
};