Skip to content

Commit 353df87

Browse files
authored
Merge pull request #3896 from nhsuk/send-vaccination-records-to-imms-api
Send vaccination records to imms api
2 parents 163a083 + b4186b9 commit 353df87

10 files changed

Lines changed: 321 additions & 0 deletions

app/controllers/draft_vaccination_records_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ def handle_confirm
120120

121121
send_vaccination_confirmation(@vaccination_record) if should_notify_parents
122122

123+
EnqueueSyncVaccinationRecordToNHS.call(@vaccination_record)
124+
123125
# In case the user navigates back to try and edit the newly created
124126
# vaccination record.
125127
@draft_vaccination_record.update!(editing_id: @vaccination_record.id)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
module EnqueueSyncVaccinationRecordToNHS
4+
PROGRAMME_TYPES = %w[flu hpv].freeze
5+
6+
def self.call(vaccination_record)
7+
return unless Flipper.enabled?(:sync_vaccination_records_to_nhs_on_create)
8+
9+
vaccination_records =
10+
if vaccination_record.respond_to?(:klass)
11+
vaccination_record
12+
.recorded_in_service
13+
.administered
14+
.where(programmes: { type: PROGRAMME_TYPES })
15+
.includes(:programme)
16+
elsif vaccination_record.programme.type.in?(PROGRAMME_TYPES) &&
17+
vaccination_record.administered? &&
18+
vaccination_record.recorded_in_service?
19+
Array(vaccination_record)
20+
else
21+
return
22+
end
23+
24+
vaccination_records.each do |vaccination_record|
25+
SyncVaccinationRecordToNHSJob.perform_later(vaccination_record)
26+
end
27+
end
28+
end

app/models/immunisation_import.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,7 @@ def count_column(vaccination_record)
109109

110110
def postprocess_rows!
111111
StatusUpdater.call(patient: patients)
112+
113+
EnqueueSyncVaccinationRecordToNHS.call(vaccination_records)
112114
end
113115
end

config/feature_flags.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ offline_working: Prototype support for using Mavis offline.
1111

1212
immunisations_fhir_api_integration: Master switch to control communications with
1313
NHS Immunistaions FHIR API.
14+
15+
sync_vaccination_records_to_nhs_on_create: Send new vaccinations recorded by
16+
nurses to NHS Immunisations FHIR API.

spec/features/flu_vaccination_administered_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
given_i_am_signed_in_with_flu_programme
88
and_there_is_a_flu_session_today_with_two_patients_ready_to_vaccinate
99
and_there_are_nasal_and_injection_batches
10+
and_sync_vaccination_records_to_nhs_on_create_feature_is_enabled
1011

1112
when_i_go_to_the_nasal_only_patient
1213
and_i_record_that_the_patient_has_been_vaccinated_with_nasal_spray
1314
then_i_see_the_check_and_confirm_page_for_nasal_spray
1415
and_i_get_confirmation_after_recording
16+
and_the_vaccination_record_is_synced_to_nhs
1517
end
1618

1719
scenario "Administered with injection" do
@@ -91,6 +93,10 @@ def and_there_are_nasal_and_injection_batches
9193
)
9294
end
9395

96+
def and_sync_vaccination_records_to_nhs_on_create_feature_is_enabled
97+
Flipper.enable(:sync_vaccination_records_to_nhs_on_create)
98+
end
99+
94100
def when_i_go_to_the_nasal_only_patient
95101
visit session_record_path(@session)
96102
@patient = @nasal_patient
@@ -170,4 +176,8 @@ def and_i_pick_a_batch_for_injection
170176
choose @injection_batch.name
171177
click_button "Continue"
172178
end
179+
180+
def and_the_vaccination_record_is_synced_to_nhs
181+
assert_enqueued_with(job: SyncVaccinationRecordToNHSJob)
182+
end
173183
end

spec/features/hpv_vaccination_administered_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
scenario "Administered with common delivery site" do
77
given_i_am_signed_in
8+
and_sync_vaccination_records_to_nhs_on_create_feature_is_enabled
89

910
when_i_go_to_a_patient_that_is_ready_to_vaccinate
1011
and_i_fill_in_pre_screening_questions
@@ -41,6 +42,7 @@
4142
then_i_see_a_success_message
4243
and_i_can_no_longer_vaccinate_the_patient
4344
and_i_no_longer_see_the_patient_in_the_record_tab
45+
and_the_vaccination_record_is_synced_to_nhs
4446

