Skip to content

Commit ce63b62

Browse files
committed
Make ImportantNoticeGeneratorJob idempotent
This ensures that the job can be run multiple times for the same patient and not raise an error about duplicate indexes. Jira-Issue: MAV-2309
1 parent f4658ff commit ce63b62

2 files changed

Lines changed: 29 additions & 24 deletions

File tree

app/jobs/important_notice_generator_job.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ def process_batch(patients)
3333
patient_ids = patients.map(&:id)
3434

3535
existing_notices =
36-
ImportantNotice
37-
.where(patient_id: patient_ids)
38-
.index_by { |n| notice_key(n) }
36+
ImportantNotice.where(patient_id: patient_ids).index_by { notice_key(it) }
3937

4038
patient_team_ids =
4139
patients.each_with_object({}) do |patient, hash|
@@ -53,7 +51,7 @@ def process_batch(patients)
5351
end
5452

5553
if notices_to_create.any?
56-
ImportantNotice.import(notices_to_create, validate: false)
54+
ImportantNotice.import!(notices_to_create, on_duplicate_key_ignore: true)
5755
end
5856

5957
if notice_ids_to_dismiss.any?

spec/jobs/important_notice_generator_job_spec.rb

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,45 @@
1111
let(:patient) { create(:patient) }
1212

1313
before do
14-
# Associate patient with teams through locations
1514
create(:patient_location, patient:, session: session_a)
1615
create(:patient_location, patient:, session: session_b)
1716
end
1817

1918
describe "#perform" do
19+
subject(:perform) { described_class.new.perform(patient.id) }
20+
2021
context "when patient exists in multiple teams" do
2122
context "deceased" do
22-
it "creates deceased notices for all associated teams" do
23+
before do
2324
patient.update_columns(
2425
# if we use update! the jobs gets called automatically
2526
date_of_death: Time.current,
2627
date_of_death_recorded_at: Time.current
2728
)
29+
end
2830

29-
expect { described_class.new.perform(patient.id) }.to change {
30-
team_a.important_notices.count
31-
}.by(1).and change { team_b.important_notices.count }.by(1)
31+
it "creates deceased notices for all associated teams" do
32+
expect { perform }.to change { team_a.important_notices.count }.by(
33+
1
34+
).and change { team_b.important_notices.count }.by(1)
3235

3336
expect(team_a.important_notices.first.type).to eq("deceased")
3437
expect(team_a.important_notices.first.message).to eq(
3538
"Record updated with child’s date of death"
3639
)
40+
41+
expect { described_class.new.perform(patient.id) }.to not_change(
42+
team_a.important_notices,
43+
:count
44+
).and not_change(team_b.important_notices, :count)
3745
end
3846
end
3947

4048
context "restricted" do
49+
before { patient.update_column(:restricted_at, Time.current) }
50+
4151
it "creates restricted notices for all associated teams" do
42-
patient.update_column(:restricted_at, Time.current)
43-
expect { described_class.new.perform(patient.id) }.to change(
44-
ImportantNotice,
45-
:count
46-
).by(2)
52+
expect { perform }.to change(ImportantNotice, :count).by(2)
4753

4854
notices = patient.important_notices.where(type: :restricted)
4955
expect(notices.pluck(:team_id)).to contain_exactly(
@@ -55,11 +61,13 @@
5561
end
5662

5763
context "patient is no longer restricted" do
58-
it "dismisses existing restricted notices" do
64+
before do
5965
patient.update!(restricted_at: Time.current)
6066
patient.update_column(:restricted_at, nil)
67+
end
6168

62-
expect { described_class.new.perform(patient.id) }.to change {
69+
it "dismisses existing restricted notices" do
70+
expect { perform }.to change {
6371
ImportantNotice
6472
.active(team: team_a)
6573
.where(patient:, type: :restricted)
@@ -70,13 +78,10 @@
7078
end
7179

7280
context "invalidated" do
73-
it "creates invalidated notices for all associated teams" do
74-
patient.update_column(:invalidated_at, Time.current)
81+
before { patient.update_column(:invalidated_at, Time.current) }
7582

76-
expect { described_class.new.perform(patient.id) }.to change(
77-
ImportantNotice,
78-
:count
79-
).by(2)
83+
it "creates invalidated notices for all associated teams" do
84+
expect { perform }.to change(ImportantNotice, :count).by(2)
8085

8186
notices = patient.important_notices.where(type: :invalidated)
8287
expect(notices.pluck(:team_id)).to contain_exactly(
@@ -88,11 +93,13 @@
8893
end
8994

9095
context "patient is no longer invalidated" do
91-
it "dismisses existing invalidated notices" do
96+
before do
9297
patient.update!(invalidated_at: Time.current)
9398
patient.update_column(:invalidated_at, nil)
99+
end
94100

95-
expect { described_class.new.perform(patient.id) }.to change {
101+
it "dismisses existing invalidated notices" do
102+
expect { perform }.to change {
96103
ImportantNotice
97104
.active(team: team_a)
98105
.where(patient:, type: :invalidated)

0 commit comments

Comments
 (0)