Various queries now properly include child campaigns

This commit is contained in:
Eric Schultz 2018-11-20 16:17:57 -06:00
parent 216cefaa98
commit 8a78fad0c7
4 changed files with 36 additions and 15 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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

View file

@ -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?