Skip to content

Commit 057e29d

Browse files
authored
Merge pull request #6507 from NHSDigital/only-sync-when-relevant-fields-change
Reraise "Only sync with Imms API if a relevant field has been updated"
2 parents 201e669 + b1cdf7f commit 057e29d

4 files changed

Lines changed: 114 additions & 75 deletions

File tree

app/lib/fhir_mapper/vaccination_record.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ def initialize(vaccination_record)
1818
@vaccination_record = vaccination_record
1919
end
2020

21+
# If you add or remove fields here, update SYNCED_FIELDS in
22+
# VaccinationRecord::NHSImmunisationsAPISync so that changes to those fields
23+
# correctly trigger or stop triggering a sync to the NHS Immunisations API.
2124
def fhir_record
2225
immunisation = FHIR::Immunization.new(id: nhs_immunisations_api_id)
2326

app/models/vaccination_record/nhs_immunisations_api_sync.rb

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,34 @@
33
module VaccinationRecord::NHSImmunisationsAPISync
44
extend ActiveSupport::Concern
55

6+
# Fields whose changes trigger a sync. Keep in sync with:
7+
# - FHIRMapper::VaccinationRecord#fhir_record (fields that affect the FHIR payload)
8+
# - #should_be_in_nhs_immunisations_api? (fields that affect eligibility)
9+
SYNCED_FIELDS = %w[
10+
uuid
11+
source
12+
outcome
13+
performed_at_date
14+
performed_at_time
15+
created_at
16+
batch_number
17+
batch_expiry
18+
delivery_site
19+
delivery_method
20+
performed_ods_code
21+
dose_sequence
22+
programme_type
23+
full_dose
24+
vaccine_id
25+
patient_id
26+
location_id
27+
performed_by_user_id
28+
performed_by_given_name
29+
performed_by_family_name
30+
discarded_at
31+
notify_parents
32+
].freeze
33+
634
included do
735
scope :with_correct_source_for_nhs_immunisations_api,
836
-> do
@@ -97,10 +125,7 @@ def api_integration_cutoff_date
97125
end
98126

99127
def changes_need_to_be_synced_to_nhs_immunisations_api?
100-
changes.present? && !nhs_immunisations_api_etag_changed? &&
101-
!nhs_immunisations_api_sync_pending_at_changed? &&
102-
!nhs_immunisations_api_synced_at_changed? &&
103-
!nhs_immunisations_api_id_changed?
128+
(changes.keys & SYNCED_FIELDS).any?
104129
end
105130

106131
def touch_nhs_immunisations_api_sync_pending_at

spec/models/vaccination_record/nhs_immunisations_api_sync_spec.rb

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@
218218
describe "#sync_status" do
219219
subject(:sync_status) { vaccination_record.sync_status }
220220

221+
let(:vaccination_record) do
222+
create(:vaccination_record, outcome:, programme:, session:)
223+
end
224+
221225
before { Flipper.enable(:imms_api_sync_job, programme) }
222226

