forked from caldenrodrigues/HealthCare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
361 lines (339 loc) · 11.5 KB
/
server.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
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var server = require('http').Server(app);
//var generator = require('generate-password');
//var random = require('random-number');
var {Random} = require('random-js');
var random = new Random();
const axios = require('axios');
var mysql = require('mysql');
var cors = require('cors');
app.use(cors());
//Socket
const io = require("socket.io")(server)
//mysql
const connection = mysql.createConnection({
host: 'localhost',
user: process.env.database_user,
password: process.env.database_password,
// user: 'root',
// password: 'shadrak',
database: 'Healthcare'
});
connection.connect(function(err) {
if (!err) {
console.log('Connected to MySql!\n');
} else {
console.log(err);
}
});
io.on('connection', (socket) => {
console.log('New User')
flag = false
patient_id = '101'
//io.sockets.emit("changeAppointment")
console.log(patient_id)
socket.on('newMessage', (data) => {
console.log(data)
myvar = data
axios.post('http://localhost:5000/testQuery', {
myvar
})
.then((res) => {
strResponse = res.data;
if(strResponse.substring(0,3) != "I a"){
strResponse = strResponse.substring(2);
if(strResponse == "doctor"){
connection.query("SELECT * FROM Prescription where p_id = ?",[patient_id], function (err, result, fields) {
if (err) throw err;
doctor_id = result[0].d_id;
connection.query("Select * from Doctor where d_id = ?",[doctor_id],function(err, result, fields) {
if (err) throw err;
doctor_name = result[0].name;
doctor_contact = result[0].contact;
doctor_spec = result[0].specialization;
var json_response = {}
json_response["type"] = "doctor"
json_response["name"] = doctor_name
json_response["contact"] = doctor_contact
json_response["spec"] = doctor_spec
console.log(json_response)
io.sockets.emit('newDoctor',json_response)
})
});
}
else if(strResponse == "hospital"){
io.sockets.emit('newRegular',strResponse)
}
else if(strResponse == "appointment"){
connection.query("SELECT * FROM Prescription where p_id = ?",[patient_id], function (err, result, fields) {
if (err) throw err;
appointment_date = result[0].app_date;
doctor_id = result[0].d_id;
connection.query("Select * from Doctor where d_id = ?",[doctor_id],function(err, result, fields) {
doctor_name = result[0].name;
var json_response = {}
json_response["type"] = "appointment";
json_response["name"] = doctor_name;
json_response["date"] = appointment_date;
console.log(json_response)
io.sockets.emit('newAppointment',json_response)
});
});
}
else if(strResponse == "dose"){
connection.query("SELECT * FROM Prescription where p_id = ?",[patient_id], function (err, result, fields) {
if (err) throw err;
var json_response = {}
json_response["drug"] = result[0].drugs;
json_response["unit"] = result[0].unit;
var date = new Date();
var current_hour = date.getHours();
if(current_hour<9){
json_response["time"] = "9:00am";
}
else if(current_hour<13){
json_response["time"] = "1:00pm";
}
else if(current_hour<21){
json_response["time"] = "9:00pm";
}
else{
json_response["time"] = "9:00am";
}
console.log(json_response)
io.sockets.emit('newDose',json_response)
});
}
else if(strResponse == "reschedule"){
console.log("In")
io.sockets.emit("changeAppointment")
}
else if(strResponse == "reshedule"){
console.log("In")
io.sockets.emit("changeAppointment")
}
else{
io.sockets.emit('newRegular',strResponse)
}
}
else{
const queryQuestion = myvar
axios.post('http://localhost:5000/nlpQuery', {
queryQuestion
})
.then((res) => {
console.log(res.data)
strResponse = res.data
io.sockets.emit('newRegular',strResponse)
if(strResponse.substring(0,3) == "I a"){
connection.query("Insert into pendings values(?,?,?)",[patient_id, myvar, ""], function (err, result, fields) {
if (err) throw err;
});
}
})
.catch((err) => {
console.log(err)
})
}
})
.catch((err) => {
console.log(err)
})
})
socket.on("changedAppointment", (data) => {
p_id = "101"
app_date = data
connection.query('update Prescription set app_date = ? where p_id = ?',[app_date, p_id], function(err, res, fields){
if (err) throw err;
connection.query("SELECT * FROM Prescription where p_id = ?",[patient_id], function (err, result, fields) {
if (err) throw err;
appointment_date = result[0].app_date;
doctor_id = result[0].d_id;
connection.query("Select * from Doctor where d_id = ?",[doctor_id],function(err, result, fields) {
doctor_name = result[0].name;
var json_response = {}
json_response["type"] = "appointment";
json_response["name"] = doctor_name;
json_response["date"] = appointment_date;
console.log(json_response)
io.sockets.emit('newRegular',"Your appointment has been rescheduled as follows")
io.sockets.emit('newAppointment',json_response)
});
});
});
});
socket.on("forgotMeds", (data) => {
console.log(data)
io.sockets.emit('newRegular',"Missing your medication too frequently is not advicable")
})
})
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.get('/testSocket', function(req, res) {
connection.query("SELECT * FROM Prescription where p_id = 101", function (err, result, fields) {
if (err) throw err;
var json_response = {}
json_response["drug"] = result[0].drugs;
json_response["unit"] = result[0].unit;
var date = new Date();
var current_hour = date.getHours();
if(current_hour<9){
json_response["time"] = "9:00am";
}
else if(current_hour<13){
json_response["time"] = "1:00pm";
}
else if(current_hour<21){
json_response["time"] = "9:00pm";
}
else{
json_response["time"] = "9:00am";
}
console.log(json_response)
string_response = "Hey, Don't forget to take your " + json_response["unit"] + " of " + json_response["drug"] + " at " + json_response["time"];
io.sockets.emit('newAlert',string_response)
io.sockets.emit('newDose',json_response)
});
return res.send("Request Sent")
})
// app.get('/', (req, res) => {
// res.sendFile(__dirname + '/index.html');
// });
app.get("/", (req,res)=>{
return res.send("Welcome to BotCare");
});
app.get("/testQuery", (req,result)=>{
myvar = "What is the name of the doctor"
axios.post('http://localhost:5000/testQuery', {
myvar
})
.then((res) => {
strResponse = res.data;
strResponse = strResponse.substring(1);
if(strResponse == "doctor"){
//shadrak get data here
}
else if(strResponse == "hospital"){
//shadrak get data here
}
else if(strResponse == "appointment"){
//shadrak get data here
}
else if(strResponse == "dose"){
//shadrak get data here
}
return result.send(strResponse);
})
.catch((err) => {
console.log(err)
})
});
app.post('/addQuery', (req, result) => {
queryQuestion = req.body.ques
queryAnswer = req.body.ans
console.log("in")
axios.post('http://localhost:5000/addQuery', {
queryQuestion,
queryAnswer
})
.then((res) => {
return result.send("Success")
})
.catch((err) => {
console.log(err)
})
});
app.post('/prescription', (req, res) => {
console.log('prescription page loaded');
connection.query('select name,age from Patient where p_id=101;', function(err, result, fields){
if (err) throw err;
var json_response = {}
json_response["id"] = "103"
json_response["name"] = result[0].name
json_response["age"] = result[0].age
return res.send(json_response);
});
});
app.post('/prescriptionSubmit', (req, res) => { //Enters prescription details into Database
prescription_id = random.integer(301, 400);
console.log(prescription_id);
patient_id = req.body.p_id
diagnose = req.body.SELECT;
name = req.body.PATIENT;
age = req.body.age;
drug = req.body.DRUG;
unit = req.body.UNIT;
dose = req.body.DOSE;
app_date = req.body.DATE;
date = new Date();
precaution = req.body.PRECAUTION;
doctor_id = '201';
console.log('data received from prescription after submit.');
connection.query('delete from Prescription where p_id=101', function(err, result, fields){
if(err) throw err;
connection.query('insert into Prescription values(?,?,?,?,?,?,?,?,?,?)',[prescription_id, patient_id, date, app_date, diagnose, drug, unit, dose, doctor_id, precaution], function(err, result, fields){
if(err) throw err;
console.log("data inserted");
io.sockets.emit('newRegular',"Thank you for visiting the doctor, here is the details for your next appointment")
connection.query("Select * from Doctor where d_id = ?",[doctor_id],function(err, result, fields) {
if(err) throw err;
doctor_name = result[0].name;
var json_response = {}
json_response["type"] = "appointment";
json_response["name"] = doctor_name;
json_response["date"] = app_date;
console.log(json_response)
io.sockets.emit('newAppointment',json_response)
return res.send("Success")
});
});
});
});
app.post('/getQuery', (req, result) => {
console.log("IND")
axios.post('http://localhost:5000/getQuery', {
})
.then((res) => {
return result.send(res.data)
})
.catch((err) => {
console.log(err)
})
});
app.post('/getPendings', (req, result) => { //Sends list of all pending queries
console.log("getPendings loaded!");
connection.query('select * from pendings where p_id=101', function(err, res, fields){
if (err) throw err;
json_response = []
for(i=0;i<res.length;i++){
json_object = {}
json_object["patient_id"] = res[i].p_id
json_object["questions"] = res[i].question
json_object["question_id"] = res[i].q_id
json_response.push(json_object)
}
console.log(json_response)
return result.send(json_response)
});
});
app.post('/getAnswers', (req, res) => { //Retrieve answers from hospital portal for pending queries
console.log(req.body);
answer = req.body.ANSWER;
question_id = req.body.q_id;
person_id = req.body.p_id;
console.log(req.body);
connection.query('update pendings set answer=? where q_id=? and p_id=?',[answer, question_id, person_id],function(err, result, fields){
if(err) throw error;
console.log("Answers filled");
console.log(result);
return res.send("Success")
});
});
//port activation
server.listen(8081, () => {
console.log("Listening on 8081");
});