|
2 | 2 |
|
3 | 3 | import datetime |
4 | 4 |
|
5 | | -from typing import Literal, Union, Optional |
| 5 | +from typing import Literal, Union, Optional, Dict, Any |
6 | 6 | from fhir.resources.R4B.bundle import ( |
7 | 7 | Bundle as FhirBundle, |
8 | 8 | BundleEntry, |
@@ -141,44 +141,44 @@ def create_diagnostics_error(value): |
141 | 141 | exp_error = {"diagnostics": diagnostics} |
142 | 142 | return exp_error |
143 | 143 |
|
| 144 | +def empty_bundle(self_url: str) -> Dict[str, Any]: |
| 145 | + return { |
| 146 | + "resourceType": "Bundle", |
| 147 | + "type": "searchset", |
| 148 | + "link": [{"relation": "self", "url": self_url}], |
| 149 | + "total": 0, |
| 150 | + "entry": [], |
| 151 | + } |
144 | 152 |
|
145 | 153 | def form_json(response, _element, identifier, baseurl): |
146 | 154 | self_url = f"{baseurl}?identifier={identifier}" + (f"&_elements={_element}" if _element else "") |
147 | | - meta = {"versionId": response["version"]} if response and "version" in response else {} |
148 | | - fhir_bundle = FhirBundle(resourceType="Bundle", type="searchset", link = [BundleLink(relation="self", url=self_url)], entry=[], |
149 | | - total=0) |
150 | 155 |
|
151 | 156 | if not response: |
152 | | - return fhir_bundle.dict(by_alias=True) |
| 157 | + return empty_bundle(self_url) |
| 158 | + |
| 159 | + meta = {"versionId": response["version"]} if "version" in response else {} |
153 | 160 |
|
154 | 161 | # Full Immunization payload to be returned if only the identifier parameter was provided |
155 | | - if identifier and not _element: |
| 162 | + if _element: |
| 163 | + element = {e.strip().lower() for e in _element.split(",") if e.strip()} |
| 164 | + resource = {"resourceType": "Immunization"} |
| 165 | + if "id" in element: resource["id"] = response["id"] |
| 166 | + if "meta" in element and meta: resource["meta"] = meta |
| 167 | + |
| 168 | + else: |
156 | 169 | resource = response["resource"] |
157 | 170 | resource["meta"] = meta |
158 | 171 |
|
159 | | - imms = Immunization.construct(**resource) |
160 | | - |
161 | | - elif identifier and _element: |
162 | | - element = {e.strip().lower() for e in _element.split(",") if e.strip()} |
163 | | - resource = {"resourceType": "Immunization"} |
164 | | - if "id" in element: |
165 | | - resource["id"] = response["id"] |
166 | | - |
167 | | - # Add 'meta' if specified |
168 | | - if "meta" in element: |
169 | | - resource["id"] = response["id"] |
170 | | - resource["meta"] = meta |
171 | | - |
172 | | - imms = Immunization.construct(**resource) |
173 | | - |
174 | | - entry = BundleEntry( |
175 | | - fullUrl=f"{baseurl}/{response['id']}", |
176 | | - resource=imms, |
177 | | - search=BundleEntrySearch.construct(mode="match"), |
| 172 | + entry = BundleEntry(fullUrl=f"{baseurl}/{response['id']}", |
| 173 | + resource=Immunization.construct(**resource), |
| 174 | + search=BundleEntrySearch.construct(mode="match") if not _element else None, |
178 | 175 | ) |
179 | | - |
180 | | - fhir_bundle.entry = [entry] |
181 | | - fhir_bundle.total = 1 |
| 176 | + |
| 177 | + fhir_bundle = FhirBundle( |
| 178 | + resourceType="Bundle", type="searchset", |
| 179 | + link = [BundleLink(relation="self", url=self_url)], |
| 180 | + entry=[entry], total=1) |
| 181 | + |
182 | 182 | return fhir_bundle.dict(by_alias=True) |
183 | 183 |
|
184 | 184 |
|
|
0 commit comments