-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRequest.swift
129 lines (94 loc) · 3.72 KB
/
Request.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
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
//
// Request.swift
// SMARTMarkers
//
// Created by Raheel Sayeed on 3/1/19.
// Copyright © 2019 Boston Children's Hospital. All rights reserved.
//
import Foundation
import SMART
/**
PGHD Request Protocol
Fetches and Manages `FHIR` request resource
*/
public protocol Request: DomainResource {
/// Request identifier
var rq_identifier: String { get }
/// Title Representation of the Request
var rq_title: String? { get }
/// Person requesting the PRO
var rq_requesterName: String? { get }
/// Requested activity code
var rq_code: Coding? { get }
/// Entity requesting the PRO
var rq_requesterEntity: String? { get }
/// Date of Request
var rq_requestDate: Date? { get }
/// Primary identifying Request Code
var rq_categoryCode: String? { get }
/// Schedule
var rq_schedule: TaskSchedule? { get set }
/// Fetch Parameters
static var rq_fetchParameters: [String: String]? { get }
/// Embedded Questionnaire URL
var rq_instrumentMetadataQuestionnaireReferenceURL: URL? { get }
/// Notifies request has been updated
func rq_updated(_ completed: Bool, callback: @escaping ((_ success: Bool) -> Void))
/// Requested Instrument
func rq_resolveInstrument(callback: @escaping ((_ instrument: Instrument?, _ error: Error?) -> Void))
/// Resolve FHIR References if needed;
func rq_resolveReferences(callback: @escaping ((Bool) -> Void))
/// Call for the receiver to configure itself with the instrument, scheduling, subject (patient) and practitioner
func rq_configureNew(for instrument: Instrument, schedule: TaskSchedule?, patient: Patient?, practitioner: Practitioner?) throws
}
public extension Request {
var description : String {
return "Request: \(rq_identifier)"
}
}
public extension Request {
static func rq_create() -> Self? {
//TODO: Swift 5.1 should replace this!
return ServiceRequest() as? Self
}
static func PGHDRequests(from server: Server, for patient: Patient?, options: [String:String]?, callback: @escaping ((_ requestResources: [Self]?, _ error: Error?) -> Void)) {
var searchParams = rq_fetchParameters ?? [String:String]()
if let patientFHIRID = patient?.id?.string {
searchParams["subject"] = patientFHIRID
}
if let options = options {
for (k,v) in options {
searchParams[k] = v
}
}
PGHDRequests(from: server, options: searchParams, callback: callback)
}
static func PGHDRequests(from server: Server, options: [String:String]?, callback: @escaping ((_ requestResources: [Self]?, _ error: Error?) -> Void)) {
let search = Self.search(options as Any)
search.pageCount = 100
search.perform(server) { (bundle, error) in
if let bundle = bundle {
let resources = bundle.entry?.filter { $0.resource is Self }.map { $0.resource as! Self }
let group = DispatchGroup()
for r in resources ?? [] {
group.enter()
r.rq_resolveReferences(callback: { (_ ) in
group.leave()
})
}
group.notify(queue: .global(qos: .userInteractive), execute: {
callback(resources, nil)
})
}
else {
callback(nil, error)
}
}
}
var asFHIR : DomainResource? {
return self
}
func asRelativeReference() throws -> Reference {
return try self.asRelativeReference()
}
}