-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
424 lines (370 loc) · 12.6 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
const _=require('lodash');
const express=require('express');
const http = require('http');
const WebSocket = require('ws');
const path=require('path');
var url = require('url');
const fs=require('fs');
let cors=require('cors');
const server = http.createServer();
const wss1 = new WebSocket.Server({ noServer: true });
var mongoose= require('./server/db/mongoose.js');
var {User}= require('./server/models/user.js');
var {ambulance}= require('./server/models/ambulance.js');
var bodyParser= require('body-parser');
//important parameters in order for code to run
let app=express();
app.use(bodyParser.json());
app.use(cors());
app.use(bodyParser.urlencoded({ extended: true, limit: "50mb" }));
const publicPath = path.join(__dirname, '/uploads');
app.use(express.static(path.join(__dirname,'/images')));
app.set('viewengine','ejs');
app.get("/",(req,res)=>{ //main
claim=[]
verfalse=[]
User.find({},(err,results)=>{
_.forEach(results,function(result){
if ((result.donation_claim) == true && (result.verified == true))
{
claim.push(result);
}
})
_.forEach(results,function(result){
if (!result.verified)
{
verfalse.push(result);
}
})
res.render('dashboard.ejs',{
users:verfalse,
claimed:claim
})
});
//res.render('login.ejs');
});
//route for signing up new user
app.post('/signup',(req,res)=>{
f_count=0;
User.countDocuments({}, function (err, count) {
var user = new User({
name:req.body.name,
mobile_Number:req.body.number,
"loc": {
"type": "Point",
"coordinates": [req.body.long, req.body.lat]
},
gender:req.body.gender,
password:req.body.password,
blood_grp:req.body.blood_grp,
user_id:count
})
//console.log(req.body);
var name1 = './images/'+count+'profile'+'.jpg';
var name2 = './images/'+count+'citFront'+'.jpg';
var name3 = './images/'+count+'citBack'+'.jpg';
var img1 = req.body.image;
var img2=req.body.image_citFront;
var img3=req.body.image_citBack;
var realFile1 = Buffer.from(img1,"base64");
var realFile2 = Buffer.from(img2,"base64");
var realFile3 = Buffer.from(img3,"base64");
fs.writeFile(name1, realFile1, function(err) {
if(err)
console.log(err);
});
fs.writeFile(name2, realFile2, function(err) {
if(err)
console.log(err);
});
fs.writeFile(name3, realFile3, function(err) {
if(err)
console.log(err);
});
user.save().then((result,err)=>{
res.send('user successfully created');
});
});
});
//ambulance signup
app.post('/ambulance',(req,res)=>{
ambulance.countDocuments( function (err, count) {
console.log('hey')
var obj=new ambulance({name:req.body.name,mobile_Number:req.body.number,
"loc": {
"type": "Point",
"coordinates": [req.body.long, req.body.lat]
},"vehicle_id":count+1});
obj.save().then((result,err)=>{
res.redirect('/');
})
})
})
//get ambulance data
app.get('/getambulance',(req,res)=>{
// ambulance.aggregate(
// [
// { "$geoNear": {
// "near": {
// "type": "Point",
// "coordinates": [parseFloat(req.query.long),parseFloat(req.query.lat)] // first longitude then latitude --- if
// //place is at west use "-" sign ahead.
// },
// "distanceField": "distance",
// "spherical": false,
// "maxDistance": 10000
// }}
// ],
// function(err,results) {
// if (results)
// {
// var final_result=[];
// _.forEach(results,function(result){
//
// send_data={
// "id":result.vehicle_id,
// "latitude":result.loc.coordinates[1],
// "longitude":result.loc.coordinates[0],
// "name":result.name,
// "number":result.mobile_Number,
// "online":result.online,
// "distance":result.distance
// }
// final_result.push(send_data);
// })
// res.send(final_result); }
// }
// )
ambulance.find({"t":1},(err,results)=>{
if (results)
{
var final_result=[];
_.forEach(results,function(result){
send_data={
"id":result.vehicle_id,
"latitude":result.loc.coordinates[1],
"longitude":result.loc.coordinates[0],
"name":result.name,
"number":result.mobile_Number,
"online":result.online,
"distance":result.distance
}
final_result.push(send_data);
})
res.send(final_result);
}
})
})
//to search the required donor
app.post('/search_donor',(req,res)=>{
console.log( req.query);
User.aggregate(
[
{ "$geoNear": {
"near": {
"type": "Point",
"coordinates": [parseFloat(req.body.long),parseFloat(req.body.lat)] // first longitude then latitude --- if
//place is at west use "-" sign ahead.
},
"distanceField": "distance",
"spherical": false,
"maxDistance": 10000
}}
],
function(err,results) {
// console.log(err);
// console.log(results);
if (results)
{
final_result=[];
_.forEach(results,function(result){
if ((result.blood_grp == req.body.blood_grp) && (result.gender == req.body.gender) && (result.user_id != parseInt(req.body.userid)) && (result.online == true)){
send_data={
"id":result.user_id,
"latitude":result.loc.coordinates[1],
"longitude":result.loc.coordinates[0],
"credits":result.credits,
"gender":result.gender,
"blood_grp":result.blood_grp
}
final_result.push(send_data);
}
})
}
res.send(final_result);
}
)
})
// for socket
wss1.on('connection',function connection(ws) {
ws.on('message', function incoming(message) {
parsedData = JSON.parse(message);
ws.userid=parsedData.userId;
User.findOneAndUpdate({user_id:ws.userid},{online:true},(err,result)=>{
})
console.log(parsedData);
console.log('received: %s', parsedData.msg);
wss1.clients.forEach(function each(client) {
// if (client.userid){
// User.findOneAndUpdate({user_id:client.userid},{online:true},()=>{
// console.log('status changed');
// })
// }
if (client.userid == parsedData.sendId && client.readyState === WebSocket.OPEN) {
if (parsedData.userId > parsedData.sendId)
{
s=parsedData.sendId;
p=parsedData.userId;
}else{
p=parsedData.sendId;
s=parsedData.userId;
}
fs.appendFile('./messageFiles/'+s+'-'+p+'.txt',JSON.stringify({"userid":parsedData.userId,"message":parsedData.msg})+'\n', (err) => {
if (err) throw err;
console.log('The "data to append" was appended to file!');
});
client.send(JSON.stringify({"msg":parsedData.msg,"send":"notown"}));
ws.send(JSON.stringify({"msg":parsedData.msg,"send":"own"}));
}
});
});
// ws.send('hey there! welcome from 1!');
});
// function intervalfunction(){
// console.log("ma call bhairaxu")
// wss1.clients.forEach(function(client){
//
// if (client.userid){
// c=0;
// User.find({verified:false},(err,results)=>{
// console.log(results);
// _.forEach(results,function(result){
// if (client.userid == result.user_id)
// {
// c=c+1;
// }
// })
// })
// console.log("c ko value",c)
// if (c==0){
// User.findOneAndUpdate({user_id:client.userid},{online:true},(err,result)=>{
// console.log('call bhaye hai');
// })
// }
//
// }
//
// })
//
// }
//
// setInterval(intervalfunction,4000);
server.on('upgrade', function upgrade(request, socket, head) {
const pathname = url.parse(request.url).pathname;
if (pathname === '/foo') {
wss1.handleUpgrade(request, socket, head, function done(ws) {
wss1.emit('connection', ws, request);
});
} else if (pathname === '/bar') {
wss2.handleUpgrade(request, socket, head, function done(ws) {
wss2.emit('connection', ws, request);
});
} else {
console.log('ma call bhaye');
socket.destroy();
}
});
app.get('/test',(req,res)=>{
User.countDocuments({ gender: 'male' }, function (err, count) {
console.log('there are %d jungle adventures', count);
});
});
//login route
app.post('/login',(req,res)=>{
User.findOne({mobile_Number:parseInt(req.body.number),password:req.body.password},(err,result)=>{
if(err){
res.send('user not found');
}
if (result){
send_data={
"id":result.user_id,
"name":result.name,
"credits":result.credits,
"gender":result.gender,
"blood_grp":result.blood_grp,
"online":result.online
}
res.send(send_data);
}
}).then((err)=>{
res.send('user not found');
})
})
//to verify the user
app.get('/verify',(req,res)=>{
console.log(req.query);
var user_id=req.query.id;
User.findOneAndUpdate({user_id:user_id},{verified:true},()=>{
res.redirect("/");
})
})
//image upload function
app.use(express.static(__dirname+'/public'));
app.post("/image", function(req, res){
var name = req.body.name;
var img = req.body.image;
var realFile = Buffer.from(img,"base64");
fs.writeFile(name, realFile, function(err) {
if(err)
console.log(err);
});
res.send("OK");
});
//routes for website
app.get('/get_unverified',(req,res)=>{
User.find({verified:false},(err,results)=>{
res.send(results);
})
})
// app.get('/get_claimed',(req,res)=>{
// User.find({donation_claim:true},(err,results)=>{
// res.render
// })
// })
//donor will to donate
app.get('/willchanger',(req,res)=>{
console.log(req.query.user_id);
User.findOne({user_id:parseInt(req.query.user_id)},(err,results)=>{
if (err) {
throw err
}
will=results.online;
User.findOneAndUpdate({user_id:parseInt(req.query.user_id)},{online:!will},(err,results)=>{
if (err){
throw err;
}
res.send('done');
})
}).catch((err)=>{
res.send("error");;
})
});
//donor verfication related jobs in website
app.get('/donorverify',(req,res)=>{
User.findOneAndUpdate({"user_id":parseInt(req.query.id)},{donation_claim: true,$inc:{'credits':100}},(err,results)=>{
// res.redirect('/');
res.send('successfully verified');
})
});
//donor verification request submitted by donor
app.get('/verifyme',(req,res)=>{
User.findOneAndUpdate({"user_id":parseInt(req.query.user_id)},{donation_claim: true},(err,results)=>{
res.send('successful');
})
})
//socket route
server.listen(8080);
//main server route
app.listen(3000,()=>{
console.log('server is high')
})