Skip to content

Commit dfafd3f

Browse files
authored
Merge branch 'master' into AMB-2144-mesh-integration
2 parents 8d6e1ae + f418f80 commit dfafd3f

File tree

57 files changed

+12195
-1168
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+12195
-1168
lines changed

backend/src/fhir_service.py

Lines changed: 19 additions & 29 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

@@ -79,27 +78,18 @@ def get_immunization_by_identifier(
7978

8079
def get_immunization_by_id(self, imms_id: str, imms_vax_type_perms: str) -> Optional[dict]:
8180
"""
82-
Get an Immunization by its ID. Return None if not found. If the patient doesn't have an NHS number,
81+
Get an Immunization by its ID. Return None if it is not found. If the patient doesn't have an NHS number,
8382
return the Immunization without calling PDS or checking S flag.
8483
"""
8584
if not (imms_resp := self.immunization_repo.get_immunization_by_id(imms_id, imms_vax_type_perms)):
8685
return None
8786

88-
# Remove fields rom the imms resource which are not to be returned for read
89-
imms_filtered_for_read = Filter.read(imms_resp.get("Resource", {}))
90-
91-
# Handle s-flag filtering, where applicable
92-
if not (nhs_number := obtain_field_value(imms_filtered_for_read, FieldNames.patient_identifier_value)):
93-
imms_filtered_for_read_and_s_flag = imms_filtered_for_read
94-
else:
95-
if patient := self.pds_service.get_patient_details(nhs_number):
96-
imms_filtered_for_read_and_s_flag = handle_s_flag(imms_filtered_for_read, patient)
97-
else:
98-
raise UnhandledResponseError("unable to validate NHS number with downstream service")
87+
# Returns the Immunisation full resource with no obfuscation
88+
resource = imms_resp.get("Resource", {})
9989

10090
return {
10191
"Version": imms_resp.get("Version", ""),
102-
"Resource": Immunization.parse_obj(imms_filtered_for_read_and_s_flag),
92+
"Resource": Immunization.parse_obj(resource),
10393
}
10494

10595
def get_immunization_by_id_all(self, imms_id: str, imms: dict) -> Optional[dict]:
@@ -271,15 +261,18 @@ def process_patient_for_bundle(patient: dict):
271261
"""
272262

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

277267
# Remove unwanted identifier fields
278-
new_identifiers = []
279-
for identifier in new_patient["identifier"]:
280-
identifier_fields_to_keep = ["system", "value"]
281-
new_identifiers.append({k: v for k, v in identifier.items() if k in identifier_fields_to_keep})
282-
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")
283276

284277
return new_patient
285278

@@ -323,25 +316,22 @@ def search_immunizations(
323316
if self.is_valid_date_from(r, date_from) and self.is_valid_date_to(r, date_to)
324317
]
325318

326-
# Check whether the Superseded NHS number present in PDS
327-
if pds_patient := self.pds_service.get_patient_details(nhs_number):
328-
if pds_patient["identifier"][0]["value"] != nhs_number:
329-
return create_diagnostics()
330-
331319
# Create the patient URN for the fullUrl field.
332320
# NOTE: This UUID is assigned when a SEARCH request is received and used only for referencing the patient
333321
# resource from immunisation resources within the bundle. The fullUrl value we are using is a urn (hence the
334322
# FHIR key name of "fullUrl" is somewhat misleading) which cannot be used to locate any externally stored
335323
# patient resource. This is as agreed with VDS team for backwards compatibility with Immunisation History API.
336324
patient_full_url = f"urn:uuid:{str(uuid4())}"
337325

326+
imms_patient_record = get_contained_patient(resources[-1]) if resources else None
327+
338328
# Filter and amend the immunization resources for the SEARCH response
339-
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]
340330

341331
# Add bundle entries for each of the immunization resources
342332
entries = [
343333
BundleEntry(
344-
resource=Immunization.parse_obj(handle_s_flag(imms, pds_patient)),
334+
resource=Immunization.parse_obj(imms),
345335
search=BundleEntrySearch(mode="match"),
346336
fullUrl=f"https://api.service.nhs.uk/immunisation-fhir-api/Immunization/{imms['id']}",
347337
)
@@ -352,7 +342,7 @@ def search_immunizations(
352342
if len(resources) > 0:
353343
entries.append(
354344
BundleEntry(
355-
resource=self.process_patient_for_bundle(pds_patient),
345+
resource=self.process_patient_for_bundle(imms_patient_record),
356346
search=BundleEntrySearch(mode="include"),
357347
fullUrl=patient_full_url,
358348
)

backend/src/filter.py

Lines changed: 5 additions & 19 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
@@ -96,25 +96,11 @@ class Filter:
9696
"""Functions for filtering a FHIR Immunization Resource"""
9797

9898
@staticmethod
99-
def read(imms: dict) -> dict:
100-
"""Apply filtering for READ request"""
101-
imms.pop("identifier")
102-
return imms
103-
104-
@staticmethod
105-
def search(imms: dict, patient_full_url: str, bundle_patient: dict = None) -> dict:
99+
def search(imms: dict, patient_full_url: str) -> dict:
106100
"""Apply filtering for an individual FHIR Immunization Resource as part of SEARCH request"""
107101
imms = remove_reference_to_contained_practitioner(imms)
108-
imms.pop("contained")
109-
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))
110103
imms = add_use_to_identifier(imms)
111-
return imms
104+
imms.pop("contained")
112105

113-
@staticmethod
114-
def s_flag(imms: dict) -> dict:
115-
"""Apply filtering for patients with 'RESTRICTED' flag"""
116-
imms = replace_address_postal_codes(imms)
117-
imms = replace_organization_values(imms)
118-
if imms.get("location"):
119-
imms["location"] = {"identifier": {"system": "urn:iso:std:iso:3166", "value": "GB"}}
120106
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)