Skip to content

Commit 05f92d1

Browse files
committed
Merge branch 'master' into VED-83-int-ref
2 parents b40997b + f418f80 commit 05f92d1

17 files changed

+145
-1078
lines changed

backend/src/fhir_service.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@
1919
from models.field_names import FieldNames
2020
from models.errors import InvalidPatientId, CustomValidationError, UnhandledResponseError
2121
from models.fhir_immunization import ImmunizationValidator
22-
from models.utils.generic_utils import nhs_number_mod11_check, get_occurrence_datetime, create_diagnostics, form_json
22+
from models.utils.generic_utils import nhs_number_mod11_check, get_occurrence_datetime, create_diagnostics, form_json, get_contained_patient
2323
from models.constants import Constants
2424
from models.errors import MandatoryError
2525
from pds_service import PdsService
26-
from s_flag_handler import handle_s_flag
2726
from timer import timed
2827
from filter import Filter
2928

@@ -262,15 +261,18 @@ def process_patient_for_bundle(patient: dict):
262261
"""
263262

264263
# Remove unwanted top-level fields
265-
fields_to_keep = ["id", "resourceType", "identifier", "birthDate"]
264+
fields_to_keep = {"resourceType", "identifier"}
266265
new_patient = {k: v for k, v in patient.items() if k in fields_to_keep}
267266

268267
# Remove unwanted identifier fields
269-
new_identifiers = []
270-
for identifier in new_patient["identifier"]:
271-
identifier_fields_to_keep = ["system", "value"]
272-
new_identifiers.append({k: v for k, v in identifier.items() if k in identifier_fields_to_keep})
273-
new_patient["identifier"] = new_identifiers
268+
identifier_fields_to_keep = {"system", "value"}
269+
new_patient["identifier"] = [
270+
{k: v for k, v in identifier.items() if k in identifier_fields_to_keep}
271+
for identifier in new_patient.get("identifier", [])
272+
]
273+
274+
if new_patient["identifier"]:
275+
new_patient["id"] = new_patient["identifier"][0].get("value")
274276

275277
return new_patient
276278

@@ -314,25 +316,22 @@ def search_immunizations(
314316
if self.is_valid_date_from(r, date_from) and self.is_valid_date_to(r, date_to)
315317
]
316318

317-
# Check whether the Superseded NHS number present in PDS
318-
if pds_patient := self.pds_service.get_patient_details(nhs_number):
319-
if pds_patient["identifier"][0]["value"] != nhs_number:
320-
return create_diagnostics()
321-
322319
# Create the patient URN for the fullUrl field.
323320
# NOTE: This UUID is assigned when a SEARCH request is received and used only for referencing the patient
324321
# resource from immunisation resources within the bundle. The fullUrl value we are using is a urn (hence the
325322
# FHIR key name of "fullUrl" is somewhat misleading) which cannot be used to locate any externally stored
326323
# patient resource. This is as agreed with VDS team for backwards compatibility with Immunisation History API.
327324
patient_full_url = f"urn:uuid:{str(uuid4())}"
328325

326+
imms_patient_record = get_contained_patient(resources[-1]) if resources else None
327+
329328
# Filter and amend the immunization resources for the SEARCH response
330-
resources_filtered_for_search = [Filter.search(imms, patient_full_url, pds_patient) for imms in resources]
329+
resources_filtered_for_search = [Filter.search(imms, patient_full_url) for imms in resources]
331330

332331
# Add bundle entries for each of the immunization resources
333332
entries = [
334333
BundleEntry(
335-
resource=Immunization.parse_obj(handle_s_flag(imms, pds_patient)),
334+
resource=Immunization.parse_obj(imms),
336335
search=BundleEntrySearch(mode="match"),
337336
fullUrl=f"https://api.service.nhs.uk/immunisation-fhir-api/Immunization/{imms['id']}",
338337
)
@@ -343,7 +342,7 @@ def search_immunizations(
343342
if len(resources) > 0:
344343
entries.append(
345344
BundleEntry(
346-
resource=self.process_patient_for_bundle(pds_patient),
345+
resource=self.process_patient_for_bundle(imms_patient_record),
347346
search=BundleEntrySearch(mode="include"),
348347
fullUrl=patient_full_url,
349348
)

backend/src/filter.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Functions for filtering a FHIR Immunization Resource"""
22

3-
from models.utils.generic_utils import is_actor_referencing_contained_resource, get_contained_practitioner
3+
from models.utils.generic_utils import is_actor_referencing_contained_resource, get_contained_practitioner, get_contained_patient
44
from constants import Urls
55

66

@@ -14,7 +14,7 @@ def remove_reference_to_contained_practitioner(imms: dict) -> dict:
1414

1515
# Remove reference to the contained practitioner from imms[performer]
1616
imms["performer"] = [
17-
x for x in imms["performer"] if not is_actor_referencing_contained_resource(x, contained_practitioner["id"])
17+
x for x in imms["performer"] if not is_actor_referencing_contained_resource(x, contained_practitioner["id"])
1818
]
1919

2020
return imms
@@ -94,20 +94,13 @@ def add_use_to_identifier(imms: dict) -> dict:
9494

9595
class Filter:
9696
"""Functions for filtering a FHIR Immunization Resource"""
97+
9798
@staticmethod
98-
def search(imms: dict, patient_full_url: str, bundle_patient: dict = None) -> dict:
99+
def search(imms: dict, patient_full_url: str) -> dict:
99100
"""Apply filtering for an individual FHIR Immunization Resource as part of SEARCH request"""
100101
imms = remove_reference_to_contained_practitioner(imms)
101-
imms.pop("contained")
102-
imms["patient"] = create_reference_to_patient_resource(patient_full_url, bundle_patient)
102+
imms["patient"] = create_reference_to_patient_resource(patient_full_url, get_contained_patient(imms))
103103
imms = add_use_to_identifier(imms)
104-
return imms
104+
imms.pop("contained")
105105

106-
@staticmethod
107-
def s_flag(imms: dict) -> dict:
108-
"""Apply filtering for patients with 'RESTRICTED' flag"""
109-
imms = replace_address_postal_codes(imms)
110-
imms = replace_organization_values(imms)
111-
if imms.get("location"):
112-
imms["location"] = {"identifier": {"system": "urn:iso:std:iso:3166", "value": "GB"}}
113106
return imms

backend/src/s_flag_handler.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

backend/tests/sample_data/completed_covid19_immunization_event_filtered_for_s_flag.json

Lines changed: 0 additions & 155 deletions
This file was deleted.

0 commit comments

Comments
 (0)