-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PRMDR-477 refactor document reference search handler (#209)
* [PRMDR-477] refactor doc ref search lambda --------- Co-authored-by: NogaNHS <noga.sasson1@nhs.net>
- Loading branch information
1 parent
2949f8c
commit 5c4bf62
Showing
10 changed files
with
304 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import json | ||
import os | ||
from json import JSONDecodeError | ||
|
||
from botocore.exceptions import ClientError | ||
from enums.metadata_field_names import DocumentReferenceMetadataFields | ||
from models.document_reference import DocumentReference | ||
from pydantic import ValidationError | ||
from services.document_service import DocumentService | ||
from utils.audit_logging_setup import LoggingService | ||
from utils.exceptions import DynamoServiceException | ||
from utils.lambda_exceptions import DocumentRefSearchException | ||
|
||
logger = LoggingService(__name__) | ||
|
||
|
||
class DocumentReferenceSearchService(DocumentService): | ||
def get_document_references(self, nhs_number: str): | ||
try: | ||
list_of_table_names = json.loads(os.environ["DYNAMODB_TABLE_LIST"]) | ||
|
||
results: list[dict] = [] | ||
for table_name in list_of_table_names: | ||
logger.info(f"Searching for results in {table_name}") | ||
documents: list[ | ||
DocumentReference | ||
] = self.fetch_documents_from_table_with_filter( | ||
nhs_number, | ||
table_name, | ||
attr_filter={DocumentReferenceMetadataFields.DELETED.value: ""}, | ||
) | ||
|
||
results.extend( | ||
document.model_dump( | ||
include={"file_name", "created", "virus_scanner_result"}, | ||
by_alias=True, | ||
) | ||
for document in documents | ||
) | ||
return results | ||
except ( | ||
JSONDecodeError, | ||
ValidationError, | ||
ClientError, | ||
DynamoServiceException, | ||
) as e: | ||
logger.error( | ||
f"An error occurred when using document reference search service: {str(e)}", | ||
) | ||
raise DocumentRefSearchException( | ||
500, "An error occurred when searching for available documents" | ||
) |
103 changes: 24 additions & 79 deletions
103
lambdas/tests/unit/handlers/test_document_reference_search_handler.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.