-
Notifications
You must be signed in to change notification settings - Fork 2
/
procedure.go
180 lines (162 loc) · 6.37 KB
/
procedure.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package hdsfhir
import fhir "github.com/intervention-engine/fhir/models"
type Procedure struct {
Entry
AnatomicalTarget *CodeObject `json:"anatomical_target"`
Values []ResultValue `json:"values"`
}
func (p *Procedure) FHIRModels() []interface{} {
if p.isProcedureRequest() {
return p.convertProcedureRequest()
}
return p.convertProcedure()
}
func (p *Procedure) isProcedureRequest() bool {
statusConcept := p.StatusCode.FHIRCodeableConcept("")
switch {
case statusConcept.MatchesCode("http://hl7.org/fhir/ValueSet/v3-ActStatus", "cancelled"):
return true
case statusConcept.MatchesCode("http://hl7.org/fhir/ValueSet/v3-ActStatus", "held"):
return true
case statusConcept.MatchesCode("http://hl7.org/fhir/ValueSet/v3-ActStatus", "new"):
return true
case statusConcept.MatchesCode("http://hl7.org/fhir/ValueSet/v3-ActStatus", "suspended"):
return true
case statusConcept.MatchesCode("http://hl7.org/fhir/ValueSet/v3-ActStatus", "nullified"):
return true
// NOTE: this is not a real ActStatus, but HDS seems to use it
case statusConcept.MatchesCode("http://hl7.org/fhir/ValueSet/v3-ActStatus", "ordered"):
return true
// NOTE: this is not a real ActStatus, but HDS seems to use it
case statusConcept.MatchesCode("http://hl7.org/fhir/ValueSet/v3-ActStatus", "recommended"):
return true
case p.MoodCode == "RQO":
return true
}
return false
}
func (p *Procedure) convertProcedure() []interface{} {
fhirProcedure := &fhir.Procedure{}
fhirProcedure.Id = p.GetTempID()
fhirProcedure.Subject = p.Patient.FHIRReference()
fhirProcedure.Status = p.convertProcedureStatus()
fhirProcedure.Code = p.Codes.FHIRCodeableConcept(p.Description)
if p.NegationInd {
t := true
fhirProcedure.NotPerformed = &t
}
if p.NegationReason != nil {
cc := p.NegationReason.FHIRCodeableConcept("")
fhirProcedure.ReasonNotPerformed = []fhir.CodeableConcept{*cc}
}
if p.AnatomicalTarget != nil {
cc := p.AnatomicalTarget.FHIRCodeableConcept("")
fhirProcedure.BodySite = []fhir.CodeableConcept{*cc}
}
fhirProcedure.PerformedPeriod = p.GetFHIRPeriod()
fhirProcedure.Encounter = p.Patient.MatchingEncounterReference(p.Entry)
models := []interface{}{fhirProcedure}
if len(p.Values) > 0 {
// Create the diagnostic report model with its own ID and slots for results
internalReportID := &TemporallyIdentified{}
fhirReport := &fhir.DiagnosticReport{}
fhirReport.Id = internalReportID.GetTempID()
fhirReport.Status = "final"
fhirReport.Code = &fhir.CodeableConcept{
Coding: []fhir.Coding{
{
System: "http://loinc.org",
Code: "59776-5",
Display: "Procedure findings narrative",
},
},
Text: "Procedure findings narrative",
}
fhirReport.Subject = p.Patient.FHIRReference()
fhirReport.Encounter = p.Patient.MatchingEncounterReference(p.Entry)
fhirReport.EffectivePeriod = p.GetFHIRPeriod()
if p.EndTime != nil {
fhirReport.Issued = p.EndTime.FHIRDateTime() // Not perfect, but it's a required field
}
// TODO: Technically, "performer" is required, but we don't want to make up data
fhirReport.Result = make([]fhir.Reference, len(p.Values))
models = append(models, fhirReport)
// Link the procedure to the report
fhirProcedure.Report = []fhir.Reference{*internalReportID.FHIRReference()}
// Create the observation values
for i := range p.Values {
observation := p.Values[i].FHIRModels()[0].(*fhir.Observation)
observation.Code = p.Codes.FHIRCodeableConcept(p.Description)
observation.Subject = p.Patient.FHIRReference()
observation.Encounter = p.Patient.MatchingEncounterReference(p.Entry)
observation.EffectivePeriod = p.GetFHIRPeriod()
models = append(models, observation)
// Link the report results to the observation
fhirReport.Result[i] = *p.Values[i].FHIRReference()
}
}
return models
}
// convertProcedureStatus maps the status to a code in the required FHIR value set:
// http://hl7.org/fhir/DSTU2/valueset-procedure-status.html
func (p *Procedure) convertProcedureStatus() string {
var status string
statusConcept := p.StatusCode.FHIRCodeableConcept("")
switch {
case statusConcept.MatchesCode("http://hl7.org/fhir/ValueSet/v3-ActStatus", "aborted"):
status = "aborted"
case statusConcept.MatchesCode("http://hl7.org/fhir/ValueSet/v3-ActStatus", "active"):
status = "in-progress"
case statusConcept.MatchesCode("http://hl7.org/fhir/ValueSet/v3-ActStatus", "obsolete"):
status = "entered-in-error"
case len(p.StatusCode) == 0 && p.EndTime == nil:
status = "in-progress"
default:
status = "completed"
}
return status
}
func (p *Procedure) convertProcedureRequest() []interface{} {
fhirProcedureRequest := &fhir.ProcedureRequest{}
fhirProcedureRequest.Id = p.GetTempID()
fhirProcedureRequest.Subject = p.Patient.FHIRReference()
fhirProcedureRequest.Status = p.convertProcedureRequestStatus()
fhirProcedureRequest.Code = p.Codes.FHIRCodeableConcept(p.Description)
if p.AnatomicalTarget != nil {
cc := p.AnatomicalTarget.FHIRCodeableConcept("")
fhirProcedureRequest.BodySite = []fhir.CodeableConcept{*cc}
}
if p.Time != nil {
fhirProcedureRequest.OrderedOn = p.Time.FHIRDateTime()
} else if p.StartTime != nil {
fhirProcedureRequest.OrderedOn = p.StartTime.FHIRDateTime()
} else if p.EndTime != nil {
fhirProcedureRequest.OrderedOn = p.EndTime.FHIRDateTime()
}
fhirProcedureRequest.Encounter = p.Patient.MatchingEncounterReference(p.Entry)
return []interface{}{fhirProcedureRequest}
}
// convertProcedureRequestStatus maps the status to a code in the required FHIR value set:
// http://hl7.org/fhir/DSTU2/valueset-procedure-request-status.html
func (p *Procedure) convertProcedureRequestStatus() string {
var status string
statusConcept := p.StatusCode.FHIRCodeableConcept("")
switch {
case p.NegationInd == true:
status = "rejected"
case statusConcept.MatchesCode("http://hl7.org/fhir/ValueSet/v3-ActStatus", "cancelled"):
status = "rejected"
case statusConcept.MatchesCode("http://hl7.org/fhir/ValueSet/v3-ActStatus", "held"):
status = "suspended"
case statusConcept.MatchesCode("http://hl7.org/fhir/ValueSet/v3-ActStatus", "suspended"):
status = "suspended"
case statusConcept.MatchesCode("http://hl7.org/fhir/ValueSet/v3-ActStatus", "nullified"):
status = "rejected"
// NOTE: this is not a real ActStatus, but HDS seems to use it
case statusConcept.MatchesCode("http://hl7.org/fhir/ValueSet/v3-ActStatus", "recommended"):
status = "proposed"
default:
status = "accepted"
}
return status
}