Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct "total donated" amount in user's monthly report #1563

Merged
merged 4 commits into from
Dec 5, 2018

Conversation

marcogbarcellos
Copy link
Contributor

This PR properly filters orders to return orders only when their subscriptions are active.

references opencollective/opencollective#1504
fixes opencollective/opencollective#1505

@marcogbarcellos marcogbarcellos requested a review from znarf December 3, 2018 13:51
@znarf znarf requested a review from Betree December 3, 2018 14:10
Copy link
Member

@Betree Betree left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edit: Sorry @marcogbarcellos I forgot something in the previous query, please see the comment below.

include: [
{
model: models.Subscription,
where: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a where clause to the include automatically makes it required. I think we may miss orders that don't have a subscription here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum, I'll better use just a filter then. thanks for the good catch.

@marcogbarcellos marcogbarcellos force-pushed the fix-1504/orders-user-report branch from 8b48432 to 158a904 Compare December 4, 2018 01:37
@coveralls
Copy link

coveralls commented Dec 4, 2018

Coverage Status

Coverage remained the same at 69.59% when pulling 33dfb89 on fix-1504/orders-user-report into 909e79a on master.

@Betree
Copy link
Member

Betree commented Dec 4, 2018

--Removed--

[Op.or]: {
FromCollectiveId,
UsingVirtualCardFromCollectiveId: FromCollectiveId,
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to support Virtual Card here at the moment.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes sorry @marcogbarcellos, I didn't realized this was user report and user cannot emit virtualcards, my first comment was misleading.

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];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if collectiveOrder is null?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ordersByCollectiveId[o.CollectiveId] = o;
});
// 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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be possible that we have collectives with multiple donations each month (multiple single time donation, mix of subscription + single time, and even multiple subscriptions).

How do we handle that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This script was considering only subscription orders. At first I thought it was intentional but It makes senses to cover all orders regardless. I'll update the code to reflect it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marcogbarcellos Was the user-report limited to subscriptions until now? If that's the case, where was that enforced?

Copy link
Contributor Author

@marcogbarcellos marcogbarcellos Dec 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, i explained wrong. I meant it was only getting the latest order of each collective which would ONLY make sense if it was considering subscriptions only(afaik a contributor is supposed to have only one subscription per collective). Actually still wouldn't because the subscription deactivation were being ignored as well. The following part of master branch's user-report is wrong because it's basically iterating over the Order.findAll result and if there is more than one order of the same collective it would consider only the latest one:
https://github.com/opencollective/opencollective-api/blob/master/cron/monthly/user-report.js#L204-L207

@marcogbarcellos marcogbarcellos force-pushed the fix-1504/orders-user-report branch 2 times, most recently from 3a9266d to e461b0a Compare December 4, 2018 14:56
@marcogbarcellos
Copy link
Contributor Author

marcogbarcellos commented Dec 5, 2018

all good to merge here @znarf @Betree ?

else return 1;
// It's only possible to have one order with subscription active Per Collective
const collectiveOrders = ordersByCollectiveId[collective.id] && ordersByCollectiveId[collective.id];
collectiveOrders &&
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@znarf that commented you've just marked me is outdated. I'm just pushing to collectivesWithOrders after validatingcollectiveOrders.

if (get(a, 'order.totalAmount') > get(b, 'order.totalAmount')) return -1;
else return 1;
// It's only possible to have one order with subscription active Per Collective
const collectiveOrders = ordersByCollectiveId[collective.id] && ordersByCollectiveId[collective.id];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't looks good.

else return 1;
// It's only possible to have one order with subscription active Per Collective
const collectiveOrders = ordersByCollectiveId[collective.id] && ordersByCollectiveId[collective.id];
collectiveOrders &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, avoid the && syntax, if will be easier to read.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed and done. thanks.

@marcogbarcellos marcogbarcellos force-pushed the fix-1504/orders-user-report branch 2 times, most recently from a9b9bc8 to 36b77fb Compare December 5, 2018 09:28
@znarf
Copy link
Member

znarf commented Dec 5, 2018

@marcogbarcellos What about line 214 comment?

@marcogbarcellos
Copy link
Contributor Author

marcogbarcellos commented Dec 5, 2018

@znarf the line 214 comment doesn't make sense anymore as we are getting all orders given a FromCollective and a Collective.

@@ -451,6 +452,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)}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove console.log ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this console.log may be really helpful to check the logs after running these scripts. in this case it'll show the consolidated stats per user. I think it worths keeping this console.log as we already have other logs in this report as well. But let me know if you think it's too much.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sense. Do you know how to retrieve logs from a cron task?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

afaik the same way we can log the app but instead of using --app we'd use the scheduler job id. example: heroku logs -d scheduler.5255

… don't have subscription OR have active subscriptions, consider UsingVirtualCardFromCollectiveId as well

fix eslint
…ubscription ones) of the contributors

fix: better validation

fix: better validation
@marcogbarcellos marcogbarcellos force-pushed the fix-1504/orders-user-report branch from 36b77fb to 33dfb89 Compare December 5, 2018 16:14
@marcogbarcellos marcogbarcellos merged commit 8b5044b into master Dec 5, 2018
@marcogbarcellos marcogbarcellos deleted the fix-1504/orders-user-report branch December 5, 2018 16:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

User Monthly report shows wrong donated amount
4 participants