-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathresource_watch.go
61 lines (54 loc) · 1.59 KB
/
resource_watch.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
package subscription
import (
"time"
"github.com/gin-gonic/gin"
fhirmodels "github.com/intervention-engine/fhir/models"
)
func GenerateResourceWatch(subUpdateQueue chan<- ResourceUpdateMessage) gin.HandlerFunc {
return func(c *gin.Context) {
c.Next()
if c.IsAborted() {
return
}
if c.Request.Method == "GET" {
return
}
if resourceType, ok := c.Get("Resource"); ok {
if resource, ok := c.Get(resourceType.(string)); ok {
HandleResourceUpdate(subUpdateQueue, resource)
}
}
return
}
}
func HandleResourceUpdate(subUpdateQueue chan<- ResourceUpdateMessage, resource interface{}) {
for patientID, timestamp := range triggeredPatients(resource) {
ru := NewResourceUpdateMessage(patientID, timestamp.Format(time.RFC3339))
subUpdateQueue <- ru
}
}
func triggeredPatients(resource interface{}) map[string]time.Time {
result := make(map[string]time.Time)
// TODO: This code causes panics when the expected time fields are not present!
// Need to determine correct course of action when that occurs.
switch t := resource.(type) {
case *fhirmodels.Condition:
result[t.Patient.ReferencedID] = t.OnsetDateTime.Time
case *fhirmodels.MedicationStatement:
result[t.Patient.ReferencedID] = t.EffectivePeriod.Start.Time
case *fhirmodels.Encounter:
result[t.Patient.ReferencedID] = t.Period.Start.Time
case *fhirmodels.Bundle:
for _, entry := range t.Entry {
if entry.Resource != nil {
subResult := triggeredPatients(entry.Resource)
for k, v := range subResult {
if t, ok := result[k]; !ok || v.After(t) {
result[k] = v
}
}
}
}
}
return result
}