-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·1052 lines (928 loc) · 34.7 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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var express = require('express');
var mongodb = require('mongodb');
var mongo = require('mongodb').MongoClient;
var bodyParser = require('body-parser');
var session = require('client-sessions'); //CITATION: Sessions: https://stormpath.com/blog/everything-you-ever-wanted-to-know-about-node-dot-js-sessions/
var ObjectId = require('mongodb').ObjectID;
var nodemailer = require('nodemailer'); //CITATION Email notifications: https://nodemailer.com/
var bcrypt = require('bcrypt');
var uri = "mongodb://admin:[email protected]:39135/heroku_lfhmjwj6";
var lastTime = 0;
var app = express();
var db = null;
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// views is directory for all template files
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
//whenever new session starts, clear cookies
app.use(session({
cookieName: 'session',
//high entropy string to encode/decode cookies
secret: 'weoirdfbxlkqpelwhfsseksm129575',
duration: 60 * 60 * 1000,
//keeps session running 5 minutes after last click
activeDuration: 60 * 60 * 1000,
httpOnly: true,
secure: true,
ephemeral: true
}));
mongo.connect(uri, function(err, dbRes) {
//console.log("connected to database");
db = dbRes;
});
app.use(function(req, res, next) {
if(db === null) {
console.log("shit");
}
else {
var usersCollection = db.collection('users');
if (req.session && req.session.user) {
usersCollection.findOne({"username": req.session.user['username'] }, function(err, user) {
if (user) {
req.user = user;
delete req.user.password; // delete the password from the session
//format is { _id: 5691889a13b90b0300ec85b7, username: '[email protected]' }
req.session.user = user; //refresh the session value
//req.session.user["lastTime"] = 0;
res.locals.user = user;
}
// finishing processing the middleware and run the route
next();
});
} else {
next();
}
}
});
var smtpConfig = {
host: 'smtp.gmail.com',
port: 465,
secure: true, // use SSL
auth: {
user: '[email protected]',
pass: 'alwaysblue'
}
};
var transporter = nodemailer.createTransport(smtpConfig);
//implement option?
app.post('/logout', function(req, res) {
req.session.reset();
res.send('clear_cookies_success');
});
function requireLogin (req, res, next) {
if (!req.user) {
res.redirect('/');
//or render?
} else {
next();
}
};
app.get('/', function(request, response) {
response.render('pages/index');
});
app.get('/start_page', requireLogin, function(request, response) {
response.render('pages/start_page', {
username: request.session.user['username']
});
//cookies are stored
});
app.get('/dashboard', function(request, response) {
response.render('pages/dashboard', {
username: request.session.user['username']
});
});
app.get('/about', function(request, response) {
response.render('pages/about', {
username: request.session.user['username']
});
});
app.get('/visualization', function(request, response) {
response.render('pages/visualization', {
username: request.session.user['username']
});
});
app.listen(app.get('port'), function() {
//console.log('Node app is running on port', app.get('port'));
});
app.post('/getlostmappins', function(req, res, next) {
var lostCollection = db.collection('lost');
lostCollection.find().toArray(function (err1, result1) {
if (err1) {
console.log(err1);
}
else {
res.send(result1);
}
});
});
app.post('/getfoundmappins', function(req, res, next) {
var foundCollection = db.collection('found');
foundCollection.find().toArray(function (err, result) {
if (err) {
console.log(err);
}
else {
res.send(result);
}
});
});
app.post('/getlostviz', function(req, res, next) {
var lostCollection = db.collection('lost');
lostCollection.find().toArray(function (err, result) {
if (err) {
console.log(err);
}
else {
res.send(result);
}
});
});
app.post('/getfoundviz', function(req, res, next) {
var foundCollection = db.collection('found');
foundCollection.find().toArray(function (err, result) {
if (err) {
console.log(err);
}
else {
res.send(result);
}
});
});
app.post('/checkuser', function(req, res, next) {
var userName = req.body.username;
var passWord = req.body.password;
var usersCollection = db.collection('users');
usersCollection.find({username: userName}).toArray(function (err, result) {
if (err) {
console.log(err);
}
else if (result.length && result[0]['password'] !== undefined) {
var hash = result[0]['password'];
bcrypt.compare(passWord, hash, function(err, hashres) {
if(hashres) {
req.session.user = result[0];
//req.session.user["lastTime"] = 0;
//console.log(JSON.stringify(req.session.user) + " THIS IS THE USER......");
res.send("start_page");
}
else {
res.send('Wrong password');
}
});
}
else {
res.send("No user with that username");
}
});
});
app.post('/adduser', function (req, res, next) {
// Catching variables passed in the form
var userName = req.body.username;
var passWord = req.body.password;
var usersCollection = db.collection('users');
usersCollection.find({username: userName}).toArray(function (err, result) {
if (err) {
console.log(err);
}
else if (result.length) {
res.send("username_exists");
}
else {
//Adding the new entry to the db
bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash(passWord, salt, function(err, hash) {
// Store hash in your password DB.
usersCollection.insert({
"username" : userName,
"password" : hash,
"lost_list" : [],
"found_list" : []
}, function (err, insertResult) {
// Send back a success message
usersCollection.find({username: userName}).toArray(function (err, result) {
if (err) { //error
console.log(err);
}
else if (result.length) { //found user we just entered
req.session.user = result[0];
//req.session.user["lastTime"] = 0;
res.send('username_added');
}
else { //oops
}
});
});
});
});
}
});
});
//CITATION Geocoding: https://developers.google.com/maps/documentation/javascript/geocoding and https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple
//CITATIOn Distance between two points: http://stackoverflow.com/questions/1502590/calculate-distance-between-two-points-in-google-maps-v3
//stack overflow code
var rad = function(x) {
return x * Math.PI / 180;
};
//stack overflow code
//p1 = [lat,lng]
var getDistance = function(p1, p2) {
var R = 6378137; // Earth’s mean radius in meter
var dLat = rad(p2[0] - p1[0]);
var dLong = rad(p2[1] - p1[1]);
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(rad(p1[0])) * Math.cos(rad(p2[0])) *
Math.sin(dLong / 2) * Math.sin(dLong / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c;
return d; // returns the distance in meter
};
//insert new lost form, update lost form's found_matches_list if possible, update those found forms lost_matches_lists
app.post('/addlostform', function (req, res, next) {
var usersCollection = db.collection('users');
var lostCollection = db.collection('lost');
var foundCollection = db.collection('found');
//console.log("THIS IS THE USER!!!!! " + JSON.stringify(req.session.user));
username = req.session.user["username"];
//lastTime = req.session.user["lastTime"];
// var d = new Date();
// var currTime = d.getTime()/60000;
// var prevTime = lastTime;
// //req.session.user["lastTime"] = num_min;
// console.log(prevTime + "LOST PREV TIME!!!!!!!!!!");
// console.log(currTime + "LOST CURR TIME!!!!!!!!!!!!");
// lastTime = currTime;
// if (currTime - prevTime < 1) {
// //have a pop up saying you can only submit one form a minute pls no spam
// console.log("LESS THAN ONE MINUTE!!!!!");
// res.send("spam");
// }
//else {
var final_found_matches_list = []
//add a new lost form
var new_lost_item = {
"username" : username,
"item_category" : req.body.item_category,
"item_subcategory": req.body.item_subcategory,
"item_location": [req.body.item_lat,req.body.item_lng],
"found_matches_list": [],
"recovered" : false
};
lostCollection.insert(new_lost_item, function (err, insertResult) {
if (err) { //error
console.log(err);
}
else {
var lost_form_id = new_lost_item._id
//go through all found forms and find possible matches
//add id of lost form to all potential found forms 'lost_matches_list'
//add ids of all found forms to the 'found_matches_list' of the lost item
foundCollection.find({"item_category":req.body.item_category,"item_subcategory":req.body.item_subcategory }).toArray(function (err, result) {
if (err) {
console.log(err);
}
else {
//res.send(JSON.stringify(result));
for(var i = 0; i < result.length; i++) {
var distance = 0;
if ((result[i]['item_location'][0] !== "none") && (req.body.item_lat !== "none")){
var found_location = [parseFloat(result[i]['item_location'][0]),parseFloat(result[i]['item_location'][1])];
var lost_location = [parseFloat(req.body.item_lat),parseFloat(req.body.item_lng)];
distance = getDistance(found_location,lost_location);
}
if (distance < 804){ //meters of .5 mile
var object_fun = {}
object_fun[result[i]['_id']] = "red";
final_found_matches_list.push(object_fun);
//find previous lost_list of found forms
prev_list = result[i]["lost_matches_list"];
new_dict = {};
new_dict[lost_form_id] = "gray";
prev_list.push(new_dict);
foundCollection.update(
{ "_id": result[i]["_id"]},
{
$set: {
"lost_matches_list": prev_list
}
}
);
}
}
//update the lost_collection with new found_matches_list
//onsole.log("finalasdfasdf " + final_found_matches_list);
lostCollection.update(
{ "_id": lost_form_id},
{
$set: {
"found_matches_list": final_found_matches_list
}
}
);
//update the user's data to include that form's id in list
usersCollection.find({username: username}).toArray(function (err, result) {
if (err) {
console.log(err);
}
else if(result[0]['lost_list'] !== undefined) {
var prev_list = result[0]['lost_list']
prev_list.push(lost_form_id)
usersCollection.update(
{ "username": username },
{
$set: {
"lost_list": prev_list
}
}
);
}
});
}
});
}
});
//}
});
app.post('/addfoundform', function (req, res, next) {
var usersCollection = db.collection('users');
var lostCollection = db.collection('lost');
var foundCollection = db.collection('found');
username = req.session.user["username"];
// var d = new Date();
// var currTime = d.getTime()/60000;
// var prevTime = lastTime;
// console.log(prevTime + "FOUND PREV TIME!!!!!!!!!!");
// console.log(currTime + "FOUND CURR TIME!!!!!!!!!!!!");
// lastTime = currTime;
// if (currTime - prevTime < 1) {
// //have a pop up saying you can only submit one form a minute pls no spam
// console.log("LESS THAN ONE MINUTE!!!!!");
// res.send("spam");
// }
//else {
var final_lost_matches_list = []
//add a new lost form
var new_found_item = {
"username" : username,
"item_category" : req.body.item_category,
"item_subcategory": req.body.item_subcategory,
"item_location": [req.body.item_lat,req.body.item_lng],
"lost_matches_list": [],
"item_description": req.body.item_description,
"recovered" : false
};
foundCollection.insert(new_found_item, function (err, insertResult) {
if (err) { //error
console.log(err);
}
else {
var found_form_id = new_found_item._id;
//go through all found forms and find possible matches
//add id of lost form to all potential found forms 'lost_matches_list'
//add ids of all found forms to the 'found_matches_list' of the lost item
lostCollection.find({"item_category":req.body.item_category,"item_subcategory":req.body.item_subcategory }).toArray(function (err, result) {
if (err) {
console.log(err);
}
else {
//res.send(JSON.stringify(result));
for(var i = 0; i < result.length; i++) {
//var category = result[i]['item_category'];
//var subcategory = " > " + lost_arr[i]['item_subcategory'];
//var found_location= new google.maps.LatLng(parseFloat(result[i]['item_location'][0]), parseFloat(result[i]['item_location'][1]));
//var lost_location= new google.maps.LatLng(parseFloat(req.body.item_lat),parseFloat(req.body.item_lng))
//var distance= google.maps.geometry.spherical.computeDistanceBetween (found_location, lost_location);
var distance = 0;
if ((result[i]['item_location'][0] !== "none") && (req.body.item_lat !== "none")){
var lost_location = [parseFloat(result[i]['item_location'][0]),parseFloat(result[i]['item_location'][1])];
var found_location = [parseFloat(req.body.item_lat),parseFloat(req.body.item_lng)];
distance = getDistance(found_location,lost_location);
}
if (distance < 804.67) { //meters of .5 mile
//idk syntax
var object_fun = {}
object_fun[result[i]['_id']] = "gray";
final_lost_matches_list.push(object_fun);
//find previous lost_list of found forms
//console.log(JSON.stringify(final_found_matches_list) + "!!!!!!");
prev_list = result[i]["found_matches_list"];
new_dict = {};
new_dict[found_form_id] = "red";
prev_list.push(new_dict);
//EMAIL VERIFICATION: notifying loser that there was a match
var mailOptions = {
from: 'ihtfyp <[email protected]>', // sender address
to: result[i]['username'], // list of receivers
subject: 'IHTFYP: Someone found an item that could be yours!', // Subject line
text: '', // plaintext body
html: 'Please check your <a href="https://ihtfyp.herokuapp.com">dashboard</a> to answer a verification question about your lost item. <br></br> Good luck! <br></br>IHTFYP' // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
});
lostCollection.update(
{ "_id": result[i]["_id"]},
{
$set: {
"found_matches_list": prev_list
}
}
);
}
}
foundCollection.update(
{ "_id": found_form_id},
{
$set: {
"lost_matches_list": final_lost_matches_list
}
});
usersCollection.find({username: username}).toArray(function (err, result) {
if (err) {
console.log(err);
}
else if(result[0]['found_list'] !== undefined){
var prev_list = result[0]['found_list']
prev_list.push(found_form_id)
usersCollection.update(
{ "username": username },
{
$set: {
"found_list": prev_list
}
});
}
});
}
});
}
});
//}
});
app.post('/getlost', function(req, res, next) {
var lostCollection = db.collection('lost');
username = req.session.user["username"];
lostCollection.find({username: username}).toArray(function (err, result) {
if (err) {
console.log(err);
}
else {
res.send(JSON.stringify(result));
}
});
});
app.post('/getfound', function(req, res, next) {
var foundCollection = db.collection('found');
username = req.session.user["username"];
foundCollection.find({username: username}).toArray(function (err, result) {
if (err) {
console.log(err);
}
else {
res.send(JSON.stringify(result));
}
});
});
app.post('/answerverification', function(req, res, next) {
var lost_id = req.body.lost_id;
var found_id = req.body.found_id;
var verificationCollection = db.collection('verification');
verificationCollection.find({"lost_id": lost_id, "found_id": found_id}).toArray(function (err, result) {
if (err) {
console.log(err);
}
else {
res.send(JSON.stringify(result));
}
});
});
app.post('/getmatched', function(req, res, next) {
var matchedCollection = db.collection('matched');
matchedCollection.count(function(err, count) {
res.send(JSON.stringify(count));
});
});
app.post('/addverification', function(req, res, next) {
var verificationCollection = db.collection('verification');
var foundCollection = db.collection('found');
var lostCollection = db.collection('lost');
var lost_id = req.body.lost_id;
var found_id = req.body.found_id;
var verification_text = req.body.verification_text;
//Adding the new entry to the db
verificationCollection.insert({
"lost_id" : lost_id,
"found_id" : found_id,
"verification_text" : verification_text
}, function (err, insertResult) {
foundCollection.find({"_id": ObjectId(found_id)}).toArray(function (err, result) {
if (err) {
console.log(err);
}
else if(result[0]['lost_matches_list'] !== undefined){
var prev_lost_matches_list = result[0]['lost_matches_list'];
for(var i = 0; i < prev_lost_matches_list.length; i++) {
var keys = Object.keys(prev_lost_matches_list[i]);
if(keys[0] === lost_id) {
prev_lost_matches_list[i][keys[0]] = 'red';
break;
}
}
foundCollection.update(
{ "_id": ObjectId(found_id)},
{
$set: {
"lost_matches_list": prev_lost_matches_list
}
}
);
//EMAIL VERIFICATION: notifying finder about loser's verification
var mailOptions = {
from: 'ihtfyp <[email protected]>', // sender address
to: result[0]['username'], // list of receivers
subject: 'IHTFYP: Someone thinks you found their item!', // Subject line
text: '', // plaintext body
html: 'Please check your <a href="https://ihtfyp.herokuapp.com">dashboard</a> to confirm that what you found is theirs. <br></br> Thank you! <br></br>IHTFYP' // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
});
}
});
lostCollection.find({"_id": ObjectId(lost_id)}).toArray(function (err, result) {
if (err) {
console.log(err);
}
else if(result[0]['found_matches_list'] !== undefined){
var prev_found_matches_list = result[0]['found_matches_list'];
for(var i = 0; i < prev_found_matches_list.length; i++) {
var keys = Object.keys(prev_found_matches_list[i]);
if(keys[0] === found_id) {
prev_found_matches_list[i][keys[0]] = 'gray';
break;
}
}
lostCollection.update(
{ "_id": ObjectId(lost_id)},
{
$set: {
"found_matches_list": prev_found_matches_list
}
}
);
}
});
});
});
app.post('/yesverification', function(req, res, next) {
var foundCollection = db.collection('found');
var lostCollection = db.collection('lost');
var lost_id = req.body.lost_id;
var found_id = req.body.found_id;
foundCollection.find({"_id": ObjectId(found_id)}).toArray(function (err, result) {
var description = result[0]['item_description'];
var finderemail = result[0]['username'];
if (err) {
console.log(err);
}
else {
var prev_lost_matches_list = result[0]['lost_matches_list'];
console.log("PREV LIST IS " + JSON.stringify(prev_lost_matches_list));
for(var i = 0; i < prev_lost_matches_list.length; i++) {
var keys = Object.keys(prev_lost_matches_list[i]);
if(keys[0] === lost_id) {
prev_lost_matches_list[i][keys[0]] = 'blue';
break;
}
}
foundCollection.update(
{ "_id": ObjectId(found_id)},
{
$set: {
"lost_matches_list": prev_lost_matches_list
}
}
);
}
lostCollection.find({"_id": ObjectId(lost_id)}).toArray(function (err, result) {
if (err) {
console.log(err);
}
else {
console.log("UPDATING THE LOST ITEM ........" + JSON.stringify(result));
var prev_found_matches_list = result[0]['found_matches_list'];
console.log("PREV LIST IS " + JSON.stringify(prev_found_matches_list));
for(var i = 0; i < prev_found_matches_list.length; i++) {
var keys = Object.keys(prev_found_matches_list[i]);
if(keys[0] === found_id) {
prev_found_matches_list[i][keys[0]] = 'blue';
break;
}
}
lostCollection.update(
{ "_id": ObjectId(lost_id)},
{
$set: {
"found_matches_list": prev_found_matches_list
}
}
);
var emailtext = "";
if(description === "") {
emailtext = "Someone confirmed your verification answer. Their email is " + finderemail + ". Please contact them to make sure this is your item-- ask for a picture if there is any ambiguity. Then, arrange a pickup, and delete this item from your dashboard to stop receiving email notifications. <br></br> Thank you! <br></br>IHTFYP";
}
else {
emailtext = "Someone confirmed your verification answer. Here is their description of the object they found: " + description + "<br></br> Their email is " + finderemail + ". Please contact them to make sure this is your item-- ask for a picture if there is any ambiguity. Then, arrange a pickup, and delete this item from your dashboard to stop receiving email notifications. <br></br> Thank you! <br></br>IHTFYP";
}
//EMAIL VERIFICATION: notifying loser that the finder confirmed their verification
var mailOptions = {
from: 'ihtfyp <[email protected]>', // sender address
to: result[0]['username'], // list of receivers
subject: 'IHTFYP: Someone may have found your item! :)', // Subject line
text: '', // plaintext body
html: emailtext // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
});
}
});
});
});
app.post('/noverification', function(req, res, next) {
var foundCollection = db.collection('found');
var lostCollection = db.collection('lost');
var lost_id = req.body.lost_id;
var found_id = req.body.found_id;
foundCollection.find({"_id": ObjectId(found_id)}).toArray(function (err, result) {
if (err) {
console.log(err);
}
else if(result[0]['lost_matches_list'] !== undefined){
var prev_lost_matches_list = result[0]['lost_matches_list'];
for(var i = 0; i < prev_lost_matches_list.length; i++) {
var keys = Object.keys(prev_lost_matches_list[i]);
if(keys[0] === lost_id) {
prev_lost_matches_list.splice(i, 1);
break;
}
}
foundCollection.update(
{ "_id": ObjectId(found_id)},
{
$set: {
"lost_matches_list": prev_lost_matches_list
}
}
);
}
});
lostCollection.find({"_id": ObjectId(lost_id)}).toArray(function (err, result) {
if (err) {
console.log(err);
}
else if(result[0]['found_matches_list'] !== undefined){
var prev_found_matches_list = result[0]['found_matches_list'];
for(var i = 0; i < prev_found_matches_list.length; i++) {
var keys = Object.keys(prev_found_matches_list[i]);
if(keys[0] === found_id) {
prev_found_matches_list.splice(i, 1);
break;
}
}
lostCollection.update(
{ "_id": ObjectId(lost_id)},
{
$set: {
"found_matches_list": prev_found_matches_list
}
}
);
}
});
});
app.post('/deletefound', function(req, res, next) {
var foundCollection = db.collection('found');
var lostCollection = db.collection('lost');
var usersCollection = db.collection('users');
var found_id = req.body.found_id;
foundCollection.find({"_id": ObjectId(found_id)}).toArray(function (err, result) {
if (err) {
console.log(err);
}
else if(result[0]['lost_matches_list'] !== undefined) {
var prev_lost_matches_list = result[0]['lost_matches_list'];
for(var i = 0; i < prev_lost_matches_list.length; i++) {
(function(value) {
var lostkeys = Object.keys(prev_lost_matches_list[value]);
lostCollection.find({"_id": ObjectId(lostkeys[0])}).toArray(function (err, result) {
if (err) {
console.log(err);
}
else if(result[0] !== undefined && result[0]['found_matches_list'] !== undefined){
var prev_found_matches_list = result[0]['found_matches_list'];
for(var j = 0; j < prev_found_matches_list.length; j++) {
var foundkeys = Object.keys(prev_found_matches_list[j]);
if(foundkeys[0] === found_id) {
prev_found_matches_list.splice(j, 1);
lostCollection.update(
{ "_id": ObjectId(lostkeys[0])},
{
$set: {
"found_matches_list": prev_found_matches_list
}
}
);
}
}
}
});
})(i);
};
}
});
username = req.session.user["username"];
usersCollection.find({username: username}).toArray(function(err, result) {
if(err) {
console.log(err);
}
else if(result[0]['found_list'] !== undefined){
var prev_found_list = result[0]['found_list'];
for(var k = 0; k < prev_found_list.length; k++) {
if(found_id === prev_found_list[k]['_id']) {
prev_found_list.splice(k, 1);
break;
}
}
usersCollection.update(
{ "username": username},
{
$set: {
"found_list": prev_found_list
}
}
);
}
});
foundCollection.remove({"_id": ObjectId(found_id)});
});
app.post('/deletelostnomatch', function(req, res, next) {
var foundCollection = db.collection('found');
var lostCollection = db.collection('lost');
var usersCollection = db.collection('users');
var lost_id = req.body.lost_id;
lostCollection.find({"_id": ObjectId(lost_id)}).toArray(function (err, result) {
if (err) {
console.log(err);
}
else if(result[0] !== undefined && result[0]['found_matches_list'] !== undefined){
var prev_found_matches_list = result[0]['found_matches_list'];
for(var i = 0; i < prev_found_matches_list.length; i++) {
(function(value) {
var foundkeys = Object.keys(prev_found_matches_list[value]);
foundCollection.find({"_id": ObjectId(foundkeys[0])}).toArray(function (err, result) {
if (err) {
console.log(err);
}
else if(result[0]['lost_matches_list'] !== undefined){
var prev_lost_matches_list = result[0]['lost_matches_list'];
for(var j = 0; j < prev_lost_matches_list.length; j++) {
var lostkeys = Object.keys(prev_lost_matches_list[j]);
if(lostkeys[0] === lost_id) {
prev_lost_matches_list.splice(j, 1);
foundCollection.update(
{ "_id": ObjectId(foundkeys[0])},
{
$set: {
"lost_matches_list": prev_lost_matches_list
}
}
);
}
}
}
});
})(i);
};
}
});
username = req.session.user["username"];
usersCollection.find({username: username}).toArray(function(err, result) {
if(err) {
console.log(err);
}
else if(result[0]['lost_list'] !== undefined){
var prev_lost_list = result[0]['lost_list'];
for(var k = 0; k < prev_lost_list.length; k++) {
if(lost_id === prev_lost_list[k]['_id']) {
prev_lost_list.splice(k, 1);
break;
}
}
usersCollection.update(
{ "username": username},
{
$set: {
"lost_list": prev_lost_list
}
}
);
}
});
lostCollection.remove({"_id": ObjectId(lost_id)});
});
//delete lost and found item
app.post('/deletelostyesmatch', function(req, res, next) {
var foundCollection = db.collection('found');
var lostCollection = db.collection('lost');
var usersCollection = db.collection('users');
var matchedCollection = db.collection('matched');
var lost_id = req.body.lost_id;
var found_id;
var founder_username;
var item_category;
var item_subcategory;
lostCollection.find({"_id": ObjectId(lost_id)}).toArray(function (err, result) {
if (err || result[0]['item_category'] === undefined || result[0]["item_subcategory"] === undefined) {
console.log(err);
}
else if(result[0]['found_matches_list'] !== undefined){
item_category = result[0]['item_category'];
item_subcategory = result[0]['item_subcategory'];
//DELETE ITEM FROM FINDER
var prev_found_matches_list = result[0]['found_matches_list'];
for(var i = 0; i < prev_found_matches_list.length; i++) {
(function(value) {
var foundkeys = Object.keys(prev_found_matches_list[value]);
foundCollection.find({"_id": ObjectId(foundkeys[0])}).toArray(function (err, result) {
if (err) {
console.log(err);
}
else if(result[0]['lost_matches_list'] !== undefined){
var prev_lost_matches_list = result[0]['lost_matches_list'];
for(var j = 0; j < prev_lost_matches_list.length; j++) {
var lostkeys = Object.keys(prev_lost_matches_list[j]);
if(lostkeys[0] === lost_id) {
if(prev_lost_matches_list[j][lostkeys[0]] === 'blue') {
founder_username = result[0]['username'];
found_id = foundkeys[0];
}