Skip to content

Commit 29e494e

Browse files
committed
Move submission_email checks to batch service job
Shifts the checks for a form having a submission_email out of the submission batch service and into the batch job. This will mean that the job will terminate early when no submission_email is present, rather than continuing even though no email gets sent.
1 parent d2d3b5d commit 29e494e

File tree

4 files changed

+60
-62
lines changed

4 files changed

+60
-62
lines changed

app/jobs/send_submission_batch_job.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ def perform(form_id:, mode_string:, date:, delivery:)
1818
mode = Mode.new(mode_string)
1919
set_submission_batch_logging_attributes(form:, mode:)
2020

21+
if form.submission_email.blank?
22+
if mode.preview?
23+
Rails.logger.info "Skipping sending batch for preview submissions, as the submission email address has not been set"
24+
return
25+
else
26+
raise StandardError, "Form id: #{form.id} is missing a submission email address"
27+
end
28+
end
29+
2130
message_id = AwsSesSubmissionBatchService.new(submissions_query: submissions, form:, date:, mode:).send_batch
2231

2332
delivery.update!(

app/services/aws_ses_submission_batch_service.rb

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,6 @@ def initialize(submissions_query:, form:, date:, mode:)
77
end
88

99
def send_batch
10-
if @form.submission_email.blank?
11-
if @mode.preview?
12-
Rails.logger.info "Skipping sending batch for preview submissions, as the submission email address has not been set"
13-
return
14-
else
15-
raise StandardError, "Form id: #{@form.id} is missing a submission email address"
16-
end
17-
end
18-
19-
deliver_batch_email
20-
end
21-
22-
private
23-
24-
def deliver_batch_email
2510
files = {}
2611

2712
csvs = CsvGenerator.generate_batched_submissions(submissions_query: @submissions_query, is_s3_submission: false)

spec/jobs/send_submission_batch_job_spec.rb

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,52 +8,74 @@
88
let(:date) { Date.new(2022, 12, 14) }
99
let(:delivery) { create(:delivery, delivery_schedule: "daily") }
1010

11-
let(:form_document) { create(:v2_form_document, :with_steps, name: "My Form", submission_email: "to@example.com") }
11+
let(:form_document) { create(:v2_form_document, :with_steps, name: "My Form", submission_email:) }
12+
let(:submission_email) { "to@example.com" }
1213
let(:form_id) { form_document.form_id }
1314
let(:submissions) { [] }
1415

1516
before do
1617
submissions
1718
ActionMailer::Base.deliveries.clear
19+
described_class.perform_later(form_id:, mode_string:, date:, delivery:)
1820
end
1921

20-
context "when the job is processed" do
22+
context "when there are no submissions" do
2123
before do
22-
described_class.perform_later(form_id:, mode_string:, date:, delivery:)
23-
travel 5.seconds do
24-
@job_ran_at = Time.zone.now
25-
perform_enqueued_jobs
26-
end
24+
perform_enqueued_jobs
2725
end
2826

29-
context "when there are no submissions" do
30-
it "does not send an email" do
31-
expect(ActionMailer::Base.deliveries).to be_empty
32-
end
27+
it "does not send an email" do
28+
expect(ActionMailer::Base.deliveries).to be_empty
29+
end
3330

34-
it "does not update the delivery" do
35-
expect(delivery.reload.last_attempt_at).to be_nil
36-
end
31+
it "does not update the delivery" do
32+
expect(delivery.reload.last_attempt_at).to be_nil
33+
end
34+
end
35+
36+
context "when there are submissions" do
37+
let(:submissions_to_include) do
38+
create_list(
39+
:submission,
40+
3,
41+
form_document:,
42+
form_id:,
43+
mode: mode_string,
44+
created_at: date.beginning_of_day + 1.hour,
45+
)
3746
end
47+
let(:submission_not_on_date) do
48+
create(:submission, form_document:, form_id:, mode: mode_string, created_at: date.beginning_of_day - 1.day)
49+
end
50+
let(:preview_submission) do
51+
create(:submission, :preview, form_document:, form_id:, created_at: date.beginning_of_day + 1.hour)
52+
end
53+
let(:submissions) { [submissions_to_include, submission_not_on_date, preview_submission] }
54+
55+
context "when the form does not have a submission email address" do
56+
let(:submission_email) { nil }
3857

39-
context "when there are submissions" do
40-
let(:submissions_to_include) do
41-
create_list(
42-
:submission,
43-
3,
44-
form_document:,
45-
form_id:,
46-
mode: mode_string,
47-
created_at: date.beginning_of_day + 1.hour,
48-
)
58+
it "raises an error" do
59+
expect {
60+
perform_enqueued_jobs
61+
}.to raise_error(StandardError, "Form id: #{form_id} is missing a submission email address")
4962
end
50-
let(:submission_not_on_date) do
51-
create(:submission, form_document:, form_id:, mode: mode_string, created_at: date.beginning_of_day - 1.day)
63+
64+
context "when the mode is preview" do
65+
let(:mode_string) { "preview-live" }
66+
67+
it "does not call the submission batch service" do
68+
perform_enqueued_jobs
69+
expect(AwsSesSubmissionBatchService).not_to receive(:new)
70+
end
5271
end
53-
let(:preview_submission) do
54-
create(:submission, :preview, form_document:, form_id:, created_at: date.beginning_of_day + 1.hour)
72+
end
73+
74+
context "when the form has a submission email address" do
75+
before do
76+
@job_ran_at = Time.zone.now
77+
perform_enqueued_jobs
5578
end
56-
let(:submissions) { [submissions_to_include, submission_not_on_date, preview_submission] }
5779

5880
it "sends an email" do
5981
expect(ActionMailer::Base.deliveries.count).to eq(1)

spec/services/aws_ses_submission_batch_service_spec.rb

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,6 @@
1616
create_list(:submission, 3, form_document:)
1717
end
1818

19-
context "when the form does not have a submission email address" do
20-
let(:submission_email) { nil }
21-
22-
it "raises an error" do
23-
expect(AwsSesSubmissionBatchMailer).not_to receive(:batch_submission_email)
24-
expect { service.send_batch }.to raise_error(StandardError, "Form id: #{form.id} is missing a submission email address")
25-
end
26-
27-
context "when the mode is preview" do
28-
let(:mode) { instance_double(Mode, preview?: true) }
29-
30-
it "does not send an email" do
31-
expect(AwsSesSubmissionBatchMailer).not_to receive(:batch_submission_email)
32-
service.send_batch
33-
end
34-
end
35-
end
36-
3719
it "calls the SubmissionFilenameGenerator with a nil form version" do
3820
expect(SubmissionFilenameGenerator).to receive(:batch_csv_filename).with(form_name: form.name, date:, mode:, form_version: nil)
3921
service.send_batch

0 commit comments

Comments
 (0)