Skip to content

Commit 2a548ae

Browse files
committed
VED-367: refactor logic, rename variables from code review
1 parent 7d31bae commit 2a548ae

File tree

3 files changed

+14
-47
lines changed

3 files changed

+14
-47
lines changed

backend/src/fhir_controller.py

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,7 @@ def _validate_id(self, _id: str) -> Optional[dict]:
482482
else:
483483
return None
484484

485-
def _validate_identifier_system(self, _id: str, _element: str) -> Optional[dict]:
486-
485+
def _validate_identifier_system(self, _id: str, _elements: str) -> Optional[dict]:
487486
if not _id:
488487
return create_operation_outcome(
489488
resource_id=str(uuid.uuid4()),
@@ -506,11 +505,13 @@ def _validate_identifier_system(self, _id: str, _element: str) -> Optional[dict]
506505
'e.g. "http://xyz.org/vaccs|2345-gh3s-r53h7-12ny"'
507506
),
508507
)
508+
509+
if not _elements:
510+
return None
509511

510-
element_lower = _element.lower()
511-
result = element_lower.split(",")
512-
is_present = all(key in ["id", "meta"] for key in result)
513-
if _element and not is_present:
512+
requested_elements = {e.strip().lower() for e in _elements.split(",") if e.strip()}
513+
requested_elements_valid = requested_elements.issubset({"id", "meta"})
514+
if _elements and not requested_elements_valid:
514515
return create_operation_outcome(
515516
resource_id=str(uuid.uuid4()),
516517
severity=Severity.error,
@@ -561,12 +562,10 @@ def fetch_identifier_system_and_element(self, event: dict):
561562
body = event["body"]
562563
not_required_keys = ["-date.from", "-date.to", "-immunization.target", "_include", "patient.identifier"]
563564

564-
# Get Search Identifer Parameters
565+
# Get Search Query Parameters
565566
if query_params and not body:
566-
query_string_has_immunization_identifier = "identifier" in event.get(
567-
"queryStringParameters", {}
568-
)
569-
query_string_has_element = "_elements" in event.get("queryStringParameters", {})
567+
query_string_has_immunization_identifier = "identifier" in query_params
568+
query_string_has_element = "_elements" in query_params
570569
identifier = query_params.get("identifier", "")
571570
element = query_params.get("_elements", "")
572571
query_check = check_keys_in_sources(event, not_required_keys)
@@ -613,21 +612,12 @@ def create_response_for_identifier(self, not_required, has_identifier, has_eleme
613612
)
614613
return self.create_response(400, error)
615614

616-
if "patient.identifier" not in not_required and not_required and has_identifier:
617-
error = create_operation_outcome(
618-
resource_id=str(uuid.uuid4()),
619-
severity=Severity.error,
620-
code=Code.server_error,
621-
diagnostics="Search parameter identifier must have the following parameter: _elements",
622-
)
623-
return self.create_response(400, error)
624-
625615
if not_required and has_element:
626616
error = create_operation_outcome(
627617
resource_id=str(uuid.uuid4()),
628618
severity=Severity.error,
629619
code=Code.server_error,
630-
diagnostics="Search parameter _elements must have the following parameter: identifier",
620+
diagnostics="Search parameter _elements must have the following parameter: identifier",
631621
)
632622
return self.create_response(400, error)
633623

backend/src/search_imms_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def search_imms(event: events.APIGatewayProxyEventV1, controller: FhirController
4242
# Parse the URL encoded body
4343
parsed_body = urllib.parse.parse_qs(decoded_body)
4444

45-
# Check for 'immunization.identifier' in body
45+
# Check for 'identifier' in body
4646
body_has_immunization_identifier = "identifier" in parsed_body
4747
body_has_immunization_element = "_elements" in parsed_body
4848
if (

backend/tests/test_fhir_controller.py

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -240,29 +240,6 @@ def test_get_imms_by_identifer_both_body_and_query_params_present(self, mock_get
240240
body = json.loads(response["body"])
241241
self.assertEqual(body["resourceType"], "OperationOutcome")
242242

243-
@patch("fhir_controller.get_supplier_permissions")
244-
def test_get_imms_by_identifer_imms_identifier_and_element_not_present(self, mock_get_supplier_permissions):
245-
"""it should return Immunization Id if it exists"""
246-
# Given
247-
mock_get_supplier_permissions.return_value = ["COVID19.CRUDS"]
248-
self.service.get_immunization_by_identifier.return_value = {"id": "test", "Version": 1}
249-
lambda_event = {
250-
"headers": {"SupplierSystem": "test"},
251-
"queryStringParameters": {
252-
"-immunization.target": "test",
253-
"identifier": "https://supplierABC/identifiers/vacc|f10b59b3-fc73-4616-99c9-9e882ab31184",
254-
},
255-
"body": None,
256-
}
257-
# When
258-
response = self.controller.get_immunization_by_identifier(lambda_event)
259-
# Then
260-
self.service.get_immunization_by_identifier.assert_not_called
261-
262-
self.assertEqual(response["statusCode"], 400)
263-
body = json.loads(response["body"])
264-
self.assertEqual(body["resourceType"], "OperationOutcome")
265-
266243
@patch("fhir_controller.get_supplier_permissions")
267244
def test_get_imms_by_identifer_both_identifier_present(self, mock_get_supplier_permissions):
268245
"""it should return Immunization Id if it exists"""
@@ -355,7 +332,7 @@ def test_validate_immunization_identifier_in_invalid_format(self,mock_get_suppli
355332
"details": {
356333
"coding": [{"system": "https://fhir.nhs.uk/Codesystem/http-error-codes", "code": "INVALID"}]
357334
},
358-
"diagnostics": "immunization.identifier must be in the format of immunization.identifier.system|immunization.identifier.value e.g. http://pinnacle.org/vaccs|2345-gh3s-r53h7-12ny",
335+
"diagnostics": "identifier must be in the format of identifier.system|identifier.value e.g. http://pinnacle.org/vaccs|2345-gh3s-r53h7-12ny",
359336
}
360337
],
361338
}
@@ -677,7 +654,7 @@ def test_validate_immunization_identifier_is_empty(self, mock_get_supplier_permi
677654
"details": {
678655
"coding": [{"system": "https://fhir.nhs.uk/Codesystem/http-error-codes", "code": "INVALID"}]
679656
},
680-
"diagnostics": "immunization.identifier must be in the format of immunization.identifier.system|immunization.identifier.value e.g. http://pinnacle.org/vaccs|2345-gh3s-r53h7-12ny",
657+
"diagnostics": "identifier must be in the format of identifier.system|identifier.value e.g. http://pinnacle.org/vaccs|2345-gh3s-r53h7-12ny",
681658
}
682659
],
683660
}

0 commit comments

Comments
 (0)