Skip to content

Commit 93fc96b

Browse files
Fix session selection logic
`Session`s which are `generic_clinic`s use `send_invitations_at` instead of `send_consent_requests_at`. Both need notifications to be sent, so must be extracted separately in the DB query.
1 parent b91216d commit 93fc96b

File tree

2 files changed

+225
-62
lines changed

2 files changed

+225
-62
lines changed

app/jobs/enqueue_vaccinations_search_in_nhs_job.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@ def perform(sessions = nil)
88
begin
99
flu = Programme.flu.sole
1010
Session
11-
.includes(:session_dates)
11+
.includes(:session_dates, :location)
1212
.has_programmes([flu])
13-
.where("sessions.send_consent_requests_at <= ?", 2.days.from_now)
13+
.where(
14+
"(locations.type IN (?) AND sessions.send_invitations_at <= ?) OR " \
15+
"(locations.type IN (?, ?) AND sessions.send_consent_requests_at <= ?)",
16+
Location.types[:generic_clinic], 2.days.from_now,
17+
Location.types[:community_clinic], Location.types[:school], 2.days.from_now
18+
)
1419
.where("session_dates.value >= ?", Time.zone.today)
15-
.references(:session_dates)
20+
.references(:session_dates, :locations)
1621
end
1722

1823
patient_ids =

spec/jobs/enqueue_vaccinations_search_in_nhs_job_spec.rb

Lines changed: 217 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -14,92 +14,250 @@
1414

1515
before { allow(SearchVaccinationRecordsInNHSJob).to receive(:perform_bulk) }
1616

17-
let(:send_consent_requests_at) {}
18-
let(:days_before_consent_reminders) { 7 }
19-
let!(:session) do
20-
create(
21-
:session,
22-
programmes: [flu],
23-
academic_year: AcademicYear.pending,
24-
dates:,
25-
send_consent_requests_at:,
26-
days_before_consent_reminders:,
27-
team:,
28-
location:,
29-
patients: [patient]
30-
)
31-
end
17+
context "with a normal school session" do
18+
let(:send_consent_requests_at) {}
19+
let(:days_before_consent_reminders) { 7 }
20+
let!(:session) do
21+
create(
22+
:session,
23+
programmes: [flu],
24+
academic_year: AcademicYear.pending,
25+
dates:,
26+
send_consent_requests_at:,
27+
days_before_consent_reminders:,
28+
team:,
29+
location:,
30+
patients: [patient]
31+
)
32+
end
3233

33-
context "with an specific, unscheduled session" do
34-
before { described_class.perform_now([session]) }
34+
context "with an specific, unscheduled session" do
35+
before { described_class.perform_now([session]) }
3536

36-
let(:dates) { [] }
37-
let(:days_before_consent_reminders) { nil }
37+
let(:dates) { [] }
38+
let(:days_before_consent_reminders) { nil }
3839

39-
it { should have_received(:perform_bulk).once.with([[patient.id]]) }
40-
end
40+
it { should have_received(:perform_bulk).once.with([[patient.id]]) }
41+
end
4142

42-
context "session with dates in the future" do
43-
before { described_class.perform_now }
43+
context "session with dates in the future" do
44+
before { described_class.perform_now }
4445

45-
let(:dates) { [7.days.from_now] }
46-
let(:send_consent_requests_at) { 14.days.ago }
46+
let(:dates) { [7.days.from_now] }
47+
let(:send_consent_requests_at) { 14.days.ago }
4748

48-
it { should have_received(:perform_bulk).once.with([[patient.id]]) }
49+
it { should have_received(:perform_bulk).once.with([[patient.id]]) }
4950

50-
context "community clinic session" do
51-
let(:location) { create(:community_clinic, team:, programmes: [flu]) }
52-
let(:school) { create(:school, team:, programmes: [flu]) }
51+
context "community clinic session" do
52+
let(:location) { create(:community_clinic, team:, programmes: [flu]) }
53+
let(:school) { create(:school, team:, programmes: [flu]) }
5354

