-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathecw_integrations.py
More file actions
1131 lines (1002 loc) · 43.1 KB
/
ecw_integrations.py
File metadata and controls
1131 lines (1002 loc) · 43.1 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from datetime import datetime
import json
import time
from typing import Optional
from urllib.parse import quote_plus, urlencode
import aiohttp
from fake_useragent import UserAgent
from fastapi import HTTPException
from fastapi.logger import logger
from integrations.ecw.ecw_config import (
BASE_URL,
ECW_URLS,
AuthTokens,
reduced_visit_types,
)
from integrations.ecw.ecw_utils import (
create_new_appointment_formdata_v2,
generate_batch_allergy_flags_xml,
generate_batch_medhx_flag_xml,
generate_encounter_details_flag_xml,
generate_family_history_formdata_notes_xml,
generate_formdata_xml_for_history,
generate_medical_history_text_xml,
generate_set_allergy_item_xml,
generate_social_history_formdata_xml,
parse_progress_note_html,
parse_xml_response,
)
from urllib import parse
from submodule_integrations.ecw.ecw_models import (
AddFamilyHistoryNoteRequest,
AddSocialHistoryNoteRequest,
AddSurgicalAndHospitilizationItemsRequest,
AppointmentRequest,
GetAppointmentsRequest,
GetPatientsRequest,
UpdateMedHxAllergyRequest,
get_default_date,
)
from submodule_integrations.models.integration import Integration
from submodule_integrations.utils.errors import (
IntegrationAPIError,
)
import json
import traceback
class ECWIntegration(Integration):
def __init__(self, auth_tokens: AuthTokens, user_agent: str = UserAgent().chrome):
super().__init__("ecw")
self.user_agent = user_agent
self.network_requester = None
self.url = "https://nybukaapp.eclinicalweb.com/mobiledoc/jsp/catalog/xml"
self.auth_tokens: AuthTokens = auth_tokens
self.client_session = aiohttp.ClientSession()
@classmethod
async def create(cls, auth_tokens: AuthTokens, network_requester=None):
instance = cls(auth_tokens)
instance.network_requester = network_requester
return instance
async def _make_request(self, method: str, url: str, **kwargs):
if self.network_requester:
response = await self.network_requester.request(method, url, **kwargs)
return response
else:
async with self.client_session.request(method, url, **kwargs) as response:
return await self._handle_response(response)
async def close_session(self):
await self.client_session.close()
logger.debug("Closed client session in EcwIntegrations")
async def _handle_response(self, response: aiohttp.ClientResponse):
response_text = await response.text()
status = response.status
parsed_data = None
try:
if response_text.strip().startswith(
"<?xml"
) or response_text.strip().startswith("<root"):
parsed_data = parse_xml_response(response_text)
elif response_text.strip().startswith("<HTML>"):
parsed_data = parse_progress_note_html(response_text)
elif response_text.strip().startswith("{"):
parsed_data = json.loads(response_text)
else:
return response_text
except Exception as e:
await self.close_session()
logger.warning(f"Response parsing failed: {str(e)}")
# logger.warning(f"Cause: {traceback.print_exc()}")
parsed_data = {"error": {"message": "Parsing error", "raw": response_text}}
if 200 <= status < 300:
return parsed_data
error_message = parsed_data.get("error", {}).get("message", "Unknown error")
error_code = parsed_data.get("error", {}).get("code", str(status))
logger.debug(f"{status} - {parsed_data}")
if 400 <= status < 500:
await self.close_session()
raise HTTPException(status_code=status, detail=parsed_data)
elif status >= 500:
await self.close_session()
raise IntegrationAPIError(
self.integration_name,
f"Downstream server error (translated to HTTP 501): {error_message}",
501,
error_code,
)
else:
await self.close_session()
raise IntegrationAPIError(
self.integration_name,
f"{error_message} (HTTP {status})",
status,
error_code,
)
async def _setup_headers(self, content_type: str = None):
_headers = {
"User-Agent": self.user_agent,
"Cookie": self.auth_tokens.Cookie,
"Sec-Ch-Ua": "'Chromium';v='134', 'Not:A-Brand';v='24', 'Google Chrome';v='134'",
"x-csrf-token": self.auth_tokens.x_csrf_token,
}
if content_type:
_headers["Content-type"] = content_type
return _headers
async def get_facilities(self, close_session: bool = True):
logger.debug("Fetching list of all facilities")
try:
headers = await self._setup_headers()
url = ECW_URLS["get facilities"].format(
sessionDID=self.auth_tokens.sessionDID,
TrUserId=self.auth_tokens.TrUserId,
timestamp=int(time.time() * 1000),
clientTimezone="UTC",
)
return await self._make_request(
method="GET",
url=url,
headers=headers,
)
except Exception as exc:
logger.debug(exc)
raise
finally:
if close_session:
await self.close_session()
async def get_providers(self, page: int, close_session: bool = True):
logger.debug(f"Fetching page: {page} of providers")
try:
headers = await self._setup_headers()
url = ECW_URLS["get providers"].format(
page=page,
sessionDID=self.auth_tokens.sessionDID,
TrUserId=self.auth_tokens.TrUserId,
timestamp=int(time.time() * 1000),
clientTimezone="UTC",
)
return await self._make_request(
method="POST",
url=url,
headers=headers,
)
except Exception as exc:
logger.debug(exc)
raise
finally:
if close_session:
await self.close_session()
async def get_provider(self, providerName: str, close_session: bool = True):
logger.debug(f"Looking for provider: {providerName}")
try:
headers = await self._setup_headers()
url = ECW_URLS["get provider"].format(
provider=providerName.lower(),
sessionDID=self.auth_tokens.sessionDID,
TrUserId=self.auth_tokens.TrUserId,
timestamp=int(time.time() * 1000),
clientTimezone="UTC",
)
return await self._make_request(
method="POST",
url=url,
headers=headers,
)
except Exception as exc:
logger.debug(exc)
raise
finally:
if close_session:
await self.close_session()
async def get_reasons(self, close_session: bool = True):
logger.debug(f"Fetching reasons")
try:
headers = await self._setup_headers()
url = ECW_URLS["get resons"].format(
sessionDID=self.auth_tokens.sessionDID,
TrUserId=self.auth_tokens.TrUserId,
timestamp=int(time.time() * 1000),
clientTimezone="UTC",
)
return await self._make_request(
method="GET",
url=url,
headers=headers,
)
except Exception as exc:
logger.debug(exc)
raise
finally:
if close_session:
await self.close_session()
async def get_appointments(self, get_appointments_request: GetAppointmentsRequest):
logger.debug("Fetching list of appointments")
try:
eDate = get_appointments_request.eDate or get_default_date()
maxCount = get_appointments_request.maxCount or 100
logger.debug(
f"Fetching {maxCount} doctor's appointments for user: {self.auth_tokens.TrUserId}"
)
headers = await self._setup_headers(
content_type="application/x-www-form-urlencoded; charset=UTF-8"
)
url = ECW_URLS["get appointments"].format(
sessionDID=self.auth_tokens.sessionDID,
TrUserId=self.auth_tokens.TrUserId,
timestamp=int(time.time() * 1000),
clientTimezone="UTC",
)
payload = {
"eDate": eDate,
"doctorId": get_appointments_request.providerId,
"sortBy": "time",
"facilityId": get_appointments_request.facilityId,
"apptTime": 0,
"checkinstatus": 0,
"FacilityGrpId": 0,
"maxCount": maxCount,
"nCounter": 0,
"DeptId": 0,
"fromWeb": "yes",
"fromAfterCare": "officeVisit",
"tabId": 3,
"toDate": "",
"selectedChkShowASCvisits": "false",
"includeResourceAppt": "true",
}
encoded_payload = urlencode(payload)
return await self._make_request(
method="POST",
url=url,
headers=headers,
data=encoded_payload,
)
except Exception as exc:
logger.debug(exc)
raise
finally:
await self.close_session()
async def get_patients(
self, get_patients_request: GetPatientsRequest, close_session: bool = True
):
try:
logger.debug(
f"Searching patients with last name '{get_patients_request.lastName}'"
+ (
f" and first name '{get_patients_request.firstName}'"
if get_patients_request.firstName
else ""
)
)
headers = await self._setup_headers(
content_type="application/x-www-form-urlencoded; charset=UTF-8"
)
url = ECW_URLS["get patients"].format(
sessionDID=self.auth_tokens.sessionDID,
TrUserId=self.auth_tokens.TrUserId,
timestamp=int(time.time() * 1000),
clientTimezone="UTC",
)
primary_search_value = get_patients_request.lastName
if get_patients_request.firstName:
primary_search_value += f", {get_patients_request.firstName}"
payload = {
"counter": 1,
"firstName": get_patients_request.firstName or "",
"lastName": get_patients_request.lastName,
"primarySearchValue": primary_search_value,
"SearchBy": "Name",
"StatusSearch": "Active",
"limitstart": 0,
"limitrange": get_patients_request.maxCount,
"MAXCOUNT": get_patients_request.maxCount,
"device": "webemr",
"callFromScreen": "PatientSearch",
"action": "Patient",
"donorProfileStatus": 2,
"AddlSearchBy": "DateOfBirth",
"AddlSearchVal": "",
"userType": "",
"orderBy": "",
}
encoded_payload = urlencode(payload)
return await self._make_request(
method="POST",
url=url,
headers=headers,
data=encoded_payload,
)
except Exception as exc:
logger.debug(exc)
raise
finally:
if close_session:
await self.close_session()
async def validate_provider(self, provider_name: str):
logger.debug(f"Validating provider/resource <{provider_name}>")
provider = await self.get_provider(
providerName=provider_name, close_session=False
)
provider_response = None
if provider and len(provider.get("result")) > 0:
provider_response = provider.get("result")[0]["id"]
return provider_response
async def validate_reason(self, reason_client: str):
logger.debug(f"Validating reason: {reason_client}")
reasons = await self.get_reasons(close_session=False)
reason_item = None
for reason in reasons["reasons"]:
if reason["name"].lower() == reason_client.lower():
reason_item = reason["name"]
return reason_item
async def validate_visit_type(self, visit_type: str):
logger.debug(f"Validating visit type <{visit_type}>")
visit_type_res = None
for visit in reduced_visit_types:
if visit["Description"].lower() == visit_type.lower():
visit_type_res = visit["Name"]
return visit_type_res
async def validate_facilities(self, facility_name: str):
logger.debug(f"Validating user facility <{facility_name}>")
facilities_list = await self.get_facilities(close_session=False)
facilities_res = None
for facility in facilities_list["facilities"]:
if facility["Name"].lower() == facility_name.lower():
facilities_res = {"id": facility["Id"], "pos": facility["POS"]}
return facilities_res
async def create_appointment(self, request: AppointmentRequest):
logger.debug(
f"{'Updating' if request.encounterId else 'Creating'} appointment for patient <{request.patient_name}>"
)
try:
headers = await self._setup_headers()
patient_response = await self.get_patients(
GetPatientsRequest(
lastName=request.patient_name.split(",")[0],
firstName=request.patient_name.split(",")[-1],
),
close_session=False,
)
if not patient_response.get("patients"):
logger.debug(f"No patients found by name: {request.patient_name}")
raise HTTPException(
404, {"message": f"Patient: {request.patient_name} not found"}
)
patient_id = patient_response.get("patients")[0]["id"]
start = request.start_time.replace(" ", "%20")
date = parse.quote(
str(datetime.strptime(request.date, "%m/%d/%Y").strftime("%m/%d/%Y"))
)
id = self.auth_tokens.sessionDID
patient_name = parse.quote(str(request.patient_name.upper()))
url = ECW_URLS["get appointment"].format(
start=start,
id=id,
patient_name=patient_name,
date=date,
patient_id=patient_id,
sessionDID=self.auth_tokens.sessionDID,
TrUserId=self.auth_tokens.TrUserId,
timestamp=int(time.time() * 1000),
clientTimezone="UTC",
)
logger.debug("Making request to get appointment form")
logger.debug(f"Get Form URL: {url}")
await self._make_request(
method="GET",
url=url,
headers=headers,
)
facility_id = await self.validate_facilities(request.facility_name)
if not facility_id:
logger.debug(f"No facility found by name {request.facility_name}")
raise HTTPException(
status_code=404, detail={"message": "Facility not found"}
)
provider_id = await self.validate_provider(request.provider)
if not provider_id:
logger.debug(f"No provider found by name {request.provider}")
raise HTTPException(
status_code=404, detail={"message": "Provider not found"}
)
resource_name = request.resource or request.provider
resource_id = await self.validate_provider(resource_name)
if not resource_id:
logger.debug(f"No resource found by name {request.provider}")
raise HTTPException(
status_code=404, detail={"message": "Resource not found"}
)
reason_name = await self.validate_reason(request.reason)
if not reason_name:
logger.debug(f"No reason found by description/name: {request.reason}")
raise HTTPException(
status_code=404, detail={"message": "Reason not found"}
)
visit_type = await self.validate_visit_type(request.visit_type)
if not visit_type:
logger.debug(f"No visit type found by name {request.visit_type}")
raise HTTPException(
status_code=404, detail={"message": "Visit Type not found"}
)
logger.debug("Creating appointment XML formdata request body")
create_payload = await create_new_appointment_formdata_v2(
patient_id=patient_id,
date_str=request.date,
start_time_str=request.start_time,
end_time_str=request.end_time,
visit_type_name=visit_type,
reason_str=reason_name,
doctor_id_str=provider_id,
status_code_str=request.visit_status,
facility_id_str=str(facility_id["id"]),
diagnosis_str=request.diagnosis,
patient_email_str=request.email,
tr_user_id_str=self.auth_tokens.TrUserId,
resource_id_str=resource_id,
pos_str=facility_id["pos"],
# general_notes_str=request.general_notes
)
logger.debug("URL encoding appointment XML formdata")
encoded_payload = parse.quote_plus(str(create_payload))
form_data_payload = f"FormData={encoded_payload}"
logger.debug(f"Formatted form data payload: {form_data_payload[:200]}...")
if request.encounterId:
url = ECW_URLS["update appointment"].format(
encounterId=request.encounterId,
sessionDID=self.auth_tokens.sessionDID,
TrUserId=self.auth_tokens.TrUserId,
timestamp=int(time.time() * 1000),
clientTimezone="UTC",
)
else:
url = ECW_URLS["post appointment"].format(
sessionDID=self.auth_tokens.sessionDID,
TrUserId=self.auth_tokens.TrUserId,
timestamp=int(time.time() * 1000),
clientTimezone="UTC",
)
logger.debug(f"Final POST URL: {url}")
fin_header = await self._setup_headers(
content_type="application/x-www-form-urlencoded; charset=UTF-8"
)
fin_header["origin"] = "https://nybukaapp.eclinicalweb.com"
fin_header["referer"] = (
"https://nybukaapp.eclinicalweb.com/mobiledoc/jsp/webemr/index.jsp"
)
fin_header["x-requested-with"] = "XMLHttpRequest"
fin_header["isajaxrequest"] = "true"
return await self._make_request(
method="POST",
url=url,
headers=fin_header,
data=form_data_payload,
)
except Exception as exc:
logger.debug(exc)
raise
finally:
await self.close_session()
async def get_progress_notes(self, encounterId: str):
logger.debug(f"Fetching progress notes for encounter: <{encounterId}>")
try:
headers = await self._setup_headers()
url = ECW_URLS["get progress notes"].format(
encounterId=encounterId,
sessionDID=self.auth_tokens.sessionDID,
TrUserId=self.auth_tokens.TrUserId,
timestamp=int(time.time() * 1000),
clientTimezone="UTC",
)
return await self._make_request(method="GET", url=url, headers=headers)
except Exception as exc:
logger.debug(exc)
raise
finally:
await self.close_session()
async def update_history_add_only(
self, request_data: AddSurgicalAndHospitilizationItemsRequest
):
logger.debug("Updating surgical history and hospitilization info")
try:
headers = await self._setup_headers()
batch_items_to_send = []
timestamp_ms_base = int(time.time() * 1000)
auth_params = {
"sessionDID": self.auth_tokens.sessionDID,
"TrUserId": self.auth_tokens.TrUserId,
}
common_device_params = {
"Device": "webemr",
"ecwappprocessid": "0",
"clientTimezone": "UTC",
}
surgical_actually_changed = False
if request_data.new_surgical_items:
surgical_actually_changed = True
get_surg_url_params = {
"encounterId": request_data.encounter_id,
**auth_params,
**common_device_params,
"timestamp": timestamp_ms_base,
"calledFromHospCtrl": "true",
}
get_surg_url = ECW_URLS["get_surgical_history"].format(
**get_surg_url_params
)
try:
logger.debug(f"Fetching surgical history: {get_surg_url}")
surg_response_xml = await self._make_request(
"GET", get_surg_url, headers=headers
)
existing_surgical_items = surg_response_xml.get(
"surgical_history", []
)
except Exception as e:
logger.debug(
f"Error fetching/parsing existing surgical history: {e}"
)
existing_surgical_items = []
combined_surgical_items = []
current_max_idx = 0
for item_dict in existing_surgical_items:
combined_surgical_items.append(item_dict)
if (
item_dict.get("displayIndex")
and int(item_dict["displayIndex"]) > current_max_idx
):
current_max_idx = int(item_dict["displayIndex"])
for new_item in request_data.new_surgical_items:
current_max_idx += 1
combined_surgical_items.append(
{
"reason": new_item.reason,
"date": new_item.date,
"cptcode": new_item.cptcode if new_item.cptcode else "",
"displayIndex": str(current_max_idx),
}
)
form_data_xml_surg = generate_formdata_xml_for_history(
combined_surgical_items, "surgical"
)
batch_url_params_surg = {
"mode": "webemr",
"patientId": request_data.patient_id,
"EncounterId": request_data.encounter_id,
**auth_params,
**common_device_params,
"timestamp": timestamp_ms_base + 1,
"surgicalHxChanged": str(surgical_actually_changed).lower(),
}
query_string_surg = urlencode(batch_url_params_surg)
batch_items_to_send.append(
{
"url": f"{ECW_URLS['set_surgical_history']}?{query_string_surg}",
"param": [
{"paramName": "FormData", "paramValue": form_data_xml_surg}
],
}
)
encounter_details_xml_surg = generate_encounter_details_flag_xml(
request_data.encounter_id,
"Surgical History",
surgical_actually_changed,
)
batch_url_params_enc_surg = {
"mode": "webEMR",
"Id": request_data.encounter_id,
"sectionName": "Surgical History",
"historyChanged": str(surgical_actually_changed).lower(),
"ptId": request_data.patient_id,
**auth_params,
**common_device_params,
"timestamp": timestamp_ms_base + 2,
}
query_string_enc_surg = urlencode(batch_url_params_enc_surg)
batch_items_to_send.append(
{
"url": f"{ECW_URLS['set_encounter_details']}?{query_string_enc_surg}",
"param": [
{
"paramName": "FormData",
"paramValue": encounter_details_xml_surg,
}
],
"args": {},
}
)
hospitalization_actually_changed = False
if request_data.new_hospitalization_items:
hospitalization_actually_changed = True
get_hosp_url_params = {
"encounterId": request_data.encounter_id,
**auth_params,
**common_device_params,
"timestamp": timestamp_ms_base + 3,
"calledFromHospCtrl": "true",
}
get_hosp_url = ECW_URLS["get_hospitalization_history"].format(
**get_hosp_url_params
)
try:
logger.debug(f"Fetching hospitalization history: {get_hosp_url}")
hosp_response_xml = await self._make_request(
"GET", get_hosp_url, headers=headers
)
existing_hosp_items = hosp_response_xml.get(
"hospitalization_history", []
)
except Exception as e:
logger.debug(f"Error fetching/parsing existing hosp history: {e}")
existing_hosp_items = []
combined_hosp_items = []
current_max_idx_hosp = 0
for item_dict in existing_hosp_items:
combined_hosp_items.append(item_dict)
if (
item_dict.get("displayIndex")
and int(item_dict["displayIndex"]) > current_max_idx_hosp
):
current_max_idx_hosp = int(item_dict["displayIndex"])
for new_item in request_data.new_hospitalization_items:
current_max_idx_hosp += 1
combined_hosp_items.append(
{
"reason": new_item.reason,
"date": new_item.date,
"displayIndex": str(current_max_idx_hosp),
}
)
form_data_xml_hosp = generate_formdata_xml_for_history(
combined_hosp_items, "hospitalization"
)
batch_url_params_hosp = {
"mode": "webemr",
"patientId": request_data.patient_id,
"EncounterId": request_data.encounter_id,
**auth_params,
**common_device_params,
"timestamp": timestamp_ms_base + 4,
"hospHxChanged": str(hospitalization_actually_changed).lower(),
}
query_string_hosp = urlencode(batch_url_params_hosp)
batch_items_to_send.append(
{
"url": f"{ECW_URLS['set_hospitalization_history']}?{query_string_hosp}",
"param": [
{"paramName": "FormData", "paramValue": form_data_xml_hosp}
],
}
)
if hospitalization_actually_changed:
encounter_details_xml_hosp = generate_encounter_details_flag_xml(
request_data.encounter_id,
"Hospitalization",
hospitalization_actually_changed,
)
batch_url_params_enc_hosp = {
"mode": "webEMR",
"Id": request_data.encounter_id,
"sectionName": "Hospitalization",
"historyChanged": str(hospitalization_actually_changed).lower(),
"ptId": request_data.patient_id,
**auth_params,
**common_device_params,
"timestamp": timestamp_ms_base + 5,
}
query_string_enc_hosp = urlencode(batch_url_params_enc_hosp)
batch_items_to_send.append(
{
"url": f"{ECW_URLS['set_encounter_details']}?{query_string_enc_hosp}",
"param": [
{
"paramName": "FormData",
"paramValue": encounter_details_xml_hosp,
}
],
"args": {},
}
)
if not batch_items_to_send:
return {"message": "No new history items to add."}
json_x_payload_string = json.dumps(batch_items_to_send)
final_form_data = {
"_csrf": self.auth_tokens.x_csrf_token,
"x": json_x_payload_string,
}
batch_update_url = ECW_URLS["batch_ajax"]
final_headers = await self._setup_headers(
content_type="application/x-www-form-urlencoded; charset=UTF-8"
)
final_headers["Referer"] = f"{BASE_URL}/mobiledoc/jsp/webemr/index.jsp"
final_headers["X-Requested-With"] = "XMLHttpRequest"
return await self._make_request(
method="POST",
url=batch_update_url,
headers=final_headers,
data=final_form_data,
)
except Exception as exc:
logger.debug(exc)
raise
finally:
await self.close_session()
async def add_family_history_note(self, request_data: AddFamilyHistoryNoteRequest):
logger.debug("Adding family history note")
try:
form_data_notes_xml = generate_family_history_formdata_notes_xml(
request_data.encounter_id, request_data.plain_text_notes
)
form_payload_dict = {
"Id": request_data.encounter_id,
"TrUserId": self.auth_tokens.TrUserId,
"patientId": request_data.patient_id,
"action": "SAVE",
"isDashboard": "false",
"familymodified": "true",
"FormDataNotes": form_data_notes_xml,
}
final_form_data_string = urlencode(form_payload_dict)
target_url = ECW_URLS["add family history notes"]
final_headers = await self._setup_headers(
content_type="application/x-www-form-urlencoded; charset=UTF-8"
)
final_headers["X-Requested-With"] = "XMLHttpRequest"
logger.debug(f"Family History URL: {target_url}")
logger.debug(
f"Family History Form Data String (first 500 chars): {final_form_data_string[:500]}..."
)
response_data = await self._make_request(
method="POST",
url=target_url,
headers=final_headers,
data=final_form_data_string,
)
if isinstance(response_data, str) and not response_data.strip():
return {
"status": "success",
"message": "Family history note likely saved (empty response received).",
}
return {
"status": "unknown",
"raw_response": response_data,
}
except Exception as exc:
logger.debug(exc)
raise
finally:
await self.close_session()
async def add_social_history_note(self, request_data: AddSocialHistoryNoteRequest):
logger.debug("Adding social history note")
try:
timestamp_ms = int(time.time() * 1000)
form_data_xml = generate_social_history_formdata_xml(
request_data.encounter_id, request_data.plain_text_notes
)
auth_params = {
"sessionDID": self.auth_tokens.sessionDID,
"TrUserId": self.auth_tokens.TrUserId,
}
common_device_params = {
"Device": "webemr",
"ecwappprocessid": "0",
"clientTimezone": "UTC",
}
batch_url_params = {
"encounterId": request_data.encounter_id,
"type": "Social",
"patientId": request_data.patient_id,
**auth_params,
**common_device_params,
"timestamp": timestamp_ms,
}
query_string = urlencode(batch_url_params)
set_annual_notes_url = f"{ECW_URLS.get('set_annual_notes')}?{query_string}"
batch_item = {
"url": set_annual_notes_url,
"param": [{"paramName": "FormData", "paramValue": form_data_xml}],
"args": {},
}
x_payload_list = [batch_item]
json_x_payload_string = json.dumps(x_payload_list)
final_form_data_string = f"_csrf={quote_plus(self.auth_tokens.x_csrf_token)}&x={quote_plus(json_x_payload_string)}"
batch_ajax_url = ECW_URLS["batch_ajax"]
final_headers = await self._setup_headers(
content_type="application/x-www-form-urlencoded; charset=UTF-8"
)
final_headers["X-Requested-With"] = "XMLHttpRequest"
logger.debug(f"Social History Batch URL: {batch_ajax_url}")
logger.debug(
f"Social History Data String (first 300 chars): {final_form_data_string[:300]}..."
)
return await self._make_request(
method="POST",
url=batch_ajax_url,
headers=final_headers,
data=final_form_data_string,
)
except Exception as exc:
logger.debug(exc)
raise
finally:
await self.close_session()
async def search_allergies(self, search_text: str, n_limit: Optional[str] = "9"):
logger.debug(f"Searching for allergy: {search_text}")
try:
timestamp_ms = int(time.time() * 1000)
headers = await self._setup_headers()
params = {
"searchType": "0",
"calledFrom": "MedReconciliation",
"searchText": search_text,
"TrUserId": self.auth_tokens.TrUserId,
"RxTypeID": "12846",
"nEncounterId": "0",
"nLimit": n_limit,
"rxDrugSearchType": "1",
"hideMSClinical": "false",
"facilityId": "0",
"bObsolete": "0",
"showDeletedDrug": "0",
"enhancedMedicationSearchType": "searchAllergy",
"startsWithContainsSearchEnabled": "-1",
"fuzzySearchEnabled": "-1",
"mnemonicSearchEnabled": "-1",
"proximitySearchEnabled": "-1",
"genericWithBrandSearchEnabled": "-1",
"section": "AllergyDrugRxNotes1",
"sessionDID": self.auth_tokens.sessionDID,
"Device": "webemr",
"ecwappprocessid": "0",
"timestamp": timestamp_ms,
"clientTimezone": "UTC",
}
query_string = urlencode(params)
search_url = f"{ECW_URLS['allergy_quick_search']}?{query_string}"
return await self._make_request("GET", search_url, headers=headers)
except Exception as exc:
logger.debug(exc)
raise
finally:
await self.close_session()
async def update_med_hx_and_allergies(
self, request_data: UpdateMedHxAllergyRequest
):
try:
logger.debug("Updating medical histor and/or allergy information")
responses = {}
timestamp_base = int(time.time() * 1000)
auth_params = {
"sessionDID": self.auth_tokens.sessionDID,
"TrUserId": self.auth_tokens.TrUserId,
}
common_device_params = {
"Device": "webemr",
"ecwappprocessid": "0",
"clientTimezone": "UTC",
}
if request_data.medical_history_text is not None:
med_hx_xml = generate_medical_history_text_xml(
request_data.encounter_id, request_data.medical_history_text
)
med_hx_url_params = {
"historyChanged": "true",
"sectionName": "Medical History",
"Id": request_data.encounter_id,
"mode": "webEMR",
"ptId": request_data.patient_id,