|
4 | 4 | subject(:call) { described_class.call(team_id: team.id) } |
5 | 5 |
|
6 | 6 | let(:team) do |
7 | | - create(:team, :with_careplus_enabled, programmes: Programme.all) |
| 7 | + create( |
| 8 | + :team, |
| 9 | + :with_careplus_enabled, |
| 10 | + programmes: Programme.all, |
| 11 | + careplus_integration_enabled_at: Time.zone.local(2025, 8, 28, 10) |
| 12 | + ) |
8 | 13 | end |
9 | 14 | let(:programme) { Programme.hpv } |
10 | 15 | let(:session) { create(:session, team:, programmes: [programme]) } |
|
167 | 172 | expect { call }.not_to change(CareplusReport, :count) |
168 | 173 | end |
169 | 174 | end |
| 175 | + |
| 176 | + context "when a patient gains an NHS number yesterday" do |
| 177 | + it "includes records created after the integration was enabled" do |
| 178 | + patient = |
| 179 | + create( |
| 180 | + :patient, |
| 181 | + session:, |
| 182 | + nhs_number_first_added_at: Time.zone.local(2025, 8, 31, 9) |
| 183 | + ) |
| 184 | + record = |
| 185 | + create( |
| 186 | + :vaccination_record, |
| 187 | + patient:, |
| 188 | + session:, |
| 189 | + programme:, |
| 190 | + performed_at: Date.new(2025, 8, 29), |
| 191 | + created_at: Time.zone.local(2025, 8, 29, 12), |
| 192 | + updated_at: Time.zone.local(2025, 8, 29, 12) |
| 193 | + ) |
| 194 | + |
| 195 | + expect { call }.to change(CareplusReport, :count).by(1) |
| 196 | + |
| 197 | + expect(CareplusReport.last.vaccination_records).to contain_exactly(record) |
| 198 | + end |
| 199 | + |
| 200 | + it "does not include records created before the integration was enabled" do |
| 201 | + team.update!( |
| 202 | + careplus_integration_enabled_at: Time.zone.local(2025, 8, 30, 10) |
| 203 | + ) |
| 204 | + |
| 205 | + patient = |
| 206 | + create( |
| 207 | + :patient, |
| 208 | + session:, |
| 209 | + nhs_number_first_added_at: Time.zone.local(2025, 8, 31, 9) |
| 210 | + ) |
| 211 | + create( |
| 212 | + :vaccination_record, |
| 213 | + patient:, |
| 214 | + session:, |
| 215 | + programme:, |
| 216 | + performed_at: Date.new(2025, 8, 29), |
| 217 | + created_at: Time.zone.local(2025, 8, 29, 12), |
| 218 | + updated_at: Time.zone.local(2025, 8, 29, 12) |
| 219 | + ) |
| 220 | + |
| 221 | + expect { call }.not_to change(CareplusReport, :count) |
| 222 | + end |
| 223 | + |
| 224 | + it "deduplicates records that also changed yesterday" do |
| 225 | + patient = |
| 226 | + create( |
| 227 | + :patient, |
| 228 | + session:, |
| 229 | + nhs_number_first_added_at: Time.zone.local(2025, 8, 31, 9) |
| 230 | + ) |
| 231 | + record = |
| 232 | + create( |
| 233 | + :vaccination_record, |
| 234 | + patient:, |
| 235 | + session:, |
| 236 | + programme:, |
| 237 | + performed_at: yesterday, |
| 238 | + created_at: Time.zone.local(2025, 8, 29, 12), |
| 239 | + updated_at: Time.zone.local(2025, 8, 31, 12) |
| 240 | + ) |
| 241 | + |
| 242 | + expect { call }.to change(CareplusReport, :count).by(1).and( |
| 243 | + change(CareplusReportVaccinationRecord, :count).by(1) |
| 244 | + ) |
| 245 | + |
| 246 | + expect(CareplusReport.last.vaccination_records).to contain_exactly(record) |
| 247 | + expect(WebMock).to have_requested(:post, endpoint).once |
| 248 | + end |
| 249 | + end |
| 250 | + |
| 251 | + context "when CarePlus is configured but not manually enabled" do |
| 252 | + before { team.update!(careplus_integration_enabled_at: nil) } |
| 253 | + |
| 254 | + it "does nothing" do |
| 255 | + expect { call }.not_to change(CareplusReport, :count) |
| 256 | + end |
| 257 | + end |
170 | 258 | end |
0 commit comments