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
@@ -76,7 +76,7 @@ def get_immunization_by_identifier(
7676 base_url = f"{ get_service_url ()} /Immunization"
7777 response = form_json (imms_resp , element , identifier , base_url )
7878 return response
79-
79+
8080 def get_immunization_by_id (self , imms_id : str , imms_vax_type_perms : str ) -> Optional [dict ]:
8181 """
8282 Get an Immunization by its ID. Return None if not found. If the patient doesn't have an NHS number,
@@ -85,26 +85,19 @@ def get_immunization_by_id(self, imms_id: str, imms_vax_type_perms: str) -> Opti
8585 if not (imms_resp := self .immunization_repo .get_immunization_by_id (imms_id , imms_vax_type_perms )):
8686 return None
8787
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" , {}))
88+ # Returns the Immunisation full resource with no obfuscation
89+ resource = imms_resp .get ("Resource" , {})
90+ imms_filtered_for_read = Filter .read (resource ) if resource else {}
9091
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" )
9992
10093 return {
10194 "Version" : imms_resp .get ("Version" , "" ),
102- "Resource" : Immunization .parse_obj (imms_filtered_for_read_and_s_flag ),
95+ "Resource" : Immunization .parse_obj (imms_filtered_for_read ),
10396 }
10497
10598 def get_immunization_by_id_all (self , imms_id : str , imms : dict ) -> Optional [dict ]:
10699 """
107- Get an Immunization by its ID. Return None if not found. If the patient doesn't have an NHS number,
100+ Get an Immunization by its ID. Return None if it is not found. If the patient doesn't have an NHS number,
108101 return the Immunization without calling PDS or checking S flag.
109102 """
110103 imms ["id" ] = imms_id
@@ -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 ())} "
337-
338- # 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 ]
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 ]
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