-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathUser+FHIR.swift
101 lines (85 loc) · 2.47 KB
/
User+FHIR.swift
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
//
// User+FHIR.swift
// c3-pro
//
// Created by Pascal Pfiffner on 7/20/15.
// Copyright (c) 2015 Boston Children's Hospital. All rights reserved.
//
import SMART
import HealthKit
extension User {
/**
Returns a tuple containing (patient, observations) derived from the receiver.
Observations contain body height and body weight, if they are set on the receiver.
*/
public func c3_asPatientAndObservations() -> (patient: Patient, observations: [Observation]?) {
let patient = Patient()
patient.id = userId?.fhir_string
// gender
switch biologicalSex {
case .male:
patient.gender = .male
case .female:
patient.gender = .female
case .other:
patient.gender = .other
default:
break
}
// birthdate
if let bday = birthDate {
patient.birthDate = bday.fhir_asDate()
}
// observations
var observations = [Observation]()
do {
let subject = try patient.asRelativeReference() // only fails if there is no id
if let height = c3_heightAsObservation(for: subject) {
observations.append(height)
}
if let weight = c3_weightAsObservation(for: subject) {
observations.append(weight)
}
}
catch let error {
c3_logIfDebug("\(error)")
}
return (patient: patient, observations: observations)
}
/**
Returns the receiver's body height as an Observation with LOINC code 8302-2, nil if there's no height.
*/
public func c3_heightAsObservation(for subject: Reference) -> Observation? {
if let height = bodyheight {
if let quantity = try? height.c3_asFHIRQuantityInUnit(HKUnit.meterUnit(with: .centi)) {
let obs = c3_observation(for: quantity, withLOINCCode: "8302-2")
obs.subject = subject
return obs
}
}
return nil
}
/**
Returns the receiver's weight as an Observation with LOINC code 3141-9, nil if there's no weight.
*/
public func c3_weightAsObservation(for subject: Reference) -> Observation? {
if let weight = bodyweight {
if let quantity = try? weight.c3_asFHIRQuantityInUnit(HKUnit.gramUnit(with: .kilo)) {
let obs = c3_observation(for: quantity, withLOINCCode: "3141-9")
obs.subject = subject
return obs
}
}
return nil
}
func c3_observation(for quantity: Quantity, withLOINCCode loinc: FHIRString) -> Observation {
let coding = Coding()
coding.system = FHIRURL("http://loinc.org")
coding.code = loinc
let code = CodeableConcept()
code.coding = [coding]
let obs = Observation(code: code, status: .final)
obs.valueQuantity = quantity
return obs
}
}