Skip to content

Commit

Permalink
[PRMP-1287] remove NrlAttachment model
Browse files Browse the repository at this point in the history
  • Loading branch information
NogaNHS committed Jan 3, 2025
1 parent ff13b52 commit b11dac0
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 115 deletions.
10 changes: 6 additions & 4 deletions lambdas/handlers/manage_nrl_pointer_handler.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json

from enums.nrl_sqs_upload import NrlActionTypes
from models.fhir.R4.nrl_fhir_document_reference import FhirDocumentReference
from models.fhir.R4.nrl_fhir_document_reference import DocumentReferenceInfo
from models.nrl_sqs_message import NrlSqsMessage
from services.base.nhs_oauth_service import NhsOauthService
from services.base.ssm_service import SSMService
Expand Down Expand Up @@ -49,11 +49,13 @@ def lambda_handler(event, context):
)
match nrl_message.action:
case NrlActionTypes.CREATE:
document = FhirDocumentReference(
document = DocumentReferenceInfo(
**nrl_verified_message,
custodian=nrl_api_service.end_user_ods_code,
).document_ref_dict()
nrl_api_service.create_new_pointer(json.dumps(document))
).create_fhir_document_reference_object()
nrl_api_service.create_new_pointer(
document.model_dump_json(exclude_none=True)
)
case NrlActionTypes.DELETE:
nrl_api_service.delete_pointer(
nrl_message.nhs_number, nrl_message.snomed_code_doc_type
Expand Down
151 changes: 72 additions & 79 deletions lambdas/models/fhir/R4/nrl_fhir_document_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,89 +8,10 @@
Period,
Reference,
)
from models.nrl_sqs_message import NrlAttachment
from pydantic import BaseModel, ConfigDict
from pydantic.alias_generators import to_camel


class FhirDocumentReference(BaseModel):
model_config = ConfigDict(alias_generator=to_camel)
nhs_number: str
custodian: str
snomed_code_doc_type: SnomedCode = SnomedCodes.LLOYD_GEORGE.value
snomed_code_category: SnomedCode = SnomedCodes.CARE_PLAN.value
snomed_code_practice_setting: SnomedCode = (
SnomedCodes.GENERAL_MEDICAL_PRACTICE.value
)
attachment: Optional[NrlAttachment] = NrlAttachment()

def document_ref_dict(self):
fhir_base_url = "https://fhir.nhs.uk/Id"
snomed_url = "http://snomed.info/sct"

dooc = DocumentReference(
subject={
"identifier": {
"system": fhir_base_url + "/nhs-number",
"value": self.nhs_number,
}
},
custodian={
"identifier": {
"system": fhir_base_url + "/ods-organization-code",
"value": self.custodian,
},
},
type={
"coding": [
{
"system": snomed_url,
"code": self.snomed_code_doc_type.code,
"display": self.snomed_code_doc_type.display_name,
}
]
},
content=[
{
"attachment": self.attachment.model_dump(
by_alias=True, exclude_none=True
),
}
],
category=[
{
"coding": [
{
"system": snomed_url,
"code": self.snomed_code_category.code,
"display": self.snomed_code_category.display_name,
}
]
}
],
author=[
{
"identifier": {
"system": fhir_base_url + "/ods-organization-code",
"value": self.custodian,
}
}
],
context={
"practiceSetting": {
"coding": [
{
"system": snomed_url,
"code": self.snomed_code_practice_setting.code,
"display": self.snomed_code_practice_setting.display_name,
}
]
}
},
)
return dooc.model_dump(exclude_none=True)


