/
rsdb.clj
1224 lines (1105 loc) · 61.6 KB
/
rsdb.clj
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
(ns com.eldrix.pc4.server.rsdb
"Integration with the rsdb backend.
`rsdb` is the Apple WebObjects 'legacy' application that was PatientCare v1-3.
Depending on real-life usage, we could pre-fetch relationships and therefore
save multiple database round-trips, particularly for to-one relationships.
Such a change would be trivial because pathom would simply not bother
trying to resolve data that already exists. "
(:require [clojure.spec.alpha :as s]
[clojure.string :as str]
[clojure.tools.logging.readable :as log]
[com.wsscode.pathom3.connect.indexes :as pci]
[com.wsscode.pathom3.connect.built-in.resolvers :as pbir]
[com.wsscode.pathom3.connect.operation :as pco]
[com.wsscode.pathom3.interface.eql :as p.eql]
[honey.sql :as sql]
[next.jdbc :as jdbc]
[com.eldrix.hermes.core]
[com.eldrix.hermes.verhoeff :as verhoeff]
[com.eldrix.pc4.server.dates :as dates]
[com.eldrix.pc4.server.rsdb.auth :as auth]
[com.eldrix.pc4.server.rsdb.db :as db]
[com.eldrix.pc4.server.rsdb.forms :as forms]
[com.eldrix.pc4.server.rsdb.patients :as patients]
[com.eldrix.pc4.server.rsdb.projects :as projects]
[com.eldrix.pc4.server.rsdb.results :as results]
[com.eldrix.pc4.server.rsdb.users :as users]
[com.eldrix.pc4.server.rsdb.patients :as patients])
(:import (com.zaxxer.hikari HikariDataSource)
(java.time LocalDate LocalDateTime)
(java.util Base64)
(org.jsoup Jsoup)
(org.jsoup.safety Safelist)
(com.eldrix.pc4.server.rsdb.auth AuthorizationManager)))
(s/def :uk.gov.ons.nhspd/PCD2 string?)
(s/def :info.snomed.Concept/id (s/and int? verhoeff/valid?))
;;
;;
(s/def :t_death_certificate/part1a (s/nilable string?))
(s/def :t_death_certificate/part1b (s/nilable string?))
(s/def :t_death_certificate/part1c (s/nilable string?))
(s/def :t_death_certificate/part2 (s/nilable string?))
(s/def :t_diagnosis/id int?)
(s/def :t_diagnosis/date_diagnosis (s/nilable #(instance? LocalDate %)))
(s/def :t_diagnosis/date_onset (s/nilable #(instance? LocalDate %)))
(s/def :t_diagnosis/date_to (s/nilable #(instance? LocalDate %)))
(s/def :t_diagnosis/status #{"INACTIVE_REVISED" "ACTIVE" "INACTIVE_RESOLVED" "INACTIVE_IN_ERROR"})
(s/def :t_diagnosis/diagnosis (s/keys :req [:info.snomed.Concept/id]))
(s/def :t_encounter/episode_fk int?)
(s/def :t_encounter/date_time #(instance? LocalDateTime %))
(s/def :t_encounter/id int?)
(s/def :t_encounter/patient_fk int?)
(s/def :t_encounter/encounter_template_fk int?)
(s/def :t_episode/id int?)
(s/def :t_medication/id int?)
(s/def :t_medication/date_from (s/nilable #(instance? LocalDate %)))
(s/def :t_medication/date_to (s/nilable #(instance? LocalDate %)))
(s/def :t_ms_event/id int?)
(s/def :t_ms_event/date #(instance? LocalDate %))
(s/def :t_ms_event/impact string?)
(s/def :t_ms_event/summary_multiple_sclerosis_fk int?)
(s/def :t_patient/id int?)
(s/def :t_patient/patient_identifier int?)
(s/def :t_patient/nhs_number (s/and string? com.eldrix.concierge.nhs-number/valid?))
(s/def :t_patient/date_death (s/nilable #(instance? LocalDate %)))
(s/def :t_user/id int?)
(s/def :t_smoking_history/id int?)
(s/def :t_smoking_history/current_cigarettes_per_day int?)
(s/def :t_smoking_history/status #{"NEVER_SMOKED" "CURRENT_SMOKER" "EX_SMOKER"})
;;
;;
(s/def ::user-id int?)
(s/def ::project-id int?)
(s/def ::nhs-number #(com.eldrix.concierge.nhs-number/valid? (clojure.string/replace % " " "")))
(s/def ::sex #{:MALE :FEMALE :UNKNOWN})
(s/def ::date-birth #(instance? LocalDate %))
(s/def ::register-patient-by-pseudonym
(s/keys :req-un [::user-id ::project-id ::nhs-number ::sex ::date-birth]))
(defn ordered-diagnostic-dates? [{:t_diagnosis/keys [date_onset date_diagnosis date_to]}]
(and
(or (nil? date_onset) (nil? date_diagnosis) (.isBefore date_onset date_diagnosis) (.equals date_onset date_diagnosis))
(or (nil? date_onset) (nil? date_to) (.isBefore date_onset date_to) (.equals date_onset date_to))
(or (nil? date_diagnosis) (nil? date_to) (.isBefore date_diagnosis date_to) (.equals date_diagnosis date_to))))
(defn valid-diagnosis-status? [{:t_diagnosis/keys [status date_to]}]
(case status
"ACTIVE" (nil? date_to)
"INACTIVE_IN_ERROR" date_to
"INACTIVE_RESOLVED" date_to
"INACTIVE_REVISED" date_to
false))
(defn html->text
"Convert a string containing HTML to plain text."
[^String html]
(Jsoup/clean html (Safelist.)))
(pco/defresolver patient-by-identifier
[{:com.eldrix.rsdb/keys [conn]} {patient_identifier :t_patient/patient_identifier}]
{::pco/output [:t_patient/id
:t_patient/patient_identifier
:t_patient/sex
:t_patient/status
:t_patient/title
:t_patient/first_names
:t_patient/last_name
:t_patient/email
:t_patient/country_of_birth_concept_fk
:t_patient/date_birth
:t_patient/date_death
:t_patient/nhs_number
:t_patient/surgery_fk
:t_patient/ethnic_origin_concept_fk
:t_patient/racial_group_concept_fk
:t_patient/occupation_concept_fk]}
(db/execute-one! conn (sql/format {:select [:*] :from [:t_patient] :where [:= :patient_identifier patient_identifier]})))
(pco/defresolver patient->hospitals
[{conn :com.eldrix.rsdb/conn} {patient-id :t_patient/id}]
{::pco/output [{:t_patient/hospitals [:t_patient_hospital/hospital_fk
:t_patient_hospital/patient_identifier
:t_patient_hospital/authoritative]}]}
{:t_patient/hospitals (db/execute! conn (sql/format {:select [:*]
:from [:t_patient_hospital]
:where [:= :patient_fk patient-id]}))})
(pco/defresolver patient-hospital->hospital
[{hospital_fk :t_patient_hospital/hospital_fk}]
{:t_patient_hospital/hospital {:urn:oid:2.16.840.1.113883.2.1.3.2.4.18.48/id hospital_fk}})
(pco/defresolver patient->country-of-birth
[{concept-id :t_patient/country_of_birth_concept_fk}]
{::pco/output [{:t_patient/country_of_birth [:info.snomed.Concept/id]}]}
(when concept-id {:t_patient/country_of_birth {:info.snomed.Concept/id concept-id}}))
(pco/defresolver patient->ethnic-origin
[{concept-id :t_patient/ethnic_origin_concept_fk}]
{::pco/output [{:t_patient/ethnic_origin [:info.snomed.Concept/id]}]}
(when concept-id {:t_patient/ethnic_origin {:info.snomed.Concept/id concept-id}}))
(pco/defresolver patient->racial-group
[{concept-id :t_patient/racial_group_concept_fk}]
{::pco/output [{:t_patient/racial_group [:info.snomed.Concept/id]}]}
(when concept-id {:t_patient/racial_group {:info.snomed.Concept/id concept-id}}))
(pco/defresolver patient->occupation
[{concept-id :t_patient/occupation_concept_fk}]
{::pco/output [{:t_patient/occupation [:info.snomed.Concept/id]}]}
(when concept-id {:t_patient/occupation {:info.snomed.Concept/id concept-id}}))
(pco/defresolver patient->surgery
[{surgery-fk :t_patient/surgery_fk}]
{::pco/output [{:t_patient/surgery [:urn:oid:2.16.840.1.113883.2.1.3.2.4.18.48/id]}]}
(when surgery-fk {:t_patient/surgery {:urn:oid:2.16.840.1.113883.2.1.3.2.4.18.48/id surgery-fk}}))
(pco/defresolver patient->death_certificate
[{conn :com.eldrix.rsdb/conn} {patient-pk :t_patient/id}]
{::pco/output [{:t_patient/death_certificate [:t_death_certificate/part1a
:t_death_certificate/part1b
:t_death_certificate/part1c
:t_death_certificate/part2]}]}
(when-let [certificate (patients/fetch-death-certificate conn patient-pk)]
{:t_patient/death_certificate certificate}))
(pco/defresolver patient->diagnoses
[{conn :com.eldrix.rsdb/conn} {patient-pk :t_patient/id}]
{::pco/output [{:t_patient/diagnoses [:t_diagnosis/concept_fk
{:t_diagnosis/diagnosis [:info.snomed.Concept/id]}
:t_diagnosis/date_diagnosis
:t_diagnosis/date_diagnosis_accuracy
:t_diagnosis/date_onset
:t_diagnosis/date_onset_accuracy
:t_diagnosis/date_to
:t_diagnosis/date_to_accuracy
:t_diagnosis/status
:t_diagnosis/full_description]}]}
(let [diagnoses (db/execute! conn (sql/format {:select [:*]
:from [:t_diagnosis]
:where [:= :patient_fk patient-pk]}))]
{:t_patient/diagnoses
(map #(assoc % :t_diagnosis/diagnosis {:info.snomed.Concept/id (:t_diagnosis/concept_fk %)}) diagnoses)}))
(pco/defresolver patient->summary-multiple-sclerosis ;; this is misnamed, but belies the legacy system's origins.
[{conn :com.eldrix.rsdb/conn} {patient-identifier :t_patient/patient_identifier}]
{::pco/output [{:t_patient/summary_multiple_sclerosis [:t_summary_multiple_sclerosis/id
:t_summary_multiple_sclerosis/ms_diagnosis_fk
:t_ms_diagnosis/id ; we flatten this to-one attribute
:t_ms_diagnosis/name]}]}
(let [sms (patients/fetch-summary-multiple-sclerosis conn patient-identifier)]
{:t_patient/summary_multiple_sclerosis sms}))
(pco/defresolver summary-multiple-sclerosis->events
[{conn :com.eldrix.rsdb/conn} {sms-id :t_summary_multiple_sclerosis/id}]
{::pco/output [{:t_summary_multiple_sclerosis/events
[:t_ms_event/id
:t_ms_event/date
:t_ms_event/is_relapse
:t_ms_event/site_arm_motor :t_ms_event/site_ataxia
:t_ms_event/site_bulbar :t_ms_event/site_cognitive
:t_ms_event/site_diplopia :t_ms_event/site_face_motor
:t_ms_event/site_face_sensory :t_ms_event/site_leg_motor
:t_ms_event/site_limb_sensory :t_ms_event/site_optic_nerve
:t_ms_event/site_other :t_ms_event/site_psychiatric
:t_ms_event/site_sexual :t_ms_event/site_sphincter
:t_ms_event/site_unknown :t_ms_event/site_vestibular
:t_ms_event_type/id ;; the event type is a to-one relationship, so we flatten this
:t_ms_event_type/name
:t_ms_event_type/abbreviation]}]}
(let [events (patients/fetch-ms-events conn sms-id)]
{:t_summary_multiple_sclerosis/events events}))
(pco/defresolver patient->medications
[{conn :com.eldrix.rsdb/conn} {patient-id :t_patient/id}]
{::pco/output [{:t_patient/medications [:t_medication/date_from
:t_medication/date_to
:t_medication/date_from_accuracy
:t_medication/date_to_accuracy
:t_medication/indication
:t_medication/medication_concept_fk
{:t_medication/medication [:info.snomed.Concept/id]}
:t_medication/more_information
:t_medication/temporary_stop
:t_medication/reason_for_stopping
:t_medication/dose
:t_medication/frequency
:t_medication/units
:t_medication/as_required
:t_medication/route
:t_medication/type
:t_medication/prescriptions]}]}
(let [medication (db/execute! conn (sql/format {:select [:*]
:from [:t_medication]
:where [:= :patient_fk patient-id]}))]
{:t_patient/medications
(map #(assoc % :t_medication/medication {:info.snomed.Concept/id (:t_medication/medication_concept_fk %)}) medication)}))
(def address-properties [:t_address/address1
:t_address/address2
:t_address/address3
:t_address/address4
:t_address/date_from
:t_address/date_to
:t_address/housing_concept_fk
:t_address/postcode
:t_address/ignore_invalid_address])
(pco/defresolver patient->addresses
[{:com.eldrix.rsdb/keys [conn]} {id :t_patient/id}]
{::pco/output [{:t_patient/addresses address-properties}]}
{:t_patient/addresses (patients/fetch-patient-addresses conn id)})
(pco/defresolver patient->address
"Returns the current address, or the address for the specified date.
Will make use of existing data in t_patient/addresses, if key exists.
This resolver takes an optional parameter :date. If provided, the address
for the specified date will be given.
Parameters:
- :date - a ISO LOCAL DATE string e.g \"2020-01-01\" or an instance of
java.time.LocalDate."
[{conn :com.eldrix.rsdb/conn :as env} {:t_patient/keys [id addresses]}]
{::pco/input [:t_patient/id
(pco/? :t_patient/addresses)]
::pco/output [:t_patient/address address-properties]}
(let [date (:date (pco/params env)) ;; doesn't matter if nil
date' (cond (nil? date) nil
(string? date) (LocalDate/parse date)
:else date)
addresses' (or addresses (patients/fetch-patient-addresses conn id))]
{:t_patient/address (patients/address-for-date addresses' date')}))
(def lsoa-re #"^[a-zA-Z]\d{8}$")
(pco/defresolver address->stored-lsoa
"Returns a stored LSOA for the address specified.
In general, LSOA should be determined by navigating to the PCD2 postal code
and then to the LSOA wanted, such as LSOA11.
This resolver returns an LSOA if one is stored in the address itself.
Currently, we simply look in the address1 field for a match, as we cannot
currently make database schema changes from this application. In the future,
this resolver could also use a dedicated stored LSOA field without clients
needing to know about the implementation change."
[{:t_address/keys [address1]}]
{::pco/output [:t_address/lsoa]}
(when (re-matches lsoa-re address1)
{:t_address/lsoa address1}))
(pco/defresolver address->housing
[{concept-id :t_address/housing_concept_fk}]
{::pco/output [{:t_address/housing [:info.snomed.Concept/id]}]}
(when concept-id {:t_address/housing {:info.snomed.Concept/id concept-id}}))
(pco/defresolver patient->episodes
[{conn :com.eldrix.rsdb/conn} {patient-id :t_patient/id}]
{::pco/output [{:t_patient/episodes [:t_episode/date_discharge
:t_episode/date_referral
:t_episode/date_registration
:t_episode/status
:t_episode/discharge_user_fk
:t_episode/id
:t_episode/notes
:t_episode/project_fk
:t_episode/referral_user_fk
:t_episode/registration_user_fk
:t_episode/stored_pseudonym
:t_episode/external_identifier]}]}
{:t_patient/episodes (->> (jdbc/execute! conn (sql/format {:select [:*]
:from [:t_episode]
:where [:= :patient_fk patient-id]
:order-by [[:t_episode/date_registration :asc]
[:t_episode/date_referral :asc]
[:t_episode/date_discharge :asc]]}))
(map #(assoc % :t_episode/status (projects/episode-status %))))})
(def project-properties
[:t_project/id :t_project/name :t_project/title
:t_project/long_description
:t_project/type :t_project/date_from :t_project/date_to
:t_project/exclusion_criteria :t_project/inclusion_criteria
:t_project/address1 :t_project/address2 :t_project/address3
:t_project/address4 :t_project/postcode
:t_project/parent_project_fk
:t_project/virtual :t_project/can_own_equipment
:t_project/specialty_concept_fk
:t_project/care_plan_information
:t_project/is_private])
(pco/defresolver episode->project
[{conn :com.eldrix.rsdb/conn} {project-id :t_episode/project_fk}]
{::pco/output [{:t_episode/project project-properties}]}
{:t_episode/project (db/execute-one! conn (sql/format {:select [:*]
:from [:t_project]
:where [:= :id project-id]}))})
(pco/defresolver project-by-identifier
[{conn :com.eldrix.rsdb/conn} {project-id :t_project/id}]
{::pco/output [:t_project/id :t_project/name
:t_project/title :t_project/long_description
:t_project/type
:t_project/date_from :t_project/date_to
:t_project/exclusion_criteria :t_project/inclusion_criteria
{:t_project/administrator_user [:t_user/id]}
:t_project/address1 :t_project/address2
:t_project/address3 :t_project/address4
:t_project/postcode
:t_project/ethics
{:t_project/parent_project [:t_project/id]}
:t_project/virtual
:t_project/can_own_equipment
{:t_project/specialty [:info.snomed.Concept/id]}
:t_project/advertise_to_all
:t_project/care_plan_information
:t_project/is_private]}
(when-let [p (projects/fetch-project conn project-id)]
(cond-> p
(:t_project/administrator_user_fk p)
(assoc :t_project/administrator_user {:t_user/id (:t_project/administrator_user_fk p)})
(:t_project/parent_project_fk p)
(assoc :t_project/parent_project {:t_project/id (:t_project/parent_project_fk p)}))))
(pco/defresolver project->count_registered_patients ;; TODO: should include child projects?
[{conn :com.eldrix.rsdb/conn} {project-id :t_project/id}]
{::pco/output [:t_project/count_registered_patients]}
{:t_project/count_registered_patients (projects/count-registered-patients conn [project-id])})
(pco/defresolver project->count_pending_referrals ;; TODO: should include child projects?
[{conn :com.eldrix.rsdb/conn} {project-id :t_project/id}]
{::pco/output [:t_project/count_pending_referrals]}
{:t_project/count_pending_referrals (projects/count-pending-referrals conn [project-id])})
(pco/defresolver project->count_discharged_episodes ;; TODO: should include child projects?
[{conn :com.eldrix.rsdb/conn} {project-id :t_project/id}]
{::pco/output [:t_project/count_discharged_episodes]}
{:t_project/count_discharged_episodes (projects/count-discharged-episodes conn [project-id])})
(pco/defresolver project->slug
[{title :t_project/title}]
{::pco/output [:t_project/slug]}
{:t_project/slug (projects/make-slug title)})
(pco/defresolver project->active?
[{date-from :t_project/date_from date-to :t_project/date_to :as project}]
{::pco/output [:t_project/active?]}
{:t_project/active? (projects/active? project)})
(pco/defresolver project->long-description-text
[{desc :t_project/long_description}]
{::pco/output [:t_project/long_description_text]}
{:t_project/long_description_text (html->text desc)})
(pco/defresolver project->encounter_templates
[{conn :com.eldrix.rsdb/conn} {project-id :t_project/id}]
{::pco/output [{:t_project/encounter_templates [:t_encounter_template/id
:t_encounter_template/encounter_type_fk
:t_encounter_template/is_deleted
:t_encounter_template/title
:t_encounter_type/id
{:t_encounter_template/encounter_type [:t_encounter_type/id]}]}]}
{:t_project/encounter_templates
(->> (db/execute! conn (sql/format {:select [:id :encounter_type_fk :is_deleted :title]
:from [:t_encounter_template]
:where [:= :project_fk project-id]}))
(map #(let [encounter-type-id (:t_encounter_template/encounter_type_fk %)]
(assoc % :t_encounter_type/id encounter-type-id
:t_encounter_template/encounter_type {:t_encounter_type/id encounter-type-id}))))})
(pco/defresolver project->parent
[{parent-id :t_project/parent_project_fk}]
{::pco/output [{:t_project/parent [:t_project/id]}]}
{:t_project/parent {:t_project/id parent-id}})
(pco/defresolver project->all-parents
[{conn :com.eldrix.rsdb/conn} {project-id :t_project/id}]
{::pco/output [{:t_project/all-parents [:t_project/id]}]}
{:t_project/all-parents (projects/all-parents conn project-id)})
(pco/defresolver project->all-children
[{conn :com.eldrix.rsdb/conn} {project-id :t_project/id}]
{::pco/output [{:t_project/all-children [:t_project/id]}]}
{:t_project/all-parents (projects/all-children conn project-id)})
(pco/defresolver project->specialty
[{specialty-concept-fk :t_project/specialty_concept_fk}]
{::pco/output [{:t_project/specialty [:info.snomed.Concept/id]}]}
{:t_project/specialty {:info.snomed.Concept/id specialty-concept-fk}})
(pco/defresolver project->common-concepts
"Resolve common concepts for the project, optionally filtering by a SNOMED
expression (ECL)."
[{conn :com.eldrix.rsdb/conn hermes :com.eldrix/hermes :as env} {:t_project/keys [id]}]
{::pco/input [:t_project/id]
::pco/output [{:t_project/common_concepts [:info.snomed.Concept/id]}]}
(let [concept-ids (projects/common-concepts conn id)
ecl (:ecl (pco/params env))]
(when (seq concept-ids)
(if (str/blank? ecl)
(reduce (fn [acc v] (conj acc {:info.snomed.Concept/id v})) [] concept-ids)
(let [concepts (str/join " OR " concept-ids)]
(->> (com.eldrix.hermes.core/expand-ecl hermes (str "(" concepts ") AND " ecl))
(map #(hash-map :info.snomed.Concept/id (:conceptId %)))))))))
(pco/defresolver project->users
[{conn :com.eldrix.rsdb/conn} {id :t_project/id}]
{::pco/output [{:t_project/users [:t_user/id]}]}
{:t_project/users (projects/fetch-users conn id)})
(pco/defresolver patient->encounters
[{:com.eldrix.rsdb/keys [conn]} {patient-id :t_patient/id}]
{::pco/output [{:t_patient/encounters [:t_encounter/id
:t_encounter/date_time
:t_encounter/active
:t_encounter/is_deleted
:t_encounter/hospital_fk
:t_encounter/ward
:t_encounter/episode_fk
:t_encounter/consultant_user_fk
:t_encounter/encounter_template_fk
:t_encounter/notes]}]}
{:t_patient/encounters (->> (db/execute! conn (sql/format {:select [:*]
:from [:t_encounter]
:where [:= :patient_fk patient-id]
:order-by [[:date_time :desc]]}))
(map #(assoc % :t_encounter/active (not (:t_encounter/is_deleted %)))))})
(pco/defresolver patient->results
[{:com.eldrix.rsdb/keys [conn]} {patient-identifier :t_patient/patient_identifier}]
{::pco/output [{:t_patient/results [:t_result_mri_brain/date
:t_result_mri_brain/report
:t_result_mri_brain/with_gadolinium
:t_result_jc_virus/date
:t_result_jc_virus/jc_virus
:t_result_csf_ocb/date
:t_result_csf_ocb/result]}]}
{:t_patient/results (com.eldrix.pc4.server.rsdb.results/results-for-patient conn patient-identifier)})
(pco/defresolver encounter->users
"Return the users for the encounter.
We flatten the relationship here, avoiding the join table."
[{conn :com.eldrix.rsdb/conn} {encounter-id :t_encounter/id}]
{::pco/output [{:t_encounter/users [:t_user/id]}]}
{:t_encounter/users
(->> (jdbc/execute! conn (sql/format {:select [:userid]
:from [:t_encounter_user]
:where [:= :encounterid encounter-id]}))
(map :t_encounter_user/userid)
(map #(hash-map :t_user/id %)))})
(pco/defresolver encounter->hospital
[{hospital-id :t_encounter/hospital_fk}]
{::pco/output [{:t_encounter/hospital [:urn:oid:2.16.840.1.113883.2.1.3.2.4.18.48/id]}]}
(when hospital-id {:t_encounter/hospital {:urn:oid:2.16.840.1.113883.2.1.3.2.4.18.48/id hospital-id}}))
(pco/defresolver encounter->encounter_template
[{:com.eldrix.rsdb/keys [conn]} {encounter-template-fk :t_encounter/encounter_template_fk}]
{::pco/output [{:t_encounter/encounter_template [:t_encounter_template/id
:t_encounter_template/encounter_type_fk]}]}
{:t_encounter/encounter_template (jdbc/execute-one! conn (sql/format {:select [:*] :from [:t_encounter_template]
:where [:= :id encounter-template-fk]}))})
(pco/defresolver encounter_template->encounter_type
[{:com.eldrix.rsdb/keys [conn]} {encounter-type-id :t_encounter_template/encounter_type_fk}]
{::pco/output [{:t_encounter_template/encounter_type [:t_encounter_type/id
:t_encounter_type/name
:t_encounter_type/seen_in_person]}]}
{:t_encounter_template/encounter_type (jdbc/execute-one! conn (sql/format {:select [:*] :from [:t_encounter_type] :where [:= :id encounter-type-id]}))})
(pco/defresolver encounter->form_edss
[{:com.eldrix.rsdb/keys [conn]} encounters]
{::pco/input [:t_encounter/id]
::pco/output [{:t_encounter/form_edss [:t_form_edss/id
:t_form_edss/edss
:t_form_edss/edss_score
:t_form_edss_fs/id
:t_form_edss_fs/edss_score]}]
::pco/batch? true}
(map #(when-not (every? nil? (vals %)) (hash-map :t_encounter/form_edss %))
(forms/encounters->form_edss conn encounters)))
(pco/defresolver encounter->form_ms_relapse
[{:com.eldrix.rsdb/keys [conn]} encounters]
{::pco/input [:t_encounter/id]
::pco/output [{:t_encounter/form_ms_relapse [:t_form_ms_relapse/id
:t_form_ms_relapse/in_relapse
:t_form_ms_relapse/activity
:t_form_ms_relapse/progression
:t_form_ms_disease_course/id
:t_form_ms_disease_course/name]}]
::pco/batch? true}
(map #(when-not (every? nil? (vals %))
(hash-map :t_encounter/form_ms_relapse %)) (forms/encounters->form_ms_relapse conn encounters)))
(pco/defresolver encounter->form_weight_height
[{:com.eldrix.rsdb/keys [conn]} encounters]
{::pco/input [:t_encounter/id]
::pco/output [{:t_encounter/form_weight_height [:t_form_weight_height/id
:t_form_weight_height/weight_kilogram
:t_form_weight_height/height_metres]}]
::pco/batch? true}
(map #(when-not (every? nil? (vals %))
(hash-map :t_encounter/form_weight_height %)) (forms/encounters->form_weight_height conn encounters)))
(pco/defresolver encounter->form_smoking
[{:com.eldrix.rsdb/keys [conn]} encounters]
{::pco/input [:t_encounter/id]
::pco/output [{:t_encounter/form_smoking_history [:t_smoking_history/id
:t_smoking_history/current_cigarettes_per_day
:t_smoking_history/status]}]
::pco/batch? true}
(map #(when-not (every? nil? (vals %))
(hash-map :t_encounter/form_smoking_history %)) (forms/encounters->form_smoking_history conn encounters)))
(pco/defresolver encounter->forms_generic_procedures
[{:com.eldrix.rsdb/keys [conn]} encounters]
{::pco/input [:t_encounter/id]
::pco/output [{:t_encounter/forms_generic_procedure [:t_form_procedure_generic/id
:t_form_procedure_generic/procedure_concept_fk
:info.snomed.Concept/id
:t_form_procedure_generic/notes]}]}
;;TODO: this is to show we would handle forms that support multiple per encounter like this....
(throw (ex-info "not implemented" {})))
(def user-properties
[:t_user/username
:t_user/title
:t_user/first_names
:t_user/last_name
:t_user/postnomial
:t_user/custom_initials
:t_user/email
:t_user/custom_job_title
:t_user/job_title_fk
:t_user/send_email_for_messages
:t_user/authentication_method
:t_user/professional_registration])
(pco/defresolver user-by-username
[{conn :com.eldrix.rsdb/conn} {username :t_user/username}]
{::pco/output user-properties}
(users/fetch-user conn username))
(pco/defresolver user-by-id
[{conn :com.eldrix.rsdb/conn} {id :t_user/id}]
{::pco/output user-properties}
(users/fetch-user-by-id conn id))
(pco/defresolver user-by-nadex
"Resolves rsdb user properties from a NADEX username if that user is
registered with rsdb with NADEX authentication."
[{conn :com.eldrix.rsdb/conn} {username :wales.nhs.nadex/sAMAccountName}]
{::pco/output user-properties}
(when-let [user (users/fetch-user conn username)]
(when (= (:t_user/authentication_method user) "NADEX")
user)))
(pco/defresolver user->photo
[{conn :com.eldrix.rsdb/conn} {:t_user/keys [username]}]
{::pco/output [{:t_user/photo [:t_photo/data
:t_photo/mime_type]}]}
(when-let [photo (com.eldrix.pc4.server.rsdb.users/fetch-user-photo conn username)]
{:t_user/photo {:t_photo/data (.encodeToString (Base64/getEncoder) (:data photo))
:t_photo/mime_type (:mimetype photo)}}))
(pco/defresolver user->full-name
[{:t_user/keys [title first_names last_name]}]
{::pco/output [:t_user/full_name]}
{:t_user/full_name (str title " " first_names " " last_name)})
(pco/defresolver user->initials
[{:t_user/keys [first_names last_name custom_initials]}]
{::pco/output [:t_user/initials]}
{:t_user/initials (if custom_initials
custom_initials
(str (apply str (map first (str/split first_names #"\s"))) (first last_name)))})
(pco/defresolver user->nadex
"Turn rsdb-derived user data into a representation from NADEX. This means
other resolvers (e.g. providing a FHIR view) can work on rsdb data!"
[{:t_user/keys [username first_names last_name title custom_job_title email professional_registration]
job_title :t_job_title/name
regulator :t_professional_registration_authority/abbreviation}]
{::pco/output [:wales.nhs.nadex/sAMAccountName
:wales.nhs.nadex/sn
:wales.nhs.nadex/givenName
:wales.nhs.nadex/personalTitle
:wales.nhs.nadex/mail
:urn:oid:2.5.4/commonName
{:wales.nhs.nadex/professionalRegistration [:regulator :code]}
:wales.nhs.nadex/title]}
{:wales.nhs.nadex/sAMAccountName username
:wales.nhs.nadex/sn last_name
:wales.nhs.nadex/givenName first_names
:wales.nhs.nadex/personalTitle title
:urn:oid:2.5.4/commonName (str/join " " [title first_names last_name])
:wales.nhs.nadex/mail email
:wales.nhs.nadex/professionalRegistration {:regulator regulator :code professional_registration}
:wales.nhs.nadex/title (or custom_job_title job_title)})
(pco/defresolver user->fhir-name
[{:t_user/keys [first_names last_name title postnomial]}]
{::pco/output [{:org.hl7.fhir.Practitioner/name [:org.hl7.fhir.HumanName/prefix
:org.hl7.fhir.HumanName/given
:org.hl7.fhir.HumanName/family
:org.hl7.fhir.HumanName/suffix
:org.hl7.fhir.HumanName/use]}]}
{:org.hl7.fhir.Practitioner/name [{:org.hl7.fhir.HumanName/prefix [title]
:org.hl7.fhir.HumanName/given (str/split first_names #"\s")
:org.hl7.fhir.HumanName/family last_name
:org.hl7.fhir.HumanName/suffix (str/split postnomial #"\s")
:org.hl7.fhir.HumanName/use :org.hl7.fhir.name-use/usual}]})
(pco/defresolver user->active-projects
[{conn :com.eldrix.rsdb/conn} {username :t_user/username}]
{::pco/output [{:t_user/active_projects [:t_project/id]}]}
{:t_user/active_projects (vec (filter projects/active? (users/projects conn username)))})
(pco/defresolver user->latest-news
[{conn :com.eldrix.rsdb/conn} {username :t_user/username}]
{::pco/output [{:t_user/latest_news [:t_news/id
:t_news/title
:t_news/body
{:t_news/author [:t_user/id]}]}]}
{:t_user/latest_news (vec (->> (users/fetch-latest-news conn username)
(map #(assoc % :t_news/author (select-keys % [:t_user/id :t_user/first_names :t_user/last_name])))))})
(def sex->fhir-patient
{"MALE" :org.hl7.fhir.administrative-gender/male
"FEMALE" :org.hl7.fhir.administrative-gender/female
"UNKNOWN" :org.hl7.fhir.administrative-gender/unknown})
(pco/defresolver patient->fhir-gender
[{sex :t_patient/sex}]
{:org.hl7.fhir.Patient/gender (get sex->fhir-patient sex)})
(pco/defresolver patient->fhir-human-name
[{:t_patient/keys [title first_names last_name]}]
{:org.hl7.fhir.Patient/name [{:org.hl7.fhir.HumanName/prefix (str/split title #" ")
:org.hl7.fhir.HumanName/given (str/split first_names #" ")
:org.hl7.fhir.HumanName/family last_name}]})
(comment
(s/explain-data ::register-patient-by-pseudonym {:user-id 5
:project-id 1
:nhs-number "111 111 1111"
:sex :MALE
:date-birth (LocalDate/of 1970 1 1)})
)
(pco/defmutation register-patient-by-pseudonym!
"Register a legacy pseudonymous patient. This will be deprecated in the
future.
TODO: switch to more pluggable and routine coercion of data, rather than
managing by hand
TODO: fix :authenticated-user so it includes a fuller dataset for each request
- we are already fetching user once, so just include properties and pass along"
[{conn :com.eldrix.rsdb/conn
config :com.eldrix.rsdb/config
manager :authorization-manager
user :authenticated-user}
{:keys [project-id nhs-number gender date-birth] :as params}]
{::pco/op-name 'pc4.rsdb/register-patient-by-pseudonym
::pco/output [:t_patient/patient_identifier
:t_episode/stored_pseudonym
:t_episode/project_fk]}
(log/info "register patient by pseudonym: " {:user user :params params})
(if-not (and manager (auth/authorized? manager #{project-id} :PATIENT_REGISTER))
(throw (ex-info "Not authorized" {:authorization-manager manager}))
(if-let [global-salt (:legacy-global-pseudonym-salt config)]
(let [params' (assoc params :user-id (:t_user/id (users/fetch-user conn (:value user))) ;; TODO: remove fetch
:salt global-salt
:nhs-number (str/replace nhs-number #"\s" "") ;; TODO: better automated coercion
:date-birth (LocalDate/parse date-birth))] ;; TODO: better automated coercion
(if (s/valid? ::register-patient-by-pseudonym params')
(projects/register-legacy-pseudonymous-patient conn params')
(log/error "invalid call" (s/explain-data ::register-patient-by-pseudonym params'))))
(log/error "unable to register patient by pseudonym; missing global salt: check configuration"
{:expected [:com.eldrix.rsdb/config :legacy-global-pseudonym-salt]
:config config}))))
(pco/defmutation search-patient-by-pseudonym
"Search for a patient using a pseudonymous project-specific identifier.
This uses the legacy approach, which *will* be deprecated."
[{conn :com.eldrix.rsdb/conn} {:keys [project-id pseudonym] :as params}]
{::pco/op-name 'pc4.rsdb/search-patient-by-pseudonym
::pco/output [:t_patient/patient_identifier
:t_episode/stored_pseudonym
:t_episode/project_fk]}
(log/debug "search-patient-by-pseudonym" params)
(projects/search-by-project-pseudonym conn project-id pseudonym))
(defn guard-can-for-patient? ;; TODO: turn into a macro for defmutation?
[{conn :com.eldrix.rsdb/conn manager :authorization-manager} patient-identifier permission]
(when-not patient-identifier
(throw (ex-info "invalid request: missing patient-identifier" {})))
(let [project-ids (patients/active-project-identifiers conn patient-identifier)]
(when-not (auth/authorized? manager project-ids permission)
(throw (ex-info "You are not authorised to perform this operation" {:patient-identifier patient-identifier
:permission permission})))))
(s/def ::save-diagnosis
(s/and
(s/keys :req [:t_patient/patient_identifier
:t_diagnosis/diagnosis
:t_diagnosis/status]
:opt [:t_diagnosis/date_onset
:t_diagnosis/date_diagnosis
:t_diagnosis/date_onset_accuracy
:t_diagnosis/date_diagnosis_accuracy
:t_diagnosis/date_to
:t_diagnosis/date_to_accuracy])
valid-diagnosis-status?
ordered-diagnostic-dates?))
(pco/defmutation save-diagnosis!
[{conn :com.eldrix.rsdb/conn
manager :authorization-manager
user :authenticated-user
:as env} params]
{::pco/op-name 'pc4.rsdb/save-diagnosis}
(log/info "save diagnosis request: " params "user: " user)
(let [params' (assoc params ::user-id (:t_user/id (users/fetch-user conn (:value user))) ;; TODO: remove fetch of user id
:t_diagnosis/concept_fk (get-in params [:t_diagnosis/diagnosis :info.snomed.Concept/id]))]
(if-not (s/valid? ::save-diagnosis params')
(do (log/error "invalid call" (s/explain-data ::create-diagnosis params'))
(throw (ex-info "Invalid data" (s/explain-data ::create-diagnosis params'))))
(do (guard-can-for-patient? env (:t_patient/patient_identifier params) :PATIENT_EDIT)
(let [diag (if (:t_diagnosis/id params')
(patients/update-diagnosis conn params')
(patients/create-diagnosis conn params'))]
(assoc-in diag [:t_diagnosis/diagnosis :info.snomed.Concept/id] (:t_diagnosis/concept_fk diag)))))))
(s/def ::save-medication
(s/and
(s/keys :req [::user-id
:t_patient/patient_identifier]
:opt [:t_medication/id
:t_medication/medication
:t_medication/date_from
:t_medication/date_to])))
(pco/defmutation save-medication!
[{conn :com.eldrix.rsdb/conn
manager :authorization-manager
user :authenticated-user
:as env} params]
{::pco/op-name 'pc4.rsdb/save-medication}
(log/info "save medication request: " params "user: " user)
(let [params' (assoc params ::user-id (:t_user/id (users/fetch-user conn (:value user))) ;; TODO: remove fetch of user id
:t_medication/medication_concept_fk (get-in params [:t_medication/medication :info.snomed.Concept/id]))]
(if-not (s/valid? ::save-medication params')
(do (log/error "invalid call" (s/explain-data ::save-medication params'))
(throw (ex-info "Invalid data" (s/explain-data ::save-medication params'))))
(do (guard-can-for-patient? env (:t_patient/patient_identifier params) :PATIENT_EDIT)
(let [med (if (:t_medication/id params')
(if (:t_medication/medication_concept_fk params')
(patients/update-medication conn params')
(patients/delete-medication conn params'))
(patients/create-medication conn params'))]
(assoc-in med [:t_medication/medication :info.snomed.Concept/id] (:t_medication/medication_concept_fk med)))))))
(s/def ::save-ms-diagnosis
(s/keys :req [:t_user/id
:t_ms_diagnosis/id
:t_patient/patient_identifier]))
(pco/defmutation save-patient-ms-diagnosis! ;; TODO: could update main diagnostic list...
[{conn :com.eldrix.rsdb/conn
manager :authorization-manager
user :authenticated-user
:as env} {patient-identifier :t_patient/patient_identifier :as params}]
{::pco/op-name 'pc4.rsdb/save-ms-diagnosis}
(log/info "save ms diagnosis:" params " user:" user)
(let [params' (assoc params :t_user/id (:t_user/id (users/fetch-user conn (:value user))))]
(if-not (s/valid? ::save-ms-diagnosis params')
(do (log/error "invalid call" (s/explain-data ::save-ms-diagnosis params'))
(throw (ex-info "Invalid data" (s/explain-data ::save-ms-diagnosis params'))))
(do (guard-can-for-patient? env patient-identifier :PATIENT_EDIT)
(patients/save-ms-diagnosis! conn params')
(patient->summary-multiple-sclerosis env params)))))
(s/def ::save-pseudonymous-postal-code
(s/keys :req [:t_patient/patient_identifier
:uk.gov.ons.nhspd/PCD2]))
(pco/defmutation save-pseudonymous-patient-postal-code!
[{conn :com.eldrix.rsdb/conn
ods :com.eldrix.clods.graph/svc
user :authenticated-user}
{patient-identifier :t_patient/patient_identifier
postcode :uk.gov.ons.nhspd/PCD2 :as params}]
{::pco/op-name 'pc4.rsdb/save-pseudonymous-patient-postal-code}
(log/info "saving pseudonymous postal code" {:params params :ods ods})
(if-not (s/valid? ::save-pseudonymous-postal-code params)
(throw (ex-info "invalid request" (s/explain-data ::save-pseudonymous-postal-code params)))
(if (str/blank? postcode)
(patients/save-pseudonymous-patient-lsoa! conn {:t_patient/patient_identifier patient-identifier
:uk.gov.ons.nhspd/LSOA11 ""})
(let [pc (com.eldrix.clods.core/fetch-postcode ods postcode)]
(patients/save-pseudonymous-patient-lsoa! conn {:t_patient/patient_identifier patient-identifier
:uk.gov.ons.nhspd/LSOA11 (get pc "LSOA11")})))))
(s/def ::save-ms-event
(s/keys :req [:t_ms_event/date
:t_ms_event_type/id
:t_ms_event/impact
:t_ms_event/summary_multiple_sclerosis_fk]))
(pco/defmutation save-ms-event!
[{conn :com.eldrix.rsdb/conn
manager :authorization-manager
user :authenticated-user
:as env} params]
{::pco/op-name 'pc4.rsdb/save-ms-event}
(log/info "save ms event request: " params "user: " user)
(if-not (s/valid? ::save-ms-event params)
(do (log/error "invalid call" (s/explain-data ::save-ms-event params))
(throw (ex-info "Invalid data" (s/explain-data ::save-ms-event params))))
(let [patient-identifier (or (:t_patient/patient_identifier params)
(patients/patient-identifier-for-ms-event conn params))]
(do (guard-can-for-patient? env patient-identifier :PATIENT_EDIT)
(patients/save-ms-event! conn (-> params
(dissoc :t_patient/patient_identifier
:t_ms_event_type/id
:t_ms_event_type/abbreviation
:t_ms_event_type/name
:t_ms_event/is_relapse)
(assoc :t_ms_event/ms_event_type_fk (:t_ms_event_type/id params))))))))
(s/def ::delete-ms-event
(s/keys :req [:t_user/id
:t_ms_event/id]))
(pco/defmutation delete-ms-event!
[{conn :com.eldrix.rsdb/conn
manager :authorization-manager
user :authenticated-user
:as env} {ms-event-id :t_ms_event/id :as params}]
{::pco/op-name 'pc4.rsdb/delete-ms-event}
(log/info "delete ms event:" params " user:" user)
(let [params' (assoc params :t_user/id (:t_user/id (users/fetch-user conn (:value user))))] ;; TODO: need a better way than this...
(if-not (s/valid? ::delete-ms-event params')
(do (log/error "invalid call" (s/explain-data ::delete-ms-event params'))
(throw (ex-info "Invalid data" (s/explain-data ::delete-ms-event params'))))
(when-let [patient-identifier (patients/patient-identifier-for-ms-event conn params')]
(guard-can-for-patient? env patient-identifier :PATIENT_EDIT)
(patients/delete-ms-event! conn params')))))
(s/def ::save-encounter
(s/keys :req [:t_patient/patient_identifier
:t_encounter/date_time
:t_episode/id]
:opt [:t_encounter/id ;; if we've saving an existing encounter
:t_encounter_template/id
:t_form_weight_height/id ;; if we're updating an existing form
:t_form_weight_height/height_metres
:t_form_weight_height/weight_kilogram
:t_form_edss/id ;; if we're updating an existing form
:t_form_edss/edss_score
:t_form_ms_relapse/id ;; if we're updating an existing form
:t_form_ms_relapse/in_relapse
:t_form_ms_relapse/ms_disease_course_fk
:t_smoking_history/id
:t_smoking_history/current_cigarettes_per_day
:t_smoking_history/status]))
(pco/defmutation save-encounter!
[{conn :com.eldrix.rsdb/conn
manager :authorization-manager
user :authenticated-user
:as env} params]
{::pco/op-name 'pc4.rsdb/save-encounter}
(log/info "save encounter request: " params "user: " user)
(let [date (:t_encounter/date_time params)
params' (assoc params
:t_encounter/date_time (if (instance? LocalDate date) (.atStartOfDay date) date)
:t_user/id (:t_user/id (users/fetch-user conn (:value user))))] ;; TODO: need a better way than this...
(when-not (s/valid? ::save-encounter params')
(log/error "invalid call" (s/explain-data ::save-encounter params'))
(throw (ex-info "Invalid data" (s/explain-data ::save-encounter params'))))
(do (guard-can-for-patient? env (:t_patient/patient_identifier params) :PATIENT_EDIT)
(forms/save-encounter-with-forms! conn params'))))
(s/def ::delete-encounter (s/keys :req [:t_encounter/id :t_patient/patient_identifier]))
(pco/defmutation delete-encounter!
[{conn :com.eldrix.rsdb/conn
manager :authorization-manager
user :authenticated-user
:as env}
{encounter-id :t_encounter/id
patient-identifier :t_patient/patient_identifier :as params}]
{::pco/op-name 'pc4.rsdb/delete-encounter}
(log/info "delete encounter:" encounter-id " user:" user)
(if-not (s/valid? ::delete-encounter params)
(do (log/error "invalid delete encounter" (s/explain-data ::delete-encounter params))
(throw (ex-info "Invalid 'delete encounter' data:" (s/explain-data ::delete-encounter params))))
(do (guard-can-for-patient? env patient-identifier :PATIENT_EDIT)
(patients/delete-encounter! conn encounter-id))))
(s/def ::save-result (s/keys :req [:t_patient/patient_identifier]))
(pco/defmutation save-result!
[{conn :com.eldrix.rsdb/conn
manager :authorization-manager
user :authenticated-user
:as env} params]
{::pco/op-name 'pc4.rsdb/save-result}
(log/info "save result request: " params "user: " user)
(when-not (s/valid? ::save-result params)
(log/error "invalid save result request" (s/explain-data ::save-result params))
(throw (ex-info "Invalid save result request" (s/explain-data ::save-result params))))
(let [params' (assoc params
:patient_fk (patients/patient-identifier->pk conn (:t_patient/patient_identifier params))
:user_fk (:t_user/id (users/fetch-user conn (:value user))))] ;; TODO: need a better way than this...
(do (guard-can-for-patient? env (:t_patient/patient_identifier params) :PATIENT_EDIT)
(results/save-result! conn params'))))
(pco/defmutation delete-result!
[{conn :com.eldrix.rsdb/conn
manager :authorization-manager
user :authenticated-user
:as env} params]
{::pco/op-name 'pc4.rsdb/delete-result}
(log/info "delete result request: " params "user: " user)
(do (guard-can-for-patient? env (:t_patient/patient_identifier params) :PATIENT_EDIT)
(results/delete-result! conn params)))
(s/def ::notify-death (s/keys :req [:t_patient/patient_identifier
:t_patient/date_death]
:opt [:t_death_certificate/part1a
:t_death_certificate/part1b
:t_death_certificate/part1c
:t_death_certificate/part2]))
(pco/defmutation notify-death!
[{conn :com.eldrix.rsdb/conn
manager :authorization-manager
user :authenticated-user
:as env}
{patient-identifier :t_patient/patient_identifier :as params}]
{::pco/op-name 'pc4.rsdb/notify-death}
(log/info "notify death request: " params "user: " user)
(when-not (s/valid? ::notify-death params)
(throw (ex-info "Invalid notify-death request" (s/explain-data ::notify-death params))))
(guard-can-for-patient? env patient-identifier :PATIENT_EDIT)
(patients/notify-death! conn params))
(s/def :t_user/username string?)
(s/def :t_user/password string?)
(s/def :t_user/new_password string?)
(s/def ::change-password (s/keys :req [:t_user/username
:t_user/new_password]
:opt [:t_user/password]))