Skip to content

Commit 60b18ac

Browse files
authored
[NDR-310] Fix bug introduced by the S3 file key index. (#874)
1 parent dfc2768 commit 60b18ac

File tree

6 files changed

+331
-75
lines changed

6 files changed

+331
-75
lines changed

lambdas/models/document_reference.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,7 @@ class Subject(BaseModel):
4646

4747

4848
class UpdateBody(BaseModel):
49-
model_config = ConfigDict(
50-
validate_by_alias=True,
51-
alias_generator=to_camel
52-
)
49+
model_config = ConfigDict(validate_by_alias=True, alias_generator=to_camel)
5350
resource_type: str
5451
subject: Subject
5552
content: list[SingleAttachment]
@@ -61,7 +58,7 @@ class GenericEventModel(BaseModel):
6158
validate_by_alias=True,
6259
validate_by_name=True,
6360
populate_by_name=True,
64-
alias_generator=to_camel
61+
alias_generator=to_camel,
6562
)
6663
http_method: str
6764
query_string_parameters: dict
@@ -190,7 +187,7 @@ def _build_s3_key(data: dict) -> str:
190187
s3_key = "/".join(key_parts)
191188

192189
return s3_key
193-
190+
194191
@staticmethod
195192
def _build_final_s3_key(data: dict) -> str:
196193
"""Build the S3 key from document data."""
@@ -244,4 +241,3 @@ def infer_doc_status(self) -> str | None:
244241
if self.uploading:
245242
return "preliminary"
246243
return None
247-

lambdas/services/base/dynamo_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def query_table(
4141
table_name,
4242
search_key,
4343
search_condition: str,
44-
index_name: str = None,
44+
index_name: str | None = None,
4545
requested_fields: list[str] = None,
4646
query_filter: Attr | ConditionBase = None,
4747
) -> list[dict]:

lambdas/services/document_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def fetch_documents_from_table(
5555
table: str,
5656
search_condition: str,
5757
search_key: str,
58-
index_name: str = None,
58+
index_name: str | None = None,
5959
query_filter: Attr | ConditionBase = None,
6060
) -> list[DocumentReference]:
6161
documents = []

