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
@@ -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 )
0 commit comments