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,25 +78,23 @@ 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
8887 # Returns the Immunisation full resource with no obfuscation
8988 resource = imms_resp .get ("Resource" , {})
90- imms_filtered_for_read = Filter .read (resource ) if resource else {}
91-
9289
9390 return {
9491 "Version" : imms_resp .get ("Version" , "" ),
95- "Resource" : Immunization .parse_obj (imms_filtered_for_read ),
92+ "Resource" : Immunization .parse_obj (resource ),
9693 }
9794
9895 def get_immunization_by_id_all (self , imms_id : str , imms : dict ) -> Optional [dict ]:
9996 """
100- Get an Immunization by its ID. Return None if it is not found. If the patient doesn't have an NHS number,
97+ Get an Immunization by its ID. Return None if not found. If the patient doesn't have an NHS number,
10198 return the Immunization without calling PDS or checking S flag.
10299 """
103100 imms ["id" ] = imms_id
@@ -264,15 +261,18 @@ def process_patient_for_bundle(patient: dict):
264261 """
265262
266263 # Remove unwanted top-level fields
267- fields_to_keep = [ "id" , " resourceType" , "identifier" , "birthDate" ]
264+ fields_to_keep = { " resourceType" , "identifier" }
268265 new_patient = {k : v for k , v in patient .items () if k in fields_to_keep }
269266
270267 # Remove unwanted identifier fields
271- new_identifiers = []
272- for identifier in new_patient ["identifier" ]:
273- identifier_fields_to_keep = ["system" , "value" ]
274- new_identifiers .append ({k : v for k , v in identifier .items () if k in identifier_fields_to_keep })
275- 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" )
276276
277277 return new_patient
278278
@@ -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 ())} "
330325
326+ imms_patient_record = get_contained_patient (resources [- 1 ]) if resources else None
327+
331328 # 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 ]
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