-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
executable file
·533 lines (459 loc) · 20.3 KB
/
index.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
var DEFAULT_PORT = process.env.PORT || 8000;
var DEFAULT_HOST = '127.0.0.1'
var SERVER_NAME = 'healthrecords'
var getRequestCounter = 0;
var postRequestCounter = 0;
var putRequestCounter = 0;
var deleteRequestCounter = 0;
var patientArray = [];
var http = require ('http');
var mongoose = require ("mongoose");
//var port = process.env.PORT;
var ipaddress = process.env.IP; // TODO: figure out which IP to use for the heroku
// Here we find an appropriate database to connect to, defaulting to
// localhost if we don't find one.
var uristring =
process.env.MONGODB_URI ||
'mongodb://tekstil:[email protected]:51753/mapd713groupproject';
//'mongodb://localhost/e-health-db';
// Makes connection asynchronously. Mongoose will queue up database
// operations and release them when the connection is complete.
mongoose.connect(uristring, function (err, res) {
if (err) {
console.log ('ERROR connecting to: ' + uristring + '. ' + err);
} else {
console.log ('Successfully connected to: ' + uristring);
}
});
// This is the schema. Note the types, validation and trim
// statements. They enforce useful constraints on the data.
var patientSchema = new mongoose.Schema({
first_name: String,
last_name: String,
blood_group: String,
address: String,
date_of_birth: String,
date_admitted: String,
department: String,
doctor: String,
ailment:String,
});
var patientRecordsSchema = new mongoose.Schema({
patient_id: String,
pulse: String,
nurse_name: String,
allergy: String,
BMI: String,
surgery: String
});
var userSchema = new mongoose.Schema({
first_name: String,
last_name: String,
mail_address: String,
phone: String,
department: String,
position: String,
username: String,
password: String
});
// Compiles the schema into a model, opening (or creating, if
// nonexistent) the 'Patients' collection in the MongoDB database
var Patient = mongoose.model('Patient', patientSchema);
var PatientRecord = mongoose.model('PatientRecord', patientRecordsSchema);
var User = mongoose.model('User', userSchema);
var restify = require('restify')
// Create the restify server
, server = restify.createServer({ name: SERVER_NAME})
if (typeof ipaddress === "undefined") {
// Log errors on OpenShift but continue w/ 127.0.0.1 - this
// allows us to run/test the app locally.
console.warn('No process.env.IP var, using default: ' + DEFAULT_HOST);
ipaddress = DEFAULT_HOST;
};
//delete the ipaddress parameter while deploting to heroku. Otherwise, use the ipaddress parameter in local.
server.listen(DEFAULT_PORT,function () {
console.log('Server %s listening at %s', server.name, server.url)
console.log('Resources:')
console.log(' /patients')
console.log(' /patients/:id')
})
server
// Allow the use of POST
.use(restify.plugins.fullResponse())
// Maps req.body to req.params so there is no switching between them
.use(restify.plugins.bodyParser())
// Get a single patient by its patient id
server.get('/login/:username', function (req, res, next) {
getRequestCounter++;
console.log('received GET request.');
console.log("Processed Request Counter --> GET: " + getRequestCounter + ", POST: " + postRequestCounter + ", PUT: " + putRequestCounter +", DELETE: " +deleteRequestCounter);
// Find a single patient by their id within save
User.findOne({ username: req.params.username}, function (error, user) {
// If there are any errors, pass them to next in the correct format
if (error) return next(new restify.InvalidArgumentError(JSON.stringify(error.errors)))
if (user) {
// Send the patient if no issues
res.send(user)
console.log('Sending response to GET request.');
console.log('OK');
} else {
// Send 404 header if the patient doesn't exist
res.send(404)
console.log("Error occurred in sending Response.");
}
})
})
// Create a new user
server.post('/register', function (req, res, next) {
postRequestCounter++;
console.log('received POST request.');
console.log("new user requestabc");
console.log("Processed Request Counter --> GET: " + getRequestCounter + ", POST: " + postRequestCounter + ", PUT: " + putRequestCounter +", DELETE: " +deleteRequestCounter);
// Make sure first_name is defined
if (req.params.first_name === undefined ) {
// If there are any errors, pass them to next in the correct format
return next(new restify.InvalidArgumentError('first_name must be supplied'))
}
// Make sure last_name is defined
if (req.params.last_name === undefined ) {
// If there are any errors, pass them to next in the correct format
return next(new restify.InvalidArgumentError('last_name must be supplied'))
}
// Make sure mail_address is defined
if (req.params.mail_address === undefined ) {
// If there are any errors, pass them to next in the correct format
return next(new restify.InvalidArgumentError('mail_address must be supplied'))
}
// Make sure phone is defined
if (req.params.phone === undefined ) {
// If there are any errors, pass them to next in the correct format
return next(new restify.InvalidArgumentError('phone must be supplied'))
}
// Make sure department is defined
if (req.params.department === undefined ) {
// If there are any errors, pass them to next in the correct format
return next(new restify.InvalidArgumentError('department must be supplied'))
}
// Make sure position is defined
if (req.params.position === undefined ) {
// If there are any errors, pass them to next in the correct format
return next(new restify.InvalidArgumentError('department must be supplied'))
}
// Make sure username is defined
if (req.params.username === undefined ) {
// If there are any errors, pass them to next in the correct format
return next(new restify.InvalidArgumentError('username must be supplied'))
}
if (req.params.password === undefined ) {
// If there are any errors, pass them to next in the correct format
return next(new restify.InvalidArgumentError('password must be supplied'))
}
var newuser = {
first_name: req.params.first_name,
last_name: req.params.last_name,
mail_address: req.params.mail_address,
phone: req.params.phone,
department: req.params.department,
position: req.params.position,
username: req.params.username,
password: req.params.password
}
// Create the user using the persistence engine
User.create( newuser, function (error, user) {
// If there are any errors, pass them to next in the correct format
if (error) {
console.log('Error on creating user.');
return next(new restify.InvalidArgumentError(JSON.stringify(error.errors)));
}
// Send the patient if no issues
res.send(201, user)
})
console.log('Sending response to POST request.');
})
// Get all patients in the system
server.get('/patients', function (req, res, next) {
getRequestCounter++;
console.log('received GET request.');
console.log("Processed Request Counter --> GET: " + getRequestCounter + ", POST: " + postRequestCounter + ", PUT: " + putRequestCounter +", DELETE: " +deleteRequestCounter);
// Find every entity within the given collection
Patient.find({}, function (error, patients) {
// Return all of the patients in the system
res.send(patients)
console.log('Sending response to GET request.');
})
})
// Get a single patient by its patient id
server.get('/patients/:id', function (req, res, next) {
getRequestCounter++;
console.log('received GET request.');
console.log("Processed Request Counter --> GET: " + getRequestCounter + ", POST: " + postRequestCounter + ", PUT: " + putRequestCounter +", DELETE: " +deleteRequestCounter);
// Find a single patient by their id within save
Patient.findOne({ _id: req.params.id }, function (error, patient) {
// If there are any errors, pass them to next in the correct format
if (error) return next(new restify.InvalidArgumentError(JSON.stringify(error.errors)))
if (patient) {
// Send the patient if no issues
res.send(patient)
console.log('Sending response to GET request.');
console.log('OK');
} else {
// Send 404 header if the patient doesn't exist
res.send(404)
console.log("Error occurred in sending Response.");
}
})
})
// Create a new patient
server.post('/patients', function (req, res, next) {
postRequestCounter++;
console.log('received POST request.');
console.log("Processed Request Counter --> GET: " + getRequestCounter + ", POST: " + postRequestCounter + ", PUT: " + putRequestCounter +", DELETE: " +deleteRequestCounter);
// Make sure first_name is defined
if (req.params.first_name === undefined ) {
// If there are any errors, pass them to next in the correct format
return next(new restify.InvalidArgumentError('first_name must be supplied'))
}
if (req.params.last_name === undefined ) {
// If there are any errors, pass them to next in the correct format
return next(new restify.InvalidArgumentError('last_name must be supplied'))
}
if (req.params.blood_group === undefined ) {
// If there are any errors, pass them to next in the correct format
return next(new restify.InvalidArgumentError('blood_group must be supplied'))
}
if (req.params.address === undefined ) {
// If there are any errors, pass them to next in the correct format
return next(new restify.InvalidArgumentError('address must be supplied'))
}
if (req.params.date_of_birth === undefined ) {
// If there are any errors, pass them to next in the correct format
return next(new restify.InvalidArgumentError('date_of_birth must be supplied'))
}
if (req.params.date_admitted === undefined ) {
// If there are any errors, pass them to next in the correct format
return next(new restify.InvalidArgumentError('date_admitted must be supplied'))
}
if (req.params.department === undefined ) {
// If there are any errors, pass them to next in the correct format
return next(new restify.InvalidArgumentError('department must be supplied'))
}
if (req.params.doctor === undefined ) {
// If there are any errors, pass them to next in the correct format
return next(new restify.InvalidArgumentError('doctor must be supplied'))
}
if (req.params.ailment === undefined ) {
// If there are any errors, pass them to next in the correct format
return next(new restify.InvalidArgumentError('ailment must be supplied'))
}
var newpatient = {
first_name: req.params.first_name,
last_name: req.params.last_name,
blood_group: req.params.blood_group,
address: req.params.address,
date_of_birth: req.params.date_of_birth,
date_admitted: req.params.date_admitted,
department: req.params.department,
doctor: req.params.doctor,
ailment:req.params.ailment
}
// Create the patient using the persistence engine
Patient.create( newpatient, function (error, patient) {
// If there are any errors, pass them to next in the correct format
if (error) {
console.log('Error on creating patient.');
return next(new restify.InvalidArgumentError(JSON.stringify(error.errors)));
}
// Send the patient if no issues
res.send(201, patient)
patientArray.push(patient);
console.log('patient Array: ' + patientArray);
})
console.log('Sending response to POST request.');
})
// Update a patient by their id
server.put('/patients/:id', function (req, res, next) {
putRequestCounter++;
console.log('received PUT request.');
console.log("Processed Request Counter --> GET: " + getRequestCounter + ", POST: " + postRequestCounter + ", PUT: " + putRequestCounter +", DELETE: " +deleteRequestCounter);
// Make sure first_name is defined
if (req.params.first_name === undefined ) {
// If there are any errors, pass them to next in the correct format
return next(new restify.InvalidArgumentError('first_name must be supplied'))
}
if (req.params.last_name === undefined ) {
// If there are any errors, pass them to next in the correct format
return next(new restify.InvalidArgumentError('last_name must be supplied'))
}
if (req.params.blood_group === undefined ) {
// If there are any errors, pass them to next in the correct format
return next(new restify.InvalidArgumentError('blood_group must be supplied'))
}
if (req.params.address === undefined ) {
// If there are any errors, pass them to next in the correct format
return next(new restify.InvalidArgumentError('address must be supplied'))
}
if (req.params.date_of_birth === undefined ) {
// If there are any errors, pass them to next in the correct format
return next(new restify.InvalidArgumentError('date_of_birth must be supplied'))
}
if (req.params.date_admitted === undefined ) {
// If there are any errors, pass them to next in the correct format
return next(new restify.InvalidArgumentError('date_admitted must be supplied'))
}
if (req.params.department === undefined ) {
// If there are any errors, pass them to next in the correct format
return next(new restify.InvalidArgumentError('department must be supplied'))
}
if (req.params.doctor === undefined ) {
// If there are any errors, pass them to next in the correct format
return next(new restify.InvalidArgumentError('doctor must be supplied'))
}
if (req.params.ailment === undefined ) {
// If there are any errors, pass them to next in the correct format
return next(new restify.InvalidArgumentError('ailment must be supplied'))
}
var newpatient = {
first_name: req.params.first_name,
last_name: req.params.last_name,
blood_group: req.params.blood_group,
address: req.params.address,
date_of_birth: req.params.date_of_birth,
date_admitted: req.params.date_admitted,
department: req.params.department,
doctor: req.params.doctor,
ailment:req.params.ailment
}
// Update the patient with the persistence engine
Patient.update(newpatient, function (error, patient) {
// If there are any errors, pass them to next in the correct format
if (error) return next(new restify.InvalidArgumentError(JSON.stringify(error.errors)))
console.log('Sending response to PUT request.');
// Send a 200 OK response
res.send(200)
})
})
// Delete patient with the given id
server.del('/patients/:id', function (req, res, next) {
deleteRequestCounter++;
console.log('received DELETE request.');
console.log("Processed Request Counter --> GET: " + getRequestCounter + ", POST: " + postRequestCounter + ", PUT: " + putRequestCounter +", DELETE: " +deleteRequestCounter);
// Delete the patient with the persistence engine
Patient.delete(req.params.id, function (error, patient) {
// If there are any errors, pass them to next in the correct format
if (error) return next(new restify.InvalidArgumentError(JSON.stringify(error.errors)))
// Send a 200 OK response
res.send()
console.log('Sending response to DELETE request.');
})
})
// Delete all patients in the system
server.del('/patients', function (req, res) {
deleteRequestCounter++;
console.log('received DELETE request.');
console.log("Processed Request Counter --> GET: " + getRequestCounter + ", POST: " + postRequestCounter + ", PUT: " + putRequestCounter +", DELETE: " +deleteRequestCounter);
// Find every entity within the given collection
Patient.deleteMany({}, function (error) {
// Return all of the patients in the system
res.send()
console.log('Sending response to DELETE request.');
})
})
// Create a new patient record by patients it
server.post('/patients/:id/records', function (req, res, next) {
console.log('RECORDS POSTTTT LOLOLOO request.');
postRequestCounter++;
console.log('received POST request.');
console.log("Processed Request Counter --> GET: " + getRequestCounter + ", POST: " + postRequestCounter + ", PUT: " + putRequestCounter +", DELETE: " +deleteRequestCounter);
if (req.params.patient_id === undefined ) {
// If there are any errors, pass them to next in the correct format
return next(new restify.InvalidArgumentError('patient_id must be supplied'))
}
if (req.params.pulse === undefined ) {
// If there are any errors, pass them to next in the correct format
return next(new restify.InvalidArgumentError('pulse must be supplied'))
}
if (req.params.nurse_name === undefined ) {
// If there are any errors, pass them to next in the correct format
return next(new restify.InvalidArgumentError('nurse_name must be supplied'))
}
if (req.params.allergy === undefined ) {
// If there are any errors, pass them to next in the correct format
return next(new restify.InvalidArgumentError('allergy must be supplied'))
}
if (req.params.bmi === undefined ) {
// If there are any errors, pass them to next in the correct format
return next(new restify.InvalidArgumentError('bmi must be supplied'))
}
if (req.params.surgery === undefined ) {
// If there are any errors, pass them to next in the correct format
return next(new restify.InvalidArgumentError('surgery must be supplied'))
}
var newpatientrecord = {
patient_id: req.body.patient_id,
pulse: req.body.pulse,
nurse_name: req.body.nurse_name,
allergy: req.body.allergy,
BMI: req.body.bmi,
surgery: req.body.surgery
}
// Create the patient using the persistence engine
PatientRecord.create( newpatientrecord, function (error, record) {
// If there are any errors, pass them to next in the correct format
if (error) {
console.log('Error on creating patient record.');
return next(new restify.InvalidArgumentError(JSON.stringify(error.errors)));
}
// Send the patient if no issues
res.send(201, record)
})
console.log('Sending response to POST request.');
})
// Get patient records by its patient id
server.get('/patients/:id/records', function (req, res, next) {
console.log('************GET RECORDS request. PATIENT ID =' + req.params.id);
getRequestCounter++;
console.log('received GET request.');
console.log("Processed Request Counter --> GET: " + getRequestCounter + ", POST: " + postRequestCounter + ", PUT: " + putRequestCounter +", DELETE: " +deleteRequestCounter);
// Find a single patient by their id within save
Patient.findOne({ _id: req.params.id }, function (error, patient) {
// If there are any errors, pass them to next in the correct format
if (error){
console.log(error)
return next(new restify.InvalidArgumentError(JSON.stringify(error.errors)))
}
if (!patient) {
console.log(`->No patient with ID [${req.params.id}] found`);
res.send(404)
}else{
console.log(`hahahah`);
PatientRecord.find({patient_id: req.params.id}, function (error, records) {
// If there are any errors, pass them to next in the correct format
if (error){
console.log(error)
return next(new restify.InvalidArgumentError(JSON.stringify(error.errors)))
}
if (!records) {
console.log(`->No patient record with Patient ID [${req.params.id}] found`);
res.send(404)
}
else{
res.send(records)
console.log('Sending response to GET request.');
console.log('OK');
}
})
}
})
})
// Delete all patients in the system
server.del('/patients/:id/records', function (req, res) {
deleteRequestCounter++;
console.log('received DELETE request for PATIENT RECORDS.');
console.log("Processed Request Counter --> GET: " + getRequestCounter + ", POST: " + postRequestCounter + ", PUT: " + putRequestCounter +", DELETE: " +deleteRequestCounter);
// Find every entity within the given collection
PatientRecord.deleteMany({patient_id: req.params.patient_id}, function (error) {
// Return all of the patients in the system
res.send()
console.log('Sending response to DELETE request.');
})
})