Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions backend/src/fhir_service.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from uuid import uuid4
import datetime
import os
Expand Down Expand Up @@ -26,6 +27,8 @@
from timer import timed
from filter import Filter

logging.basicConfig(level="INFO")
logger = logging.getLogger()

def get_service_url(
service_env: str = os.getenv("IMMUNIZATION_ENV"),
Expand Down Expand Up @@ -349,13 +352,18 @@ def _validate_patient(self, imms: dict) -> dict:
If the NHS number exists, get the patient details from PDS and return the patient details.
"""
try:
nhs_number = [x for x in imms["contained"] if x["resourceType"] == "Patient"][0]["identifier"][0]["value"]
contained_patient = get_contained_patient(imms)
nhs_number = contained_patient["identifier"][0]["value"]
except (KeyError, IndexError):
nhs_number = None
return {}

if not nhs_number:
return {}

if os.getenv("PDS_CHECK_ENABLED") == "false":
logger.warning("Skipping PDS check")
return contained_patient

patient = self.pds_service.get_patient_details(nhs_number)
# To check whether the Superseded NHS number present in PDS
if patient:
Expand Down
13 changes: 12 additions & 1 deletion backend/tests/test_fhir_service.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import json
import os
import uuid
import datetime
import unittest
from copy import deepcopy
from unittest import skip
from unittest.mock import create_autospec
from unittest.mock import create_autospec, patch
from decimal import Decimal

from fhir.resources.R4B.bundle import Bundle as FhirBundle, BundleEntry
Expand Down Expand Up @@ -431,6 +432,16 @@ def test_patient_error(self):
self.assertEqual(e.exception.patient_identifier, invalid_nhs_number)
self.imms_repo.create_immunization.assert_not_called()

@patch.dict(os.environ, {"PDS_CHECK_ENABLED": "false"}, False)
def test_pds_check_skipped(self):
bad_patient_imms = create_covid_19_immunization_dict_no_id("a-bad-patient-id")
self.imms_repo.create_immunization.return_value = bad_patient_imms

self.fhir_service.create_immunization(bad_patient_imms, "COVID19:create", "Test")

self.imms_repo.create_immunization.assert_called()
self.fhir_service.pds_service.get_patient_details.assert_not_called()


class TestUpdateImmunization(unittest.TestCase):
"""Tests for FhirService.update_immunization"""
Expand Down
1 change: 1 addition & 0 deletions terraform/endpoints.tf
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ locals {
"IMMUNIZATION_BASE_PATH" = strcontains(local.environment, "pr-") ? "immunisation-fhir-api-${local.environment}" : "immunisation-fhir-api"
# except for prod and ref, any other env uses PDS int environment
"PDS_ENV" = local.environment == "prod" ? "prod" : local.environment == "ref" ? "ref" : "int",
"PDS_CHECK_ENABLED" = tostring(local.environment != "int")
"SPLUNK_FIREHOSE_NAME" = module.splunk.firehose_stream_name
"SQS_QUEUE_URL" = "https://sqs.eu-west-2.amazonaws.com/${local.immunisation_account_id}/${local.short_prefix}-ack-metadata-queue.fifo"
"REDIS_HOST" = data.aws_elasticache_cluster.existing_redis.cache_nodes[0].address
Expand Down
Loading