4547
when_i_go_back
4648
and_i_save_changes
@@ -104,6 +106,10 @@ def given_i_am_signed_in
104106
sign_in organisation.users.first
105107
end
106108

109+
def and_sync_vaccination_records_to_nhs_on_create_feature_is_enabled
110+
Flipper.enable(:sync_vaccination_records_to_nhs_on_create)
111+
end
112+
107113
def when_i_go_to_a_patient_that_is_ready_to_vaccinate
108114
visit session_record_path(@session)
109115
click_link @patient.full_name
@@ -242,4 +248,8 @@ def and_a_text_is_sent_to_the_parent_confirming_the_vaccination
242248
:vaccination_administered_hpv
243249
)
244250
end
251+
252+
def and_the_vaccination_record_is_synced_to_nhs
253+
assert_enqueued_with(job: SyncVaccinationRecordToNHSJob)
254+
end
245255
end

spec/features/menacwy_vaccination_administered_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
scenario "Administered" do
77
given_i_am_signed_in
8+
and_sync_vaccination_records_to_nhs_on_create_feature_is_enabled
89

910
when_i_go_to_a_patient_that_is_ready_to_vaccinate
1011
and_i_record_that_the_patient_has_been_vaccinated
@@ -37,6 +38,7 @@
3738
when_i_confirm_the_details
3839
then_i_see_a_success_message
3940
and_i_no_longer_see_the_patient_in_the_record_tab
41+
and_the_vaccination_record_is_not_synced_to_nhs
4042

4143
when_i_go_back
4244
and_i_save_changes
@@ -82,6 +84,10 @@ def given_i_am_signed_in
8284
sign_in organisation.users.first
8385
end
8486

87+
def and_sync_vaccination_records_to_nhs_on_create_feature_is_enabled
88+
Flipper.enable(:sync_vaccination_records_to_nhs_on_create)
89+
end
90+
8591
def when_i_go_to_a_patient_that_is_ready_to_vaccinate
8692
visit session_record_path(@session)
8793
click_link @patient.full_name
@@ -200,4 +206,8 @@ def and_a_text_is_sent_to_the_parent_confirming_the_vaccination
200206
:vaccination_administered_menacwy
201207
)
202208
end
209+
210+
def and_the_vaccination_record_is_not_synced_to_nhs
211+
assert_no_enqueued_jobs(only: SyncVaccinationRecordToNHSJob)
212+
end
203213
end

spec/features/td_ipv_vaccination_administered_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
scenario "Administered" do
77
given_i_am_signed_in
8+
and_sync_vaccination_records_to_nhs_on_create_feature_is_enabled
89

910
when_i_go_to_a_patient_that_is_ready_to_vaccinate
1011
and_i_record_that_the_patient_has_been_vaccinated
@@ -37,6 +38,7 @@
3738
when_i_confirm_the_details
3839
then_i_see_a_success_message
3940
and_i_no_longer_see_the_patient_in_the_record_tab
41+
and_the_vaccination_record_is_not_synced_to_nhs
4042

4143
when_i_go_back
4244
and_i_save_changes
@@ -82,6 +84,10 @@ def given_i_am_signed_in
8284
sign_in organisation.users.first
8385
end
8486

