-
-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathuser-report.js
executable file
·435 lines (396 loc) · 13.8 KB
/
user-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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
import '../../server/env';
import fs from 'fs';
import path from 'path';
import config from 'config';
import debugLib from 'debug';
import { groupBy, isEmpty, pick, uniq } from 'lodash';
import moment from 'moment';
import ORDER_STATUS from '../../server/constants/order-status';
import roles from '../../server/constants/roles';
import emailLib from '../../server/lib/email';
import { getConsolidatedInvoicePdfs } from '../../server/lib/pdf';
import { reportErrorToSentry } from '../../server/lib/sentry';
import { formatCurrencyObject, parseToBoolean } from '../../server/lib/utils';
import models, { Op } from '../../server/models';
import { runCronJob } from '../utils';
if (parseToBoolean(process.env.SKIP_USER_REPORT) && !process.env.OFFCYCLE) {
console.log('Skipping because SKIP_USER_REPORT is set.');
process.exit();
}
// Only run on the first of the month
const today = new Date();
if (config.env === 'production' && today.getDate() !== 1 && !process.env.OFFCYCLE) {
console.log('OC_ENV is production and today is not the first of month, script aborted!');
process.exit();
}
process.env.PORT = 3066;
const d = process.env.START_DATE ? new Date(process.env.START_DATE) : 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');
/**
* Returns the list of Users that are subscribed to the user.monthlyreport
* for the current backer collective (User/Org/Collective)
* @param {*} backerCollective
*/
const fetchUserSubscribers = async (notificationType, backerCollective) => {
const unsubscriptions = await models.Notification.findAll({
attributes: ['UserId'],
where: {
CollectiveId: backerCollective.id,
type: notificationType,
active: false,
},
});
const unsubscribedUserIds = unsubscriptions.map(n => n.UserId);
console.log(
`${unsubscribedUserIds.length} users have unsubscribed from the ${notificationType} report for ${backerCollective.type} ${backerCollective.slug}`,
);
const admins = await backerCollective.getAdminUsers();
const subscribers = admins.filter(a => a && unsubscribedUserIds.indexOf(a.id) === -1);
return subscribers;
};
const init = async () => {
const startTime = new Date();
const query = {
attributes: ['FromCollectiveId'],
where: {
type: 'CREDIT',
OrderId: { [Op.ne]: null }, // make sure we don't consider collectives paying out expenses as backers of user collectives
RefundTransactionId: null, // make sure we don't consider refunds
createdAt: { [Op.gte]: startDate, [Op.lt]: endDate },
},
include: [
{
model: models.Collective,
as: 'collective',
where: { type: 'COLLECTIVE' },
},
],
};
let FromCollectiveIds;
if (process.env.SLUGS) {
const slugs = process.env.SLUGS.split(',');
const res = await models.Collective.findAll({
attributes: ['id'],
where: { slug: { [Op.in]: slugs } },
});
FromCollectiveIds = res.map(r => r.id);
} else if (process.env.DEBUG && process.env.DEBUG.match(/preview/)) {
FromCollectiveIds = [21272, 20568, 1729, 12671]; // fcb-event-user, fcb-event-anonymous, xdamman, coinbase
} else {
const transactions = await models.Transaction.findAll(query);
FromCollectiveIds = uniq(transactions.map(t => t.FromCollectiveId));
}
console.log(`Preparing the ${month} report for ${FromCollectiveIds.length} backers`);
for (const FromCollectiveId of FromCollectiveIds) {
await processBacker(FromCollectiveId);
}
const timeLapsed = Math.round((new Date() - startTime) / 1000);
console.log(`Total run time: ${timeLapsed}s`);
};
const processBacker = async FromCollectiveId => {
const backerCollective = await models.Collective.findByPk(FromCollectiveId);
console.log('>>> Processing backer', backerCollective.slug);
const query = {
attributes: ['CollectiveId', 'HostCollectiveId'],
where: {
FromCollectiveId,
type: 'CREDIT',
createdAt: { [Op.gte]: startDate, [Op.lt]: endDate },
},
include: [
{
model: models.Collective,
as: 'collective',
where: { type: 'COLLECTIVE' },
},
],
};
const transactions = await models.Transaction.findAll(query);
console.log('>>> transactions found', transactions.length);
const distinctTransactions = [],
collectiveIds = {};
for (const t of transactions) {
if (!collectiveIds[t.CollectiveId]) {
collectiveIds[t.CollectiveId] = true;
distinctTransactions.push(t);
}
}
if (distinctTransactions.length === 0) {
console.log('>>> no transaction for', backerCollective.slug);
return;
}
console.log(`>>> Collective ${FromCollectiveId} has backed ${distinctTransactions.length} collectives`);
const collectives = await Promise.all(
distinctTransactions.map(transaction => processCollective(transaction.CollectiveId)),
);
const subscribers = await fetchUserSubscribers('user.monthlyreport', backerCollective);
console.log(`>>> Collective ${FromCollectiveId} has ${subscribers.length} subscribers`);
if (subscribers.length === 0) {
console.log('>>> no subscriber');
return;
}
const orders = await models.Order.findAll({
attributes: ['id', 'CollectiveId', 'totalAmount', 'currency'],
where: {
FromCollectiveId,
[Op.or]: {
createdAt: { [Op.gte]: startDate, [Op.lt]: endDate },
SubscriptionId: { [Op.ne]: null },
},
status: {
[Op.in]: [ORDER_STATUS.PAID, ORDER_STATUS.ACTIVE],
},
deletedAt: null,
},
include: [{ model: models.Subscription }],
});
// group orders(by collective) that either don't have subscription or have active subscription
const ordersByCollectiveId = groupBy(
orders.filter(o => !o.Subscription || o.Subscription.isActive),
'CollectiveId',
);
const collectivesWithOrders = [];
collectives.map(collective => {
if (ordersByCollectiveId[collective.id]) {
collectivesWithOrders.push({
...collective,
orders: ordersByCollectiveId[collective.id].map(order => order.info),
order: computeOrderSummary(ordersByCollectiveId[collective.id]),
});
}
});
const stats = await computeStats(collectivesWithOrders);
// monthlyConsolidatedInvoices is array of attachments
const monthlyConsolidatedInvoices = await getConsolidatedInvoicePdfs(backerCollective);
try {
for (const user of subscribers) {
const data = {
config: { host: config.host },
month,
fromCollective: backerCollective.info,
collectives: collectivesWithOrders,
manageSubscriptionsUrl: `${config.host.website}/recurring-contributions`,
stats,
tags: stats.allTags || {},
consolidatedPdfs: isEmpty(monthlyConsolidatedInvoices) ? null : true,
};
if (data.tags['open source']) {
data.tags.opensource = true;
}
data[backerCollective.type] = true;
const options = {
attachments: monthlyConsolidatedInvoices,
};
return sendEmail(user, data, options);
}
} catch (e) {
console.error(e);
reportErrorToSentry(e);
}
};
const now = new Date();
const processEvents = events => {
const res = {
upcoming: [],
past: [],
};
events.forEach(event => {
event.stats = { confirmed: 0, interested: 0 };
event.members.forEach(member => {
if (member.role === roles.FOLLOWER) {
event.stats.interested++;
}
});
event.orders.forEach(order => {
if (order.processedAt !== null) {
event.stats.confirmed++;
}
});
if (new Date(event.startsAt) > now) {
res.upcoming.push(event.info);
} else {
res.past.push(event.info);
}
});
return res;
};
/**
* Processes the stats of a given collective and keeps the result in memory cache
* Returns collective data object with
* {
* ...{'id', 'name', 'slug', 'website', 'image', 'description', 'currency','publicUrl', 'tags', 'backgroundImage', 'settings', 'totalDonations', 'contributorsCount'},
* stats: { balance, totalDonations, totalPaidExpenses, updates },
* contributorsCounts,
* yearlyIncome,
* expenses
* events
* updates
* nextGoal
* }
*/
const collectivesData = {};
const processCollective = async CollectiveId => {
if (collectivesData[CollectiveId]) {
return collectivesData[CollectiveId];
}
const collective = await models.Collective.findByPk(CollectiveId);
const promises = [
collective.getBackersStats(startDate, endDate),
collective.getBalance({ endDate }),
collective.getTotalTransactions(startDate, endDate, 'donation'),
collective.getTotalTransactions(startDate, endDate, 'expense'),
collective.getExpenses(null, startDate, endDate),
collective.getYearlyBudget(),
models.Expense.findAll({
where: {
CollectiveId: collective.id,
createdAt: { [Op.gte]: startDate, [Op.lt]: endDate },
},
limit: 3,
order: [['id', 'DESC']],
include: [models.User],
}),
collective.getEvents({
where: { startsAt: { [Op.gte]: startDate } },
order: [['startsAt', 'DESC']],
include: [
{ model: models.Member, as: 'members' },
{ model: models.Order, as: 'orders' },
],
}),
models.Update.findAll({
where: {
CollectiveId: collective.id,
publishedAt: { [Op.gte]: startDate, [Op.lt]: endDate },
},
order: [['createdAt', 'DESC']],
}),
collective.getNextGoal(endDate),
];
const results = await Promise.all(promises);
console.log('***', collective.name, '***');
const data = {};
data.collective = pick(collective, [
'id',
'name',
'slug',
'website',
'image',
'currency',
'publicUrl',
'tags',
'backgroundImage',
'settings',
'totalDonations',
'contributorsCount',
'description',
]);
data.collective.stats = results[0];
data.collective.stats.balance = results[1];
data.collective.stats.totalDonations = results[2];
data.collective.stats.totalPaidExpenses = -results[3];
data.collective.contributorsCount =
collective.data && collective.data.githubContributors
? Object.keys(collective.data.githubContributors).length
: data.collective.stats.backers.lastMonth;
data.collective.yearlyIncome = results[5];
data.collective.expenses = results[6].map(expense => expense.info);
data.collective.events = processEvents(results[7]);
data.collective.updates = results[8].map(update => update.info);
data.collective.stats.updates = results[8].length;
const nextGoal = results[9];
if (nextGoal) {
nextGoal.tweet = `🚀 ${collective.twitterHandle ? `@${collective.twitterHandle}` : collective.name} is at ${
nextGoal.percentage
} of their next goal: ${nextGoal.title}.\nJoin me in helping them get there! 🙌\nhttps://opencollective.com/${
collective.slug
}`;
data.collective.nextGoal = nextGoal;
}
console.log(data.collective.stats);
collectivesData[CollectiveId] = data.collective;
return data.collective;
};
const computeOrderSummary = orders => {
const orderSummary = {
totalAmount: '',
totalAmountPerCurrency: {},
Subscription: null,
};
if (orders && orders.length > 0) {
for (const order of orders) {
orderSummary.totalAmountPerCurrency[order.currency] = orderSummary.totalAmountPerCurrency[order.currency] || 0;
orderSummary.totalAmountPerCurrency[order.currency] += order.totalAmount;
if (order.Subscription && order.Subscription.isActive) {
orderSummary.Subscription = order.Subscription.dataValues;
}
}
}
orderSummary.totalAmount = formatCurrencyObject(orderSummary.totalAmountPerCurrency);
return orderSummary;
};
const computeStats = async collectives => {
const tagsIndex = {};
const stats = {
collectives: collectives.length,
expenses: 0,
totalSpentPerCurrency: {},
totalDonatedPerCurrency: {},
};
collectives.forEach(collective => {
const expenses = collective.expenses;
if (collective.tags) {
collective.tags.map(t => {
tagsIndex[t] = tagsIndex[t] || 0;
tagsIndex[t]++;
});
}
if (collective.orders && collective.orders.length > 0) {
for (const order of collective.orders) {
stats.totalDonatedPerCurrency[order.currency] = stats.totalDonatedPerCurrency[order.currency] || 0;
stats.totalDonatedPerCurrency[order.currency] += order.totalAmount;
}
}
if (expenses && expenses.length > 0) {
stats.expenses += expenses.length;
expenses.forEach(expense => {
stats.totalSpentPerCurrency[expense.currency] = stats.totalSpentPerCurrency[expense.currency] || 0;
stats.totalSpentPerCurrency[expense.currency] += expense.amount;
});
}
});
stats.allTags = tagsIndex;
stats.totalSpentString = formatCurrencyObject(stats.totalSpentPerCurrency);
stats.totalDonatedString = formatCurrencyObject(stats.totalDonatedPerCurrency);
console.log(`>>> Stats: ${JSON.stringify(stats, null, 2)}`);
return stats;
};
const sendEmail = (recipient, data, options = {}) => {
if (recipient.length === 0) {
return;
}
data.recipient = recipient;
if (process.env.ONLY && recipient.email !== process.env.ONLY) {
debug('Skipping ', recipient.email);
return Promise.resolve();
}
if (process.env.SEND_EMAIL_TO) {
recipient.email = process.env.SEND_EMAIL_TO;
}
if (process.env.DEBUG && process.env.DEBUG.match(/preview/) && options.attachments) {
options.attachments.map(attachment => {
const filepath = path.resolve(`/tmp/${attachment.filename}`);
fs.writeFileSync(filepath, attachment.content);
console.log('>>> preview attachment', filepath);
});
}
return emailLib.send('user.monthlyreport', recipient.email, data, options);
};
if (require.main === module) {
runCronJob('user-report', init, 23 * 60 * 60);
}