Skip to content

Commit 64259d5

Browse files
authored
Merge pull request #3234 from nhsuk/self-consent-invalidate-vaccinated
Don't invalidate self-consent if vaccinated
2 parents 5bef94a + 48a3174 commit 64259d5

File tree

2 files changed

+86
-30
lines changed

2 files changed

+86
-30
lines changed

app/jobs/invalidate_self_consents_job.rb

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,36 @@ 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
98

10-
team_ids
11-
.product(programme_ids)
12-
.each do |team_id, programme_id|
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|
1318
consents =
1419
Consent
1520
.via_self_consent
16-
.where(team_id:, programme_id:)
21+
.where(academic_year:, team:, programme:)
22+
.where(patient: patients)
1723
.where("created_at < ?", Date.current.beginning_of_day)
1824
.not_withdrawn
1925

2026
triages =
2127
Triage
22-
.where(team_id:, programme_id:)
23-
.where("created_at < ?", Date.current.beginning_of_day)
28+
.where(academic_year:, team:, programme:)
2429
.where(patient_id: consents.pluck(:patient_id))
30+
.where("created_at < ?", Date.current.beginning_of_day)
2531

2632
ActiveRecord::Base.transaction do
2733
consents.invalidate_all
2834
triages.invalidate_all
2935
end
3036
end
37+
end
3138
end
3239
end

spec/jobs/invalidate_self_consents_job_spec.rb

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

6+
let(:academic_year) { AcademicYear.current }
7+
let(:patient) { consent.patient }
8+
let(:programme) { consent.programme }
9+
let(:team) { consent.team }
10+
611
context "with parental consent from yesterday" do
7-
let(:consent) { create(:consent, created_at: 1.day.ago) }
12+
let(:consent) { create(:consent, academic_year:, created_at: 1.day.ago) }
13+
14+
before { create(:patient_vaccination_status, patient:, programme:) }
815

916
it "does not invalidate the consent" do
1017
expect { perform_now }.not_to(change { consent.reload.invalidated? })
@@ -14,10 +21,11 @@
1421
let(:triage) do
1522
create(
1623
:triage,
24+
academic_year:,
1725
created_at: 1.day.ago,
18-
team: consent.team,
19-
programme: consent.programme,
20-
patient: consent.patient
26+
team:,
27+
programme:,
28+
patient:
2129
)
2230
end
2331

@@ -28,20 +36,17 @@
2836
end
2937

3038
context "with parental consent from today" do
31-
let(:consent) { create(:consent) }
39+
let(:consent) { create(:consent, academic_year:) }
40+
41+
before { create(:patient_vaccination_status, patient:, programme:) }
3242

3343
it "does not invalidate the consent" do
3444
expect { perform_now }.not_to(change { consent.reload.invalidated? })
3545
end
3646

3747
context "with triage" do
3848
let(:triage) do
39-
create(
40-
:triage,
41-
team: consent.team,
42-
programme: consent.programme,
43-
patient: consent.patient
44-
)
49+
create(:triage, academic_year:, team:, programme:, patient:)
4550
end
4651

4752
it "does not invalidate the triage" do
@@ -51,7 +56,11 @@
5156
end
5257

5358
context "with self-consent from yesterday" do
54-
let(:consent) { create(:consent, :self_consent, created_at: 1.day.ago) }
59+
let(:consent) do
60+
create(:consent, :self_consent, academic_year:, created_at: 1.day.ago)
61+
end
62+
63+
before { create(:patient_vaccination_status, patient:, programme:) }
5564

5665
it "invalidates the consent" do
5766
expect { perform_now }.to change { consent.reload.invalidated? }.from(
@@ -63,10 +72,11 @@
6372
let(:triage) do
6473
create(
6574
:triage,
75+
academic_year:,
6676
created_at: 1.day.ago,
67-
team: consent.team,
68-
programme: consent.programme,
69-
patient: consent.patient
77+
team:,
78+
programme:,
79+
patient:
7080
)
7181
end
7282

@@ -76,23 +86,55 @@
7686
).to(true)
7787
end
7888
end
89+
90+
context "if the patient was vaccinated" do
91+
before do
92+
create(
93+
:vaccination_record,
94+
team:,
95+
programme:,
96+
patient:,
97+
created_at: 1.day.ago
98+
)
99+
100+
patient.vaccination_statuses.update_all(status: :vaccinated)
101+
end
102+
103+
it "does not invalidate the consent" do
104+
expect { perform_now }.not_to(change { consent.reload.invalidated? })
105+
end
106+
107+
context "with triage" do
108+
let(:triage) do
109+
create(
110+
:triage,
111+
academic_year:,
112+
created_at: 1.day.ago,
113+
team:,
114+
programme:,
115+
patient:
116+
)
117+
end
118+
119+
it "does not invalidate the triage" do
120+
expect { perform_now }.not_to(change { triage.reload.invalidated? })
121+
end
122+
end
123+
end
79124
end
80125

81126
context "with self-consent from today" do
82-
let(:consent) { create(:consent, :self_consent) }
127+
let(:consent) { create(:consent, :self_consent, academic_year:) }
128+
129+
before { create(:patient_vaccination_status, patient:, programme:) }
83130

84131
it "does not invalidate the consent" do
85132
expect { perform_now }.not_to(change { consent.reload.invalidated? })
86133
end
87134

88135
context "with triage" do
89136
let(:triage) do
90-
create(
91-
:triage,
92-
team: consent.team,
93-
programme: consent.programme,
94-
patient: consent.patient
95-
)
137+
create(:triage, academic_year:, team:, programme:, patient:)
96138
end
97139

98140
it "does not invalidate the triage" do
@@ -114,6 +156,7 @@
114156
:consent,
115157
:self_consent,
116158
patient:,
159+
academic_year:,
117160
created_at: 1.day.ago,
118161
programme: self_programme,
119162
team:
@@ -123,12 +166,18 @@
123166
create(
124167
:consent,
125168
patient:,
169+
academic_year:,
126170
created_at: 1.day.ago,
127171
programme: parent_programme,
128172
team:
129173
)
130174
end
131175

176+
before do
177+
create(:patient_vaccination_status, patient:, programme: self_programme)
178+
create(:patient_vaccination_status, patient:, programme: parent_programme)
179+
end
180+
132181
it "does not invalidate the parent consent" do
133182
expect { perform_now }.not_to(
134183
change { parent_consent.reload.invalidated? }

0 commit comments

Comments
 (0)