Fix bug where searches of supporters filtered by an event don't include donors at the event

This commit is contained in:
Eric Schultz 2018-06-22 15:30:39 -05:00
parent 13bcba935c
commit 1028b65421
4 changed files with 63 additions and 8 deletions

View file

@ -0,0 +1,5 @@
class AddIndexToSupporterIdOnTickets < ActiveRecord::Migration
def change
add_index :tickets, :supporter_id
end
end

View file

@ -0,0 +1,6 @@
class AddIndexToEventIdOnDonationsAndEvents < ActiveRecord::Migration
def change
add_index :tickets, :event_id
add_index :donations, :event_id
end
end

View file

@ -3141,6 +3141,13 @@ CREATE INDEX index_cards_on_id_and_holder_type_and_holder_id_and_inactive ON pub
CREATE INDEX index_charges_on_payment_id ON public.charges USING btree (payment_id); CREATE INDEX index_charges_on_payment_id ON public.charges USING btree (payment_id);
--
-- Name: index_donations_on_event_id; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX index_donations_on_event_id ON public.donations USING btree (event_id);
-- --
-- Name: index_exports_on_nonprofit_id; Type: INDEX; Schema: public; Owner: - -- Name: index_exports_on_nonprofit_id; Type: INDEX; Schema: public; Owner: -
-- --
@ -3218,6 +3225,20 @@ CREATE INDEX index_supporters_on_import_id ON public.supporters USING btree (imp
CREATE INDEX index_supporters_on_name ON public.supporters USING btree (name); CREATE INDEX index_supporters_on_name ON public.supporters USING btree (name);
--
-- Name: index_tickets_on_event_id; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX index_tickets_on_event_id ON public.tickets USING btree (event_id);
--
-- Name: index_tickets_on_supporter_id; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX index_tickets_on_supporter_id ON public.tickets USING btree (supporter_id);
-- --
-- Name: index_users_on_confirmation_token; Type: INDEX; Schema: public; Owner: - -- Name: index_users_on_confirmation_token; Type: INDEX; Schema: public; Owner: -
-- --
@ -4275,3 +4296,7 @@ INSERT INTO schema_migrations (version) VALUES ('20180216124311');
INSERT INTO schema_migrations (version) VALUES ('20180217124311'); INSERT INTO schema_migrations (version) VALUES ('20180217124311');
INSERT INTO schema_migrations (version) VALUES ('20180608205049');
INSERT INTO schema_migrations (version) VALUES ('20180608212658');

View file

@ -245,16 +245,35 @@ module QuerySupporters
if query[:campaign_id].present? 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=#{query[:campaign_id].to_i}")
end end
if query[:event_id].present? if query[:event_id].present?
select_tickets_supporters = Qx.select("event_ticket_supporters.supporter_id")
.from(
"#{Qx.select("MAX(tickets.event_id) AS event_id", "tickets.supporter_id")
.from(:tickets)
.where("event_id = $event_id", event_id: query[:event_id])
.group_by(:supporter_id).as('event_ticket_supporters').parse}"
)
select_donation_supporters =
Qx.select("event_donation_supporters.supporter_id")
.from(
"#{Qx.select("MAX(donations.event_id) AS event_id", "donations.supporter_id")
.from(:donations)
.where("event_id = $event_id", event_id: query[:event_id] )
.group_by(:supporter_id).as('event_donation_supporters').parse}")
union_expr = "(
#{select_tickets_supporters.parse}
UNION DISTINCT
#{select_donation_supporters.parse}
) AS event_supporters"
expr = expr expr = expr
.add_join( .add_join(
Qx.select("MAX(tickets.event_id) AS event_id", "tickets.supporter_id") union_expr,
.from(:tickets) "event_supporters.supporter_id=supporters.id"
.group_by(:supporter_id)
.as(:tickets),
"tickets.supporter_id=supporters.id"
) )
.and_where("tickets.event_id=$id", id: query[:event_id])
end end
if ['asc', 'desc'].include? query[:sort_name] if ['asc', 'desc'].include? query[:sort_name]
expr = expr.order_by(["supporters.name", query[:sort_name]]) expr = expr.order_by(["supporters.name", query[:sort_name]])