54-
it { should have_received(:perform_bulk).exactly(:once) }
55+
it { should have_received(:perform_bulk).exactly(:once) }
56+
end
57+
58+
context "generic clinic session" do
59+
let(:location) { create(:generic_clinic, team:, programmes: [flu]) }
60+
let(:school) { create(:school, team:, programmes: [flu]) }
61+
62+
it { should have_received(:perform_bulk).exactly(:once) }
63+
end
5564
end
5665

57-
context "generic clinic session" do
58-
let(:location) { create(:generic_clinic, team:, programmes: [flu]) }
59-
let(:school) { create(:school, team:, programmes: [flu]) }
66+
context "session with dates in the past",
67+
within_academic_year: {
68+
from_start: 7.days
69+
} do
70+
before { described_class.perform_now }
71+
72+
let(:dates) { [7.days.ago] }
73+
let(:send_consent_requests_at) { 30.days.ago }
74+
75+
it { should_not have_received(:perform_bulk) }
76+
end
77+
78+
context "session with dates in the past and the future",
79+
within_academic_year: {
80+
from_start: 7.days
81+
} do
82+
before { described_class.perform_now }
83+
84+
let(:send_consent_requests_at) { 28.days.ago }
85+
let(:dates) { [7.days.ago, 7.days.from_now] }
6086

6187
it { should have_received(:perform_bulk).exactly(:once) }
6288
end
63-
end
6489

65-
context "session with dates in the past",
66-
within_academic_year: {
67-
from_start: 7.days
68-
} do
69-
before { described_class.perform_now }
90+
context "session with send_invitations_at in the future" do
91+
before { described_class.perform_now }
7092

71-
let(:dates) { [7.days.ago] }
72-
let(:send_consent_requests_at) { 30.days.ago }
93+
let(:send_consent_requests_at) { 2.days.from_now }
94+
let(:dates) { [17.days.from_now] }
95+
96+
it { should have_received(:perform_bulk).exactly(:once) }
97+
end
98+
99+
context "session with send_consent_requests_at too far in the future" do
100+
let(:send_consent_requests_at) { 3.days.from_now }
101+
let(:dates) { [17.days.from_now] }
73102

74103
it { should_not have_received(:perform_bulk) }
104+
end
75105
end
76106

77-
context "session with dates in the past and the future",
78-
within_academic_year: {
79-
from_start: 7.days
80-
} do
81-
before { described_class.perform_now }
107+
shared_examples "notification date logic" do |notification_date_field|
108+
context "with notification date within threshold" do
109+
let!(:session) do
110+
session_attributes = {
111+
programmes: [flu],
112+
academic_year: AcademicYear.pending,
113+
dates:,
114+
team:,
115+
location:,
116+
patients: [patient]
117+
}
118+
119+
# Set the appropriate notification date field
120+
if notification_date_field == :send_invitations_at
121+
session_attributes[:send_invitations_at] = 1.day.from_now
122+
session_attributes[:send_consent_requests_at] = nil
123+
else
124+
session_attributes[:send_invitations_at] = nil
125+
session_attributes[:send_consent_requests_at] = 1.day.from_now
126+
session_attributes[:days_before_consent_reminders] = 7
127+
end
128+
129+
create(:session, session_attributes)
130+
end
82131

83-
let(:send_consent_requests_at) { 28.days.ago }
84-
let(:dates) { [7.days.ago, 7.days.from_now] }
132+
before { described_class.perform_now }
85133

86-
it { should have_received(:perform_bulk).exactly(:once) }
87-
end
134+
it "includes the session" do
135+
expect(SearchVaccinationRecordsInNHSJob).to have_received(:perform_bulk).with(
136+
[patient.id].zip
137+
)
138+
end
139+
end
88140

89-
context "session with send_invitations_at in the future" do
90-
before { described_class.perform_now }
141+
context "with notification date too far in future" do
142+
let!(:session) do
143+
session_attributes = {
144+
programmes: [flu],
145+
academic_year: AcademicYear.pending,
146+
dates:,
147+
team:,
148+
location:,
149+
patients: [patient]
150+
}
151+
152+
# Set the appropriate notification date field
153+
if notification_date_field == :send_invitations_at
154+
session_attributes[:send_invitations_at] = 3.days.from_now
155+
session_attributes[:send_consent_requests_at] = nil
156+
else
157+
session_attributes[:send_invitations_at] = nil
158+
session_attributes[:send_consent_requests_at] = 3.days.from_now
159+
session_attributes[:days_before_consent_reminders] = 7
160+
end
161+
162+
create(:session, session_attributes)
163+
end
91164

