Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def additional_information_text
case sync_status
when :not_synced
is_not_a_synced_programme =
!vaccination_record.programme.can_write_to_immunisations_api?
!vaccination_record.programme.can_sync_to_immunisations_api?
if is_not_a_synced_programme
"Records are currently not synced for this programme"
elsif notify_parents == false
Expand Down
77 changes: 77 additions & 0 deletions app/jobs/search_vaccination_records_in_nhs_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# frozen_string_literal: true

class SearchVaccinationRecordsInNHSJob < ApplicationJob
include ImmunisationsAPIThrottlingConcern

queue_as :immunisation_api

def perform(patient)
tx_id = SecureRandom.urlsafe_base64(16)
SemanticLogger.tagged(tx_id:, job_id: provider_job_id || job_id) do
Sentry.set_tags(tx_id:, job_id: provider_job_id || job_id)

return unless Flipper.enabled?(:imms_api_search_job)

programmes = Programme.can_search_in_immunisations_api

if patient.nhs_number.nil?
incoming_vaccination_records = []
else
fhir_bundle =
NHS::ImmunisationsAPI.search_immunisations(patient, programmes:)

incoming_vaccination_records =
extract_vaccination_records(fhir_bundle).map do |fhir_record|
if FHIRMapper::VaccinationRecord::MAVIS_SYSTEM_NAME.in?(
fhir_record.identifier.map(&:system)
)
next
end

FHIRMapper::VaccinationRecord.from_fhir_record(
fhir_record,
patient:
)
end
incoming_vaccination_records = incoming_vaccination_records.compact
end

existing_vaccination_records =
patient
.vaccination_records
.includes(:identity_check)
.where(programme: programmes, source: :nhs_immunisations_api)

existing_vaccination_records.find_each do |vaccination_record|
incoming_vaccination_record =
incoming_vaccination_records.find do
it.nhs_immunisations_api_id ==
vaccination_record.nhs_immunisations_api_id
end

if incoming_vaccination_record
vaccination_record.update!(
incoming_vaccination_record
.attributes
.except("id", "uuid", "created_at")
.merge(updated_at: Time.current)
)

incoming_vaccination_records.delete(incoming_vaccination_record)
else
vaccination_record.destroy!
end
end

# Remaining incoming_vaccination_records are new
incoming_vaccination_records.each(&:save!)
end
end

def extract_vaccination_records(fhir_bundle)
fhir_bundle
.entry
.map { it.resource if it.resource.resourceType == "Immunization" }
.compact
end
end
10 changes: 4 additions & 6 deletions app/lib/fhir_mapper/vaccination_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ class UnknownVaccine < StandardError

delegate_missing_to :@vaccination_record

MAVIS_SYSTEM_NAME =
"http://manage-vaccinations-in-schools.nhs.uk/vaccination_records"

def initialize(vaccination_record)
@vaccination_record = vaccination_record
end
Expand Down Expand Up @@ -119,11 +122,7 @@ def self.from_fhir_record(fhir_record, patient:)
private

def fhir_identifier
FHIR::Identifier.new(
system:
"http://manage-vaccinations-in-schools.nhs.uk/vaccination_records",
value: uuid
)
FHIR::Identifier.new(system: MAVIS_SYSTEM_NAME, value: uuid)
end

def fhir_vaccination_procedure_extension
Expand Down Expand Up @@ -151,7 +150,6 @@ def fhir_status
when "completed"
"administered"
when "not-done"
# "refused"
# TODO: handle this more gracefully
raise "Cannot import not-done vaccination records"
else
Expand Down
4 changes: 2 additions & 2 deletions app/lib/nhs/immunisations_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def should_be_in_immunisations_api?(
)
vaccination_record.kept? && vaccination_record.recorded_in_service? &&
vaccination_record.administered? &&
vaccination_record.programme.can_write_to_immunisations_api? &&
vaccination_record.programme.can_sync_to_immunisations_api? &&
(ignore_nhs_number || vaccination_record.patient.nhs_number.present?) &&
vaccination_record.notify_parents &&
vaccination_record.patient.not_invalidated?
Expand All @@ -226,7 +226,7 @@ def search_immunisations(patient, programmes:, date_from: nil, date_to: nil)

