Skip to content
Open
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
8 changes: 6 additions & 2 deletions lambdas/handlers/post_fhir_document_reference_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
PostFhirDocumentReferenceService,
)
from utils.audit_logging_setup import LoggingService
from utils.constants.api import DOCUMENT_RETRIEVE_ENDPOINT
from utils.decorators.handle_lambda_exceptions import handle_lambda_exceptions_fhir
from utils.lambda_exceptions import DocumentRefException
from utils.lambda_response import ApiGatewayResponse
Expand Down Expand Up @@ -30,13 +31,16 @@ def lambda_handler(event, context):

fhir_doc_ref_service = PostFhirDocumentReferenceService()

fhir_response = fhir_doc_ref_service.process_fhir_document_reference(
fhir_response, document_id = fhir_doc_ref_service.process_fhir_document_reference(
event.get("body"), event.get("requestContext")
)

# Construct the Location header for FHIR compliance
location_url = f"{DOCUMENT_RETRIEVE_ENDPOINT}/{document_id}"

return ApiGatewayResponse(
status_code=201, body=fhir_response, methods="POST"
).create_api_gateway_response()
).create_api_gateway_response(headers={"Location": location_url})

except DocumentRefException as exception:
logger.error(f"Error processing FHIR document reference: {str(exception)}")
Expand Down
4 changes: 2 additions & 2 deletions lambdas/services/document_reference_search_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from services.document_service import DocumentService
from utils.audit_logging_setup import LoggingService
from utils.common_query_filters import NotDeleted, UploadCompleted
from utils.constants.api import DOCUMENT_RETRIEVE_ENDPOINT
from utils.dynamo_query_filter_builder import DynamoQueryFilterBuilder
from utils.exceptions import DynamoServiceException
from utils.lambda_exceptions import DocumentRefSearchException
Expand Down Expand Up @@ -225,12 +226,11 @@ def create_document_reference_fhir_response(
self,
document_reference: DocumentReference,
) -> dict:
document_retrieve_endpoint = os.getenv("DOCUMENT_RETRIEVE_ENDPOINT_APIM", "")
document_details = Attachment(
title=document_reference.file_name,
creation=document_reference.document_scan_creation
or document_reference.created,
url=document_retrieve_endpoint
url=DOCUMENT_RETRIEVE_ENDPOINT
+ "/"
+ document_reference.document_snomed_code_type
+ "~"
Expand Down
6 changes: 2 additions & 4 deletions lambdas/services/fhir_document_reference_service_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from services.document_service import DocumentService
from utils.audit_logging_setup import LoggingService
from utils.common_query_filters import CurrentStatusFile
from utils.constants.api import DOCUMENT_RETRIEVE_ENDPOINT
from utils.dynamo_utils import DocTypeTableRouter
from utils.exceptions import (
FhirDocumentReferenceException,
Expand Down Expand Up @@ -194,11 +195,8 @@ def _create_fhir_response(
if presigned_url:
attachment_url = presigned_url
else:
document_retrieve_endpoint = os.getenv(
"DOCUMENT_RETRIEVE_ENDPOINT_APIM", ""
)
attachment_url = (
document_retrieve_endpoint
DOCUMENT_RETRIEVE_ENDPOINT
+ "/"
+ document_reference_ndr.document_snomed_code_type
+ "~"
Expand Down
9 changes: 6 additions & 3 deletions lambdas/services/post_fhir_document_reference_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ def __init__(self):

def process_fhir_document_reference(
self, fhir_document: str, api_request_context: dict = {}
) -> str:
) -> tuple[str, str]:
"""
Process a FHIR Document Reference request

Args:
fhir_document: FHIR Document Reference object

Returns:
FHIR Document Reference response JSON object
Tuple of (FHIR Document Reference response JSON object, document ID)
"""
try:
common_name = validate_common_name_in_mtls(api_request_context)
Expand Down Expand Up @@ -75,7 +75,10 @@ def process_fhir_document_reference(
document_reference, validated_fhir_doc, dynamo_table
)

return self._create_fhir_response(document_reference, presigned_url)
fhir_response = self._create_fhir_response(document_reference, presigned_url)
document_id = f"{document_reference.document_snomed_code_type}~{document_reference.id}"

return fhir_response, document_id

except (ValidationError, InvalidNhsNumberException) as e:
logger.error(f"FHIR document validation error: {str(e)}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,28 @@ def mock_service(mocker):
return mock_service_instance


def test_mtls_lambda_handler_success(valid_mtls_event, context, mock_service):
def test_mtls_lambda_handler_success(valid_mtls_event, context, mock_service, mocker):
"""Test successful lambda execution."""
mock_response = {"resourceType": "DocumentReference", "id": "test-id"}
mock_document_id = "717391000000106~pdm-doc-456"

mock_service.process_fhir_document_reference.return_value = json.dumps(
mock_response
# Mock environment variable
mocker.patch.dict(
"os.environ",
{"DOCUMENT_RETRIEVE_ENDPOINT_APIM": "https://api.example.com/DocumentReference"},
)

mock_service.process_fhir_document_reference.return_value = (
json.dumps(mock_response),
mock_document_id,
)

result = lambda_handler(valid_mtls_event, context)

assert result["statusCode"] == 201
assert json.loads(result["body"]) == mock_response
assert "Location" in result["headers"]
assert result["headers"]["Location"] == f"https://api.example.com/DocumentReference/{mock_document_id}"

mock_service.process_fhir_document_reference.assert_called_once_with(
valid_mtls_event["body"], valid_mtls_event["requestContext"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,28 @@ def mock_service(mocker):
return mock_service_instance


def test_lambda_handler_success(valid_event, context, mock_service):
def test_lambda_handler_success(valid_event, context, mock_service, mocker):
"""Test successful lambda execution."""
mock_response = {"resourceType": "DocumentReference", "id": "test-id"}
mock_document_id = "16521000000101~test-doc-123"

mock_service.process_fhir_document_reference.return_value = json.dumps(
mock_response
# Mock environment variable
mocker.patch.dict(
"os.environ",
{"DOCUMENT_RETRIEVE_ENDPOINT_APIM": "https://api.example.com/DocumentReference"},
)

mock_service.process_fhir_document_reference.return_value = (
json.dumps(mock_response),
mock_document_id,
)

result = lambda_handler(valid_event, context)

assert result["statusCode"] == 201
assert json.loads(result["body"]) == mock_response
assert "Location" in result["headers"]
assert result["headers"]["Location"] == f"https://api.example.com/DocumentReference/{mock_document_id}"

mock_service.process_fhir_document_reference.assert_called_once_with(
valid_event["body"], valid_event["requestContext"]
Expand All @@ -77,6 +87,7 @@ def test_lambda_handler_exception_handling(valid_event, context, mock_service):

assert result["statusCode"] == 400
assert "resourceType" in json.loads(result["body"])
assert "Location" not in result["headers"]

mock_service.process_fhir_document_reference.assert_called_once_with(
valid_event["body"], valid_event["requestContext"]
Expand Down
3 changes: 3 additions & 0 deletions lambdas/utils/constants/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import os

DOCUMENT_RETRIEVE_ENDPOINT = os.getenv("DOCUMENT_RETRIEVE_ENDPOINT_APIM", "")
Loading