Skip to content

Commit 597bde9

Browse files
committed
VED-350: refactor repository to return full resource and filter per params
1 parent 3b96e29 commit 597bde9

File tree

3 files changed

+26
-24
lines changed

3 files changed

+26
-24
lines changed

backend/src/fhir_repository.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ def get_immunization_by_identifier(
100100
if not validate_permissions(imms_vax_type_perms,ApiOperationCode.SEARCH, [vaccine_type]):
101101
raise UnauthorizedVaxError()
102102
resource = json.loads(item["Resource"])
103+
resp = dict(resource)
103104
resp["id"] = resource.get("id")
104105
resp["version"] = int(response["Items"][0]["Version"])
105106
return resp

backend/src/models/utils/generic_utils.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -136,52 +136,53 @@ def create_diagnostics_error(value):
136136

137137

138138
def form_json(response, _element, identifier, baseurl):
139-
# Elements to include, based on the '_element' parameter
139+
140+
resource = response
141+
self_url = f"{baseurl}?identifier={identifier}"
142+
if _element:
143+
self_url += f"&_elements={_element}"
144+
140145
if not response:
141146
json = {
142147
"resourceType": "Bundle",
143148
"type": "searchset",
144149
"link": [
145-
{"relation": "self", "url": f"{baseurl}?identifier={identifier}&_elements={_element}"}
150+
{"relation": "self", "url": f"{baseurl}?identifier={identifier}"}
146151
],
147152
"entry": [],
148153
"total": 0,
149154
}
150155
return json
151156

152-
# Basic structure for the JSON output
157+
# Full Immunization payload to be returned if only the identifier parameter was provided
158+
if identifier and _element:
159+
__elements = _element.lower()
160+
element = __elements.split(",")
161+
162+
resource = {"resourceType": "Immunization"}
163+
164+
# Add 'id' if specified
165+
if "id" in element:
166+
json["entry"][0]["resource"]["id"] = response["id"]
167+
168+
# Add 'meta' if specified
169+
if "meta" in element:
170+
resource["id"] = response["id"]
171+
resource["meta"] = {"versionId": response["version"]}
172+
153173
json = {
154174
"resourceType": "Bundle",
155175
"type": "searchset",
156176
"link": [{"relation": "self", "url": f"{baseurl}?identifier={identifier}&_elements={_element}"}],
157177
"entry": [
158178
{
159179
"fullUrl": f"https://api.service.nhs.uk/immunisation-fhir-api/Immunization/{response['id']}",
160-
"resource": {"resourceType": "Immunization"},
180+
"resource": resource,
161181
}
162182
],
163183
"total": 1,
164184
}
165185

166-
# Full Immunization payload to be returned if only the identifier parameter was provided
167-
if identifier and (not _element):
168-
json["entry"].append({
169-
"fullUrl": f"https://api.service.nhs.uk/immunisation-fhir-api/Immunization/{response.get('id')}",
170-
"resource": response,
171-
})
172-
return json
173-
174-
__elements = _element.lower()
175-
element = __elements.split(",")
176-
# Add 'id' if specified
177-
if "id" in element:
178-
json["entry"][0]["resource"]["id"] = response["id"]
179-
180-
# Add 'meta' if specified
181-
if "meta" in element:
182-
json["entry"][0]["resource"]["id"] = response["id"]
183-
json["entry"][0]["resource"]["meta"] = {"versionId": response["version"]}
184-
185186
return json
186187

187188

backend/tests/test_fhir_repository.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def test_get_immunization_by_identifier(self):
5353
"""it should find an Immunization by id"""
5454
imms_id = "a-id#an-id"
5555
resource = dict()
56-
resource["Resource"] = {"id": "test", "version": 1}
56+
resource["Resource"] = {"foo": "bar", "id": "test", "version": 1}
5757
self.table.query = MagicMock(
5858
return_value={
5959
"Items": [

0 commit comments

Comments
 (0)