lambdas/services/upload_document_reference_service.py

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def __init__(self):
3333
self.staging_s3_bucket_name = os.environ["STAGING_STORE_BUCKET_NAME"]
3434
self.table_name = os.environ["LLOYD_GEORGE_DYNAMODB_NAME"]
3535
self.destination_bucket_name = os.environ["LLOYD_GEORGE_BUCKET_NAME"]
36+
self.doc_type = SnomedCodes.LLOYD_GEORGE.value
3637
self.document_service = DocumentService()
3738
self.dynamo_service = DynamoDBService()
3839
self.virus_scan_service = get_virus_scan_service()
@@ -67,7 +68,7 @@ def handle_upload_document_reference_request(
6768
logger.error(f"Unexpected error processing document reference: {str(e)}")
6869
logger.error(f"Failed to process document reference: {object_key}")
6970
return
70-
71+
7172
def _get_infrastructure_for_document_key(self, object_parts: list[str]) -> None:
7273
doc_type = None
7374
if object_parts[0] != "fhir_upload" or not (
@@ -76,6 +77,7 @@ def _get_infrastructure_for_document_key(self, object_parts: list[str]) -> None:
7677
return
7778

7879
try:
80+
self.doc_type = doc_type
7981
self.table_name = self.table_router.resolve(doc_type)
8082
self.destination_bucket_name = self.bucket_router.resolve(doc_type)
8183
except KeyError:
@@ -126,7 +128,9 @@ def _process_preliminary_document_reference(
126128
):
127129
"""Process the preliminary (uploading) document reference with virus scanning and file operations"""
128130
try:
129-
virus_scan_result = self._perform_virus_scan(preliminary_document_reference, object_key)
131+
virus_scan_result = self._perform_virus_scan(
132+
preliminary_document_reference, object_key
133+
)
130134
preliminary_document_reference.virus_scanner_result = virus_scan_result
131135

132136
if virus_scan_result == VirusScanResult.CLEAN:
@@ -143,22 +147,38 @@ def _process_preliminary_document_reference(
143147
preliminary_document_reference.uploaded = True
144148
preliminary_document_reference.uploading = False
145149

146-
updated_doc_status = None
147-
if virus_scan_result != VirusScanResult.CLEAN:
148-
updated_doc_status = "cancelled"
150+
if self.doc_type.code != SnomedCodes.PATIENT_DATA.value.code:
151+
updated_doc_status = None
152+
if virus_scan_result != VirusScanResult.CLEAN:
153+
updated_doc_status = "cancelled"
149154

150-
preliminary_document_reference.doc_status = updated_doc_status
151-
self._update_dynamo_table(preliminary_document_reference)
152-
else:
153-
updated_doc_status = "final"
154-
preliminary_document_reference.doc_status = updated_doc_status
155+
preliminary_document_reference.doc_status = updated_doc_status
156+
self._update_dynamo_table(preliminary_document_reference)
157+
else:
158+
updated_doc_status = "final"
159+
preliminary_document_reference.doc_status = updated_doc_status
155160

156-
self._finalize_and_supersede_with_transaction(
157-
preliminary_document_reference
158-
)
161+
self._finalize_and_supersede_with_transaction(
162+
preliminary_document_reference
163+
)
159164

160-
# Update NRL Pointer
161-
# TODO: PRMP-390
165+
# Update NRL Pointer
166+
# TODO: PRMP-390
167+
#
168+
else:
169+
try:
170+
preliminary_document_reference.doc_status = (
171+
"cancelled"
172+
if virus_scan_result != VirusScanResult.CLEAN
173+
else "final"
174+
)
175+
176+
self._update_dynamo_table(preliminary_document_reference)
177+
except Exception as e:
178+
logger.error(
179+
f"Error processing document reference {preliminary_document_reference.id}: {str(e)}"
180+
)
181+
raise
162182

163183
except TransactionConflictException as e:
164184
logger.error(
@@ -300,7 +320,9 @@ def _finalize_and_supersede_with_transaction(self, new_document: DocumentReferen
300320
new_document.uploading = False
301321
new_document.file_size = None
302322
self._update_dynamo_table(new_document)
303-
self.delete_file_from_bucket(new_document.file_location, new_document.s3_version_id)
323+
self.delete_file_from_bucket(
324+
new_document.file_location, new_document.s3_version_id
325+
)
304326
raise
305327
except Exception as e:
306328
logger.error(
@@ -350,22 +372,23 @@ def copy_files_from_staging_bucket(
350372
"""Copy files from staging bucket to destination bucket"""
351373
try:
352374
logger.info("Copying files from staging bucket")
353-
354-
dest_file_key = document_reference.s3_file_key
375+
dest_file_key = f"{document_reference.nhs_number}/{document_reference.id}"
376+
if self.doc_type.code != SnomedCodes.PATIENT_DATA.value.code:
377+
dest_file_key = document_reference.s3_file_key
355378

356379
copy_result = self.s3_service.copy_across_bucket(
357380
source_bucket=self.staging_s3_bucket_name,
358381
source_file_key=source_file_key,
359382
dest_bucket=self.destination_bucket_name,
360383
dest_file_key=dest_file_key,
361384
)
362-
385+
if self.doc_type.code == SnomedCodes.PATIENT_DATA.value.code:
386+
document_reference.s3_file_key = dest_file_key
363387
document_reference.s3_bucket_name = self.destination_bucket_name
364388
document_reference.file_location = document_reference._build_s3_location(
365389
self.destination_bucket_name, dest_file_key
366390
)
367391
document_reference.s3_version_id = copy_result.get("VersionId")
368-
369392
return copy_result
370393

371394
except ClientError as e:
@@ -386,7 +409,9 @@ def delete_file_from_staging_bucket(self, source_file_key: str):
386409
def delete_file_from_bucket(self, file_location: str, version_id: str):
387410
"""Delete file from bucket"""
388411
try:
389-
s3_bucket_name, source_file_key = DocumentReference._parse_s3_location(file_location)
412+
s3_bucket_name, source_file_key = DocumentReference._parse_s3_location(
413+
file_location
414+
)
390415
logger.info(
391416
f"Deleting file from bucket: {s3_bucket_name}/{source_file_key}"
392417
)
@@ -412,6 +437,8 @@ def _update_dynamo_table(
412437
"uploaded",
413438
"uploading",
414439
}
440+
if self.doc_type.code == SnomedCodes.PATIENT_DATA.value.code:
441+
update_fields.add("s3_file_key")
415442

416443
self.document_service.update_document(
417444
table_name=self.table_name,

0 commit comments

Comments
 (0)