-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
408 lines (376 loc) · 15.1 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
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
const express = require('express');
var passport = require('passport');
var Amadeus = require('amadeus');
var Strategy = require('passport-local').Strategy;
const bodyParser = require('body-parser');
const axios = require('axios');
const config = require('./config');
const utils = require('./utils');
const https = require('https');
const app = express();
const port = process.env.PORT || 5000;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Use application-level middleware for common functionality, including
// logging, parsing, and session handling.
app.use(require('morgan')('combined'));
app.use(require('cookie-parser')());
app.use(require('express-session')({ secret: 'thequickbrownfoxjumpsoversomething', resave: true, saveUninitialized: false }));
// Initialize Passport and restore authentication state, if any, from the
// session.
app.use(passport.initialize());
app.use(passport.session());
var amadeus = new Amadeus({
clientId: config.clientId,
clientSecret: config.clientSecret,
});
var MongoClient = require('mongodb').MongoClient;
var ObjectID = new require('mongodb').ObjectID;
MongoClient.connect(config.url, {useNewUrlParser:true},function(err, client) {
if (err) throw err;
app.locals.db = client.db(config.db);
app.listen(port, () => console.log(`Listening on port ${port}`));
console.log("Database connected");
});
passport.use(new Strategy(
function(username, password, cb) {
app.locals.db.collection("companies").findOne({"username":username}, function(err, user) {
if (err) { return cb(err); }
if (!user) { return cb(null, false); }
if (user.password != utils.hashPassword(password)) { return cb(null, false); }
return cb(null, {_id:user._id,company_name: user.company_name,username:user.username});
});
}));
passport.serializeUser(function(user, cb) {
cb(null, {_id:user._id,company_name: user.company_name,username:user.username});
});
passport.deserializeUser(function(user, cb) {
app.locals.db.collection("companies").findOne({"_id":ObjectID(user._id)}, function (err, user) {
if (err) { return cb(err); }
cb(null, utils.parseOutPassword(user));
});
});
app.get('/yelp',function (req, res) {
axios.get('https://api.yelp.com/v3/businesses/search?limit=3&categories=venues&location='+req.query.location,{
headers:{
'Authorization': 'Bearer hGly48lgeqWTCo_lvHZYzvpNmoyuvK-awoGpsF5kWzr_loJYaD0wKDogWo171o-sWX1bzhRkNdVDmXndguWNt_DCV23DpSN4XVLgzUj7XntW1Go_55YPtQeDK-hwXHYx',
},
}).then((response) => {
var response = response.data;
res.send(response);
},
(error) => {
var status = error.response.status
}
);
});
app.get('/yelp2',function (req, res) {
axios.get('https://api.yelp.com/v3/businesses/search?limit=3&categories=hotels&location='+req.query.location,{
headers:{
'Authorization': 'Bearer hGly48lgeqWTCo_lvHZYzvpNmoyuvK-awoGpsF5kWzr_loJYaD0wKDogWo171o-sWX1bzhRkNdVDmXndguWNt_DCV23DpSN4XVLgzUj7XntW1Go_55YPtQeDK-hwXHYx',
},
}).then((response) => {
var response = response.data;
res.send(response);
},
(error) => {
var status = error.response.status
}
);
});
app.get('/api/destination', async function(req,res){
var data = await amadeus.shopping.flightDestinations.get({
origin : req.query.origin
});
data = await JSON.parse(data.body);
res.send(data.data[0]);
});
app.get('/api/airport',async function (req,res) {
// Airport Nearest Relevant Airport (for London)
var data = await amadeus.referenceData.locations.airports.get({
longitude : parseFloat(req.query.lng),
latitude : parseFloat(req.query.lat)
});
data = await JSON.parse(data.body);
res.send(data["data"][0]);
});
app.get('/api/account',
function(req, res) {
res.send(utils.parseOutPassword(req.user));
});
app.get('/api/login',
function(req, res){
res.send('login');
});
app.post('/api/login',
passport.authenticate('local'),
function(req, res) {
res.status(200).send(req.user);
});
app.get('/api/logout',
function(req, res){
req.logout();
res.redirect('/');
});
app.put('/api/employee/:id',
async function(req,res){
try{
var name = req.body.name;
var airline_class = req.body.airline_class;
var longitude = req.body.longitude;
var location = req.body.location;
var latitude = req.body.latitude;
var data = {};
if(utils.validVenueName(name)){
data['employee.$.name'] = name;
}
if(utils.validLocation(location)){
data['employee.$.location'] = location;
}
if(!isNaN(parseFloat(longitude))){
data['employee.$.longitude'] = longitude;
}
if(!isNaN(parseFloat(latitude))) {
data['employee.$.latitude'] = latitude;
}
if(utils.validAirlineClass(airline_class)){
data['employee.$.airline_class'] = airline_class;
}
if(data != {}) {
const db = req.app.locals.db;
console.log(data);
var company = await db.collection("companies").findOneAndUpdate({_id: ObjectID(req.user._id),'employee._id':ObjectID(req.params.id)}, {
$set: data,
}, {returnOriginal: false});
console.log(req.user._id);
if (company) {
res.send(utils.parseOutPassword(company.value));
} else {
res.send({"message": "Could not update employee"});
}
}else{
res.send({"message":"Invalid data"});
}
}catch (e) {
console.log(e);
res.end();
}
});
app.delete('/api/employee/:id',
async function(req,res){
try{
var employee_id = req.params.id;
const db = req.app.locals.db;
var company = await db.collection("companies").findOneAndUpdate({_id:ObjectID(req.user._id)},{$pull:{employee:{_id:ObjectID(employee_id)}}},{returnOriginal:false});
if(company){
res.send(utils.parseOutPassword(company.value));
}else{
res.send({"message":"Could not delete employee"});
}
}catch (e) {
console.log(e);
res.end();
}
});
app.post('/api/employee',
async function(req,res){
try{
var name = req.body.name;
var airline_class = req.body.airline_class;
var longitude = req.body.longitude;
var latitude = req.body.latitude;
var location = req.body.location;
if(utils.validName(name) && !isNaN(parseFloat(longitude)) && !isNaN(parseFloat(latitude)) && utils.validAirlineClass(airline_class) && utils.validLocation(location)){
const db = req.app.locals.db;
var company = await db.collection("companies").findOneAndUpdate({_id:ObjectID(req.user._id)},{$push:{employee:{name:name,airline_class:airline_class,longitude:longitude,location:location,latitude:latitude,_id:ObjectID()}}},{returnOriginal:false});
if(company){
res.send(utils.parseOutPassword(company.value));
}else{
res.send({"message":"Could not create employee"});
}
}else{
res.send("error")//TODO throw better error
}
}catch (e) {
console.log(e);
res.end();
}
});
app.delete('/api/venue/:id',
async function(req,res){
try{
var employee_id = req.params.id;
const db = req.app.locals.db;
var company = await db.collection("companies").findOneAndUpdate({_id:ObjectID(req.user._id)},{$pull:{venue:{_id:ObjectID(employee_id)}}},{returnOriginal:false});
if(company){
res.send(utils.parseOutPassword(company.value));
}else{
res.send({"message":"Could not delete venue"});
}
}catch (e) {
console.log(e);
res.end();
}
});
app.put('/api/venue/:id',
async function(req,res){
try{
var name = req.body.name;
var longitude = req.body.longitude;
var latitude = req.body.latitude;
var location = req.body.location;
var data = {};
if(utils.validVenueName(name)){
data['venue.$.name'] = name;
}
if(!isNaN(parseFloat(longitude))){
data['venue.$.longitude'] = longitude;
}
if(!isNaN(parseFloat(latitude))) {
data['venue.$.latitude'] = latitude;
}
if(utils.validLocation(location)){
data['venue.$.location'] = location;
}
if(data != {}) {
const db = req.app.locals.db;
var company = await db.collection("companies").findOneAndUpdate({_id: ObjectID(req.user._id),'venue._id':ObjectID(req.params.id)}, {
$set: data,
}, {returnOriginal: false});
if (company) {
res.send(utils.parseOutPassword(company.value));
} else {
res.send({"message": "Could not update venue"});
}
}else{
res.send({"message":"Invalid data"});
}
}catch (e) {
console.log(e);
res.end();
}
});
app.post('/api/venue',
async function(req,res){
try{
var name = req.body.name;
var longitude = req.body.longitude;
var latitude = req.body.latitude;
var location = req.body.location;
if(utils.validVenueName(name) && !isNaN(parseFloat(longitude)) && !isNaN(parseFloat(latitude)) && utils.validLocation(location)){
const db = req.app.locals.db;
var company = await db.collection("companies").findOneAndUpdate({_id:ObjectID(req.user._id)},{$push:{venue:{name:name,longitude:longitude,latitude:latitude,location:location,_id:ObjectID()}}},{returnOriginal:false});
if(company){
res.send(utils.parseOutPassword(company.value));
}else{
res.send({"message":"Could not create venue"});
}
}else{
res.send("error")//TODO throw better error
}
}catch (e) {
console.log(e);
res.end();
}
});
app.post('/api/signup', async (req, res) => {
try {
var username = req.body.username;
var password = req.body.password;
var company_name = req.body.company_name;
if(utils.validCompanyName(company_name) && utils.validPassword(password) && utils.validUsername(username)) {
const db = req.app.locals.db;
const company = await db.collection("companies").findOne({$or:[{"company_name":company_name},{"username":username}]});
if (company) {
//Company or username already exists
res.send({"message":"Company Name or Username already exists"});
} else {
//Sign up the company
var user = await db.collection("companies").insertOne({"company_name":company_name,"employee":[],"venue":[],"username":username,"password":utils.hashPassword(password)});
if(user){
user = user.ops[0];
res.send(utils.parseOutPassword(user));
}else{
//Could not create user
res.send("error")//TODO throw better error
}
}
}else{
res.end();
}
}catch (e) {
console.log(e);
res.end();
}
});
app.post('/api/search', async (req, res) => {
try {
var startDate = req.body.startDate;
var endDate = req.body.endDate;
var peopleInputs = req.body.peopleInputs;
console.log(peopleInputs);
var venueInputs = req.body.venueInputs;
var dest_set = [];
var src_dict = {};
for(var i = 0; i < venueInputs.length; i++) {
var dest_response = await amadeus.referenceData.locations.airports.get({
longitude : venueInputs[i].longitude,
latitude : venueInputs[i].latitude
});
var data = JSON.parse(dest_response.body);
data = data.data;
venueInputs[i].iata = data[0]['iataCode'];
dest_set.push(data[0]['iataCode']);
}
for(var i = 0; i < peopleInputs.length; i++) {
var src_response = await amadeus.referenceData.locations.airports.get({
longitude : peopleInputs[i].longitude,
latitude : peopleInputs[i].latitude
});
var temp = JSON.parse(src_response.body);
temp = temp.data;
var airportCode = temp[0]['iataCode'];
peopleInputs[i].iata = airportCode;
if(airportCode in src_dict) {
src_dict[airportCode] = src_dict[airportCode] + 1;
}
else {
src_dict[airportCode] = 1;
}
}
var endpoint_url = 'https://us-central1-hackathon-232619.cloudfunctions.net/pythonCall?';
endpoint_url+=('&dl=' + JSON.stringify(src_dict));
endpoint_url+=('&mo=' + dest_set.join(","));
endpoint_url+=('&date=' + formatDate(startDate));
console.log(endpoint_url);
https.get(endpoint_url, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
var t = data.split(",");
var city = t[0].substring(2,t[0].length)+","+t[1].substring(1,t[1].length-1);
var cost = t[2].substring(t[2].length-2).trim();
cost = cost.substring(0,cost.length-1);
var response = {peopleInputs:peopleInputs,venueInputs:venueInputs,city:city,cost:cost};
res.send(response);
});
}).on("error", (err) => {
console.log("Error");
});
//TODO: parse and return requested info
}catch (e) {
console.log(e);
res.end();
}
});
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year, month, day].join('-');
}