-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathulogic.js
270 lines (258 loc) · 8.81 KB
/
ulogic.js
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**
* Transaction for recording appointment between doctor and patient.
* @param {org.ehr.basic.confirmAppoint} atx The sample transaction instance.
* @transaction
*/
/** async function confirmAppoint(atx) {
// if ( atx.appoint.patient.id != atx.patient.id ){
// throw new Error("Visitor is not same as the patient in the appoint.!")
// }
if (atx.appoint.status === 'CONFIRMED'){
throw new Error('Appointment already confirmed!')
}
if (atx.appoint.status === 'REJECTED'){
throw new Error('Appointment already rejected!')
}
if ( atx.visitor.id != atx.visitor.prescriptions.visitor.id){
throw new Error("Visitor is not same as the patient in the prescr.!")
}
if ( atx.doctor.speciality != atx.appoint.group){
throw new Error('Doctor is not specialized into the required disease category ')
}
//Assuming all is ok
getAssetRegistry('org.ehr.basic.Appointment').then(function(data){
atx.appoint.status = 'CONFIRMED' //match with the enum val ??????
atx.appoint.assigned = atx.doctor
return await data.update(atx.appoint)
//////// also emit some event!!!!!!!!!!!!!!!
}).catch(function(e){
throw new Error(e)
})
}
*/
/**
* Transaction for recording appointment between doctor and patient.
* @param {org.ehr.basic.confirmAppoint} atx The sample transaction instance.
* @transaction
*/
/** async function confirmAppoint(atx) {
// if ( atx.appoint.patient.id != atx.patient.id ){
// throw new Error("Visitor is not same as the patient in the appoint.!")
// }
if (atx.appoint.status === 'CONFIRMED'){
throw new Error('Appointment already confirmed!')
}
if (atx.appoint.status === 'CONSULTED'){
throw new Error('Appointment already consulted!')
}
if (atx.appoint.status === 'REJECTED'){
throw new Error('Appointment already rejected!')
}
if ( atx.visitor.id != atx.visitor.prescriptions.visitor.id){
throw new Error("Visitor is not same as the patient in the prescr.!")
}
if ( atx.doctor.speciality != atx.appoint.group){
throw new Error('Doctor is not specialized into the required disease category ')
}
//Assuming all is ok
getAssetRegistry('org.ehr.basic.Appointment').then(function(data){
atx.appoint.status = 'CONFIRMED' //match with the enum val ??????
atx.appoint.assigned = atx.doctor
return await data.update(atx.appoint)
//////// also emit some event!!!!!!!!!!!!!!!
}).catch(function(e){
throw new Error(e)
})
}
*/
/**
* Transaction for recording appointment between doctor and patient.
* @param {org.ehr.basic.consult} atx The sample transaction instance.
* @transaction
*/
/** async function consult(atx){
if(atx.presc.isMedPrescribed && atx.presc.isTestPrescribed ){
throw new Error('Prescription can not be both test and medicine')
}
if(!(atx.presc.isMedPrescribed || atx.presc.isTestPrescribed )){
throw new Error('Prescription should prescibe either test or medicine')
}
if (atx.appoint.status === 'PENDING'){
throw new Error('Appointment yet to be confirmed!')
}
if (atx.appoint.appointmentId != atx.presc.appoint.appointmentId){
throw new Error('Prescription not matching with the appointment!')
}
if (atx.appoint.status === 'CONSULTED'){
throw new Error('Appointment already consulted!')
}
if (atx.appoint.status === 'REJECTED'){
throw new Error('Appointment already rejected!')
}
if(atx.patient.id != atx.appoint.patient.id){
throw new Error('Patient Proxy not allowed!')
}
if ( atx.presc.appoint.doctor.id != atx.doctor.id){
throw new Error('You are not the designated doctor')
}
// some checks are missing
getAssetRegistry('org.ehr.basic.Appointment').then(function(assets){
atx.appoint.status = 'CONSULTED'
return await assets.update(atx.appoint)
}).catch(function(error){
throw new Error(error)
}).then(function(data){
getAssetRegistry('org.ehr.basic.Prescription').then(function(assets){
atx.presc.appoint = atx.appoint
return await assets.update(atx.presc)
// deducted the consultanceFee here
}).catch(function(error){
throw new Error(error)
})
}).catch(function(error){
throw new Error(error)
})
}
*/
/**
* Transaction for recording appointment between doctor and patient.
* @param {org.ehr.basic.confirmAppoint} atx The sample transaction instance.
* @transaction
*/
async function confirmAppoint(atx) {
// if ( atx.appoint.patient.id != atx.patient.id ){
// throw new Error("Visitor is not same as the patient in the appoint.!")
// }
if (atx.appoint.status === 'CONFIRMED'){
throw new Error('Appointment already confirmed!')
}
if (atx.appoint.status === 'CONSULTED'){
throw new Error('Appointment already consulted!')
}
if (atx.appoint.status === 'REJECTED'){
throw new Error('Appointment already rejected!')
}
if ( atx.visitor.id != atx.visitor.prescriptions.visitor.id){
throw new Error("Visitor is not same as the patient in the prescr.!")
}
if ( atx.doctor.speciality != atx.appoint.group){
throw new Error('Doctor is not specialized into the required disease category ')
}
//Assuming all is ok
getAssetRegistry('org.ehr.basic.Appointment').then(function(data){
atx.appoint.status = 'CONFIRMED' //match with the enum val ??????
atx.appoint.assigned = atx.doctor
return await data.update(atx.appoint)
//////// also emit some event!!!!!!!!!!!!!!!
}).catch(function(e){
throw new Error(e)
})
}
/**
* Transaction for recording appointment between doctor and patient.
* @param {org.ehr.basic.consult} atx The sample transaction instance.
* @transaction
*/
async function consult(atx){
if(atx.presc.isMedPrescribed && atx.presc.isTestPrescribed ){
throw new Error('Prescription can not be both test and medicine')
}
if(!(atx.presc.isMedPrescribed || atx.presc.isTestPrescribed )){
throw new Error('Prescription should prescibe either test or medicine')
}
if (atx.appoint.status === 'PENDING'){
throw new Error('Appointment yet to be confirmed!')
}
if (atx.appoint.appointmentId != atx.presc.appoint.appointmentId){
throw new Error('Prescription not matching with the appointment!')
}
if (atx.appoint.status === 'CONSULTED'){
throw new Error('Appointment already consulted!')
}
if (atx.appoint.status === 'REJECTED'){
throw new Error('Appointment already rejected!')
}
if(atx.patient.id != atx.appoint.patient.id){
throw new Error('Patient Proxy not allowed!')
}
if ( atx.presc.appoint.doctor.id != atx.doctor.id){
throw new Error('You are not the designated doctor')
}
// some checks are missing
getAssetRegistry('org.ehr.basic.Appointment').then(function(assets){
atx.appoint.status = 'CONSULTED'
return await assets.update(atx.appoint)
}).catch(function(error){
throw new Error(error)
}).then(function(data){
getAssetRegistry('org.ehr.basic.Prescription').then(function(assets){
atx.presc.appoint = atx.appoint
return await assets.update(atx.presc)
// deducted the consultanceFee here
}).catch(function(error){
throw new Error(error)
})
}).catch(function(error){
throw new Error(error)
})
}
/**
* Transaction for buying test from pathalogy lab.
* @param {org.ehr.basic.byTest} atx The sample transaction instance.
* @transaction
*/
async function buyTest(atx) {
if(atx.presc.isMedPrescribed && atx.presc.isTestPrescribed ){
throw new Error('Prescription can not be both test and medicine')
}
if(!(atx.presc.isMedPrescribed || atx.presc.isTestPrescribed )){
throw new Error('Prescription should prescibe either test or medicine')
}
if(atx.presc.isMedPrescribed){
throw new Error('Prescription should prescibe test only')
}
if(atx.patient.id != atx.presc.patient.id){
throw new Error('This prescription is intended to someone else')
}
if(atx.patient.id != atx.presc.appoint.insuranceId.patient.id){
throw new Error('Insurance Id is not matching with the patient')
}
let cost = 0.0
for(let i in atx.presc.test){
let index = atx.pathlab.test.indexOf(i)
if(index < 0){
throw new Error('Not Allowed')
}
cost += cost[index]
}
if(atx.presc.appoint.isInsured )
{
if(cost <= atx.presc.appoint.insuranceId.ensuredAmount){
atx.presc.appoint.insuranceId.ensuredAmount -= cost
getAssetRegistry('org.ehr.basic.Insurance').then(
(data) => {
return await data.update(atx.presc.appoint.insuranceId)
}
).catch(
(err) => {
throw new Error(err)
}
)
}
else {
throw new Error("Insurance doesn't exist")
}
}
else{
atx.patient.debt -= cost
getParticipantRegistry('org.ehr.basic.Patient').then(
(data) => {
return await data.update(atx.patient)
}
).catch(
(err) => {
throw new Error(err)
}
)
}
}