Skip to content

Commit 8d55faf

Browse files
committed
Revert "Make ImportantNoticeGeneratorJob idempotent"
This reverts commit ce63b62.
1 parent 57b1386 commit 8d55faf

2 files changed

Lines changed: 24 additions & 29 deletions

File tree

app/jobs/important_notice_generator_job.rb

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

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

3840
patient_team_ids =
3941
patients.each_with_object({}) do |patient, hash|
@@ -51,7 +53,7 @@ def process_batch(patients)
5153
end
5254

5355
if notices_to_create.any?
54-
ImportantNotice.import!(notices_to_create, on_duplicate_key_ignore: true)
56+
ImportantNotice.import(notices_to_create, validate: false)
5557
end
5658

5759
if notice_ids_to_dismiss.any?

spec/jobs/important_notice_generator_job_spec.rb

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

1313
before do
14+
# Associate patient with teams through locations
1415
create(:patient_location, patient:, session: session_a)
1516
create(:patient_location, patient:, session: session_b)
1617
end
1718

1819
describe "#perform" do
19-
subject(:perform) { described_class.new.perform(patient.id) }
20-
2120
context "when patient exists in multiple teams" do
2221
context "deceased" do
23-
before do
22+
it "creates deceased notices for all associated teams" do
2423
patient.update_columns(
2524
# if we use update! the jobs gets called automatically
2625
date_of_death: Time.current,
2726
date_of_death_recorded_at: Time.current
2827
)
29-
end
3028

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)
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)
3532

3633
expect(team_a.important_notices.first.type).to eq("deceased")
3734
expect(team_a.important_notices.first.message).to eq(
3835
"Record updated with child’s date of death"
3936
)
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)
4537
end
4638
end
4739

4840
context "restricted" do
49-
before { patient.update_column(:restricted_at, Time.current) }
50-
5141
it "creates restricted notices for all associated teams" do
52-
expect { perform }.to change(ImportantNotice, :count).by(2)
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)
5347

5448
notices = patient.important_notices.where(type: :restricted)
5549
expect(notices.pluck(:team_id)).to contain_exactly(
@@ -61,13 +55,11 @@
6155
end
6256

6357
context "patient is no longer restricted" do
64-
before do
58+
it "dismisses existing restricted notices" do
6559
patient.update!(restricted_at: Time.current)
6660
patient.update_column(:restricted_at, nil)
67-
end
6861

69-
it "dismisses existing restricted notices" do
70-
expect { perform }.to change {
62+
expect { described_class.new.perform(patient.id) }.to change {
7163
ImportantNotice
7264
.active(team: team_a)
7365
.where(patient:, type: :restricted)
@@ -78,10 +70,13 @@
7870
end
7971

8072
context "invalidated" do
81-
before { patient.update_column(:invalidated_at, Time.current) }
82-
8373
it "creates invalidated notices for all associated teams" do
84-
expect { perform }.to change(ImportantNotice, :count).by(2)
74+
patient.update_column(:invalidated_at, Time.current)
75+
76+
expect { described_class.new.perform(patient.id) }.to change(
77+
ImportantNotice,
78+
:count
79+
).by(2)
8580

8681
notices = patient.important_notices.where(type: :invalidated)
8782
expect(notices.pluck(:team_id)).to contain_exactly(
@@ -93,13 +88,11 @@
9388
end
9489

9590
context "patient is no longer invalidated" do
96-
before do
91+
it "dismisses existing invalidated notices" do
9792
patient.update!(invalidated_at: Time.current)
9893
patient.update_column(:invalidated_at, nil)
99-
end
10094

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

0 commit comments

Comments
 (0)