Skip to content

Commit 58f39df

Browse files
committed
Don't invalidate self-consent if vaccinated
If the patient was vaccinated after getting self-consent, we shouldn't invalidate that consent as it was valid when the vaccination was given. A future enhancement might be to add a concept of "lifespan" to consents, to capture exactly at which point they are invalid. At the moment, the nurses see a patient that was vaccinated, with supposedly no valid consent, which is not accurate.
1 parent 7046682 commit 58f39df

File tree

2 files changed

+66
-38
lines changed

2 files changed

+66
-38
lines changed

app/jobs/invalidate_self_consents_job.rb

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,38 @@ class InvalidateSelfConsentsJob < ApplicationJob
44
queue_as :consents
55

66
def perform
7-
team_ids = Team.pluck(:id)
8-
programme_ids = Programme.pluck(:id)
7+
academic_year = AcademicYear.current
8+
9+
Programme.find_each do |programme|
10+
patients =
11+
Patient.has_vaccination_status(
12+
%i[none_yet could_not_vaccinate],
13+
programme:,
14+
academic_year:
15+
)
16+
17+
Team.find_each do |team|
18+
# FIXME: Filter on academic year.
919

10-
team_ids
11-
.product(programme_ids)
12-
.each do |team_id, programme_id|
1320
consents =
1421
Consent
1522
.via_self_consent
16-
.where(team_id:, programme_id:)
23+
.where(team:, programme:)
24+
.where(patient: patients)
1725
.where("created_at < ?", Date.current.beginning_of_day)
1826
.not_withdrawn
1927

2028
triages =
2129
Triage
22-
.where(team_id:, programme_id:)
23-
.where("created_at < ?", Date.current.beginning_of_day)
30+
.where(team:, programme:)
2431
.where(patient_id: consents.pluck(:patient_id))
32+
.where("created_at < ?", Date.current.beginning_of_day)
2533

2634
ActiveRecord::Base.transaction do
2735
consents.invalidate_all
2836
triages.invalidate_all
2937
end
3038
end
39+
end
3140
end
3241
end

spec/jobs/invalidate_self_consents_job_spec.rb

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33
describe InvalidateSelfConsentsJob do
44
subject(:perform_now) { described_class.perform_now }
55

6+
let(:patient) { consent.patient }
7+
let(:programme) { consent.programme }
8+
let(:team) { consent.team }
9+
610
context "with parental consent from yesterday" do
711
let(:consent) { create(:consent, created_at: 1.day.ago) }
812

13+
before { create(:patient_vaccination_status, patient:, programme:) }
14+
915
it "does not invalidate the consent" do
1016
expect { perform_now }.not_to(change { consent.reload.invalidated? })
1117
end
1218

1319
context "with triage" do
1420
let(:triage) do
15-
create(
16-
:triage,
17-
created_at: 1.day.ago,
18-
team: consent.team,
19-
programme: consent.programme,
20-
patient: consent.patient
21-
)
21+
create(:triage, created_at: 1.day.ago, team:, programme:, patient:)
2222
end
2323

2424
it "does not invalidate the triage" do
@@ -30,19 +30,14 @@
3030
context "with parental consent from today" do
3131
let(:consent) { create(:consent) }
3232

33+
before { create(:patient_vaccination_status, patient:, programme:) }
34+
3335
it "does not invalidate the consent" do
3436
expect { perform_now }.not_to(change { consent.reload.invalidated? })
3537
end
3638

3739
context "with triage" do
38-
let(:triage) do
39-
create(
40-
:triage,
41-
team: consent.team,
42-
programme: consent.programme,
43-
patient: consent.patient
44-
)
45-
end
40+
let(:triage) { create(:triage, team:, programme:, patient:) }
4641

4742
it "does not invalidate the triage" do
4843
expect { perform_now }.not_to(change { triage.reload.invalidated? })
@@ -53,6 +48,8 @@
5348
context "with self-consent from yesterday" do
5449
let(:consent) { create(:consent, :self_consent, created_at: 1.day.ago) }
5550

51+
before { create(:patient_vaccination_status, patient:, programme:) }
52+
5653
it "invalidates the consent" do
5754
expect { perform_now }.to change { consent.reload.invalidated? }.from(
5855
false
@@ -61,13 +58,7 @@
6158

6259
context "with triage" do
6360
let(:triage) do
64-
create(
65-
:triage,
66-
created_at: 1.day.ago,
67-
team: consent.team,
68-
programme: consent.programme,
69-
patient: consent.patient
70-
)
61+
create(:triage, created_at: 1.day.ago, team:, programme:, patient:)
7162
end
7263

7364
it "invalidates the triage" do
@@ -76,24 +67,47 @@
7667
).to(true)
7768
end
7869
end
70+
71+
context "if the patient was vaccinated" do
72+
before do
73+
create(
74+
:vaccination_record,
75+
team:,
76+
programme:,
77+
patient:,
78+
created_at: 1.day.ago
79+
)
80+
81+
patient.vaccination_statuses.update_all(status: :vaccinated)
82+
end
83+
84+
it "does not invalidate the consent" do
85+
expect { perform_now }.not_to(change { consent.reload.invalidated? })
86+
end
87+
88+
context "with triage" do
89+
let(:triage) do
90+
create(:triage, created_at: 1.day.ago, team:, programme:, patient:)
91+
end
92+
93+
it "does not invalidate the triage" do
94+
expect { perform_now }.not_to(change { triage.reload.invalidated? })
95+
end
96+
end
97+
end
7998
end
8099

81100
context "with self-consent from today" do
82101
let(:consent) { create(:consent, :self_consent) }
83102

103+
before { create(:patient_vaccination_status, patient:, programme:) }
104+
84105
it "does not invalidate the consent" do
85106
expect { perform_now }.not_to(change { consent.reload.invalidated? })
86107
end
87108

88109
context "with triage" do
89-
let(:triage) do
90-
create(
91-
:triage,
92-
team: consent.team,
93-
programme: consent.programme,
94-
patient: consent.patient
95-
)
96-
end
110+
let(:triage) { create(:triage, team:, programme:, patient:) }
97111

98112
it "does not invalidate the triage" do
99113
expect { perform_now }.not_to(change { triage.reload.invalidated? })
@@ -129,6 +143,11 @@
129143
)
130144
end
131145

146+
before do
147+
create(:patient_vaccination_status, patient:, programme: self_programme)
148+
create(:patient_vaccination_status, patient:, programme: parent_programme)
149+
end
150+
132151
it "does not invalidate the parent consent" do
133152
expect { perform_now }.not_to(
134153
change { parent_consent.reload.invalidated? }

0 commit comments

Comments
 (0)