|
| 1 | +import os |
| 2 | +import uuid |
| 3 | +from datetime import datetime, timezone |
| 4 | +from typing import Optional |
| 5 | + |
| 6 | +from enums.lambda_error import LambdaError |
| 7 | +from services.base.s3_service import S3Service |
| 8 | +from services.document_upload_review_service import DocumentUploadReviewService |
| 9 | +from utils.audit_logging_setup import LoggingService |
| 10 | +from utils.exceptions import DynamoServiceException |
| 11 | +from utils.lambda_exceptions import GetDocumentReviewException |
| 12 | +from utils.utilities import format_cloudfront_url |
| 13 | + |
| 14 | +logger = LoggingService(__name__) |
| 15 | + |
| 16 | + |
| 17 | +class GetDocumentReviewService: |
| 18 | + """ |
| 19 | + Service for retrieving document reviews. |
| 20 | + """ |
| 21 | + |
| 22 | + def __init__(self): |
| 23 | + presigned_assume_role = os.getenv("PRESIGNED_ASSUME_ROLE") |
| 24 | + self.s3_service = S3Service(custom_aws_role=presigned_assume_role) |
| 25 | + self.document_review_service = DocumentUploadReviewService() |
| 26 | + self.cloudfront_table_name = os.environ.get("EDGE_REFERENCE_TABLE") |
| 27 | + self.cloudfront_url = os.environ.get("CLOUDFRONT_URL") |
| 28 | + |
| 29 | + def get_document_review(self, patient_id: str, document_id: str) -> Optional[dict]: |
| 30 | + """Retrieve a document review for a given patient and document. |
| 31 | +
|
| 32 | + Args: |
| 33 | + patient_id: The patient ID (NHS number). |
| 34 | + document_id: The document ID to retrieve. |
| 35 | +
|
| 36 | + Returns: |
| 37 | + Dictionary containing the document review details, or None if not found. |
| 38 | + """ |
| 39 | + try: |
| 40 | + logger.info( |
| 41 | + f"Fetching document review for patient_id: {patient_id}, document_id: {document_id}" |
| 42 | + ) |
| 43 | + |
| 44 | + document_review_item = self.document_review_service.get_item(document_id) |
| 45 | + |
| 46 | + if not document_review_item: |
| 47 | + logger.info(f"No document review found for document_id: {document_id}") |
| 48 | + return None |
| 49 | + |
| 50 | + if document_review_item.nhs_number != patient_id: |
| 51 | + logger.warning( |
| 52 | + f"Document {document_id} does not belong to patient {patient_id}" |
| 53 | + ) |
| 54 | + return None |
| 55 | + |
| 56 | + if document_review_item.files: |
| 57 | + for file_detail in document_review_item.files: |
| 58 | + presigned_url = self.create_cloudfront_presigned_url( |
| 59 | + file_detail.file_location |
| 60 | + ) |
| 61 | + file_detail.presigned_url = presigned_url |
| 62 | + |
| 63 | + document_review = document_review_item.model_dump( |
| 64 | + by_alias=True, |
| 65 | + include={ |
| 66 | + "id": True, |
| 67 | + "upload_date": True, |
| 68 | + "files": {"__all__": {"file_name": True, "presigned_url": True}}, |
| 69 | + "document_snomed_code_type": True, |
| 70 | + }, |
| 71 | + ) |
| 72 | + |
| 73 | + logger.info( |
| 74 | + f"Successfully retrieved document review for document_id: {document_id}" |
| 75 | + ) |
| 76 | + |
| 77 | + return document_review |
| 78 | + |
| 79 | + except DynamoServiceException as e: |
| 80 | + logger.error( |
| 81 | + f"{LambdaError.DocRefClient.to_str()}: {str(e)}", |
| 82 | + {"Result": "Failed to retrieve document review"}, |
| 83 | + ) |
| 84 | + raise GetDocumentReviewException(500, LambdaError.DocRefClient) |
| 85 | + except Exception as e: |
| 86 | + logger.error( |
| 87 | + f"Unexpected error retrieving document review: {str(e)}", |
| 88 | + {"Result": "Failed to retrieve document review"}, |
| 89 | + ) |
| 90 | + raise GetDocumentReviewException(500, LambdaError.DocRefClient) |
| 91 | + |
| 92 | + def create_cloudfront_presigned_url(self, file_location: str) -> str: |
| 93 | + """Create a CloudFront obfuscated pre-signed URL for a file. |
| 94 | +
|
| 95 | + Args: |
| 96 | + file_location: The S3 file key/location. |
| 97 | +
|
| 98 | + Returns: |
| 99 | + CloudFront URL that obfuscates the actual pre-signed URL. |
| 100 | + """ |
| 101 | + s3_bucket_name, file_key = file_location.removeprefix("s3://").split("/", 1) |
| 102 | + presign_url_response = self.s3_service.create_download_presigned_url( |
| 103 | + s3_bucket_name=s3_bucket_name, |
| 104 | + file_key=file_key, |
| 105 | + ) |
| 106 | + |
| 107 | + presigned_id = "review/" + str(uuid.uuid4()) |
| 108 | + |
| 109 | + deletion_date = datetime.now(timezone.utc) |
| 110 | + ttl_half_an_hour_in_seconds = self.s3_service.presigned_url_expiry |
| 111 | + dynamo_item_ttl = int(deletion_date.timestamp() + ttl_half_an_hour_in_seconds) |
| 112 | + |
| 113 | + self.document_review_service.dynamo_service.create_item( |
| 114 | + self.cloudfront_table_name, |
| 115 | + { |
| 116 | + "ID": f"{presigned_id}", |
| 117 | + "presignedUrl": presign_url_response, |
| 118 | + "TTL": dynamo_item_ttl, |
| 119 | + }, |
| 120 | + ) |
| 121 | + |
| 122 | + return format_cloudfront_url(presigned_id, self.cloudfront_url) |
0 commit comments