-
-
Notifications
You must be signed in to change notification settings - Fork 273
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
Conversation
There was a problem hiding this 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.
cron/monthly/user-report.js
Outdated
include: [ | ||
{ | ||
model: models.Subscription, | ||
where: { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
8b48432
to
158a904
Compare
--Removed-- |
cron/monthly/user-report.js
Outdated
[Op.or]: { | ||
FromCollectiveId, | ||
UsingVirtualCardFromCollectiveId: FromCollectiveId, | ||
}, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
cron/monthly/user-report.js
Outdated
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]; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
3a9266d
to
e461b0a
Compare
cron/monthly/user-report.js
Outdated
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 && |
There was a problem hiding this comment.
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
.
cron/monthly/user-report.js
Outdated
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]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't looks good.
cron/monthly/user-report.js
Outdated
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 && |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agreed and done. thanks.
a9b9bc8
to
36b77fb
Compare
@marcogbarcellos What about line 214 comment? |
@znarf the line 214 comment doesn't make sense anymore as we are getting all orders given a |
@@ -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)}`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove console.log ?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
…veId case for now on Transactions query
…ubscription ones) of the contributors fix: better validation fix: better validation
36b77fb
to
33dfb89
Compare
This PR properly filters orders to return orders only when their subscriptions are active.
references opencollective/opencollective#1504
fixes opencollective/opencollective#1505