223227
context "when patient has no NHS number" do
@@ -607,4 +611,82 @@
607611
it { should be false }
608612
end
609613
end
614+
615+
describe "#changes_need_to_be_synced_to_nhs_immunisations_api?" do
616+
subject do
617+
vaccination_record.changes_need_to_be_synced_to_nhs_immunisations_api?
618+
end
619+
620+
let(:vaccination_record) do
621+
create(:vaccination_record, programme:, session:)
622+
end
623+
624+
context "when no attributes have changed" do
625+
it { should be false }
626+
end
627+
628+
context "when a FHIR content field changes" do
629+
before { vaccination_record.batch_number = "NEWBATCH" }
630+
631+
it { should be true }
632+
end
633+
634+
context "when outcome changes" do
635+
before { vaccination_record.outcome = :refused }
636+
637+
it { should be true }
638+
end
639+
640+
context "when notify_parents changes" do
641+
before { vaccination_record.notify_parents = false }
642+
643+
it { should be true }
644+
end
645+
646+
context "when discarded_at changes" do
647+
before { vaccination_record.discarded_at = Time.current }
648+
649+
it { should be true }
650+
end
651+
652+
context "when only notes change" do
653+
before { vaccination_record.notes = "Some new note" }
654+
655+
it { should be false }
656+
end
657+
658+
context "when only protocol changes" do
659+
before { vaccination_record.protocol = :psd }
660+
661+
it { should be false }
662+
end
663+
664+
context "when only nhs_immunisations_api_etag changes" do
665+
before { vaccination_record.nhs_immunisations_api_etag = "2" }
666+
667+
it { should be false }
668+
end
669+
670+
context "when only nhs_immunisations_api_sync_pending_at changes" do
671+
before do
672+
vaccination_record.nhs_immunisations_api_sync_pending_at = Time.current
673+
end
674+
675+
it { should be false }
676+
end
677+
678+
context "when only nhs_immunisations_api_synced_at changes" do
679+
before do
680+
vaccination_record.nhs_immunisations_api_synced_at = Time.current
681+
end
682+
683+
it { should be false }
684+
end
685+
686+
context "when only nhs_immunisations_api_id changes" do
687+
before { vaccination_record.nhs_immunisations_api_id = SecureRandom.uuid }
688+
689+
it { should be false }
690+
end
691+
end
610692
end

spec/models/vaccination_record_spec.rb

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -466,77 +466,6 @@
466466
end
467467
end
468468

469-
describe "#changes_need_to_be_synced_to_nhs_immunisations_api?" do
470-
subject do
471-
vaccination_record.send(
472-
:changes_need_to_be_synced_to_nhs_immunisations_api?
473-
)
474-
end
475-
476-
let(:vaccination_record) { create(:vaccination_record) }
477-
478-
context "when the update doesn't change any attributes" do
479-
it { should be(false) }
480-
end
481-
482-
context "when regular fields have been changed" do
483-
before { vaccination_record.notes = "Updated notes" }
484-
485-
it { should be(true) }
486-
end
487-
488-
context "when only nhs_immunisations_api_etag has been changed" do
489-
before { vaccination_record.nhs_immunisations_api_etag = "new-etag" }
490-
491-
it { should be(false) }
492-
end
493-
494-
context "when only nhs_immunisations_api_sync_pending_at has been changed" do
495-
before do
496-
vaccination_record.nhs_immunisations_api_sync_pending_at = Time.current
497-
end
498-
499-
it { should be(false) }
500-
end
501-
502-
context "when only nhs_immunisations_api_synced_at has been changed" do
503-
before do
504-
vaccination_record.nhs_immunisations_api_synced_at = Time.current
505-
end
506-
507-
it { should be(false) }
508-
end
509-
510-
context "when only nhs_immunisations_api_id has been changed" do
511-
before { vaccination_record.nhs_immunisations_api_id = "new-id" }
512-
513-
it { should be(false) }
514-
end
515-
516-
context "when both regular fields and nhs_immunisations_api fields have been changed" do
517-
before do
518-
vaccination_record.assign_attributes(
519-
notes: "Updated notes",
520-
nhs_immunisations_api_etag: "new-etag"
521-
)
522-
end
523-
524-
it { should be(false) }
525-
end
526-
527-
context "when a regular field and multiple nhs_immunisations_api fields have been changed" do
528-
before do
529-
vaccination_record.assign_attributes(
530-
outcome: :refused,
531-
nhs_immunisations_api_etag: "new-etag",
532-
nhs_immunisations_api_sync_pending_at: Time.current
533-
)
534-
end
535-
536-
it { should be(false) }
537-
end
538-
end
539-
540469
describe "#for_academic_year" do
541470
before { vaccination_record.save! }
542471

0 commit comments

Comments
 (0)