Skip to content

Commit ac1040c

Browse files
authored
[PRMP-337] Refactor custodian assignment logic in document reference (#801)
* [PRMP-337] Refactor custodian assignment logic in document reference handling * [PRMP-337] Update PCSE ODS code to new value
1 parent 97ce052 commit ac1040c

File tree

6 files changed

+30
-14
lines changed

6 files changed

+30
-14
lines changed

lambdas/models/fhir/R4/fhir_document_reference.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import Any, Dict, List, Literal, Optional
22

3+
from enums.patient_ods_inactive_status import PatientOdsInactiveStatus
34
from enums.snomed_codes import SnomedCode, SnomedCodes
45
from models.document_reference import DocumentReference as NdrDocumentReference
56
from models.fhir.R4.base_models import (
@@ -12,6 +13,8 @@
1213
)
1314
from pydantic import BaseModel, Field
1415

16+
from utils.ods_utils import PCSE_ODS_CODE
17+
1518
# Constants
1619
FHIR_BASE_URL = "https://fhir.nhs.uk/Id"
1720
SNOMED_URL = "http://snomed.info/sct"
@@ -224,6 +227,8 @@ def create_fhir_document_reference_object(
224227
Returns:
225228
DocumentReference: A FHIR DocumentReference resource
226229
"""
230+
if document.custodian in PatientOdsInactiveStatus.list():
231+
document.custodian = PCSE_ODS_CODE
227232

228233
return DocumentReference(
229234
resourceType="DocumentReference",

lambdas/services/bulk_upload_service.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import pydantic
77
from botocore.exceptions import ClientError
8+
89
from enums.patient_ods_inactive_status import PatientOdsInactiveStatus
910
from enums.snomed_codes import SnomedCodes
1011
from enums.upload_status import UploadStatus
@@ -39,7 +40,6 @@
3940
validate_filename_with_patient_details_strict,
4041
validate_lg_file_names,
4142
)
42-
from utils.ods_utils import PCSE_ODS_CODE
4343
from utils.request_context import request_context
4444
from utils.unicode_utils import (
4545
contains_accent_char,
@@ -401,17 +401,13 @@ def convert_to_document_reference(
401401
).strftime("%Y-%m-%d")
402402
else:
403403
scan_date_formatted = None
404-
if current_gp_ods in PatientOdsInactiveStatus.list():
405-
custodian = PCSE_ODS_CODE
406-
else:
407-
custodian = current_gp_ods
408404
document_reference = DocumentReference(
409405
id=str(uuid.uuid4()),
410406
nhs_number=nhs_number,
411407
file_name=file_name,
412408
s3_bucket_name=s3_bucket_name,
413409
current_gp_ods=current_gp_ods,
414-
custodian=custodian,
410+
custodian=current_gp_ods,
415411
author=file_metadata.gp_practice_code,
416412
document_scan_creation=scan_date_formatted,
417413
doc_status="preliminary",

lambdas/services/post_fhir_document_reference_service.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,7 @@ def _create_document_reference(
163163

164164
custodian = fhir_doc.custodian.identifier.value if fhir_doc.custodian else None
165165
if not custodian:
166-
custodian = (
167-
current_gp_ods
168-
if current_gp_ods not in PatientOdsInactiveStatus.list()
169-
else PCSE_ODS_CODE
170-
)
166+
custodian = current_gp_ods
171167

172168
sub_folder = (
173169
"user_upload"

lambdas/tests/unit/services/test_get_fhir_document_reference_service.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
GetFhirDocumentReferenceException,
1414
InvalidDocTypeException,
1515
)
16+
from utils.lambda_exceptions import GetFhirDocumentReferenceException
17+
from utils.ods_utils import PCSE_ODS_CODE
1618

1719

1820
@pytest.fixture
@@ -191,3 +193,19 @@ def test_create_document_reference_fhir_response_non_final_status(
191193
# Verify methods were not called
192194
patched_service.s3_service.get_binary_file.assert_not_called()
193195
patched_service.get_presigned_url.assert_not_called()
196+
197+
def test_create_document_reference_fhir_response_when_patient_is_deceased(
198+
patched_service, mocker
199+
):
200+
"""Test FHIR response creation for documents with non-final status."""
201+
test_doc = create_test_doc_store_refs()[0]
202+
modified_doc = copy.deepcopy(test_doc)
203+
modified_doc.custodian = "DECE"
204+
modified_doc.doc_status = "preliminary"
205+
206+
patched_service.get_presigned_url = mocker.MagicMock()
207+
208+
result = patched_service.create_document_reference_fhir_response(modified_doc)
209+
result_json = json.loads(result)
210+
211+
assert result_json["custodian"]["identifier"]["value"] == PCSE_ODS_CODE

lambdas/tests/unit/services/test_post_fhir_document_reference_service.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pytest
44
from botocore.exceptions import ClientError
5+
56
from enums.lambda_error import LambdaError
67
from enums.mtls import MtlsCommonNames
78
from enums.snomed_codes import SnomedCode, SnomedCodes
@@ -532,7 +533,7 @@ def test_create_document_reference_without_custodian(mock_service, mocker):
532533
fhir_doc.custodian = None
533534

534535
doc_type = SnomedCode(code="test-code", display_name="Test Type")
535-
current_gp_ods = "C13579"
536+
current_gp_ods = "REST"
536537

537538
result = mock_service._create_document_reference(
538539
nhs_number="9000000009",
@@ -543,7 +544,7 @@ def test_create_document_reference_without_custodian(mock_service, mocker):
543544

544545
assert (
545546
result.custodian == current_gp_ods
546-
) # Custodian should default to current_gp_ods
547+
)
547548

548549

549550
def test_create_fhir_response_with_presigned_url(mock_service, mocker):

lambdas/utils/ods_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Sometimes, a patient will not have a generalPractitioner on PDS. Internally, we can also add codes to mark inactive
88
patients for reporting purposes. The only values that should be considered 'active' are valid ODS codes.
99
"""
10-
PCSE_ODS_CODE = "X4S4L"
10+
PCSE_ODS_CODE = "8JD29"
1111

1212

1313
def is_ods_code_active(gp_ods) -> bool:

0 commit comments

Comments
 (0)