|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative "../../app/lib/mavis_cli" |
| 4 | + |
| 5 | +describe "mavis vaccination-records sync" do |
| 6 | + context "when the vaccination record exists and has not been synced" do |
| 7 | + it "syncs the vaccination record to the NHS API" do |
| 8 | + given_a_vaccination_record_exists |
| 9 | + and_the_nhs_api_is_available |
| 10 | + when_i_run_the_sync_command |
| 11 | + then_the_vaccination_record_is_synced_to_the_immunisations_api |
| 12 | + end |
| 13 | + end |
| 14 | + |
| 15 | + context "when the vaccination record does not exist" do |
| 16 | + it "displays an error message" do |
| 17 | + when_i_run_the_sync_command_with_an_invalid_id |
| 18 | + then_an_error_message_is_displayed |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + context "when the vaccination record has already been synced" do |
| 23 | + it "displays a message indicating it has already been synced" do |
| 24 | + given_a_synced_vaccination_record_exists |
| 25 | + when_i_run_the_sync_command_for_synced_record |
| 26 | + then_the_already_synced_message_is_displayed |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + private |
| 31 | + |
| 32 | + def given_a_vaccination_record_exists |
| 33 | + organisation = create(:organisation) |
| 34 | + programme = create(:programme, type: "hpv") |
| 35 | + patient = create(:patient, organisation:) |
| 36 | + vaccine = create(:vaccine, :gardasil, programme:) |
| 37 | + batch = create(:batch, vaccine:, expiry: "2023-03-20", name: "X8U375AL") |
| 38 | + |
| 39 | + @vaccination_record = |
| 40 | + create( |
| 41 | + :vaccination_record, |
| 42 | + patient:, |
| 43 | + programme:, |
| 44 | + vaccine:, |
| 45 | + batch:, |
| 46 | + nhs_immunisations_api_synced_at: nil |
| 47 | + ) |
| 48 | + end |
| 49 | + |
| 50 | + def given_a_synced_vaccination_record_exists |
| 51 | + organisation = create(:organisation) |
| 52 | + programme = create(:programme, type: "hpv") |
| 53 | + patient = create(:patient, organisation:) |
| 54 | + @synced_vaccination_record = |
| 55 | + create( |
| 56 | + :vaccination_record, |
| 57 | + patient:, |
| 58 | + programme:, |
| 59 | + nhs_immunisations_api_synced_at: Time.current |
| 60 | + ) |
| 61 | + end |
| 62 | + |
| 63 | + def and_the_nhs_api_is_available |
| 64 | + @nhs_api_request = |
| 65 | + stub_request( |
| 66 | + :post, |
| 67 | + "https://sandbox.api.service.nhs.uk/immunisation-fhir-api/FHIR/R4/Immunization" |
| 68 | + ).with( |
| 69 | + headers: { |
| 70 | + "Content-Type" => "application/fhir+json", |
| 71 | + "Accept" => "application/fhir+json" |
| 72 | + } |
| 73 | + ).to_return(status: 200, body: "", headers: {}) |
| 74 | + end |
| 75 | + |
| 76 | + def when_i_run_the_sync_command |
| 77 | + @output = |
| 78 | + capture_output do |
| 79 | + Dry::CLI.new(MavisCLI).call( |
| 80 | + arguments: ["vaccination-records", "sync", @vaccination_record.id] |
| 81 | + ) |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + def when_i_run_the_sync_command_with_an_invalid_id |
| 86 | + @output = |
| 87 | + capture_output do |
| 88 | + Dry::CLI.new(MavisCLI).call( |
| 89 | + arguments: %w[vaccination-records sync 999999] |
| 90 | + ) |
| 91 | + end |
| 92 | + end |
| 93 | + |
| 94 | + def when_i_run_the_sync_command_for_synced_record |
| 95 | + @output = |
| 96 | + capture_output do |
| 97 | + Dry::CLI.new(MavisCLI).call( |
| 98 | + arguments: [ |
| 99 | + "vaccination-records", |
| 100 | + "sync", |
| 101 | + @synced_vaccination_record.id |
| 102 | + ] |
| 103 | + ) |
| 104 | + end |
| 105 | + end |
| 106 | + |
| 107 | + def then_the_vaccination_record_is_synced_to_the_immunisations_api |
| 108 | + expect(@nhs_api_request).to have_been_made |
| 109 | + expect(@output).to include( |
| 110 | + "Successfully synced vaccination record #{@vaccination_record.id}" |
| 111 | + ) |
| 112 | + end |
| 113 | + |
| 114 | + def then_an_error_message_is_displayed |
| 115 | + expect(@output).to include("Vaccination record with ID 999999 not found") |
| 116 | + end |
| 117 | + |
| 118 | + def then_the_already_synced_message_is_displayed |
| 119 | + expect(@output).to include("has already been synced at") |
| 120 | + end |
| 121 | +end |
0 commit comments