87+
def and_sync_vaccination_records_to_nhs_on_create_feature_is_enabled
88+
Flipper.enable(:sync_vaccination_records_to_nhs_on_create)
89+
end
90+
8591
def when_i_go_to_a_patient_that_is_ready_to_vaccinate
8692
visit session_record_path(@session)
8793
click_link @patient.full_name
@@ -200,4 +206,8 @@ def and_a_text_is_sent_to_the_parent_confirming_the_vaccination
200206
:vaccination_administered_td_ipv
201207
)
202208
end
209+
210+
def and_the_vaccination_record_is_not_synced_to_nhs
211+
assert_no_enqueued_jobs(only: SyncVaccinationRecordToNHSJob)
212+
end
203213
end
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# frozen_string_literal: true
2+
3+
describe EnqueueSyncVaccinationRecordToNHS do
4+
context "when the feature flag is disabled" do
5+
before { Flipper.disable(:sync_vaccination_records_to_nhs_on_create) }
6+
7+
let(:vaccination_record) { create(:vaccination_record) }
8+
9+
it "does not enqueue the job" do
10+
expect {
11+
described_class.call(vaccination_record)
12+
}.not_to have_enqueued_job(SyncVaccinationRecordToNHSJob)
13+
end
14+
end
15+
16+
context "when the feature flag is enabled" do
17+
before { Flipper.enable(:sync_vaccination_records_to_nhs_on_create) }
18+
19+
let(:outcome) { "administered" }
20+
let(:programme) { create(:programme, type: "flu") }
21+
let(:session) { create(:session, programmes: [programme]) }
22+
let(:vaccination_record) do
23+
create(:vaccination_record, outcome:, programme:, session:)
24+
end
25+
26+
context "with a single vaccination record" do
27+
it "enqueues the job if the vaccination record is elligible to sync" do
28+
expect {
29+
described_class.call(vaccination_record)
30+
}.to have_enqueued_job(SyncVaccinationRecordToNHSJob)
31+
end
32+
end
33+
34+
VaccinationRecord.defined_enums["outcome"].each_key do |outcome|
35+
next if outcome == "administered"
36+
37+
context "when the vaccination record outcome is #{outcome}" do
38+
let(:outcome) { outcome }
39+
40+
it "does not enqueue the job" do
41+
expect {
42+
described_class.call(vaccination_record)
43+
}.not_to have_enqueued_job(SyncVaccinationRecordToNHSJob)
44+
end
45+
end
46+
end
47+
48+
Programme.defined_enums["type"].each_key do |programme_type|
49+
next if programme_type.in? %w[flu hpv]
50+
51+
context "when the programme type is #{programme_type}" do
52+
let(:programme) { create(:programme, type: programme_type) }
53+
54+
it "does not enqueue the job" do
55+
expect {
56+
described_class.call(vaccination_record)
57+
}.not_to have_enqueued_job(SyncVaccinationRecordToNHSJob)
58+
end
59+
end
60+
end
61+
62+
context "with a vaccinaton record relation" do
63+
# The strategy is to create a vaccination record for each of the various
64+
# variations, and test that only the correct ones are allowed through
65+
66+
before do
67+
# Generate historic vaccination record (no session)
68+
create(:vaccination_record, outcome:, programme:)
69+
70+
# Generate vaccination records for all programme types
71+
Programme.defined_enums["type"].each_key do |programme_type|
72+
next if programme_type == "flu"
73+
programme = create(:programme, type: programme_type)
74+
create(:vaccination_record, outcome: "refused", session:, programme:)
75+
end
76+
77+
# Generate vaccination records for all outcomes
78+
VaccinationRecord.defined_enums["outcome"].each_key do |outcome|
79+
next if outcome == "administered"
80+
create(:vaccination_record, outcome:, session:, programme:)
81+
end
82+
end
83+
84+
let(:flu_programme) { Programme.flu.first || create(:programme, :flu) }
85+
let(:hpv_programme) { Programme.hpv.first || create(:programme, :hpv) }
86+
let!(:flu_vaccination_record) do
87+
create(
88+
:vaccination_record,
89+
programme: flu_programme,
90+
session:,
91+
outcome: :administered
92+
)
93+
end
94+
let!(:hpv_vaccination_record) do
95+
create(
96+
:vaccination_record,
97+
programme: hpv_programme,
98+
session:,
99+
outcome: :administered
100+
)
101+
end
102+
103+
it "enqueues the job for each eligible vaccination record" do
104+
expect {
105+
described_class.call(VaccinationRecord.all)
106+
}.to have_enqueued_job(SyncVaccinationRecordToNHSJob).exactly(2).times
107+
end
108+
109+
it "enqueues the eligible flu job" do
110+
expect {
111+
described_class.call(VaccinationRecord.all)
112+
}.to have_enqueued_job(SyncVaccinationRecordToNHSJob).with(
113+
flu_vaccination_record
114+
)
115+
end
116+
117+
it "enqueues the eligible hpv job" do
118+
expect {
119+
described_class.call(VaccinationRecord.all)
120+
}.to have_enqueued_job(SyncVaccinationRecordToNHSJob).with(
121+
hpv_vaccination_record
122+
)
123+
end
124+
end
125+
end
126+
end

0 commit comments

Comments
 (0)