Skip to content

Commit 1cd2f01

Browse files
authored
Merge pull request #6433 from NHSDigital/refactor-imms-methods
Refactor Imms API concern
2 parents e093cd3 + cb4be3d commit 1cd2f01

5 files changed

Lines changed: 106 additions & 113 deletions

File tree

app/lib/nhs/immunisations_api.rb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -258,15 +258,6 @@ def delete_immunisation(vaccination_record)
258258
end
259259
end
260260

261-
def should_be_in_immunisations_api?(vaccination_record)
262-
vaccination_record.kept? &&
263-
vaccination_record.correct_source_for_nhs_immunisations_api? &&
264-
vaccination_record.administered? &&
265-
Flipper.enabled?(:imms_api_sync_job, vaccination_record.programme) &&
266-
vaccination_record.notify_parents != false &&
267-
vaccination_record.patient.not_invalidated?
268-
end
269-
270261
def should_perform_search_for_patient?(patient)
271262
patient.teams.any?(&:has_point_of_care_access?)
272263
end
@@ -355,7 +346,8 @@ def next_sync_action(vaccination_record)
355346
" nhs_immunisations_api_sync_pending_at is nil"
356347
end
357348

358-
should_be_recorded = should_be_in_immunisations_api?(vaccination_record)
349+
should_be_recorded =
350+
vaccination_record.should_be_in_nhs_immunisations_api?
359351
is_recorded = is_recorded_in_immunisations_api?(vaccination_record)
360352

361353
if is_recorded

app/models/vaccination_record.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ class VaccinationRecord < ApplicationRecord
8585
include Confirmable
8686
include Discard::Model
8787
include HasDoseVolume
88+
include NHSImmunisationsAPISync
8889
include Notable
8990
include PendingChangesConcern
9091
include PerformableAtDateAndTime
9192
include PerformableBy
92-
include SyncableToNHSImmunisationsAPI
9393

9494
audited associated_with: :patient
9595

app/models/concerns/syncable_to_nhs_immunisations_api.rb renamed to app/models/vaccination_record/nhs_immunisations_api_sync.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
module SyncableToNHSImmunisationsAPI
3+
module VaccinationRecord::NHSImmunisationsAPISync
44
extend ActiveSupport::Concern
55

66
included do
@@ -52,9 +52,14 @@ def correct_source_for_nhs_immunisations_api?
5252
)
5353
end
5454

55+
def should_be_in_nhs_immunisations_api?
56+
kept? && correct_source_for_nhs_immunisations_api? && administered? &&
57+
Flipper.enabled?(:imms_api_sync_job, programme) &&
58+
notify_parents != false && patient.not_invalidated?
59+
end
60+
5561
def sync_status
56-
should_be_synced =
57-
NHS::ImmunisationsAPI.should_be_in_immunisations_api?(self)
62+
should_be_synced = should_be_in_nhs_immunisations_api?
5863
return :not_synced unless should_be_synced
5964

6065
synced_at = nhs_immunisations_api_synced_at

spec/lib/nhs/immunisations_api_spec.rb

Lines changed: 8 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -661,100 +661,6 @@
661661
end
662662
end
663663

664-
describe "should_be_in_immunisations_api?" do
665-
subject(:should_be_in_immunisations_api) do
666-
described_class.should_be_in_immunisations_api?(vaccination_record)
667-
end
668-
669-
context "when all conditions are met" do
670-
it { should be true }
671-
end
672-
673-
context "when the vaccination record has been discarded" do
674-
before { vaccination_record.discard! }
675-
676-
it { should be false }
677-
end
678-
679-
context "when the vaccination record doesn't have the correct source" do
680-
before do
681-
allow(vaccination_record).to receive(
682-
:correct_source_for_nhs_immunisations_api?
683-
).and_return(false)
684-
end
685-
686-
it { should be false }
687-
end
688-
689-
VaccinationRecord.defined_enums["outcome"].each_key do |outcome|
690-
next if outcome == "administered"
691-
692-
context "the vaccination record outcome is #{outcome}" do
693-
let(:vaccination_record) do
694-
create(
695-
:vaccination_record,
696-
outcome:,
697-
patient:,
698-
nhs_immunisations_api_synced_at:,
699-
nhs_immunisations_api_id:,
700-
nhs_immunisations_api_primary_source:,
701-
nhs_immunisations_api_etag:,
702-
nhs_immunisations_api_sync_pending_at:
703-
)
704-
end
705-
706-
it { should be false }
707-
end
708-
end
709-
710-
context "when the patient has no NHS number" do
711-
before { patient.update(nhs_number: nil) }
712-
713-
it { should be true }
714-
end
715-
716-
context "when the patient has requested that their parents aren't notified" do
717-
before do
718-
create(
719-
:consent,
720-
:given,
721-
:self_consent,
722-
patient:,
723-
programme:,
724-
notify_parents_on_vaccination: false
725-
)
726-
end
727-
728-
let(:notify_parents) { false }
729-
730-
it { should be false }
731-
end
732-
733-
context "when notify_parents is not set" do
734-
let(:notify_parents) { nil }
735-
736-
it { should be true }
737-
end
738-
739-
context "when the patient is invalidated" do
740-
before { patient.update(invalidated_at: Time.current) }
741-
742-
it { should be false }
743-
end
744-
745-
context "when the programme type is not enabled in the feature flag" do
746-
let(:programme) { Programme.menacwy }
747-
let(:vaccine) { programme.vaccines.first }
748-
749-
before do
750-
Flipper.disable(:imms_api_sync_job)
751-
Flipper.enable(:imms_api_sync_job, Programme.hpv)
752-
end
753-
754-
it { should be false }
755-
end
756-
end
757-
758664
describe "next_sync_action" do
759665
subject(:next_sync_action) do
760666
described_class.send(:next_sync_action, vaccination_record)
@@ -791,10 +697,14 @@
791697
end
792698

