diff --git a/healthchain/fhir/__init__.py b/healthchain/fhir/__init__.py index 82e68a2e..312c1edc 100644 --- a/healthchain/fhir/__init__.py +++ b/healthchain/fhir/__init__.py @@ -9,6 +9,7 @@ set_problem_list_item_category, read_content_attachment, create_document_reference, + create_document_reference_content, create_single_attachment, create_resource_from_dict, ) @@ -30,6 +31,7 @@ "set_problem_list_item_category", "read_content_attachment", "create_document_reference", + "create_document_reference_content", "create_single_attachment", "create_resource_from_dict", # Bundle operations diff --git a/healthchain/fhir/helpers.py b/healthchain/fhir/helpers.py index 444a6ea5..d8d9261a 100644 --- a/healthchain/fhir/helpers.py +++ b/healthchain/fhir/helpers.py @@ -280,7 +280,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, @@ -327,6 +326,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 read_content_attachment( document_reference: DocumentReference, include_data: bool = True, diff --git a/tests/fhir/test_helpers.py b/tests/fhir/test_helpers.py index da0fb684..25872981 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 @@ -17,6 +18,7 @@ set_problem_list_item_category, create_single_attachment, create_document_reference, + create_document_reference_content, read_content_attachment, ) @@ -245,3 +247,52 @@ def test_read_attachment_with_url(): assert attachments[0]["metadata"]["content_type"] == "application/pdf" assert attachments[0]["metadata"]["title"] == "Test URL Doc" 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 \ No newline at end of file