Campaign metrics include child campaign metrics now

This commit is contained in:
Eric Schultz 2018-11-19 16:10:56 -06:00
parent 478825a47a
commit 27877a716c
2 changed files with 101 additions and 8 deletions

View file

@ -2,21 +2,33 @@
module QueryCampaignMetrics
def self.on_donations(campaign_id)
Qx.select(
campaign = Campaign.find(campaign_id)
result = Qx.select(
"COALESCE(COUNT(DISTINCT donations.id), 0) AS supporters_count",
"COALESCE(SUM(payments.gross_amount), 0) AS total_raised",
"campaigns.goal_amount",
"campaigns.show_total_count",
"campaigns.show_total_raised")
"COALESCE(SUM(payments.gross_amount), 0) AS total_raised"
# "campaigns.goal_amount",
# "campaigns.show_total_count",
# "campaigns.show_total_raised"
)
.from("campaigns")
.left_join(
.join(
["donations", "donations.campaign_id=campaigns.id"],
["payments", "payments.donation_id=donations.id"]
)
.where("campaigns.id=$id", id: campaign_id)
.group_by('campaigns.id')
.where("campaigns.id IN (#{Qx.select("id").from('campaigns')
.where("campaigns.id = $id OR campaigns.parent_campaign_id=$id", id: campaign_id).parse
})")
.execute
.last
return {
'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
}
end
end

View file

@ -0,0 +1,81 @@
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
require 'rails_helper'
describe QueryCampaignMetrics do
describe 'calculates your metrics plus children' do
let(:nonprofit) {force_create(:nonprofit)}
let(:campaign) {force_create(:campaign, nonprofit:nonprofit, show_total_count:false, show_total_raised: false, goal_amount: 16000)}
let(:campaign_child) {force_create(:campaign, nonprofit:nonprofit, parent_campaign:campaign, show_total_count:true, show_total_raised: true, goal_amount: 8000)}
let(:campaign_child_2) {force_create(:campaign, nonprofit:nonprofit, parent_campaign:campaign, show_total_count:true, show_total_raised: true, goal_amount: 4000 )}
let(:donation) { force_create(:donation, campaign: campaign, amount: 1000)}
let(:payment) { force_create(:payment, donation: donation, gross_amount:1000)}
let(:donation2) { force_create(:donation, campaign: campaign, amount: 2000)}
let(:payment2) { force_create(:payment, donation: donation2, gross_amount:2000)}
let(:donation3) { force_create(:donation, campaign: campaign_child, amount: 4000)}
let(:payment3) { force_create(:payment, donation: donation3, gross_amount:4000)}
let(:donation4) { force_create(:donation, campaign: campaign_child_2, amount: 8000)}
let(:payment4) { force_create(:payment, donation: donation4, gross_amount:8000)}
let(:payments) do
payment
payment2
payment3
payment4
end
let (:campaign_metric) do
payments
QueryCampaignMetrics.on_donations(campaign.id)
end
let(:campaign_child_metric) do
payments
QueryCampaignMetrics.on_donations(campaign_child.id)
end
let(:campaign_child_2_metric) do
payments
QueryCampaignMetrics.on_donations(campaign_child_2.id)
end
it 'campaign metric is valid' do
expect(campaign_metric['supporters_count']).to eq 4
expect(campaign_metric['total_raised']).to eq 15000
expect(campaign_metric['goal_amount']).to eq 16000
expect(campaign_metric['show_total_count']).to eq false
expect(campaign_metric['show_total_raised']).to eq false
end
it 'campaign child metric is valid' do
expect(campaign_child_metric['supporters_count']).to eq 1
expect(campaign_child_metric['total_raised']).to eq 4000
expect(campaign_child_metric['goal_amount']).to eq 8000
expect(campaign_child_metric['show_total_count']).to eq true
expect(campaign_child_metric['show_total_raised']).to eq true
end
it 'campaign child 2metric is valid' do
expect(campaign_child_2_metric['supporters_count']).to eq 1
expect(campaign_child_2_metric['total_raised']).to eq 8000
expect(campaign_child_2_metric['goal_amount']).to eq 4000
expect(campaign_child_2_metric['show_total_count']).to eq true
expect(campaign_child_2_metric['show_total_raised']).to eq true
end
end
end