-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
340 lines (283 loc) · 7.22 KB
/
app.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
'use strict';
let fs = require('fs');
let express = require('express');
let app = express();
let bodyParser = require('body-parser');
var cfenv = require('cfenv');
var mongoose = require('mongoose');
var models = require('./models/models.js');
var Patient = models.patientModel;
var Doctor = models.doctorModel;
var Pharmacist = models.pharmacistModel;
mongoose.connect('mongodb://sssaini1:sssaini1@ds231725.mlab.com:31725/medical-chain');
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
app.use(express.static(__dirname + '/public'));
var patient = "sukhpal";
var doctor = "1";
var pharmacist = "pharmacist";
var drug1 = '', drug2 = '';
var duplicateArray = [];
app.use(function(req, res, next) {
// res.header('Access-Control-Allow-Origin', "x-requested-with");
// res.header("Access-Control-Allow-Headers", "*");
next();
})
app.post('/process', function(req, res) {
console.log('****** PROCESS *******');
console.log(drug1 + ' / ' + drug2);
res.render('process.ejs', {
duplicateArray,
});
});
// app.post('/pharma', function(req, res) {
// // console.log("the patient I am looking for in the db: " + req.body.name);
// var date = new Date().toDateString().substring(4);
// patient = 'sssaini';
// Patient.find({
// "name": patient
// }, function(err, doc) {
// console.log(doc);
// res.render('pharma-test.ejs', {
// doc,
// date
// });
// });
// });
// app.post('/doctor-info-old', function(req, res) {
// res.render('doctor-info-old.ejs');
// })
app.engine('html', require('ejs').renderFile);
// app.set('view engine', 'html');
app.get('/', function(req, res) {
//Setting the original docs
/*
var p1 = new Patient({ name: 'sssaini', age: 10, gender: "M", diseases: [], drugs: [] });
p1.save(function(err) {
if (err) {
console.log(err);
}
var d1 = new Doctor({
name: '1',
age: 20,
gender: "F",
});
d1.patients.push(p1._id);
d1.save(function(err) {
if (err) {
console.log(err);
}
var pharma1 = new Pharmacist({ name: "pharmacist", customers: [] });
pharma1.customers.push({ patient: p1._id, doctor: d1._id });
pharma1.save(function(err) {
if (err) {
console.log(err);
}
});
});
});
*/
res.render('patient-main.ejs');
});
app.get('/patient-main', function(req, res) {
res.render('patient-main.ejs')
})
app.post('/patient-info', function(req, res) {
console.log("the patient I am looking for in the db: " + req.body.name);
patient = req.body.name;
Patient.find({
"name": req.body.name
}, function(err, doc) {
console.log(doc);
// console.log('comments !!!!!:', doc[0].comments);
// console.log('drugs !!!!!!!:', doc[0].drugs);
res.render('patient-info.ejs', {
doc,
});
});
});
app.post('/doctor-main', function(req, res) {
console.log("*************doctor main *******");
console.log(req.body);
doctor = "1";
if (req.body.disease1 === "Hepatitis") { doctor = "1"; } else { doctor = "2"; }
var options = {
safe: true,
upsert: true
};
var update = {
$push: {
diseases: req.body.disease1,
comments: "Appointment booked",
}
};
Patient.findOneAndUpdate({
"name": req.body.name
}, update, options, function(err, doc) {
if (err) {
console.log('got an error ' + err);
} else {
console.log('saved');
}
});
var query = Patient.findOne({
"name": req.body.name
}, {}, function(err, doc) {
if (err) {
console.log('got an error ' + err);
}
});
query.then(function(doc2) {
console.log("patient found to add to doctor: " + doc2);
var update2 = {
$push: {
patients: doc2._id
}
};
var query2 = Doctor.findOneAndUpdate({
"name": doctor
}, update2, options, function(err, doc) {
if (err) {
console.log('got an error ' + err);
} else {
console.log('saved');
console.log("____________")
console.log(doc)
res.render('doctor-main.ejs', {
doc,
patient: doc2
});
}
});
});
});
app.post('/doctor-info', function(req, res) {
console.log("the patient I am looking for in the db: " + patient);
var query = Patient.findOne({
"name": patient
}, {}, function(err, doc) {
if (err) {
console.log('got an error ' + err);
}
})
query.then(function(doc) {
console.log("doc for doctor-info" + doc);
var query2 = Doctor.findOne({
"name": doctor
}, {}, function(err, doc) {
if (err) {
console.log('got an error ' + err);
}
})
query2.then(function(doc2) {
res.render('doctor-info.ejs', {
doc: doc2,
patient: doc
});
});
});
});
app.post('/pharmacist-main', function(req, res) {
var date = new Date().toDateString().substring(4);
duplicateArray = [];
drug1 = req.body.drug1;
drug2 = req.body.drug2;
console.log("***** Body: %j", req.body);
var comments = req.body.comments;
console.log("the patient I am looking for in the db: " + patient);
console.log("doctor is " + doctor);
var query3 = Doctor.findOne({
"name": doctor
}, "name age patients", function(err, doc) {
if (err) {
console.log('got an error ' + err);
}
})
query3.then(function(doc) {
var options = {
safe: true,
upsert: true
};
console.log(doc);
var update2 = {
$push: {
drugs: { $each: [drug1, drug2] },
comments: { $each: ["Prescription added: " + drug1 + " " + drug2, comments] },
}
};
Patient.findOneAndUpdate({
"name": patient
}, update2, options, function(err, doc) {
if (err) {
console.log('got an error ' + err);
} else {
console.log('Patient found and saved');
}
});
var options = {
safe: true,
new: true
};
var update2 = {
$push: {
customers: { patient: doc.patients[0], doctor: doc._id }
}
};
Pharmacist.findOneAndUpdate({
"name": pharmacist
}, update2, options, function(err, doc) {
if (err) {
console.log('got an error ' + err);
} else {
console.log('Pharmacist found and saved');
console.log("namw: " + doc.customers[0].patient);
var query4 = Patient.findById(
doc.customers[0].patient,
function(err, doc3) {
if (err) {
console.log('got an error ' + err);
}
patient = doc3;
})
.then(function(doc3) {
console.log('****PATIENT*****');
for (var i = 0; i < patient.drugs.length; i++) {
for (var j = i + 1; j < patient.drugs.length; j++) {
if (patient.drugs[i] === patient.drugs[j]) {
console.log('i:' , i , '/ patient drugs:' , patient.drugs[i]);
console.log('j:' , j , '/ patient drugs:' , patient.drugs[j]);
duplicateArray.push(patient.drugs[i]);
console.log(duplicateArray);
patient.drugs.splice(j, 1);
j = patient.drugs.length;
}
}
}
doc3.drugs = patient.drugs;
doc3.save(function(err, doc) {
if (err) return handleError(err);
});
console.log('DUP ARRAY ****');
console.log(duplicateArray);
res.render('pharmacist-main.ejs', {
drug1,
drug2,
doc: doc,
duplicateArray,
patient: doc3,
date,
});
});
};
});
});
});
var appEnv = cfenv.getAppEnv();
// console.log(appEnv);
// start server on the specified port and binding host
app.listen(appEnv.port, '0.0.0.0', function() {
// print a message when the server starts listening
console.log("server starting on " + appEnv.url);
});