Skip to content

Commit

Permalink
fix(cron/user-report): filter orders to get the ones with subscriptio…
Browse files Browse the repository at this point in the history
…n active
  • Loading branch information
marcogbarcellos committed Dec 3, 2018
1 parent e82a2fd commit a10577b
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions cron/monthly/user-report.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if (process.env.NODE_ENV === 'production' && today.getDate() !== 1) {

process.env.PORT = 3066;

import { get, pick, uniq } from 'lodash';
import { get, pick, uniq, groupBy } from 'lodash';
import moment from 'moment';
import config from 'config';
import Promise from 'bluebird';
Expand Down Expand Up @@ -125,7 +125,6 @@ const processBacker = async FromCollectiveId => {
],
};
const transactions = await models.Transaction.findAll(query);

console.log('>>> transactions found', transactions.length);
const distinctTransactions = [],
collectiveIds = {};
Expand Down Expand Up @@ -189,7 +188,6 @@ const processBacker = async FromCollectiveId => {
{ concurrency: 4 },
);
}

const orders = await models.Order.findAll({
attributes: ['id', 'CollectiveId', 'totalAmount', 'currency'],
where: {
Expand All @@ -198,25 +196,32 @@ const processBacker = async FromCollectiveId => {
createdAt: { [Op.gte]: startDate, [Op.lt]: endDate },
SubscriptionId: { [Op.ne]: null },
},
deletedAt: null,
},
include: [{ model: models.Subscription }],
});
const ordersByCollectiveId = {};
orders.map(o => {
ordersByCollectiveId[o.CollectiveId] = o;
include: [
{
model: models.Subscription,
where: {
isActive: true,
},
},
],
});

const ordersByCollectiveId = groupBy(orders, 'CollectiveId');
const collectivesWithOrders = [];
collectives.map(collective => {
// It's only possible to have one order with subscription active Per Collective
const collectiveOrder = ordersByCollectiveId[collective.id] && ordersByCollectiveId[collective.id][0];
collectivesWithOrders.push({
...collective,
order: ordersByCollectiveId[collective.id],
order: collectiveOrder,
});
});
collectivesWithOrders.sort((a, b) => {
if (get(a, 'order.totalAmount') > get(b, 'order.totalAmount')) return -1;
else return 1;
});

const stats = await computeStats(collectivesWithOrders, backerCollective.currency);
const relatedCollectives = await models.Collective.getCollectivesSummaryByTag(
stats.topTags,
Expand Down Expand Up @@ -451,6 +456,7 @@ const computeStats = async (collectives, currency = 'USD') => {
stats.expensesBreakdownString = `${Object.keys(categories).length > 3 ? ', mostly in' : ' in'} ${formatArrayToString(
ar,
)}`;
console.log(`>>> Stats: ${JSON.stringify(stats, null, 2)}`);
return stats;
};

Expand Down

0 comments on commit a10577b

Please sign in to comment.