Skip to content

Commit

Permalink
feat(guests): apply exclude_hosts option to empty search query
Browse files Browse the repository at this point in the history
  • Loading branch information
chillfox committed Nov 8, 2023
1 parent 00aef57 commit feb38c4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
2 changes: 1 addition & 1 deletion spec/controllers/guests_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe Guests do
emails.includes?(guest2.email).should be_true
end

it "exclude_hosts filtered should return a list of all guests excluding hosts", focus: true do
it "exclude_hosts filtered should return a list of all guests excluding hosts" do
EventMetadata.truncate
Guest.truncate
Attendee.truncate
Expand Down
22 changes: 10 additions & 12 deletions src/controllers/guests.cr
Original file line number Diff line number Diff line change
Expand Up @@ -180,22 +180,20 @@ class Guests < Application
elsif search_query.empty?
# Return the first 1500 guests
unless exclude_hosts

Check notice on line 182 in src/controllers/guests.cr

View workflow job for this annotation

GitHub Actions / Ameba

Style/UnlessElse

Favour if over unless with else
Raw output
> unless exclude_hosts
  ^
Guest
.by_tenant(tenant.id.not_nil!)
.order(:name)
.limit(1500).map { |g| attending_guest(nil, g).as(Guest | Attendee) }
else
pp "================================================================="
pp! exclude_hosts
# TODO: use a join to exclude hosts
# - first join to attendees
# - then join to event_metadatas
# - then filter out where host_email matches guest/attendee email
Guest
.by_tenant(tenant.id.not_nil!)
.where("email NOT IN (SELECT host_email FROM event_metadatas WHERE tenant_id = #{tenant.id.not_nil!})")
.order(:name)
.limit(1500).map { |g| attending_guest(nil, g).as(Guest | Attendee) }
else
query = Guest
.find_all_by_sql(<<-SQL, tenant.id.not_nil!)
SELECT g.* FROM "guests" g
LEFT JOIN "attendees" a ON a.guest_id = g.id
LEFT JOIN "event_metadatas" m ON m.id = a.event_id
WHERE g.tenant_id = $1 AND m.host_email != g.email
ORDER BY g.name ASC LIMIT 1500
SQL
query.map { |g| attending_guest(nil, g).as(Guest | Attendee) }
end
else
# Return guests based on the filter query
Expand Down

0 comments on commit feb38c4

Please sign in to comment.