diff --git a/healthchain/fhir/__init__.py b/healthchain/fhir/__init__.py index 78022bf5..9da081d3 100644 --- a/healthchain/fhir/__init__.py +++ b/healthchain/fhir/__init__.py @@ -9,6 +9,7 @@ set_condition_category, read_content_attachment, create_document_reference, + create_document_reference_content, create_single_attachment, create_resource_from_dict, convert_prefetch_to_fhir_objects, @@ -36,6 +37,7 @@ "set_condition_category", "read_content_attachment", "create_document_reference", + "create_document_reference_content", "create_single_attachment", "create_resource_from_dict", "convert_prefetch_to_fhir_objects", diff --git a/healthchain/fhir/helpers.py b/healthchain/fhir/helpers.py index d89ec14b..95114272 100644 --- a/healthchain/fhir/helpers.py +++ b/healthchain/fhir/helpers.py @@ -321,7 +321,6 @@ def create_allergy_intolerance( return allergy -# TODO: create a function that creates a DocumentReferenceContent to add to the DocumentReference def create_document_reference( data: Optional[Any] = None, url: Optional[str] = None, @@ -368,6 +367,80 @@ def create_document_reference( return document_reference +def create_document_reference_content( + attachment_data: Optional[str] = None, + url: Optional[str] = None, + content_type: str = "text/plain", + language: Optional[str] = "en-US", + title: Optional[str] = None, + **kwargs, +) -> Dict[str, Any]: + """Create a FHIR DocumentReferenceContent object. + + Creates a DocumentReferenceContent structure that can be added to a DocumentReference. + Either attachment_data or url must be provided. If attachment_data is provided, it will + be base64 encoded automatically. + + Args: + attachment_data: The content data (text that will be base64 encoded) + url: URL where the content can be accessed + content_type: MIME type (e.g., 'text/plain', 'text/html', 'application/pdf') (default: text/plain) + language: Language code (default: en-US) + title: Optional title for the content (default: "Attachment created by HealthChain") + **kwargs: Additional DocumentReferenceContent fields (e.g., format, profile) + + Returns: + Dict[str, Any]: A FHIR DocumentReferenceContent dictionary with attachment and optional language + + Example: + >>> # Create content with inline data + >>> content = create_document_reference_content( + ... attachment_data="Patient presents with fever...", + ... content_type="text/plain", + ... title="Clinical Note" + ... ) + >>> + >>> # Create content with URL reference + >>> content = create_document_reference_content( + ... url="https://example.com/document.pdf", + ... content_type="application/pdf", + ... title="Lab Report" + ... ) + >>> + >>> # Add content to a DocumentReference + >>> doc_ref = DocumentReference( + ... id="doc-1", + ... status="current", + ... content=[content] + ... ) + """ + if not attachment_data and not url: + logger.warning( + "No attachment_data or url provided for DocumentReferenceContent" + ) + + if title is None: + title = "Attachment created by HealthChain" + + attachment = create_single_attachment( + content_type=content_type, + data=attachment_data, + url=url, + title=title, + ) + + content: Dict[str, Any] = { + "attachment": attachment, + } + + if language: + content["language"] = language + + content.update(kwargs) + + return content + + def set_condition_category(condition: Condition, category: str) -> Condition: """ Set the category of a FHIR Condition to either 'problem-list-item' or 'encounter-diagnosis'. diff --git a/tests/fhir/test_helpers.py b/tests/fhir/test_helpers.py index 5da02ae1..75763b16 100644 --- a/tests/fhir/test_helpers.py +++ b/tests/fhir/test_helpers.py @@ -5,6 +5,7 @@ from fhir.resources.codeablereference import CodeableReference from fhir.resources.documentreference import DocumentReference from fhir.resources.attachment import Attachment +from fhir.resources.coding import Coding from datetime import datetime @@ -18,6 +19,7 @@ set_condition_category, create_single_attachment, create_document_reference, + create_document_reference_content, read_content_attachment, add_provenance_metadata, add_coding_to_codeable_concept, @@ -251,6 +253,54 @@ def test_read_attachment_with_url(): assert attachments[0]["metadata"]["creation"] is not None +def test_create_document_reference_content_with_data(): + """Test creating DocumentReferenceContent with inline data.""" + content = create_document_reference_content( + attachment_data="Test data", content_type="text/plain", title="Test Document" + ) + + assert "attachment" in content + assert content["attachment"].contentType == "text/plain" + assert content["attachment"].title == "Test Document" + assert content["attachment"].data is not None + assert content["language"] == "en-US" + + +def test_create_document_reference_content_with_url(): + """Test creating DocumentReferenceContent with URL.""" + content = create_document_reference_content( + url="https://example.com/doc.pdf", + content_type="application/pdf", + title="External Document", + ) + + assert "attachment" in content + assert content["attachment"].url == "https://example.com/doc.pdf" + assert content["attachment"].contentType == "application/pdf" + + +def test_create_document_reference_content_custom_language(): + """Test creating DocumentReferenceContent with custom language.""" + content = create_document_reference_content( + attachment_data="Données de test", language="fr-FR" + ) + + assert content["language"] == "fr-FR" + + +def test_create_document_reference_content_with_kwargs(): + """Test creating DocumentReferenceContent with additional fields.""" + content = create_document_reference_content( + attachment_data="Test", + format=Coding( + system="http://ihe.net/fhir/ValueSet/IHE.FormatCode.codesystem", + code="urn:ihe:pcc:handp:2008", + ), + ) + + assert "format" in content + + def test_create_resource_from_dict_success_and_failure(): cond_dict = { "id": "x",