|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +describe Careplus::AutomatedReportSender do |
| 4 | + subject(:call) { described_class.call(team_id: team.id) } |
| 5 | + |
| 6 | + let(:team) do |
| 7 | + create(:team, :with_careplus_enabled, programmes: Programme.all) |
| 8 | + end |
| 9 | + let(:programme) { Programme.hpv } |
| 10 | + let(:session) { create(:session, team:, programmes: [programme]) } |
| 11 | + let(:endpoint) do |
| 12 | + "#{Settings.careplus.base_url}/#{team.careplus_namespace}/soap.SchImms.cls" |
| 13 | + end |
| 14 | + let(:response_status) { 200 } |
| 15 | + let(:response_body) { "<result>OK</result>" } |
| 16 | + let(:yesterday) { Date.new(2025, 8, 31) } |
| 17 | + let(:yesterday_academic_year) { yesterday.academic_year } |
| 18 | + |
| 19 | + before do |
| 20 | + stub_request(:post, endpoint).to_return( |
| 21 | + status: response_status, |
| 22 | + body: response_body |
| 23 | + ) |
| 24 | + end |
| 25 | + |
| 26 | + around { |example| travel_to(Date.new(2025, 9, 1)) { example.run } } |
| 27 | + |
| 28 | + it "creates, sends, and stores a sent report with linked vaccination records" do |
| 29 | + record = |
| 30 | + create( |
| 31 | + :vaccination_record, |
| 32 | + session:, |
| 33 | + programme:, |
| 34 | + performed_at: yesterday, |
| 35 | + created_at: yesterday, |
| 36 | + updated_at: yesterday |
| 37 | + ) |
| 38 | + |
| 39 | + expect { call }.to change(CareplusReport, :count).by(1).and( |
| 40 | + change(CareplusReportVaccinationRecord, :count).by(1) |
| 41 | + ) |
| 42 | + |
| 43 | + report = CareplusReport.last |
| 44 | + |
| 45 | + expect(report).to have_attributes( |
| 46 | + team:, |
| 47 | + academic_year: yesterday_academic_year, |
| 48 | + date_from: yesterday, |
| 49 | + date_to: yesterday, |
| 50 | + status: "sent" |
| 51 | + ) |
| 52 | + expect(report.sent_at).to be_present |
| 53 | + expect(report.vaccination_records).to contain_exactly(record) |
| 54 | + expect(WebMock).to have_requested(:post, endpoint).once |
| 55 | + end |
| 56 | + |
| 57 | + it "uses yesterday and its academic year for the automated export scope" do |
| 58 | + create( |
| 59 | + :vaccination_record, |
| 60 | + session:, |
| 61 | + programme:, |
| 62 | + performed_at: yesterday, |
| 63 | + created_at: yesterday, |
| 64 | + updated_at: yesterday |
| 65 | + ) |
| 66 | + |
| 67 | + expect(Reports::AutomatedCareplusExporter).to receive( |
| 68 | + :vaccination_records_scope |
| 69 | + ).with( |
| 70 | + team:, |
| 71 | + academic_year: yesterday_academic_year, |
| 72 | + start_date: yesterday, |
| 73 | + end_date: yesterday |
| 74 | + ).and_call_original |
| 75 | + |
| 76 | + call |
| 77 | + end |
| 78 | + |
| 79 | + it "splits yesterday's scope into batches of 10000 records" do |
| 80 | + stub_const("#{described_class}::BATCH_SIZE", 2) |
| 81 | + records = |
| 82 | + Array.new(3) do |
| 83 | + create( |
| 84 | + :vaccination_record, |
| 85 | + session:, |
| 86 | + programme:, |
| 87 | + performed_at: yesterday, |
| 88 | + created_at: yesterday, |
| 89 | + updated_at: yesterday |
| 90 | + ) |
| 91 | + end |
| 92 | + |
| 93 | + expect { call }.to change(CareplusReport, :count).by(2).and( |
| 94 | + change(CareplusReportVaccinationRecord, :count).by(3) |
| 95 | + ) |
| 96 | + |
| 97 | + expect(WebMock).to have_requested(:post, endpoint).twice |
| 98 | + expect( |
| 99 | + CareplusReport |
| 100 | + .order(:id) |
| 101 | + .map { |report| report.vaccination_records.count } |
| 102 | + ).to eq([2, 1]) |
| 103 | + expect( |
| 104 | + CareplusReport |
| 105 | + .joins(:vaccination_records) |
| 106 | + .distinct |
| 107 | + .flat_map(&:vaccination_records) |
| 108 | + ).to match_array(records) |
| 109 | + end |
| 110 | + |
| 111 | + context "when CarePlus returns a failure response" do |
| 112 | + let(:response_status) { 500 } |
| 113 | + let(:response_body) { "<fault>Error</fault>" } |
| 114 | + |
| 115 | + it "marks the report as failed, keeps linked vaccination records, and raises for retry" do |
| 116 | + record = |
| 117 | + create( |
| 118 | + :vaccination_record, |
| 119 | + session:, |
| 120 | + programme:, |
| 121 | + performed_at: yesterday, |
| 122 | + created_at: yesterday, |
| 123 | + updated_at: yesterday |
| 124 | + ) |
| 125 | + |
| 126 | + expect { call }.to raise_error( |
| 127 | + described_class::FailedResponseError, |
| 128 | + "CarePlus request failed with HTTP 500: " |
| 129 | + ).and change(CareplusReport, :count).by(1).and( |
| 130 | + change(CareplusReportVaccinationRecord, :count).by(1) |
| 131 | + ) |
| 132 | + |
| 133 | + report = CareplusReport.last |
| 134 | + expect(report).to have_attributes(status: "failed") |
| 135 | + expect(report.sent_at).to be_present |
| 136 | + expect(report.vaccination_records).to contain_exactly(record) |
| 137 | + end |
| 138 | + end |
| 139 | + |
| 140 | + context "when CarePlus raises an error" do |
| 141 | + before { stub_request(:post, endpoint).to_raise(Timeout::Error) } |
| 142 | + |
| 143 | + it "marks the report as failed and keeps linked vaccination records before re-raising" do |
| 144 | + record = |
| 145 | + create( |
| 146 | + :vaccination_record, |
| 147 | + session:, |
| 148 | + programme:, |
| 149 | + performed_at: yesterday, |
| 150 | + created_at: yesterday, |
| 151 | + updated_at: yesterday |
| 152 | + ) |
| 153 | + |
| 154 | + expect { call }.to raise_error(Timeout::Error) |
| 155 | + |
| 156 | + report = CareplusReport.last |
| 157 | + expect(report).to have_attributes(status: "failed") |
| 158 | + expect(report.sent_at).to be_present |
| 159 | + expect(report.vaccination_records).to contain_exactly(record) |
| 160 | + end |
| 161 | + end |
| 162 | + |
| 163 | + context "when the team is no longer eligible for automated reports" do |
| 164 | + before { team.update!(careplus_username: nil) } |
| 165 | + |
| 166 | + it "does nothing" do |
| 167 | + expect { call }.not_to change(CareplusReport, :count) |
| 168 | + end |
| 169 | + end |
| 170 | +end |
0 commit comments