2222from authorisation .api_operation_code import ApiOperationCode
2323from authorisation .authoriser import Authoriser
2424from common .get_service_url import get_service_url
25- from common .models .constants import Constants
25+ from common .models .constants import Constants , Urls
2626from common .models .errors import (
2727 Code ,
2828 CustomValidationError ,
@@ -62,6 +62,7 @@ class FhirService:
6262 _DATA_MISSING_DATE_TIME_ERROR_MSG = (
6363 "Data quality issue - immunisation with ID %s was found containing no occurrenceDateTime"
6464 )
65+ _SINGLE_SNOMED_CODEABLE_CONCEPT_FIELDS = ("site" , "route" )
6566
6667 def __init__ (
6768 self ,
@@ -73,6 +74,36 @@ def __init__(
7374 self .immunization_repo = imms_repo
7475 self .validator = validator
7576
77+ @staticmethod
78+ def _keep_first_snomed_coding (coding : list ) -> list :
79+ snomed_seen = False
80+ filtered_coding = []
81+ for coding_entry in coding :
82+ is_snomed_coding = isinstance (coding_entry , dict ) and coding_entry .get ("system" ) == Urls .SNOMED
83+ if is_snomed_coding and snomed_seen :
84+ continue
85+
86+ snomed_seen = snomed_seen or is_snomed_coding
87+ filtered_coding .append (coding_entry )
88+
89+ return filtered_coding
90+
91+ @classmethod
92+ def _normalize_single_snomed_codeable_concepts (cls , immunization : dict ) -> None :
93+ for field_name in cls ._SINGLE_SNOMED_CODEABLE_CONCEPT_FIELDS :
94+ field = immunization .get (field_name )
95+ coding = field .get ("coding" ) if isinstance (field , dict ) else None
96+ if isinstance (coding , list ):
97+ field ["coding" ] = cls ._keep_first_snomed_coding (coding )
98+
99+ def _validate_immunization (self , immunization : dict ) -> None :
100+ self ._normalize_single_snomed_codeable_concepts (immunization )
101+
102+ try :
103+ self .validator .validate (immunization )
104+ except (ValueError , MandatoryError ) as error :
105+ raise CustomValidationError (message = str (error )) from error
106+
76107 def get_immunization_by_identifier (
77108 self , identifier : Identifier , supplier_name : str , elements : set [str ] | None
78109 ) -> FhirBundle :
@@ -117,10 +148,7 @@ def create_immunization(self, immunization: dict, supplier_system: str) -> Id:
117148 if immunization .get ("id" ) is not None :
118149 raise CustomValidationError ("id field must not be present for CREATE operation" )
119150
120- try :
121- self .validator .validate (immunization )
122- except (ValueError , MandatoryError ) as error :
123- raise CustomValidationError (message = str (error )) from error
151+ self ._validate_immunization (immunization )
124152
125153 vaccination_type = get_vaccine_type (immunization )
126154
@@ -139,10 +167,7 @@ def create_immunization(self, immunization: dict, supplier_system: str) -> Id:
139167 return self .immunization_repo .create_immunization (immunization_fhir_entity , supplier_system )
140168
141169 def update_immunization (self , imms_id : str , immunization : dict , supplier_system : str , resource_version : int ) -> int :
142- try :
143- self .validator .validate (immunization )
144- except (ValueError , MandatoryError ) as error :
145- raise CustomValidationError (message = str (error )) from error
170+ self ._validate_immunization (immunization )
146171
147172 immunization_to_update = Immunization .parse_obj (immunization )
148173
0 commit comments