class NRLFormatCode(Coding):
system: Literal["https://fhir.nhs.uk/England/CodeSystem/England-NRLFormatCode"] = (
"https://fhir.nhs.uk/England/CodeSystem/England-NRLFormatCode"
Expand Down Expand Up @@ -162,3 +83,75 @@ class DocumentReference(BaseModel):
custodian: Optional[Reference] = None
content: List[DocumentReferenceContent]
context: Optional[DocumentReferenceContext] = None


class DocumentReferenceInfo(BaseModel):
model_config = ConfigDict(alias_generator=to_camel)
nhs_number: str
custodian: str
snomed_code_doc_type: SnomedCode = SnomedCodes.LLOYD_GEORGE.value
snomed_code_category: SnomedCode = SnomedCodes.CARE_PLAN.value
snomed_code_practice_setting: SnomedCode = (
SnomedCodes.GENERAL_MEDICAL_PRACTICE.value
)
attachment: Optional[Attachment] = Attachment()

def create_fhir_document_reference_object(self):
fhir_base_url = "https://fhir.nhs.uk/Id"
snomed_url = "http://snomed.info/sct"

fhir_document_ref = DocumentReference(
subject={
"identifier": {
"system": fhir_base_url + "/nhs-number",
"value": self.nhs_number,
}
},
custodian={
"identifier": {
"system": fhir_base_url + "/ods-organization-code",
"value": self.custodian,
},
},
type={
"coding": [
{
"system": snomed_url,
"code": self.snomed_code_doc_type.code,
"display": self.snomed_code_doc_type.display_name,
}
]
},
content=[{"attachment": self.attachment}],
category=[
{
"coding": [
{
"system": snomed_url,
"code": self.snomed_code_category.code,
"display": self.snomed_code_category.display_name,
}
]
}
],
author=[
{
"identifier": {
"system": fhir_base_url + "/ods-organization-code",
"value": self.custodian,
}
}
],
context={
"practiceSetting": {
"coding": [
{
"system": snomed_url,
"code": self.snomed_code_practice_setting.code,
"display": self.snomed_code_practice_setting.display_name,
}
]
}
},
)
return fhir_document_ref
17 changes: 2 additions & 15 deletions lambdas/models/nrl_sqs_message.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
from typing import Optional

from enums.snomed_codes import SnomedCode, SnomedCodes
from models.fhir.R4.nrl_fhir_document_reference import Attachment
from pydantic import AliasGenerator, BaseModel, ConfigDict
from pydantic.alias_generators import to_camel


class NrlAttachment(BaseModel):
model_config = ConfigDict(
alias_generator=AliasGenerator(serialization_alias=to_camel)
)

content_type: str = "application/pdf"
language: str = "en-UK"
url: str = ""
size: int = 0
hash: str = ""
title: str = ""
creation: str = ""


class NrlSqsMessage(BaseModel):
model_config = ConfigDict(
alias_generator=AliasGenerator(serialization_alias=to_camel),
Expand All @@ -32,5 +19,5 @@ class NrlSqsMessage(BaseModel):
SnomedCodes.GENERAL_MEDICAL_PRACTICE.value
)
description: str = ""
attachment: Optional[NrlAttachment] = None
attachment: Optional[Attachment] = None
action: str
5 changes: 3 additions & 2 deletions lambdas/services/bulk_upload_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
from enums.patient_ods_inactive_status import PatientOdsInactiveStatus
from enums.upload_status import UploadStatus
from enums.virus_scan_result import VirusScanResult
from models.fhir.R4.nrl_fhir_document_reference import Attachment
from models.nhs_document_reference import NHSDocumentReference
from models.nrl_sqs_message import NrlAttachment, NrlSqsMessage
from models.nrl_sqs_message import NrlSqsMessage
from models.staging_metadata import MetadataFile, StagingMetadata
from repositories.bulk_upload.bulk_upload_dynamo_repository import (
BulkUploadDynamoRepository,
Expand Down Expand Up @@ -279,7 +280,7 @@ def handle_sqs_message(self, message: dict):
+ "/DocumentReference/"
+ last_document_processed.id
)
doc_details = NrlAttachment(
doc_details = Attachment(
url=document_api_endpoint,
)
nrl_sqs_message = NrlSqsMessage(
Expand Down
19 changes: 11 additions & 8 deletions lambdas/services/nrl_get_document_reference_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
from enums.lambda_error import LambdaError
from enums.patient_ods_inactive_status import PatientOdsInactiveStatus
from models.document_reference import DocumentReference
from models.fhir.R4.nrl_fhir_document_reference import FhirDocumentReference
from models.nrl_sqs_message import NrlAttachment
from models.fhir.R4.nrl_fhir_document_reference import Attachment, DocumentReferenceInfo
from requests.exceptions import HTTPError
from services.base.s3_service import S3Service
from services.base.ssm_service import SSMService
Expand Down Expand Up @@ -50,16 +49,20 @@ def handle_get_document_reference_request(self, document_id, bearer_token):
def create_document_reference_fhir_response(
self, document_reference: DocumentReference, presign_url: str
) -> dict:
document_details = NrlAttachment(
document_details = Attachment(
url=presign_url,
title=document_reference.file_name,
creation=document_reference.created,
)
fhir_document_reference = FhirDocumentReference(
nhsNumber=document_reference.nhs_number,
custodian=document_reference.current_gp_ods,
attachment=document_details,
).document_ref_dict()
fhir_document_reference = (
DocumentReferenceInfo(
nhsNumber=document_reference.nhs_number,
custodian=document_reference.current_gp_ods,
attachment=document_details,
)
.create_fhir_document_reference_object()
.model_dump(exclude_none=True)
)
return fhir_document_reference

def is_user_allowed_to_see_file(self, user_details, document_reference):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import pytest
from enums.snomed_codes import SnomedCodes
from handlers.manage_nrl_pointer_handler import lambda_handler
from models.nrl_sqs_message import NrlAttachment, NrlSqsMessage
from models.fhir.R4.nrl_fhir_document_reference import Attachment
from models.nrl_sqs_message import NrlSqsMessage
from utils.exceptions import NrlApiException


Expand All @@ -14,7 +15,7 @@ def mock_service(mocker):


def build_test_sqs_message(action="create"):
doc_details = NrlAttachment(
doc_details = Attachment(
url="https://example.org/my-doc.pdf",
)
sqs_message = NrlSqsMessage(
Expand Down
5 changes: 3 additions & 2 deletions lambdas/tests/unit/services/test_bulk_upload_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from enums.upload_status import UploadStatus
from enums.virus_scan_result import SCAN_RESULT_TAG_KEY, VirusScanResult
from freezegun import freeze_time
from models.nrl_sqs_message import NrlAttachment, NrlSqsMessage
from models.fhir.R4.nrl_fhir_document_reference import Attachment
from models.nrl_sqs_message import NrlSqsMessage
from models.pds_models import Patient
from repositories.bulk_upload.bulk_upload_s3_repository import BulkUploadS3Repository
from repositories.bulk_upload.bulk_upload_sqs_repository import BulkUploadSqsRepository
Expand Down Expand Up @@ -238,7 +239,7 @@ def test_handle_sqs_message_happy_path_single_file(
mock_ods_validation,
):
TEST_STAGING_METADATA.retries = 0
mock_nrl_attachment = NrlAttachment(
mock_nrl_attachment = Attachment(
url=f"/DocumentReference/{TEST_DOCUMENT_REFERENCE.id}",
)
mock_nrl_message = NrlSqsMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ def mock_fetch_user_info(patched_service, mocker):


@pytest.mark.parametrize(
"input, expected",
"role_code, expected",
[
("S8001:G8005:R8000", "R8000"),
("S8001:G8005:R8015", "R8015"),
("S8001:G8005:R8008", "R8008"),
],
)
def test_process_role_code_returns_correct_role(patched_service, input, expected):
assert patched_service.process_role_code(input) == expected
def test_process_role_code_returns_correct_role(patched_service, role_code, expected):
assert patched_service.process_role_code(role_code) == expected


def test_get_user_roles_and_ods_codes(patched_service):
Expand Down

0 comments on commit b11dac0

Please sign in to comment.