|
3 | 3 | describe SendAutomaticSchoolConsentRemindersJob do
|
4 | 4 | subject(:perform_now) { described_class.perform_now(session) }
|
5 | 5 |
|
6 |
| - let(:programmes) { [create(:programme)] } |
| 6 | + let(:programmes) { [create(:programme, :flu)] } |
7 | 7 |
|
8 | 8 | let(:parents) { create_list(:parent, 2) }
|
9 | 9 |
|
| 10 | + let(:manual_reminder_patient) { create(:patient, parents:, team:) } |
| 11 | + |
10 | 12 | let(:patient_with_initial_reminder_sent) do
|
11 | 13 | create(
|
12 | 14 | :patient,
|
|
19 | 21 | let(:patient_not_sent_reminder) do
|
20 | 22 | create(:patient, :consent_request_sent, parents:, programmes:)
|
21 | 23 | end
|
| 24 | + let(:patient_vaccinated_last_year) do |
| 25 | + create(:patient, :consent_request_sent, parents:, programmes:) |
| 26 | + end |
22 | 27 | let(:patient_not_sent_reminder_joined_after_first_date) do
|
23 | 28 | create(:patient, :consent_request_sent, parents:, programmes:)
|
24 | 29 | end
|
| 30 | + |
25 | 31 | let(:patient_not_sent_request) { create(:patient, parents:, programmes:) }
|
26 | 32 | let(:patient_with_consent) do
|
27 | 33 | create(:patient, :consent_given_triage_not_needed, programmes:)
|
|
32 | 38 |
|
33 | 39 | let!(:patients) do
|
34 | 40 | [
|
| 41 | + manual_reminder_patient, |
35 | 42 | patient_with_initial_reminder_sent,
|
36 | 43 | patient_not_sent_reminder,
|
37 | 44 | patient_not_sent_reminder_joined_after_first_date,
|
|
61 | 68 | )
|
62 | 69 | end
|
63 | 70 |
|
| 71 | + let(:user) { create(:user, team:) } |
| 72 | + |
64 | 73 | before do
|
65 | 74 | ConsentNotification.request.update_all(sent_at: dates.first - 1.week)
|
66 | 75 | ConsentNotification.reminder.update_all(sent_at: dates.first)
|
67 | 76 |
|
68 | 77 | patient_not_sent_reminder_joined_after_first_date.consent_notifications.update_all(
|
69 | 78 | sent_at: dates.first + 1.day
|
70 | 79 | )
|
| 80 | + |
| 81 | + create( |
| 82 | + :consent_notification, |
| 83 | + patient: manual_reminder_patient, |
| 84 | + session:, |
| 85 | + programmes:, |
| 86 | + type: :initial_reminder, |
| 87 | + sent_at: dates.first - 9.days, |
| 88 | + sent_by: user |
| 89 | + ) |
71 | 90 | end
|
72 | 91 |
|
73 | 92 | around { |example| travel_to(today) { example.run } }
|
|
85 | 104 | let(:today) { dates.first - 1.week }
|
86 | 105 |
|
87 | 106 | it "sends notifications to one patient" do
|
88 |
| - expect(ConsentNotification).to receive(:create_and_send!).once.with( |
| 107 | + expect(ConsentNotification).to receive(:create_and_send!).with( |
89 | 108 | patient: patient_not_sent_reminder,
|
90 |
| - programmes:, |
| 109 | + programmes: [programmes.first], |
91 | 110 | session:,
|
92 |
| - type: :initial_reminder |
| 111 | + type: :initial_reminder, |
| 112 | + current_user: nil |
93 | 113 | )
|
94 | 114 | perform_now
|
95 | 115 | end
|
96 | 116 |
|
97 |
| - it "records a notification" do |
| 117 | + it "records notifications" do |
98 | 118 | expect { perform_now }.to change(ConsentNotification, :count).by(1)
|
99 | 119 | end
|
100 | 120 |
|
|
106 | 126 | perform_now
|
107 | 127 | end
|
108 | 128 | end
|
| 129 | + |
| 130 | + context "when a manual reminder was sent more than three days ago" do |
| 131 | + before do |
| 132 | + create( |
| 133 | + :consent_notification, |
| 134 | + patient: patient_not_sent_reminder, |
| 135 | + session:, |
| 136 | + programmes:, |
| 137 | + type: :initial_reminder, |
| 138 | + sent_at: 4.days.ago, |
| 139 | + sent_by: user |
| 140 | + ) |
| 141 | + end |
| 142 | + |
| 143 | + it "sends automatic reminders" do |
| 144 | + expect { perform_now }.to change(ConsentNotification, :count).by(1) |
| 145 | + |
| 146 | + notification = ConsentNotification.last |
| 147 | + expect(notification.patient).to eq(patient_not_sent_reminder) |
| 148 | + expect(notification.programmes.length).to eq(1) |
| 149 | + expect(notification.automated_reminder?).to be true |
| 150 | + |
| 151 | + programme_types = notification.programmes.map(&:type) |
| 152 | + expect(programme_types).to match_array(programmes.map(&:type)) |
| 153 | + end |
| 154 | + end |
| 155 | + |
| 156 | + context "when a manual reminder was sent less than three days ago" do |
| 157 | + let(:user) { create(:user, team:) } |
| 158 | + |
| 159 | + before do |
| 160 | + create( |
| 161 | + :consent_notification, |
| 162 | + patient: patient_not_sent_reminder, |
| 163 | + session:, |
| 164 | + programmes:, |
| 165 | + type: :initial_reminder, |
| 166 | + sent_at: 2.days.ago, |
| 167 | + sent_by: user |
| 168 | + ) |
| 169 | + end |
| 170 | + |
| 171 | + it "does not send an automatic reminder" do |
| 172 | + expect { perform_now }.not_to change(ConsentNotification, :count) |
| 173 | + end |
| 174 | + end |
109 | 175 | end
|
110 | 176 |
|
111 | 177 | context "six days before the first session with reminders already sent" do
|
|
126 | 192 | end
|
127 | 193 | end
|
128 | 194 |
|
| 195 | + context "five days before the first session, eight before the second" do |
| 196 | + let(:today) { dates.first - 5.days } |
| 197 | + let(:user) { create(:user, team:) } |
| 198 | + |
| 199 | + context "when a manual reminder was sent less than three days before the reminder should have gone out" do |
| 200 | + it "does not send an automatic reminder because the first session's reminder should be skipped" do |
| 201 | + expect { perform_now }.not_to change( |
| 202 | + ConsentNotification.where(patient_id: manual_reminder_patient.id), |
| 203 | + :count |
| 204 | + ) |
| 205 | + end |
| 206 | + end |
| 207 | + end |
| 208 | + |
| 209 | + context "2 days before the first session, six before the second" do |
| 210 | + let(:today) { dates.first - 2.days } |
| 211 | + let(:user) { create(:user, team:) } |
| 212 | + |
| 213 | + context "when a manual reminder was sent less than three days before the reminder should have gone out" do |
| 214 | + it "sends the reminder for the second session" do |
| 215 | + expect { perform_now }.to change(ConsentNotification, :count).by(1) |
| 216 | + end |
| 217 | + end |
| 218 | + end |
| 219 | + |
129 | 220 | context "one day after the first session" do
|
130 | 221 | let(:today) { dates.first + 1.day }
|
131 | 222 |
|
|
153 | 244 | patient: patient_not_sent_reminder,
|
154 | 245 | programmes:,
|
155 | 246 | session:,
|
156 |
| - type: :initial_reminder |
| 247 | + type: :initial_reminder, |
| 248 | + current_user: nil |
157 | 249 | )
|
158 | 250 |
|
159 | 251 | expect(ConsentNotification).to receive(:create_and_send!).once.with(
|
160 | 252 | patient: patient_not_sent_reminder_joined_after_first_date,
|
161 | 253 | programmes:,
|
162 | 254 | session:,
|
163 |
| - type: :initial_reminder |
| 255 | + type: :initial_reminder, |
| 256 | + current_user: nil |
164 | 257 | )
|
165 | 258 |
|
166 | 259 | expect(ConsentNotification).to receive(:create_and_send!).once.with(
|
167 | 260 | patient: patient_with_initial_reminder_sent,
|
168 | 261 | programmes:,
|
169 | 262 | session:,
|
170 |
| - type: :subsequent_reminder |
| 263 | + type: :subsequent_reminder, |
| 264 | + current_user: nil |
171 | 265 | )
|
172 | 266 |
|
173 | 267 | perform_now
|
|
0 commit comments