Skip to content

Commit

Permalink
[PRMP-1257] Add log messages to adhere to audit requirements (#479)
Browse files Browse the repository at this point in the history
Add log messages to adhere to audit requirements


---------

Co-authored-by: robg-nhs <[email protected]>
  • Loading branch information
steph-torres-nhs and robg-nhs authored Jan 17, 2025
1 parent f6b1733 commit 1caa1ad
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 9 deletions.
10 changes: 10 additions & 0 deletions lambdas/handlers/manage_nrl_pointer_handler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from datetime import datetime

from enums.nrl_sqs_upload import NrlActionTypes
from models.nrl_fhir_document_reference import FhirDocumentReference
Expand Down Expand Up @@ -58,7 +59,16 @@ def lambda_handler(event, context):
.json()
)

logger.info(
f"Create pointer request: Body: {json.loads(document)}, "
f"RequestURL: {nrl_api_service.endpoint}, "
"HTTP Verb: POST, "
f"NHS Number: {nrl_message.nhs_number}, "
f"ODS Code: {nrl_api_service.end_user_ods_code}, "
f"Datetime: {int(datetime.now().timestamp())} "
)
nrl_api_service.create_new_pointer(json.loads(document))

case NrlActionTypes.DELETE:
nrl_api_service.delete_pointer(
nrl_message.nhs_number, nrl_message.snomed_code_doc_type
Expand Down
18 changes: 16 additions & 2 deletions lambdas/models/nrl_fhir_document_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,27 @@ def build_fhir_dict(self):
"content": [
{
"attachment": self.attachment.model_dump(
by_alias=True, exclude_none=True, exclude_defaults=True
by_alias=True, exclude_none=True
),
"format": {
"system": "https://fhir.nhs.uk/England/CodeSystem/England-NRLFormatCode",
"code": "urn:nhs-ic:unstructured",
"display": "Unstructured document",
},
"extension": [
{
"url": "https://fhir.nhs.uk/England/StructureDefinition/Extension-England-ContentStability",
"valueCodeableConcept": {
"coding": [
{
"system": "https://fhir.nhs.uk/England/CodeSystem/England-NRLContentStability",
"code": "static",
"display": "Static",
}
]
},
}
],
}
],
"context": {
Expand All @@ -84,7 +98,7 @@ def build_fhir_dict(self):
"display": self.snomed_code_practice_setting.display_name,
}
]
}
},
},
}
return DocumentReference(**structure_json)
16 changes: 10 additions & 6 deletions lambdas/models/nrl_sqs_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@


class NrlAttachment(BaseModel):
model_config = ConfigDict(
alias_generator=AliasGenerator(serialization_alias=to_camel),
use_enum_values=True,
)
content_type: str = "application/pdf"
language: str = "en-UK"
url: str = ""
size: int = 0
hash: str = ""
title: str = ""
creation: str = ""
url: Optional[str] = None
size: Optional[int] = None
hash: Optional[str] = None
title: Optional[str] = None
creation: Optional[str] = None


class NrlSqsMessage(BaseModel):
Expand All @@ -27,6 +31,6 @@ class NrlSqsMessage(BaseModel):
snomed_code_practice_setting: SnomedCode = (
SnomedCodes.GENERAL_MEDICAL_PRACTICE.value
)
description: str = ""
description: Optional[str] = None
attachment: Optional[NrlAttachment] = None
action: str
1 change: 1 addition & 0 deletions lambdas/services/bulk_upload_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ def handle_sqs_message(self, message: dict):
)
doc_details = NrlAttachment(
url=document_api_endpoint,
content_type="application/pdf",
)
nrl_sqs_message = NrlSqsMessage(
nhs_number=staging_metadata.nhs_number,
Expand Down
33 changes: 32 additions & 1 deletion lambdas/services/nrl_api_service.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import uuid
from datetime import datetime

import requests
from enums.snomed_codes import SnomedCode
Expand All @@ -10,6 +11,7 @@
from utils.exceptions import NrlApiException

logger = LoggingService(__name__)
NRL_USER_ID = "National-Document-Repository"


class NrlApiService:
Expand Down Expand Up @@ -45,6 +47,11 @@ def create_new_pointer(self, body, retry_on_expired: bool = True):
response = self.session.post(
url=self.endpoint, headers=self.headers, json=body
)
logger.info(
f"Create pointer response: Status code: {response.status_code}, "
f"Body: {response.json()}, "
f"Headers: {response.headers}"
)
response.raise_for_status()
logger.info("Successfully created new pointer")
except (ConnectionError, Timeout, HTTPError) as e:
Expand All @@ -70,6 +77,18 @@ def get_pointer(
response = self.session.get(
url=self.endpoint, params=params, headers=self.headers
)
logger.info(
f"Get pointer request: URL: {response.url}, "
"HTTP Verb: GET, "
f"ODS Code: {self.end_user_ods_code}, "
f"Datetime: {int(datetime.now().timestamp())}, "
f"UserID: {self.end_user_ods_code} - {NRL_USER_ID}"
)
logger.info(
f"Get pointer response: Status code: {response.status_code}, "
f"Body: {response.json()}, "
f"Headers: {response.headers}"
)
response.raise_for_status()
return response.json()
except HTTPError as e:
Expand All @@ -90,7 +109,19 @@ def delete_pointer(self, nhs_number, record_type: SnomedCode = None):
url_endpoint = self.endpoint + f"/{pointer_id}"
try:
response = self.session.delete(url=url_endpoint, headers=self.headers)
logger.info(response.json())
logger.info(
f"Delete pointer request: URL: {url_endpoint}, "
f"HTTP Verb: DELETE, "
f"ODS Code: {self.end_user_ods_code}, "
f"NHS Number: {nhs_number}, "
f"Datetime: {int(datetime.now().timestamp())}, "
f"UserID: {self.end_user_ods_code} - {NRL_USER_ID}."
)
logger.info(
f"Delete pointer response: Body: {response.json()}, "
f"Status Code: {response.status_code}, "
f"Headers: {response.headers}"
)
response.raise_for_status()
except HTTPError as e:
logger.error(e.response.json())
Expand Down
1 change: 1 addition & 0 deletions lambdas/tests/unit/services/test_nrl_api_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def test_create_new_pointer(nrl_service):
def test_create_new_pointer_raise_error(nrl_service):
mock_body = {"test": "tests"}
response = Response()
response._content = b"{}"
response.status_code = 400
nrl_service.session.post.return_value = response
pytest.raises(NrlApiException, nrl_service.create_new_pointer, mock_body)
Expand Down

0 comments on commit 1caa1ad

Please sign in to comment.