From 8a78fad0c7af29be1b42ca181cc670e7b4319d88 Mon Sep 17 00:00:00 2001 From: Eric Schultz Date: Tue, 20 Nov 2018 16:17:57 -0600 Subject: [PATCH] Various queries now properly include child campaigns --- .../get_campaign_and_event_names_and_ids.js | 12 +++++++++- lib/query/query_campaigns.rb | 23 +++++++++++-------- lib/query/query_donations.rb | 8 +++++-- lib/query/query_supporters.rb | 8 +++++-- 4 files changed, 36 insertions(+), 15 deletions(-) diff --git a/client/js/common/ajax/get_campaign_and_event_names_and_ids.js b/client/js/common/ajax/get_campaign_and_event_names_and_ids.js index 9ab7cbc4..be8b833e 100644 --- a/client/js/common/ajax/get_campaign_and_event_names_and_ids.js +++ b/client/js/common/ajax/get_campaign_and_event_names_and_ids.js @@ -7,7 +7,17 @@ module.exports = function(npo_id) { request.get(campaignsPath).end(function(err, resp){ resp.body.unshift(false) - appl.def('campaigns.data', resp.body) + let campaign_id_names = resp.body.map((i) => { + if (i.isChildCampaign) + { + return {id: i.id, name: i.name + " - " + i.creator} + } + else + { + return {id: i.id, name: i.name} + } + }) + appl.def('campaigns.data', campaign_id_names) }) request.get(eventsPath).end(function(err, resp){ resp.body.unshift(false) diff --git a/lib/query/query_campaigns.rb b/lib/query/query_campaigns.rb index 26b8d231..ec4eec89 100644 --- a/lib/query/query_campaigns.rb +++ b/lib/query/query_campaigns.rb @@ -50,7 +50,7 @@ module QueryCampaigns ["payments AS onetime", "onetime.id=payments.id AND onetime.kind='Donation'"], ["payments AS offsite", "offsite.id=payments.id AND offsite.kind='OffsitePayment'"], ["payments AS recurring", "recurring.id=payments.id AND recurring.kind='RecurringDonation'"]) - .where("donations.campaign_id" => campaign_id) + .where("donations.campaign_id IN (#{QueryCampaigns.get_campaign_and_children(campaign_id).parse})") end @@ -62,15 +62,18 @@ module QueryCampaigns def self.name_and_id(npo_id) - Psql.execute( - Qexpr.new.select( - "campaigns.name", - "campaigns.id") - .from("campaigns") - .where("campaigns.nonprofit_id=$id", id: npo_id) - .where("campaigns.deleted='f' OR campaigns.deleted IS NULL") - .order_by("campaigns.name ASC") - ) + + np = Nonprofit.find(npo_id) + campaigns = np.campaigns.not_deleted.includes(:profile).order('campaigns.name ASC') + output = campaigns.map do |i| + { + 'name' => i.name, + 'id' => i.id, + 'isChildCampaign' => i.child_campaign?, + 'creator' => i.profile&.name || "user ##{i.profile.id}" + } + end + output end def self.get_campaign_and_children(campaign_id) diff --git a/lib/query/query_donations.rb b/lib/query/query_donations.rb index bde23278..0147f13d 100644 --- a/lib/query/query_donations.rb +++ b/lib/query/query_donations.rb @@ -19,7 +19,9 @@ module QueryDonations .join(:supporters, "supporters.id=donations.supporter_id") .left_outer_join(:campaign_gifts, "campaign_gifts.donation_id=donations.id") .left_outer_join(:campaign_gift_options, "campaign_gift_options.id=campaign_gifts.campaign_gift_option_id") - .where("donations.campaign_id=$id", id: campaign_id) + .where("donations.campaign_id IN (#{QueryCampaigns + .get_campaign_and_children(campaign_id) + .parse})") .group_by("donations.id", "supporters.id") .order_by("donations.date") ) @@ -27,7 +29,9 @@ module QueryDonations def self.for_campaign_activities(id) QueryDonations.activities_expression(['donations.recurring']) - .where('donations.campaign_id=$id', id: id) + .where("donations.campaign_id IN (#{QueryCampaigns + .get_campaign_and_children(id) + .parse})") .execute end diff --git a/lib/query/query_supporters.rb b/lib/query/query_supporters.rb index 7459787b..3191c3f6 100644 --- a/lib/query/query_supporters.rb +++ b/lib/query/query_supporters.rb @@ -14,7 +14,9 @@ module QuerySupporters .left_outer_join('campaign_gifts', 'donations.id=campaign_gifts.donation_id') .left_outer_join('campaign_gift_options', 'campaign_gifts.campaign_gift_option_id=campaign_gift_options.id') .where("supporters.nonprofit_id=$id", id: np_id) - .where("donations.campaign_id=$id", id: campaign_id) + .where("donations.campaign_id IN (#{QueryCampaigns + .get_campaign_and_children(campaign_id) + .parse})") .group_by('supporters.id') .order_by('MAX(donations.date) DESC') @@ -253,7 +255,9 @@ module QuerySupporters expr = expr.and_where("tags.ids @> ARRAY[$tag_ids]", tag_ids: tag_ids) end if query[:campaign_id].present? - expr = expr.add_join("donations", "donations.supporter_id=supporters.id AND donations.campaign_id=#{query[:campaign_id].to_i}") + expr = expr.add_join("donations", "donations.supporter_id=supporters.id AND donations.campaign_id IN (#{QueryCampaigns + .get_campaign_and_children(query[:campaign_id].to_i) + .parse})") end if query[:event_id].present?