Skip to content

Commit bbe2491

Browse files
committed
Add mavis vaccination-records sync cli command
Jira-Issue: MAV-1477
1 parent c8a27f5 commit bbe2491

3 files changed

Lines changed: 159 additions & 0 deletions

File tree

app/lib/mavis_cli.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ def self.progress_bar(total)
2424
require_relative "mavis_cli/gias/check_import"
2525
require_relative "mavis_cli/gias/download"
2626
require_relative "mavis_cli/gias/import"
27+
require_relative "mavis_cli/vaccination_records/sync"
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
module MavisCLI
4+
module VaccinationRecords
5+
class Sync < Dry::CLI::Command
6+
desc "Sync a vaccination record to NHS Immunisations API"
7+
argument :vaccination_record_id,
8+
required: true,
9+
desc: "ID of vaccination record to sync"
10+
11+
def call(vaccination_record_id:, **)
12+
MavisCLI.load_rails
13+
14+
vaccination_record =
15+
::VaccinationRecord.find_by(id: vaccination_record_id)
16+
17+
if vaccination_record.nil?
18+
puts "Vaccination record with ID #{vaccination_record_id} not found"
19+
return
20+
end
21+
22+
if vaccination_record.nhs_immunisations_api_synced_at.present?
23+
puts "Vaccination record #{vaccination_record_id} has already been" \
24+
" synced at #{vaccination_record.nhs_immunisations_api_synced_at}"
25+
return
26+
end
27+
28+
SyncVaccinationRecordToNHSJob.perform_now(vaccination_record)
29+
puts "Successfully synced vaccination record #{vaccination_record_id}"
30+
end
31+
end
32+
end
33+
34+
register "vaccination-records" do |prefix|
35+
prefix.register "sync", VaccinationRecords::Sync
36+
end
37+
end
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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

Comments
 (0)