Skip to content

Commit 548920e

Browse files
Merge pull request #1831 from alphagov/theseanything/populate-bounced-at
Record `bounced_at` timestamp when processing submission bounces
2 parents 94759e5 + 1ceff4c commit 548920e

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

app/jobs/receive_submission_bounces_and_complaints_job.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,17 @@ def perform
2929
def process_bounce(submission, ses_message)
3030
set_submission_logging_attributes(submission)
3131

32+
bounce_object = ses_message["bounce"] || {}
33+
3234
# Don't mark preview submissions as bounced, just log that they bounced. We don't need to attempt to resend preview
3335
# submissions so these can be deleted as normal by the deletion job.
34-
submission.bounced! unless submission.preview?
35-
36-
bounce_object = ses_message["bounce"] || {}
36+
unless submission.preview?
37+
bounced_timestamp = Time.zone.parse(bounce_object["timestamp"])
38+
submission.update!(
39+
delivery_status: :bounced,
40+
bounced_at: bounced_timestamp,
41+
)
42+
end
3743

3844
ses_bounce = {
3945
bounce_type: bounce_object["bounceType"],

spec/jobs/receive_submission_bounces_and_complaints_job_spec.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,25 @@
7474
end
7575

7676
context "when it is for a live submission" do
77+
let(:bounce_timestamp) { "2023-01-01T12:00:00Z" }
78+
let(:ses_message_body) do
79+
{
80+
"mail" => { "messageId" => mail_message_id },
81+
"eventType" => event_type,
82+
"bounce" => { "timestamp" => bounce_timestamp },
83+
}
84+
end
85+
7786
it "updates the submission mail status to bounced" do
7887
perform_enqueued_jobs
7988
expect(submission.reload.bounced?).to be true
8089
end
8190

91+
it "updates the submission bounced_at timestamp" do
92+
perform_enqueued_jobs
93+
expect(submission.reload.bounced_at).to eq(Time.zone.parse(bounce_timestamp))
94+
end
95+
8296
it "doesn't change the mail status for other submissions" do
8397
perform_enqueued_jobs
8498
expect(other_submission.reload.pending?).to be true
@@ -145,7 +159,8 @@
145159

146160
context "when there is a bounce object with detailed information" do
147161
let(:ses_message_body) { { "mail" => { "messageId" => mail_message_id }, "eventType" => event_type, "bounce" => bounce } }
148-
let(:bounce) { { "bounceType" => "Permanent", "bounceSubType" => "General", "bouncedRecipients" => bounced_recipients } }
162+
let(:bounce) { { "bounceType" => "Permanent", "bounceSubType" => "General", "bouncedRecipients" => bounced_recipients, "timestamp" => bounce_timestamp } }
163+
let(:bounce_timestamp) { "2023-01-01T12:00:00Z" }
149164
let(:bounced_recipients) { [{ "emailAddress" => "bounce@example.com" }] }
150165

151166
it "logs the bounce details" do

0 commit comments

Comments
 (0)