diff --git a/app/models/lotteries/division_ranking.rb b/app/models/lotteries/division_ranking.rb index 3c8e6dd63..bee45807f 100644 --- a/app/models/lotteries/division_ranking.rb +++ b/app/models/lotteries/division_ranking.rb @@ -12,6 +12,7 @@ class Lotteries::DivisionRanking < ApplicationRecord waitlisted: 1, drawn_beyond_waitlist: 2, not_drawn: 3, + withdrawn: 4, } end diff --git a/db/migrate/20241220002632_update_lotteries_division_rankings_to_version_2.rb b/db/migrate/20241220002632_update_lotteries_division_rankings_to_version_2.rb new file mode 100644 index 000000000..453fd63bc --- /dev/null +++ b/db/migrate/20241220002632_update_lotteries_division_rankings_to_version_2.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 514175762..54e436acf 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -1041,7 +1041,12 @@ 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, @@ -1049,6 +1054,7 @@ 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 diff --git a/db/views/lotteries_division_rankings_v02.sql b/db/views/lotteries_division_rankings_v02.sql new file mode 100644 index 000000000..ad64bb571 --- /dev/null +++ b/db/views/lotteries_division_rankings_v02.sql @@ -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 +; diff --git a/erd.pdf b/erd.pdf index 0d1456085..fc4daf30c 100644 Binary files a/erd.pdf and b/erd.pdf differ diff --git a/spec/models/lottery_division_spec.rb b/spec/models/lottery_division_spec.rb index a88e1bae5..582fa00a0 100644 --- a/spec/models/lottery_division_spec.rb +++ b/spec/models/lottery_division_spec.rb @@ -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