|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative "../../app/lib/mavis_cli" |
| 4 | + |
| 5 | +describe "mavis reporting-api update-vaccination-events" do |
| 6 | + it "generates Reporting API Vaccination Event records" do |
| 7 | + given_a_team_exists |
| 8 | + and_there_is_a_patient_in_a_session |
| 9 | + and_there_are_some_vaccination_records |
| 10 | + when_i_run_the_reporting_api_update_vaccination_events_command |
| 11 | + then_reporting_api_vaccination_events_are_created |
| 12 | + end |
| 13 | + |
| 14 | + it "updates existing Reporting API Vaccination Event records" do |
| 15 | + given_a_team_exists |
| 16 | + and_there_is_a_patient_in_a_session |
| 17 | + and_there_are_some_vaccination_records |
| 18 | + and_the_vaccination_records_already_have_reporting_api_vaccination_events |
| 19 | + when_i_run_the_reporting_api_update_vaccination_events_command |
| 20 | + then_it_does_not_generate_new_vaccination_event_records |
| 21 | + and_it_updates_the_existing_vaccination_event_records |
| 22 | + end |
| 23 | + |
| 24 | + it "only affects Vaccination Records created after the given from_datetime" do |
| 25 | + given_a_team_exists |
| 26 | + and_there_is_a_patient_in_a_session |
| 27 | + and_there_are_some_vaccination_records |
| 28 | + when_i_run_the_reporting_api_update_vaccination_events_command_with_a_from_datetime |
| 29 | + then_event_records_are_only_created_for_vaccination_records_created_after_the_given_from_datetime |
| 30 | + end |
| 31 | + |
| 32 | + def given_a_team_exists |
| 33 | + @programme = |
| 34 | + Programme.find_by(type: "hpv") || create(:programme, type: "hpv") |
| 35 | + @team = create(:team, programmes: [@programme]) |
| 36 | + end |
| 37 | + |
| 38 | + def nurse |
| 39 | + User.find_by(fallback_role: :nurse) || create(:nurse) |
| 40 | + end |
| 41 | + |
| 42 | + def and_there_is_a_patient_in_a_session |
| 43 | + subteam = create(:subteam, team: @team) |
| 44 | + location = create(:generic_clinic, subteam:) |
| 45 | + @session = |
| 46 | + create(:session, team: @team, programmes: [@programme], location:) |
| 47 | + parent = create(:parent) |
| 48 | + @patient = |
| 49 | + create( |
| 50 | + :patient, |
| 51 | + :consent_given_triage_not_needed, |
| 52 | + team: @team, |
| 53 | + session: @session, |
| 54 | + programmes: [@programme], |
| 55 | + parents: [parent] |
| 56 | + ) |
| 57 | + @other_patient = |
| 58 | + create( |
| 59 | + :patient, |
| 60 | + :consent_given_triage_not_needed, |
| 61 | + team: @team, |
| 62 | + session: @session, |
| 63 | + programmes: [@programme], |
| 64 | + parents: [parent] |
| 65 | + ) |
| 66 | + end |
| 67 | + |
| 68 | + def and_there_are_some_vaccination_records |
| 69 | + @vr1 = |
| 70 | + create( |
| 71 | + :vaccination_record, |
| 72 | + patient: @patient, |
| 73 | + session: @session, |
| 74 | + location: @session.location, |
| 75 | + performed_by: nurse, |
| 76 | + programme: @programme, |
| 77 | + created_at: Time.current - 1.day, |
| 78 | + updated_at: Time.current - 1.day |
| 79 | + ) |
| 80 | + @vr2 = |
| 81 | + create( |
| 82 | + :vaccination_record, |
| 83 | + patient: @other_patient, |
| 84 | + session: @session, |
| 85 | + location: @session.location, |
| 86 | + performed_by: nurse, |
| 87 | + programme: @programme, |
| 88 | + created_at: Time.current - 4.hours, |
| 89 | + updated_at: Time.current - 4.hours |
| 90 | + ) |
| 91 | + end |
| 92 | + |
| 93 | + def and_the_vaccination_records_already_have_reporting_api_vaccination_events |
| 94 | + VaccinationRecord.all.find_each( |
| 95 | + &:create_or_update_reporting_api_vaccination_event! |
| 96 | + ) |
| 97 | + end |
| 98 | + |
| 99 | + def when_i_run_the_reporting_api_update_vaccination_events_command |
| 100 | + @vaccination_events_count_before = ReportingAPI::VaccinationEvent.count |
| 101 | + |
| 102 | + Dry::CLI.new(MavisCLI).call( |
| 103 | + arguments: %w[reporting-api update-vaccination-events] |
| 104 | + ) |
| 105 | + end |
| 106 | + |
| 107 | + def when_i_run_the_reporting_api_update_vaccination_events_command_with_a_from_datetime |
| 108 | + @vaccination_events_count_before = ReportingAPI::VaccinationEvent.count |
| 109 | + |
| 110 | + Dry::CLI.new(MavisCLI).call( |
| 111 | + arguments: [ |
| 112 | + "reporting-api", |
| 113 | + "update-vaccination-events", |
| 114 | + "--from", |
| 115 | + (Time.current - 8.hours).iso8601.to_s |
| 116 | + ] |
| 117 | + ) |
| 118 | + end |
| 119 | + |
| 120 | + def then_reporting_api_vaccination_events_are_created |
| 121 | + expect(ReportingAPI::VaccinationEvent.count).to be > |
| 122 | + @vaccination_events_count_before |
| 123 | + end |
| 124 | + |
| 125 | + def then_event_records_are_only_created_for_vaccination_records_created_after_the_given_from_datetime |
| 126 | + expect(ReportingAPI::VaccinationEvent.count).to eq(1) |
| 127 | + expect(ReportingAPI::VaccinationEvent.last.source_id).to eq(@vr2.id) |
| 128 | + end |
| 129 | + |
| 130 | + def then_it_does_not_generate_new_vaccination_event_records |
| 131 | + expect( |
| 132 | + ReportingAPI::VaccinationEvent.count |
| 133 | + ).to eq @vaccination_events_count_before |
| 134 | + end |
| 135 | + |
| 136 | + def and_it_updates_the_existing_vaccination_event_records |
| 137 | + expect(ReportingAPI::VaccinationEvent.pluck(:updated_at)).to all be_within( |
| 138 | + 1.second |
| 139 | + ).of(Time.current) |
| 140 | + end |
| 141 | +end |
0 commit comments