Skip to content

Commit 5ccc9fd

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 3a0fa59 commit 5ccc9fd

File tree

4 files changed

+67
-58
lines changed

4 files changed

+67
-58
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: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,52 +8,85 @@
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:)
2324
travel 5.seconds do
2425
@job_ran_at = Time.zone.now
2526
perform_enqueued_jobs
2627
end
2728
end
2829

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
30+
it "does not send an email" do
31+
expect(ActionMailer::Base.deliveries).to be_empty
32+
end
3333

34-
it "does not update the delivery" do
35-
expect(delivery.reload.last_attempt_at).to be_nil
36-
end
34+
it "does not update the delivery" do
35+
expect(delivery.reload.last_attempt_at).to be_nil
3736
end
37+
end
3838

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-
)
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+
)
49+
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)
52+
end
53+
let(:preview_submission) do
54+
create(:submission, :preview, form_document:, form_id:, created_at: date.beginning_of_day + 1.hour)
55+
end
56+
let(:submissions) { [submissions_to_include, submission_not_on_date, preview_submission] }
57+
58+
context "when the form does not have a submission email address" do
59+
let(:submission_email) { nil }
60+
61+
it "raises an error" do
62+
expect {
63+
travel 5.seconds do
64+
@job_ran_at = Time.zone.now
65+
perform_enqueued_jobs
66+
end
67+
}.to raise_error(StandardError, "Form id: #{form_id} is missing a submission email address")
4968
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)
69+
70+
context "when the mode is preview" do
71+
let(:mode_string) { "preview-live" }
72+
73+
it "does not call the submission batch service" do
74+
travel 5.seconds do
75+
@job_ran_at = Time.zone.now
76+
perform_enqueued_jobs
77+
end
78+
expect(AwsSesSubmissionBatchService).not_to receive(:new)
79+
end
5280
end
53-
let(:preview_submission) do
54-
create(:submission, :preview, form_document:, form_id:, created_at: date.beginning_of_day + 1.hour)
81+
end
82+
83+
context "when the form has a submission email address" do
84+
before do
85+
travel 5.seconds do
86+
@job_ran_at = Time.zone.now
87+
perform_enqueued_jobs
88+
end
5589
end
56-
let(:submissions) { [submissions_to_include, submission_not_on_date, preview_submission] }
5790

5891
it "sends an email" do
5992
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)