-
-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathcollective-report.js
215 lines (190 loc) · 7.05 KB
/
collective-report.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
#!/usr/bin/env node
// Only run on the first of the month
const today = new Date();
if (process.env.NODE_ENV === 'production' && today.getDate() !== 1) {
console.log('NODE_ENV is production and today is not the first of month, script aborted!');
process.exit();
}
process.env.PORT = 3066;
import _ from 'lodash';
import moment from 'moment';
import config from 'config';
import Promise from 'bluebird';
import debugLib from 'debug';
import { getTiersStats } from '../../server/lib/utils';
import models from '../../server/models';
import emailLib from '../../server/lib/email';
const d = new Date;
d.setMonth(d.getMonth() - 1);
const month = moment(d).format('MMMM');
const startDate = new Date(d.getFullYear(), d.getMonth(), 1);
const endDate = new Date(d.getFullYear(), d.getMonth()+1, 1);
console.log("startDate", startDate,"endDate", endDate);
const debug = debugLib('monthlyreport');
const {
Collective,
Notification,
User
} = models;
const processCollectives = (collectives) => {
return Promise.map(collectives, processCollective, { concurrency: 1 });
};
const init = () => {
const startTime = new Date;
const query = {
attributes: [
'id',
'slug',
'name',
'currency',
'tags'
],
where: { type: 'COLLECTIVE'},
include: [ { model: models.Transaction, required: true }]
};
let slugs;
if (process.env.DEBUG && process.env.DEBUG.match(/preview/)) {
slugs = ['vuejs', 'webpack', 'wwcodeaustin','railsgirlsatl','cyclejs','mochajs','chsf','freeridetovote','tipbox'];
}
if (process.env.SLUGS) {
slugs = process.env.SLUGS.split(',');
}
if (slugs) {
query.where.slug = { $in: slugs };
}
Collective.findAll(query)
.tap(collectives => {
console.log(`Preparing the ${month} report for ${collectives.length} collectives`);
})
.then(processCollectives)
.then(() => {
const timeLapsed = Math.round((new Date - startTime)/1000);
console.log(`Total run time: ${timeLapsed}s`);
process.exit(0)
});
}
const topBackersCache = {};
const getTopBackers = (startDate, endDate, tags) => {
tags = tags || [];
const cacheKey = `${startDate.getTime()}${endDate.getTime()}${tags.join(',')}`;
if (topBackersCache[cacheKey]) return Promise.resolve(topBackersCache[cacheKey]);
else {
return Collective.getTopBackers(startDate, endDate, tags, 5)
.then(backers => {
if (!backers) return [];
return Promise.map(backers, backer => processBacker(backer, startDate, endDate, tags))
})
.then(backers => {
backers = _.without(backers, null)
topBackersCache[cacheKey] = backers;
return backers;
});
}
};
const formatCurrency = (amount, currency) => {
return (amount / 100).toLocaleString(currency, {
style: 'currency',
currency,
minimumFractionDigits : 0,
maximumFractionDigits : 2
})
}
const generateDonationsString = (backer, orders) => {
if (!backer.name) {
debug(`Skipping ${backer.username} because it doesn't have a name (${backer.name})`);
return;
}
const donationsTextArray = [], donationsHTMLArray = [];
orders = orders.filter(order => (order.totalAmount > 0));
if (orders.length === 0) {
debug(`Skipping ${backer.name} because there is no donation`);
return;
}
for (let i=0; i<Math.min(3, orders.length); i++) {
const order = orders[i];
donationsHTMLArray.push(`${formatCurrency(order.totalAmount, order.currency)} to <a href="https://opencollective.com/${order.Collective.slug}">${order.Collective.name}</a>`);
donationsTextArray.push(`${formatCurrency(order.totalAmount, order.currency)} to https://opencollective.com/${order.Collective.slug}`);
}
const joinStringArray = (arr) => {
return arr.join(', ').replace(/,([^, ]*)$/,' and $1');
}
return {
html: joinStringArray(donationsHTMLArray),
text: joinStringArray(donationsTextArray)
};
};
const processBacker = (backer, startDate, endDate, tags) => {
return backer.getLatestDonations(startDate, endDate, tags)
.then((donations) => generateDonationsString(backer, donations))
.then(donationsString => {
backer.website = (backer.slug) ? `https://opencollective.com/${backer.slug}` : backer.website || backer.twitterHandle;
if (!donationsString || !backer.website) return null;
backer = _.pick(backer, ['name','slug','image','website']);
backer.donationsString = donationsString;
return backer;
})
};
const processCollective = (collective) => {
const promises = [
getTopBackers(startDate, endDate, collective.tags),
collective.getTiersWithUsers({ attributes: ['id', 'slug', 'name', 'image', 'firstDonation', 'lastDonation', 'totalDonations', 'tier'], until: endDate }),
collective.getBalance(endDate),
collective.getTotalTransactions(startDate, endDate, 'donation'),
collective.getTotalTransactions(startDate, endDate, 'expense'),
collective.getExpenses(null, startDate, endDate),
collective.getRelatedCollectives(3, 0, 'c."createdAt"', 'DESC'),
collective.getBackersStats(startDate, endDate)
];
let emailData = {};
return Promise.all(promises)
.then(results => {
console.log('***', collective.name, '***');
const data = { config: { host: config.host }, month, collective: {} };
data.topBackers = _.filter(results[0], (backer) => (backer.donationsString.text.indexOf(collective.slug) === -1)); // we omit own backers
return getTiersStats(results[1], startDate, endDate)
.then(res => {
data.collective = _.pick(collective, ['id', 'name', 'slug', 'currency','publicUrl']);
data.collective.tiers = res.tiers;
data.collective.stats = results[7];
data.collective.stats.balance = results[2];
data.collective.stats.totalDonations = results[3];
data.collective.stats.totalExpenses = results[4];
data.collective.expenses = results[5];
data.relatedCollectives = results[6];
emailData = data;
return collective;
});
})
.then(getRecipients)
.then(recipients => sendEmail(recipients, emailData))
.catch(e => {
console.error("Error in processing collective", collective.slug, e);
});
};
const getRecipients = (collective) => {
return Notification.findAll({
where: {
CollectiveId: collective.id,
type: 'collective.monthlyreport',
active: true
},
include: [{ model: User }]
}).then(results => {
return results.filter(r => Boolean(r.User)).map(r => r.User.dataValues)
});
}
const sendEmail = (recipients, data) => {
if (recipients.length === 0) return;
return Promise.map(recipients, recipient => {
if (!recipient.email) {
return Promise.resolve();
}
data.recipient = recipient;
if (process.env.ONLY && recipient.email !== process.env.ONLY) {
debug("Skipping ", recipient.email);
return Promise.resolve();
}
return emailLib.send('collective.monthlyreport', recipient.email, data);
});
}
init();