forked from maksrom/javascript-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimportUnisender.js
executable file
·62 lines (50 loc) · 1.6 KB
/
importUnisender.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
var mongoose = require('lib/mongoose');
var parse = require('csv-parse');
var fs = require('fs');
var co = require('co');
var thenify = require('thenify');
var Subscription = require('newsletter').Subscription;
var Newsletter = require('newsletter').Newsletter;
co(function*() {
var newsletterJs = yield Newsletter.findOne({slug: 'js'}).exec();
var newsletterAdvanced = yield Newsletter.findOne({slug: 'advanced'}).exec();
var input = fs.readFileSync("/tmp/js.csv");
var js = yield thenify(parse)(input, {});
js.shift();
js = js.map(function(record) {
return record[1];
});
var input = fs.readFileSync("/tmp/mk.csv");
var advanced = yield thenify(parse)(input, {});
advanced.shift();
advanced = advanced.map(function(record) {
return record[1];
});
var subscriptions = {};
for (var i = 0; i < js.length; i++) {
var email = js[i];
console.log(email);
var subscription = yield Subscription.findOne({email: email}).exec();
if (!subscription) {
subscription = new Subscription({
email: email
});
}
subscription.newsletters.addToSet(newsletterJs._id);
yield subscription.persist();
}
for (var i = 0; i < advanced.length; i++) {
var email = advanced[i];
console.log(email);
var subscription = yield Subscription.findOne({email: email}).exec();
if (!subscription) {
subscription = new Subscription({
email: email
});
}
subscription.newsletters.addToSet(newsletterAdvanced._id);
yield subscription.persist();
}
}).then(console.log).catch(function(e) {
console.log(e.message, e.stack , e);
});