-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
182 lines (161 loc) · 5.59 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
require('dotenv').config();
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var User = require('./models/user');
var Trip = require('./models/trips');
var lowerCase = require('./middleware/toLowerCase');
var ObjectId = require('mongoose').Types.ObjectId;
var bcrypt = require('bcrypt');
// Mongoose stuff
var mongoose = require('mongoose');
// mongoose.connect('mongodb://localhost/ryde'); // change db name here
mongoose.connect(process.env.MONGODB_URI);
// added a comment
// var index = require('./routes/index');
// var users = require('./routes/users');
var auth = require('./routes/auth');
var profile = require('./routes/profile');
var myrydes = require('./routes/myrydes');
var ryders = require('./routes/ryders');
var mydryves = require('./routes/mydryves');
var ryde = require('./routes/ryde');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
// app.use(express.static(path.join(__dirname, 'client', 'build')));
app.use(express.static(path.resolve(__dirname, 'client', 'build')));
// Do we still need this?
app.use(function(req, res, next) {
// before every route, attach the flash messages and current user to res.locals
res.locals.currentUser = req.user;
next();
});
app.post('/bigsearch', (req, res, next) => {
let body = lowerCase(req.body)
var searchOptions = {
'startAddress.zip': body.zip,
'startAddress.city': body.sCity,
'endAddress.city': body.eCity,
departDate: {$gte: body.dateTime},
pets: body.pets,
cost: {$lte: body.cost},
reoccurring: body.reoccur,
seats: {$gte: body.seat},
completed: false,
deleted:false
}
for (let key in searchOptions) {
if (searchOptions[key] === '' || searchOptions[key] === false || searchOptions[key] === undefined) {
delete searchOptions[key]
}
}
if(searchOptions.cost['$lte']==='') delete searchOptions.cost
if(searchOptions.seats['$gte']==='') delete searchOptions.seats
//for some reason this key value pair was always being deleted I assume it had to do with a hidden js having to do with $ne
if(body.userId) {
searchOptions.driverId = {$ne:ObjectId(body.userId)}
searchOptions.deniedRiders = {$ne: body.userId}
}
//console.log('searchoptions: ',searchOptions);
Trip.find(searchOptions).lean().exec( function(err, trips) {
let count = 0;
let newTrips = []
if(trips.length === 0) {
return res.send({newTrips})
} else {
trips.forEach((trip,index)=>{
//console.log('each trip time: ',trip.departDate);
// console.log('trip date: ',(new Date(trip.departDate)).toUTCString());
// console.log('search date: ',(new Date(req.body.dateTime)).toUTCString());
let id = {'_id': ObjectId(trip.driverId)}
let tripAvailability = (trip.seats - trip.ridersId.length - req.body.seat)
//if no seats Available delete from index and count up
//console.log('tripAvailability: ',tripAvailability);
tripAvailability <= 0 ? (
delete trips[index],
count++,
count === trips.length ? (
res.send({newTrips})
) : (console.log(''))
) : (
User.findOne(id, function(err, user) {
trip.driver = user.toObject();
count++;
newTrips.push(trip);
if(count === trips.length) {
res.send({newTrips});
}
})
)
})
}
})
})
app.post('/minisearch', (req,res,next) =>{
let bodh = req.body
var searchOptions ={
'startAddress.zip': bodh.startZip,
'endAddress.zip': bodh.endZip,
departDate: {$gte: bodh.date}
}
for(let key in searchOptions){
if (searchOptions[key] === '' || searchOptions[key] === false) {
delete searchOptions[key]
}
}
if(bodh.userId){
searchOptions.driverId = {$ne:ObjectId(bodh.userId)}
searchOptions.deniedRiders = {$ne: bodh.userId}
}
//console.log(searchOptions);
Trip.find(searchOptions).lean().exec( function(err, trips){
let count = 0;
let newTrips = []
if(trips.length === 0) {
return res.send([{newTrips}])
} else {
trips.forEach((trip,index)=>{
// console.log('trip date: ',(new Date(trip.departDate)).toUTCString());
// console.log('search date: ',(new Date(req.body.dateTime)).toUTCString());
let id = {'_id': ObjectId(trip.driverId)}
let tripAvailability = (trip.seats - trip.ridersId.length - req.body.seat)
//if no seats Available delete from index and count up
tripAvailability <= 0 ? (
delete trips[index],
count++,
//if end of of foreach loop send trips otherwise next trip
count === trips.length ? (
res.send({newTrips})
) : (console.log('next trip loop'))
) : (
User.findOne(id, function(err, user) {
//add key value pair of driver to trip object
trip.driver = user.toObject();
count++;
newTrips.push(trip);
if(count === trips.length) {
res.send({newTrips});
}
})
)
})
}
})
})
app.use('/auth', auth);
app.use('/profile', profile);
app.use('/myrydes', myrydes);
app.use('/ryders', ryders);
app.use('/mydryves', mydryves);
app.use('/ryde', ryde);
app.get('*', function(req, res, next) {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
const PORT = process.env.PORT || 5000
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}!`);
});
module.exports = app;