-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathupdate_document_reference.py
More file actions
146 lines (125 loc) · 5.16 KB
/
update_document_reference.py
File metadata and controls
146 lines (125 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import urllib.parse
from pydantic import ValidationError
from nrlf.core.codes import SpineErrorConcept
from nrlf.core.constants import PointerTypes
from nrlf.core.decorators import DocumentPointerRepository, request_handler
from nrlf.core.dynamodb.model import DocumentPointer
from nrlf.core.errors import OperationOutcomeError
from nrlf.core.logger import LogReference, logger
from nrlf.core.model import ConnectionMetadata, UpdateDocumentReferencePathParams
from nrlf.core.response import NRLResponse, Response, SpineErrorResponse
from nrlf.core.utils import create_fhir_instant
from nrlf.core.validators import DocumentReferenceValidator
from nrlf.producer.fhir.r4.model import DocumentReference, Meta
def _set_update_time_fields(
update_time: str, document_reference: DocumentReference
) -> DocumentReference:
"""
Set the lastUpdated timestamp on the provided DocumentReference
"""
if not document_reference.meta:
document_reference.meta = Meta()
document_reference.meta.lastUpdated = update_time
return document_reference
@request_handler(body=DocumentReference, path=UpdateDocumentReferencePathParams)
def handler(
metadata: ConnectionMetadata,
repository: DocumentPointerRepository,
body: DocumentReference,
path: UpdateDocumentReferencePathParams,
) -> Response:
"""
Entrypoint for the updateDocumentReference function
"""
logger.log(LogReference.PROUPDATE000)
if body.id != path.id:
logger.log(LogReference.PROUPDATE001, body_id=body.id, path_id=path.id)
return SpineErrorResponse.BAD_REQUEST(
diagnostics="The document id in the path does not match the document id in the body"
)
logger.log(LogReference.PROUPDATE002, body=body)
validator = DocumentReferenceValidator()
result = validator.validate(body)
if not result.is_valid:
logger.log(LogReference.PROUPDATE003)
return Response.from_issues(statusCode="400", issues=result.issues)
update_time = create_fhir_instant()
document_reference = _set_update_time_fields(
update_time, document_reference=result.resource
)
core_model = DocumentPointer.from_document_reference(document_reference)
if (
metadata.ods_code in ["V4TOL", "V4T0L"]
and core_model.type == PointerTypes.APPOINTMENT.value
):
# If bars app - don't validate the ods code against the pointer
logger.log(
LogReference.PROUPDATE002,
allow_rule="Allowing bars to update pointer",
pointer_id=core_model.id,
)
elif metadata.ods_code_parts != tuple(core_model.producer_id.split("|")):
logger.log(
LogReference.PROUPDATE004,
metadata_ods_code_parts=metadata.ods_code_parts,
producer_id=core_model.producer_id,
)
return SpineErrorResponse.AUTHOR_CREDENTIALS_ERROR(
diagnostics="The id of the provided DocumentReference does not include the expected ODS code for this organisation"
)
pointer_id = urllib.parse.quote_plus(path.id)
if not (existing_model := repository.get_by_id(pointer_id)):
logger.log(LogReference.PROUPDATE005, pointer_id=pointer_id)
return SpineErrorResponse.NO_RECORD_FOUND(
diagnostics="The requested DocumentReference could not be found"
)
try:
existing_resource = DocumentReference.model_validate_json(
existing_model.document
)
except ValidationError as exc:
logger.log(LogReference.PROUPDATE002, error=exc)
raise OperationOutcomeError(
status_code="500",
severity="error",
code="exception",
details=SpineErrorConcept.from_code("INTERNAL_SERVER_ERROR"),
diagnostics="An error occurred whilst parsing the existing document reference",
)
preserved_fields = ["date"]
for field in preserved_fields:
provided_field = getattr(result.resource, field, None)
existing_field = getattr(existing_resource, field, None)
if provided_field and provided_field != existing_field:
logger.log(
LogReference.PROUPDATE007,
field=field,
provided=provided_field,
)
setattr(document_reference, field, existing_field)
immutable_fields = [
"masterIdentifier",
"id",
"identifier",
"status",
"type",
"subject",
"custodian",
"relatesTo",
"author",
]
for field in immutable_fields:
if getattr(result.resource, field) != getattr(existing_resource, field):
logger.log(LogReference.PROUPDATE006, field=field)
return SpineErrorResponse.BAD_REQUEST(
diagnostics=f"The field '{field}' is immutable and cannot be updated",
expression=field,
)
document_pointer_update = DocumentPointer.from_document_reference(
document_reference
)
document_pointer_update.created_on = existing_model.created_on
document_pointer_update.updated_on = update_time
repository.update(document_pointer_update)
logger.log(LogReference.PROUPDATE999)
return NRLResponse.RESOURCE_UPDATED()