92-
let(:send_consent_requests_at) { 2.days.from_now }
93-
let(:dates) { [17.days.from_now] }
165+
before { described_class.perform_now }
94166

95-
it { should have_received(:perform_bulk).exactly(:once) }
167+
it "excludes the session" do
168+
expect(SearchVaccinationRecordsInNHSJob).not_to have_received(:perform_bulk)
169+
end
170+
end
96171
end
97172

98-
context "session with send_consent_requests_at too far in the future" do
99-
let(:send_consent_requests_at) { 3.days.from_now }
100-
let(:dates) { [17.days.from_now] }
173+
context "testing notification date logic for different location types" do
174+
let(:dates) { [30.days.from_now] }
101175

102-
it { should_not have_received(:perform_bulk) }
176+
context "generic clinic sessions" do
177+
let(:location) { create(:generic_clinic, team:, programmes: [flu]) }
178+
let(:school) { create(:school, team:, programmes: [flu]) }
179+
180+
include_examples "notification date logic", :send_invitations_at
181+
end
182+
183+
context "community clinic sessions" do
184+
let(:location) { create(:community_clinic, team:, programmes: [flu]) }
185+
let(:school) { create(:school, team:, programmes: [flu]) }
186+
187+
include_examples "notification date logic", :send_consent_requests_at
188+
end
189+
190+
context "school sessions" do
191+
let(:location) { create(:school, team:, programmes: [flu]) }
192+
let(:school) { location }
193+
194+
include_examples "notification date logic", :send_consent_requests_at
195+
end
196+
197+
context "mixed session types" do
198+
let(:generic_clinic) { create(:generic_clinic, team:, programmes: [flu]) }
199+
let(:community_clinic) { create(:community_clinic, team:, programmes: [flu]) }
200+
let(:school_location) { create(:school, team:, programmes: [flu]) }
201+
202+
let!(:first_patient) { create(:patient, team:, school:) }
203+
let!(:second_patient) { create(:patient, team:, school:) }
204+
let!(:third_patient) { create(:patient, team:, school:) }
205+
206+
let!(:generic_session) do
207+
create(
208+
:session,
209+
programmes: [flu],
210+
academic_year: AcademicYear.pending,
211+
dates:,
212+
send_invitations_at: 1.day.from_now,
213+
send_consent_requests_at: nil,
214+
days_before_consent_reminders: nil,
215+
team:,
216+
location: generic_clinic,
217+
patients: [first_patient]
218+
)
219+
end
220+
221+
let!(:community_session) do
222+
create(
223+
:session,
224+
programmes: [flu],
225+
academic_year: AcademicYear.pending,
226+
dates:,
227+
send_invitations_at: nil,
228+
send_consent_requests_at: 1.day.from_now,
229+
days_before_consent_reminders: 7,
230+
team:,
231+
location: community_clinic,
232+
patients: [second_patient]
233+
)
234+
end
235+
236+
let!(:school_session) do
237+
create(
238+
:session,
239+
programmes: [flu],
240+
academic_year: AcademicYear.pending,
241+
dates:,
242+
send_invitations_at: nil,
243+
send_consent_requests_at: 1.day.from_now,
244+
days_before_consent_reminders: 7,
245+
team:,
246+
location: school_location,
247+
patients: [third_patient]
248+
)
249+
end
250+
251+
before { described_class.perform_now }
252+
253+
it "includes all sessions when their respective notification dates are within threshold" do
254+
expect(SearchVaccinationRecordsInNHSJob).to have_received(:perform_bulk).once
255+
256+
expect(SearchVaccinationRecordsInNHSJob).to have_received(:perform_bulk).with(
257+
[first_patient.id, second_patient.id, third_patient.id].zip
258+
)
259+
end
260+
end
103261
end
104262
end
105263
end

0 commit comments

Comments
 (0)