793699
context "the vaccination record is already in-sync" do
794-
let(:nhs_immunisations_api_synced_at) { 1.second.ago }
795-
let(:nhs_immunisations_api_id) { Random.uuid }
796-
let(:nhs_immunisations_api_primary_source) { true }
797-
let(:nhs_immunisations_api_sync_pending_at) { 2.seconds.ago }
700+
before do
701+
vaccination_record.update_columns(
702+
nhs_immunisations_api_synced_at: 1.second.ago,
703+
nhs_immunisations_api_id: Random.uuid,
704+
nhs_immunisations_api_primary_source: true,
705+
nhs_immunisations_api_sync_pending_at: 2.seconds.ago
706+
)
707+
end
798708

799709
it { should be_nil }
800710
end

spec/models/concerns/syncable_to_nhs_immunisations_api_spec.rb renamed to spec/models/vaccination_record/nhs_immunisations_api_sync_spec.rb

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
describe SyncableToNHSImmunisationsAPI do
3+
describe VaccinationRecord::NHSImmunisationsAPISync do
44
let(:vaccination_record) do
55
build(:vaccination_record, outcome:, programme:, session:, created_at:)
66
end
@@ -521,4 +521,90 @@
521521
end
522522
end
523523
end
524+
525+
describe "#should_be_in_nhs_immunisations_api?" do
526+
subject { vaccination_record.should_be_in_nhs_immunisations_api? }
527+
528+
let(:patient) { create(:patient, session:) }
529+
let(:notify_parents) { true }
530+
let(:vaccination_record) do
531+
create(
532+
:vaccination_record,
533+
outcome:,
534+
programme:,
535+
session:,
536+
patient:,
537+
notify_parents:
538+
)
539+
end
540+
541+
before { Flipper.enable(:imms_api_sync_job, programme) }
542+
543+
context "when all conditions are met" do
544+
it { should be true }
545+
end
546+
547+
context "when the vaccination record has been discarded" do
548+
before { vaccination_record.discard! }
549+
550+
it { should be false }
551+
end
552+
553+
context "when the vaccination record doesn't have the correct source" do
554+
before do
555+
allow(vaccination_record).to receive(
556+
:correct_source_for_nhs_immunisations_api?
557+
).and_return(false)
558+
end
559+
560+
it { should be false }
561+
end
562+
563+
VaccinationRecord.defined_enums["outcome"].each_key do |outcome|
564+
next if outcome == "administered"
565+
566+
context "the vaccination record outcome is #{outcome}" do
567+
let(:vaccination_record) do
568+
create(:vaccination_record, outcome:, programme:, session:, patient:)
569+
end
570+
571+
it { should be false }
572+
end
573+
end
574+
575+
context "when the patient has no NHS number" do
576+
before { patient.update(nhs_number: nil) }
577+
578+
it { should be true }
579+
end
580+
581+
context "when the patient has requested that their parents aren't notified" do
582+
let(:notify_parents) { false }
583+
584+
it { should be false }
585+
end
586+
587+
context "when notify_parents is not set" do
588+
let(:notify_parents) { nil }
589+
590+
it { should be true }
591+
end
592+
593+
context "when the patient is invalidated" do
594+
before { patient.update(invalidated_at: Time.current) }
595+
596+
it { should be false }
597+
end
598+
599+
context "when the programme type is not enabled in the feature flag" do
600+
let(:programme) { Programme.menacwy }
601+
602+
before do
603+
Flipper.disable(:imms_api_sync_job)
604+
Flipper.enable(:imms_api_sync_job, Programme.hpv)
605+
end
606+
607+
it { should be false }
608+
end
609+
end
524610
end

0 commit comments

Comments
 (0)