if programmes.empty?
raise "Cannot search for vaccination records in the immunisations API; no programmes provided."
elsif !programmes.all?(&:can_read_from_immunisations_api?)
elsif !programmes.all?(&:can_search_in_immunisations_api?)
raise "Cannot search for vaccination records in the immunisations API; one or more programmes is not supported."
end

Expand Down
6 changes: 4 additions & 2 deletions app/models/programme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class Programme < ApplicationRecord
has_many :teams, through: :team_programmes

scope :supports_delegation, -> { flu }
scope :can_sync_to_immunisations_api, -> { flu.or(hpv) }
scope :can_search_in_immunisations_api, -> { flu }

enum :type,
{ flu: "flu", hpv: "hpv", menacwy: "menacwy", td_ipv: "td_ipv" },
Expand Down Expand Up @@ -147,9 +149,9 @@ def snomed_target_disease_name
SNOMED_TARGET_DISEASE_NAMES.fetch(type)
end

def can_write_to_immunisations_api? = flu? || hpv?
def can_sync_to_immunisations_api? = hpv? || flu?

def can_read_from_immunisations_api? = flu?
def can_search_in_immunisations_api? = flu?

private

Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/files/fhir/search_response_0_results.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"link": [
{
"relation": "self",
"url": "https://int.api.service.nhs.uk/immunisation-fhir-api/Immunization?-date.from=2025-08-01\u0026-date.to=2025-10-01\u0026immunization.target=FLU\u0026patient.identifier=https%3A%2F%2Ffhir.nhs.uk%2FId%2Fnhs-number%7C9449308357"
"url": "https://int.api.service.nhs.uk/immunisation-fhir-api/Immunization?immunization.target=FLU\u0026patient.identifier=https%3A%2F%2Ffhir.nhs.uk%2FId%2Fnhs-number%7C9449308357"
}
],
"entry": [],
Expand Down
6 changes: 3 additions & 3 deletions spec/fixtures/files/fhir/search_response_1_result.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"link": [
{
"relation": "self",
"url": "https://int.api.service.nhs.uk/immunisation-fhir-api/Immunization?-date.from=2025-08-01\u0026-date.to=2025-10-01\u0026immunization.target=FLU\u0026patient.identifier=https%3A%2F%2Ffhir.nhs.uk%2FId%2Fnhs-number%7C9449308357"
"url": "https://int.api.service.nhs.uk/immunisation-fhir-api/Immunization?immunization.target=FLU\u0026patient.identifier=https%3A%2F%2Ffhir.nhs.uk%2FId%2Fnhs-number%7C9449308357"
}
],
"entry": [
Expand All @@ -30,7 +30,7 @@
"identifier": [
{
"use": "official",
"system": "http://manage-vaccinations-in-schools.nhs.uk/vaccination_records",
"system": "https://supplierABC/identifiers/vacc",
"value": "71f538d8-1171-4204-aee4-17ff0b0ba0b0"
}
],
Expand All @@ -52,7 +52,7 @@
"value": "9449308357"
}
},
"occurrenceDateTime": "2025-08-22T14:16:03+01:00",
"occurrenceDateTime": "2025-08-23T14:16:03+01:00",
"recorded": "2025-08-22T14:16:05.246000+01:00",
"primarySource": true,
"location": {
Expand Down
153 changes: 153 additions & 0 deletions spec/fixtures/files/fhir/search_response_1_result_mavis.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
{
"resourceType": "Bundle",
"type": "searchset",
"link": [
{
"relation": "self",
"url": "https://int.api.service.nhs.uk/immunisation-fhir-api/Immunization?immunization.target=FLU\u0026patient.identifier=https%3A%2F%2Ffhir.nhs.uk%2FId%2Fnhs-number%7C9449308357"
}
],
"entry": [
{
"fullUrl": "https://api.service.nhs.uk/immunisation-fhir-api/Immunization/4e7f3c91-a14d-4139-bbcf-859e998d2028",
"resource": {
"resourceType": "Immunization",
"id": "4e7f3c91-a14d-4139-bbcf-859e998d2028",
"extension": [
{
"url": "https://fhir.hl7.org.uk/StructureDefinition/Extension-UKCore-VaccinationProcedure",
"valueCodeableConcept": {
"coding": [
{
"system": "http://snomed.info/sct",
"code": "884861000000100",
"display": "Seasonal influenza vaccination (procedure)"
}
]
}
}
],
"identifier": [
{
"use": "official",
"system": "http://manage-vaccinations-in-schools.nhs.uk/vaccination_records",
"value": "71f538d8-1171-4204-aee4-17ff0b0ba0b0"
}
],
"status": "completed",
"vaccineCode": {
"coding": [
{
"system": "http://snomed.info/sct",
"code": "43208811000001106",
"display": "Fluenz (trivalent) vaccine nasal suspension 0.2ml unit dose (AstraZeneca UK Ltd) (product)"
}
]
},
"patient": {
"reference": "urn:uuid:e06dbb8d-ef9b-454f-9c5f-fde8460a0b6a",
"type": "Patient",
"identifier": {
"system": "https://fhir.nhs.uk/Id/nhs-number",
"value": "9449308357"
}
},
"occurrenceDateTime": "2025-08-22T14:16:03+01:00",
"recorded": "2025-08-22T14:16:05.246000+01:00",
"primarySource": true,
"location": {
"identifier": {
"system": "https://fhir.hl7.org.uk/Id/urn-school-number",
"value": "100001"
}
},
"manufacturer": {
"display": "AstraZeneca"
},
"lotNumber": "BU5086",
"expirationDate": "2025-09-30",
"site": {
"coding": [
{
"system": "http://snomed.info/sct",
"code": "279549004",
"display": "Nasal cavity structure"
}
]
},
"route": {
"coding": [
{
"system": "http://snomed.info/sct",
"code": "46713006",
"display": "Nasal"
}
]
},
"doseQuantity": {
"value": 0.2,
"unit": "ml",
"system": "http://snomed.info/sct",
"code": "258773002"
},
"performer": [
{
"actor": {
"type": "Organization",
"identifier": {
"system": "https://fhir.nhs.uk/Id/ods-organization-code",
"value": "R1L"
}
}
}
],
"reasonCode": [
{
"coding": [
{
"system": "http://snomed.info/sct",
"code": "723620004"
}
]
}
],
"protocolApplied": [
{
"targetDisease": [
{
"coding": [
{
"system": "http://snomed.info/sct",
"code": "6142004",
"display": "Influenza"
}
]
}
],
"doseNumberPositiveInt": 1
}
]
},
"search": {
"mode": "match"
}
},
{
"fullUrl": "urn:uuid:e06dbb8d-ef9b-454f-9c5f-fde8460a0b6a",
"resource": {
"resourceType": "Patient",
"id": "9449308357",
"identifier": [
{
"system": "https://fhir.nhs.uk/Id/nhs-number",
"value": "9449308357"
}
]
},
"search": {
"mode": "include"
}
}
],
"total": 1
}
6 changes: 3 additions & 3 deletions spec/fixtures/files/fhir/search_response_2_results.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"link": [
{
"relation": "self",
"url": "https://int.api.service.nhs.uk/immunisation-fhir-api/Immunization?-date.from=2025-08-01\u0026-date.to=2025-10-01\u0026immunization.target=FLU\u0026patient.identifier=https%3A%2F%2Ffhir.nhs.uk%2FId%2Fnhs-number%7C9449308357"
"url": "https://int.api.service.nhs.uk/immunisation-fhir-api/Immunization?immunization.target=FLU\u0026patient.identifier=https%3A%2F%2Ffhir.nhs.uk%2FId%2Fnhs-number%7C9449308357"
}
],
"entry": [
Expand All @@ -30,7 +30,7 @@
"identifier": [
{
"use": "official",
"system": "http://manage-vaccinations-in-schools.nhs.uk/vaccination_records",
"system": "https://supplierABC/identifiers/vacc",
"value": "71f538d8-1171-4204-aee4-17ff0b0ba0b0"
}
],
Expand Down Expand Up @@ -154,7 +154,7 @@
"identifier": [
{
"use": "official",
"system": "http://manage-vaccinations-in-schools.nhs.uk/vaccination_records",
"system": "https://supplierABC/identifiers/vacc",
"value": "18441e7b-b652-4d8c-980c-b60009f95942"
}
],
Expand Down
Loading