2019-07-30 21:29:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-25 16:15:39 +00:00
|
|
|
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
|
2018-03-25 17:30:42 +00:00
|
|
|
module QueryCampaignMetrics
|
|
|
|
def self.on_donations(campaign_id)
|
2018-11-19 22:10:56 +00:00
|
|
|
campaign = Campaign.find(campaign_id)
|
|
|
|
|
2018-12-04 22:40:48 +00:00
|
|
|
result = Psql.execute(
|
2019-07-30 21:29:24 +00:00
|
|
|
Qexpr.new.select('COALESCE(COUNT(DISTINCT donations.id), 0) AS supporters_count',
|
|
|
|
'COALESCE(SUM(payments.gross_amount), 0) AS total_raised')
|
|
|
|
.from('campaigns')
|
2018-12-04 22:40:48 +00:00
|
|
|
.join(
|
2019-07-30 21:29:24 +00:00
|
|
|
'donations', 'donations.campaign_id=campaigns.id'
|
2018-12-04 22:40:48 +00:00
|
|
|
)
|
2019-07-30 21:29:24 +00:00
|
|
|
.join_lateral('payments', QueryDonations.get_first_payment_for_donation.parse, true)
|
2018-12-04 22:40:48 +00:00
|
|
|
.where("campaigns.id IN (#{QueryCampaigns
|
|
|
|
.get_campaign_and_children(campaign_id)
|
2019-07-30 21:29:24 +00:00
|
|
|
.parse})")
|
2018-12-04 22:40:48 +00:00
|
|
|
).last
|
2018-11-19 22:10:56 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
{
|
|
|
|
'supporters_count' => result['supporters_count'],
|
|
|
|
'total_raised' => result['total_raised'],
|
|
|
|
'goal_amount' => campaign.goal_amount,
|
|
|
|
'show_total_count' => campaign.show_total_count,
|
|
|
|
'show_total_raised' => campaign.show_total_raised
|
2018-11-19 22:10:56 +00:00
|
|
|
}
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
end
|