-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbundle_updater.go
161 lines (153 loc) · 5.29 KB
/
bundle_updater.go
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
package hdsfhir
import (
"fmt"
"net/url"
"sort"
"strings"
"time"
"github.com/intervention-engine/fhir/models"
)
// ConvertToConditionalUpdates converts a bundle containing POST requests to a bundle with PUT requests using
// conditional updates. For patient resources, the update is based on the Medical Record Number. For all other
// resources it is based on reasonable indicators of sameness (such as equal dates and codes).
func ConvertToConditionalUpdates(bundle *models.Bundle) error {
for _, entry := range bundle.Entry {
values := url.Values{}
switch t := entry.Resource.(type) {
case *models.AllergyIntolerance:
if check(t.Patient, t.Substance, t.Onset) {
addRefParam(values, "patient", t.Patient)
addCCParam(values, "substance", t.Substance)
addDateParam(values, "onset", t.Onset)
}
case *models.Condition:
if check(t.Patient, t.Code, t.OnsetDateTime) {
addRefParam(values, "patient", t.Patient)
addCCParam(values, "code", t.Code)
addDateParam(values, "onset", t.OnsetDateTime)
}
case *models.DiagnosticReport:
// TODO: Consider if this query is precise enough, consider searching on results too
if check(t.Subject, t.Code, t.EffectivePeriod) {
addRefParam(values, "patient", t.Subject)
addCCParam(values, "code", t.Code)
addPeriodParam(values, "date", t.EffectivePeriod)
}
case *models.Encounter:
if check(t.Patient, t.Type, t.Period) {
addRefParam(values, "patient", t.Patient)
for _, cc := range t.Type {
addCCParam(values, "type", &cc)
}
// TODO: the date param references "a date within the period the encounter lasted." Is this OK?
addPeriodParam(values, "date", t.Period)
}
case *models.Immunization:
if check(t.Patient, t.VaccineCode, t.Date) {
addRefParam(values, "patient", t.Patient)
addCCParam(values, "vaccine-code", t.VaccineCode)
addDateParam(values, "date", t.Date)
}
case *models.MedicationStatement:
if check(t.Patient, t.MedicationCodeableConcept, t.EffectivePeriod) {
addRefParam(values, "patient", t.Patient)
addCCParam(values, "code", t.MedicationCodeableConcept)
addPeriodParam(values, "effectivedate", t.EffectivePeriod)
}
case *models.Observation:
if check(t.Subject, t.Code, t.EffectivePeriod) {
addRefParam(values, "patient", t.Subject)
addCCParam(values, "code", t.Code)
addPeriodParam(values, "date", t.EffectivePeriod)
if check(t.ValueCodeableConcept) {
addCCParam(values, "value-concept", t.ValueCodeableConcept)
} else if check(t.ValueQuantity) {
q := t.ValueQuantity
if q.Code != "" {
values.Add("value-quantity", fmt.Sprintf("%g|%s|%s", *q.Value, q.System, q.Code))
} else {
values.Add("value-quantity", fmt.Sprintf("%g|%s|%s", *q.Value, q.System, q.Unit))
}
} else if check(t.ValueString) {
values.Add("value-string", t.ValueString)
}
}
case *models.Procedure:
if check(t.Subject, t.Code, t.PerformedPeriod) {
addRefParam(values, "patient", t.Subject)
addCCParam(values, "code", t.Code)
addPeriodParam(values, "date", t.PerformedPeriod)
}
case *models.ProcedureRequest:
// We can't do anything meaningful because ProcedureRequest does not have search params
// for code or orderedOn. We simply can't get precise enough for a conditional update.
case *models.Patient:
if len(t.Identifier) > 0 && t.Identifier[0].Value != "" {
values.Set("identifier", t.Identifier[0].Value)
}
}
if entry.Request.Method == "POST" && len(values) > 0 {
entry.Request.Method = "PUT"
entry.Request.Url += "?" + values.Encode()
}
}
return nil
}
func check(things ...interface{}) bool {
for _, t := range things {
switch t := t.(type) {
case *models.CodeableConcept:
if t == nil || len(t.Coding) == 0 {
return false
}
case []models.CodeableConcept:
if len(t) == 0 || !check(&t[0]) {
return false
}
case *models.FHIRDateTime:
if t == nil || t.Time.IsZero() {
return false
}
case *models.Period:
if t == nil || !check(t.Start) {
return false
}
case *models.Quantity:
if t == nil || t.Value == nil {
return false
}
case *models.Reference:
if t == nil || t.Reference == "" {
return false
}
case string:
if t == "" {
return false
}
}
}
return true
}
func addCCParam(values url.Values, name string, cc *models.CodeableConcept) {
codes := make([]string, len(cc.Coding))
for i := range cc.Coding {
codes[i] = cc.Coding[i].System + "|" + cc.Coding[i].Code
}
// sort for predictability (a.k.a., easier testing)
sort.Strings(codes)
values.Add(name, strings.Join(codes, ","))
}
func addDateParam(values url.Values, name string, date *models.FHIRDateTime) {
values.Add(name, date.Time.Format("2006-01-02T15:04:05-07:00"))
}
func addPeriodParam(values url.Values, name string, period *models.Period) {
// Due to the way searching on periods is defined, the only way we can try to match on the start date is by using
// a combination of sa (starts after) and lt (less than) query parameters.
l := period.Start.Time.Add(-1 * time.Second)
h := period.Start.Time.Add(1 * time.Second)
values.Add(name, "sa"+l.Format("2006-01-02T15:04:05-07:00"))
values.Add(name, "lt"+h.Format("2006-01-02T15:04:05-07:00"))
}
func addRefParam(values url.Values, name string, ref *models.Reference) {
values.Add(name, ref.Reference)
}