1919from models .field_names import FieldNames
2020from models .errors import InvalidPatientId , CustomValidationError , UnhandledResponseError
2121from 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
2323from models .constants import Constants
2424from models .errors import MandatoryError
2525from pds_service import PdsService
26- from s_flag_handler import handle_s_flag
2726from timer import timed
2827from 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 )
0 commit comments