Skip to content

Commit 1ca83bb

Browse files
authored
Merge pull request #2221 from alphagov/fix-travel-advice-email-alert
Fix travel advice email alert
2 parents 0926484 + 3d94b22 commit 1ca83bb

5 files changed

Lines changed: 32 additions & 21 deletions

File tree

app/notifiers/publishing_api_notifier.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def publish_index
3838
end
3939

4040
def send_alert(edition)
41-
return unless send_alert?(edition)
41+
return if edition.is_minor_update?
4242

4343
presenter = EmailAlertPresenter.new(edition)
4444
payload = presenter.present.as_json
@@ -58,10 +58,6 @@ def worker
5858
PublishingApiWorker
5959
end
6060

61-
def send_alert?(edition)
62-
edition.state == "published" && !edition.is_minor_update?
63-
end
64-
6561
def validate_tasks_order
6662
endpoints = tasks.map(&:first)
6763
send_alert_count = endpoints.count { |e| e == "send_alert" }

app/workers/scheduled_publishing_worker.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@ def perform(edition_id)
2525
return
2626
end
2727

28-
notifier.put_content(edition)
29-
notifier.patch_links(edition)
30-
notifier.email_signup(edition) if edition.previous_version.nil?
31-
notifier.publish(edition)
32-
notifier.send_alert(edition)
33-
notifier.enqueue
34-
3528
unless publishing_robot
3629
Sidekiq.logger.error("You must set up a Scheduled Publishing Robot")
3730
return
3831
end
3932

4033
edition.publish_as(publishing_robot)
34+
35+
notifier.put_content(edition)
36+
notifier.patch_links(edition)
37+
notifier.email_signup(edition) if edition.previous_version.nil?
38+
notifier.publish(edition)
39+
notifier.send_alert(edition)
40+
notifier.enqueue
4141
end
4242

4343
private

spec/controllers/admin/editions_controller_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@
232232
it "should publish the edition" do
233233
allow(TravelAdviceEdition).to receive(:find).with(draft.to_param).and_return(draft)
234234
allow(draft).to receive(:publish).and_return(true)
235+
draft.published_at = Time.zone.now
235236

236237
post :update, params: { id: draft.to_param, edition: {}, commit: "Save & Publish" }
237238

spec/notifiers/publishing_api_notifier_spec.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,6 @@
105105
end
106106
end
107107

108-
context "for a draft edition" do
109-
it "doesn't enqueue anything" do
110-
subject.send_alert(edition)
111-
subject.enqueue
112-
113-
expect(PublishingApiWorker.jobs).to be_empty
114-
end
115-
end
116-
117108
context "for a minor update" do
118109
it "doesn't enqueue anything" do
119110
edition.update!(update_type: "minor", version_number: 2)

spec/workers/scheduled_publishing_worker_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
RSpec.describe ScheduledPublishingWorker, type: :worker do
2+
include GdsApi::TestHelpers::EmailAlertApi
3+
include GdsApi::TestHelpers::PublishingApi
4+
25
describe "#perform" do
36
let(:country) { Country.find_by_slug("afghanistan") }
47
let(:edition) { create(:scheduled_travel_advice_edition, country_slug: country.slug) }
@@ -19,6 +22,26 @@
1922
expect(edition.reload.actions.first.requester.name).to eq robot.name
2023
end
2124

25+
it "#perform sends email alerts for an edition" do
26+
stub_any_publishing_api_call
27+
stub_email_alert_api_accepts_content_change
28+
29+
# Queue up and execute the ScheduledPublishingWorker, move to after scheduled time
30+
ScheduledPublishingWorker.enqueue(edition)
31+
travel_to(1.hour.from_now)
32+
ScheduledPublishingWorker.drain
33+
34+
# Check that the above created a PublishingApiWorker, then execute it
35+
expect(PublishingApiWorker.jobs.size).to eq(1)
36+
PublishingApiWorker.drain
37+
38+
# Check that the above created a EmailAlertApiWorker, then execute it
39+
expect(EmailAlertApiWorker.jobs.size).to eq(1)
40+
EmailAlertApiWorker.drain
41+
42+
assert_email_alert_api_content_change_created
43+
end
44+
2245
it "does not publish edition that isn't yet due for publication" do
2346
expect(Sidekiq.logger).to receive(:info).with("Edition of ID '#{edition.id}' is not yet due for publication.").once
2447

0 commit comments

Comments
 (0)