33import datetime
44
55from typing import Literal , Union , Optional
6+ from fhir .resources .R4B .bundle import (
7+ Bundle as FhirBundle ,
8+ BundleEntry ,
9+ BundleLink ,
10+ BundleEntrySearch ,
11+ )
12+ from fhir .resources .immunization import Immunization
613from models .constants import Constants
714import urllib .parse
815import base64
@@ -138,28 +145,23 @@ def create_diagnostics_error(value):
138145def form_json (response , _element , identifier , baseurl ):
139146 self_url = f"{ baseurl } ?identifier={ identifier } " + (f"&_elements={ _element } " if _element else "" )
140147 meta = {"versionId" : response ["version" ]} if response and "version" in response else {}
141- json = {
142- "resourceType" : "Bundle" ,
143- "type" : "searchset" ,
144- "link" : [
145- {"relation" : "self" , "url" : self_url }
146- ]
147- }
148+ fhir_bundle = FhirBundle (resourceType = "Bundle" , type = "searchset" , link = [BundleLink (relation = "self" , url = self_url )])
149+
148150 if not response :
149- json [ " entry" ] = []
150- json [ " total" ] = 0
151- return json
151+ fhir_bundle . entry = []
152+ fhir_bundle . total = 0
153+ return fhir_bundle
152154
153155 # Full Immunization payload to be returned if only the identifier parameter was provided
154156 if identifier and not _element :
155157 resource = response ["resource" ]
156158 resource ["meta" ] = meta
157159
160+ imms = Immunization .parse_obj (resource )
161+
158162 elif identifier and _element :
159163 element = {e .strip ().lower () for e in _element .split ("," ) if e .strip ()}
160164 resource = {"resourceType" : "Immunization" }
161-
162- # Add 'id' if specified
163165 if "id" in element :
164166 resource ["id" ] = response ["id" ]
165167
@@ -168,13 +170,17 @@ def form_json(response, _element, identifier, baseurl):
168170 resource ["id" ] = response ["id" ]
169171 resource ["meta" ] = meta
170172
171- json ["entry" ] = [{
172- "fullUrl" : f"https://api.service.nhs.uk/immunisation-fhir-api/Immunization/{ response ['id' ]} " ,
173- "resource" : resource ,
174- }
175- ]
176- json ["total" ] = 1
177- return json
173+ imms = Immunization .construct (** resource )
174+
175+ entry = BundleEntry (
176+ fullUrl = f"{ baseurl } /Immunization/{ response ['id' ]} " ,
177+ resource = imms ,
178+ search = BundleEntrySearch .construct (mode = "match" ),
179+ )
180+
181+ fhir_bundle .entry = [entry ]
182+ fhir_bundle .total = 1
183+ return fhir_bundle
178184
179185
180186def check_keys_in_sources (event , not_required_keys ):
0 commit comments