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
@@ -316,25 +316,22 @@ def search_immunizations(
316316 if self .is_valid_date_from (r , date_from ) and self .is_valid_date_to (r , date_to )
317317 ]
318318
319- # Check whether the Superseded NHS number present in PDS
320- if pds_patient := self .pds_service .get_patient_details (nhs_number ):
321- if pds_patient ["identifier" ][0 ]["value" ] != nhs_number :
322- return create_diagnostics ()
323-
324319 # Create the patient URN for the fullUrl field.
325320 # NOTE: This UUID is assigned when a SEARCH request is received and used only for referencing the patient
326321 # resource from immunisation resources within the bundle. The fullUrl value we are using is a urn (hence the
327322 # FHIR key name of "fullUrl" is somewhat misleading) which cannot be used to locate any externally stored
328323 # patient resource. This is as agreed with VDS team for backwards compatibility with Immunisation History API.
329324 patient_full_url = f"urn:uuid:{ str (uuid4 ())} "
330-
331- # Filter and amend the immunization resources for the SEARCH response
332- resources_filtered_for_search = [Filter .search (imms , patient_full_url , pds_patient ) for imms in resources ]
325+
326+ imms_patient_record = get_contained_patient (resources [- 1 ]) if resources else None
327+
328+ # Filter and amend the immunization resources for the SEARCH response
329+ resources_filtered_for_search = [Filter .search (imms , patient_full_url , []) for imms in resources ]
333330
334331 # Add bundle entries for each of the immunization resources
335332 entries = [
336333 BundleEntry (
337- resource = Immunization .parse_obj (handle_s_flag ( imms , pds_patient ) ),
334+ resource = Immunization .parse_obj (imms ),
338335 search = BundleEntrySearch (mode = "match" ),
339336 fullUrl = f"https://api.service.nhs.uk/immunisation-fhir-api/Immunization/{ imms ['id' ]} " ,
340337 )
@@ -345,7 +342,7 @@ def search_immunizations(
345342 if len (resources ) > 0 :
346343 entries .append (
347344 BundleEntry (
348- resource = self .process_patient_for_bundle (pds_patient ),
345+ resource = self .process_patient_for_bundle (imms_patient_record ),
349346 search = BundleEntrySearch (mode = "include" ),
350347 fullUrl = patient_full_url ,
351348 )
0 commit comments