Skip to content

Commit

Permalink
Track sending of dissolution emails and scheduling of closure
Browse files Browse the repository at this point in the history
This is so the buttons can be displayed in the correct order.
  • Loading branch information
pixeltrix committed May 23, 2024
1 parent 475135b commit 7e0bb43
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 9 deletions.
14 changes: 12 additions & 2 deletions app/models/parliament.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ def dissolution_announced?
dissolution_at? && show_dissolution_notification?
end

def dissolution_emails_sent?
dissolution_emails_sent_at?
end

def closure_scheduled?
closure_scheduled_at?
end

def registration_closed?(now = Time.current)
registration_closed_at? && registration_closed_at <= now
end
Expand Down Expand Up @@ -221,16 +229,18 @@ def start_anonymizing!(now = Time.current)
end
end

def schedule_closure!
def schedule_closure!(now = Time.current)
if dissolution_announced? && !dissolved?
ClosePetitionsEarlyJob.schedule_for(dissolution_at)
StopPetitionsEarlyJob.schedule_for(dissolution_at)
update_column(:closure_scheduled_at, now)
end
end

def send_emails!
def send_emails!(now = Time.current)
if dissolution_at? && !dissolved?
NotifyPetitionsThatParliamentIsDissolvingJob.perform_later
update_column(:dissolution_emails_sent_at, now)
end
end

Expand Down
6 changes: 3 additions & 3 deletions app/views/admin/parliaments/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
<%= form.submit 'Archive parliament', name: 'archive_parliament', class: 'button-secondary', data: { confirm: 'Archive this parliament and create a new one?' } %>
<% end %>
<% elsif @parliament.dissolving? %>
<% if @parliament.dissolution_announced? %>
<%= form.submit 'Schedule closure', name: 'schedule_closure', class: 'button-secondary', data: { confirm: 'Schedule early closure of petitions?' } %>
<% else %>
<% if [email protected]_emails_sent? %>
<%= form.submit 'Send dissolution emails', name: 'send_emails', class: 'button-secondary', data: { confirm: 'Email everyone about dissolution?' } %>
<% elsif [email protected]_scheduled? %>
<%= form.submit 'Schedule closure', name: 'schedule_closure', class: 'button-secondary', data: { confirm: 'Schedule early closure of petitions?' } %>
<% end %>
<% end %>
<% if @parliament.can_anonymize_petitions? %>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddDissolutionEmailsSentAtToParliaments < ActiveRecord::Migration[7.1]
def change
add_column :parliaments, :dissolution_emails_sent_at, :datetime
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddClosureScheduledAtToParliaments < ActiveRecord::Migration[7.1]
def change
add_column :parliaments, :closure_scheduled_at, :datetime
end
end
4 changes: 3 additions & 1 deletion 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.1].define(version: 2024_03_08_120916) do
ActiveRecord::Schema[7.1].define(version: 2024_05_23_163632) do
# These are extensions that must be enabled in order to support this database
enable_extension "intarray"
enable_extension "plpgsql"
Expand Down Expand Up @@ -471,6 +471,8 @@
t.string "parliamentary_debate_heading"
t.text "parliamentary_debate_description"
t.string "parliamentary_debate_status"
t.datetime "dissolution_emails_sent_at"
t.datetime "closure_scheduled_at"
end

create_table "petition_emails", id: :serial, force: :cascade do |t|
Expand Down
25 changes: 22 additions & 3 deletions spec/models/parliament_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -937,15 +937,25 @@

context "when parliament has announced dissolution" do
context "and the dissolution date has not passed" do
let(:now) { Time.current.floor }

subject :parliament do
FactoryBot.create(:parliament, :dissolving)
end

it "schedules a job" do
expect {
subject.send_emails!
subject.send_emails!(now)
}.to have_enqueued_job(NotifyPetitionsThatParliamentIsDissolvingJob).on_queue(:high_priority)
end

it "marks the dissolution emails as having been sent" do
expect {
subject.send_emails!(now)
}.to change {
subject.reload.dissolution_emails_sent_at
}.from(nil).to(now)
end
end

context "and the dissolution date has passsed" do
Expand Down Expand Up @@ -982,22 +992,31 @@
context "when parliament has announced dissolution" do
context "and the dissolution date has not passed" do
let(:dissolution_at) { 2.weeks.from_now }
let(:now) { Time.current.floor }

subject :parliament do
FactoryBot.create(:parliament, :dissolving, dissolution_at: dissolution_at, show_dissolution_notification: true)
end

it "schedules a job to close petitions early" do
expect {
subject.schedule_closure!
subject.schedule_closure!(now)
}.to have_enqueued_job(ClosePetitionsEarlyJob).on_queue(:high_priority).with(dissolution_at.iso8601).at(dissolution_at)
end

it "schedules a job to stop petitions collecting sponsors early" do
expect {
subject.schedule_closure!
subject.schedule_closure!(now)
}.to have_enqueued_job(StopPetitionsEarlyJob).on_queue(:high_priority).with(dissolution_at.iso8601).at(dissolution_at)
end

it "marks the closure as having been scheduled" do
expect {
subject.schedule_closure!(now)
}.to change {
subject.reload.closure_scheduled_at
}.from(nil).to(now)
end
end

context "and the dissolution date has passsed" do
Expand Down

0 comments on commit 7e0bb43

Please sign in to comment.