Skip to content

Commit

Permalink
Update division rankings view to handle withdrawn entrants
Browse files Browse the repository at this point in the history
  • Loading branch information
moveson committed Dec 20, 2024
1 parent 089a4f6 commit 944d3f0
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/models/lotteries/division_ranking.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Lotteries::DivisionRanking < ApplicationRecord
waitlisted: 1,
drawn_beyond_waitlist: 2,
not_drawn: 3,
withdrawn: 4,
}

end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class UpdateLotteriesDivisionRankingsToVersion2 < ActiveRecord::Migration[7.0]
def change
update_view :lotteries_division_rankings, version: 2, revert_to_version: 1
end
end
10 changes: 8 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2024_12_19_195819) do
ActiveRecord::Schema[7.0].define(version: 2024_12_20_002632) do
# These are extensions that must be enabled in order to support this database
enable_extension "fuzzystrmatch"
enable_extension "pg_trgm"
Expand Down Expand Up @@ -1041,14 +1041,20 @@
FROM ((lottery_tickets
JOIN lottery_draws ON ((lottery_draws.lottery_ticket_id = lottery_tickets.id)))
JOIN lottery_entrants lottery_entrants_1 ON ((lottery_entrants_1.id = lottery_tickets.lottery_entrant_id)))
WINDOW division_window AS (PARTITION BY lottery_entrants_1.lottery_division_id ORDER BY lottery_draws.created_at)
WINDOW division_window AS (PARTITION BY lottery_entrants_1.lottery_division_id ORDER BY
CASE
WHEN ((lottery_entrants_1.withdrawn IS FALSE) OR (lottery_entrants_1.withdrawn IS NULL)) THEN 0
WHEN (lottery_entrants_1.withdrawn IS TRUE) THEN 1
ELSE NULL::integer
END, lottery_draws.created_at)
)
SELECT lottery_entrants.id AS lottery_entrant_id,
lottery_divisions.name AS division_name,
ranked_draws.division_rank,
CASE
WHEN (ranked_draws.division_rank <= lottery_divisions.maximum_entries) THEN 0
WHEN (ranked_draws.division_rank <= (lottery_divisions.maximum_entries + lottery_divisions.maximum_wait_list)) THEN 1
WHEN lottery_entrants.withdrawn THEN 4
WHEN (ranked_draws.division_rank IS NOT NULL) THEN 2
ELSE 3
END AS draw_status
Expand Down
33 changes: 33 additions & 0 deletions db/views/lotteries_division_rankings_v02.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
with ranked_draws as (
select lottery_tickets.lottery_entrant_id,
rank() over division_window as division_rank
from lottery_tickets
join lottery_draws on lottery_draws.lottery_ticket_id = lottery_tickets.id
join lottery_entrants on lottery_entrants.id = lottery_tickets.lottery_entrant_id
window division_window as (
partition by lottery_entrants.lottery_division_id
order by case
when withdrawn is false or withdrawn is null then 0
when withdrawn is true then 1 end,
lottery_draws.created_at
)
)

select lottery_entrants.id as lottery_entrant_id,
lottery_divisions.name as division_name,
division_rank,
-- accepted: 0
-- waitlisted: 1
-- drawn_beyond_waitlist: 2
-- not_drawn: 3
-- withdrawn: 4
case
when division_rank <= maximum_entries then 0
when division_rank <= maximum_entries + maximum_wait_list then 1
when lottery_entrants.withdrawn then 4
when division_rank is not null then 2
else 3 end as draw_status
from lottery_entrants
left join ranked_draws on ranked_draws.lottery_entrant_id = lottery_entrants.id
join lottery_divisions on lottery_entrants.lottery_division_id = lottery_divisions.id
;
Binary file modified erd.pdf
Binary file not shown.
9 changes: 9 additions & 0 deletions spec/models/lottery_division_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@
expect(result.count).to eq(subject.maximum_entries)
expect(result).to all be_a(LotteryEntrant)
end

context "when one of the entrants has withdrawn" do
let(:withdrawn_entrant) { subject.entrants.accepted.first }
before { withdrawn_entrant.update(withdrawn: true) }

it "does not include the withdrawn entrant" do
expect(result).not_to include(withdrawn_entrant)
end
end
end

context "when some accepted entrants have been drawn" do
Expand Down

0 comments on commit 944d3f0

Please sign in to comment.