forked from sitnpaws/sit-n-paws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
517 lines (486 loc) · 18.3 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
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
const express = require('express');
const MongoClient = require('mongodb').MongoClient;
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const ObjectId = mongoose.Types.ObjectId;
const moment = require('moment');
const socket = require('socket.io');
const socketChat = require('./socketChat');
const User = require('./db/models/users');
const Listing = require('./db/models/listing');
const Stay = require('./db/models/stays');
const { Msg, Chat } = require('./db/models/chat');
const jwt = require('jsonwebtoken');
const cloudinary = require('cloudinary');
const cloudConfig = require('./cloudinary/config.js');
const multer = require('multer');
const nodemailer = require('nodemailer');
const upload = multer({dest: './uploads/'});
const debug = process.env.DEBUG || true;
const httpPort = process.env.PORT || 8080;
// This is the shape of the object from the config file which is gitignored
// const cloudConfig = {
// cloud_name: 'top-hat',
// api_key: 'API_KEY',
// api_secret: 'API_SECRET'
// };
cloudinary.config(cloudConfig);
const app = express();
app.use(express.static((__dirname + '/src/public')));
app.use(bodyParser.json());
const JWT_KEY = 'who let the dogs in?!';
const EMAIL_AUTH = {user: '[email protected]', pass: 'sitnpaws13'};
//============= AUTHENTICATION HELPER =============\\
const jwtAuth = (req, res, next) => {
const token = req.headers.authorization;
if (!token) { res.status(401).send(); return; }
try { // token validation
req.tokenPayload = jwt.verify(token, JWT_KEY);
next();
} catch(err) {
res.status(401).send();
return;
}
};
//============= EMAIL NOTIFICATION HELPER =============\\
const sendStayRequestMail = (hostEmail, guestEmail, startDate, endDate) => {
const transporter = nodemailer.createTransport({service: 'gmail', auth: EMAIL_AUTH});
const mailOptions = {
to: hostEmail,
subject: 'Hi from Sit-n-Paws! A friend wants to stay at your house from ' +
moment(startDate).format('LL') + ' to ' + moment(endDate).format('LL'),
text: 'Email the pet owner @ ' + guestEmail +
' to discuss specifics, and login to approve or reject the stay. Please respond within 24 hours!',
};
transporter.sendMail(mailOptions).then((info) => {
if (debug) { console.log('Nodemailer details: ', info); }
});
}
//handles log in information in the db, creates jwt
app.post('/api/login', (req, res) => {
var email = req.body.email;
var password = req.body.password;
User.findOne({ email: email })
.exec((err, found) => {
if (err) {
throw err;
console.log('error');
} else {
if (found) {
found.comparePassword(password).then(match => {
if (match) {
let payload = {
email: found.email,
name: found.name
};
let token = jwt.sign(payload, JWT_KEY);
res.json({
success: true,
email: found.email,
name: found.name,
token: token
});
}
})
} else {
res.send(JSON.stringify({
success: false,
error: 'Invalid Login/Password'
}));
}
}
})
});
//handles new user creations in db
app.post('/api/signup', (req, res) => {
var name = req.body.name;
var password = req.body.password;
var email = req.body.email;
User.findOne({ email: email })
.exec((err, found) => {
if (err) {
throw err;
console.log('error');
}
if (found) {
res.send(JSON.stringify({
success: false,
error: 'User already exists!',
}));
} else {
User.create({
password: password,
email: email,
name: name,
phone: '',
address: ''
})
.then((newUser) => {
let payload = {
name: newUser.name,
email: newUser.email
};
let token = jwt.sign(payload, JWT_KEY);
res.json({
success: true,
name: newUser.name,
email: newUser.email,
token: token
});
})
.catch((err) => {
console.log(err);
})
}
})
});
//handles fetching profiles in db
app.get('/api/profile', jwtAuth, (req, res) => {
let email = req.tokenPayload.email;
User.findOne({email: email}, function(err, user) {
if(err) {
console.log(err);
} else {
console.log(user);
res.json(user);
}
})
});
//handles updating profiles in db
app.put('/api/profile', jwtAuth, (req, res) => {
let email = req.tokenPayload.email;
let updateProfile = {
name: req.body.name,
phone: req.body.phone,
address: req.body.address
};
User.findOneAndUpdate({email: email}, updateProfile, { new: true}).then((updated) => {
let payload = { name: updated.name, email: updated.email };
let token = jwt.sign(payload, JWT_KEY);
res.json({
success: true,
name: updated.name,
email: updated.email,
token: token
});
}).catch((err) => { res.json('error: ', err); });
});
//middleware from multer - Check post listing for uploaded files and stores in req.files
const listingsUpload = upload.fields([{
name: 'hostPictures',
maxCount: 1
}, {
name: 'homePictures',
maxCount: 1
}]);
const listingLocalHandler = (req, res, next) => {
const { email: userEmail } = req.tokenPayload;
User.findOne({email: userEmail}).then(user => {
if (!user) { throw new Error('User not found'); }
return user;
}).then(user => {
Listing.findOne({email: req.body.email}).then(listing => {
if (listing) {
Listing.update(req.body);
res.json({success: true, message: 'Thank you, your listing has been successfully updated!', listing: listing});
next();
} else {
var newListing = new Listing({
userId: user._id,
name: req.body.name,
email: req.body.email,
zipcode: req.body.zipcode,
dogSizePreference: req.body.dogSizePreference,
dogBreedPreference: req.body.dogBreedPreference,
dogActivityPreference: req.body.dogActivityPreference,
hostPictures: 'Image is being uploaded...',
homePictures: 'Image is being uploaded...',
cost: req.body.cost
});
return newListing.save();
}
}).then(newListing => {
res.json({success: true, message: 'Thank you, your listing has been successfully saved!', listing: newListing});
next();
}).catch(err => {
console.log('Listing local handler caught error: ', err);
res.status(500).json({success: false, message: err});
});
});
}
const listingCloudinaryHandler = (req, res, next) => {
// Sends files to the Cloudinary servers and updates entries in the database
if (req.files.hostPictures) {
if (debug) { console.log('Send to cloudinary!', req.files.hostPictures[0].path); }
cloudinary.v2.uploader.upload(req.files.hostPictures[0].path, (err, result) => {
if(err) { console.log('Cloudinary error: ', err); }
if (debug) { console.log('Host Picture url: ', result.url); }
Listing.findOneAndUpdate({email: req.body.email}, {hostPictures: result.url}, (err, found) => {
if (err) { console.log(err); }
if (debug) { console.log('Updated Host Pictures: ', found); }
});
});
}
if (req.files.homePictures) {
if (debug) { console.log('Send to cloudinary!', req.files.homePictures[0].path); }
cloudinary.v2.uploader.upload(req.files.homePictures[0].path, (err, result) => {
if (err) { console.log('Cloudinary error: ', err); }
if (debug) { console.log('Home Picture url: ', result.url); }
Listing.findOneAndUpdate({email: req.body.email}, {homePictures: result.url}, (err, found) => {
if (err) { console.log(err); }
if (debug) { console.log('Updated Home Pictures: ', found); }
});
});
}
}
app.post('/api/listings', jwtAuth, listingsUpload, listingLocalHandler, listingCloudinaryHandler);
//handles getting all listings that exist
app.get('/api/listings', (req, res) => {
Listing.find({})
.exec((err, listings) => {
if (err) {
console.log('error');
} else {
res.send(listings);
}
})
});
//handles getting listings by zipcode from search
app.get('/api/listings/:zipcode', (req, res) => {
var zipcode = req.params.zipcode;
Listing.find({ "$where": `function() { return this.zipcode.toString().match(/${zipcode}/) !== null; }`})
.exec((err, listings) => {
if (err) {
console.log(err);
} else {
res.send(listings);
}
})
});
app.get('/api/stays', jwtAuth, (req, res) => {
if (debug) { console.log('request received...'); }
const { email: userEmail } = req.tokenPayload;
User.findOne({email: userEmail}).then(user => {
if (!user) {
return res.status(400).send('User not found');
} else {
const userId = user._id;
const hostStays = Stay.find({hostId: ObjectId(userId)}).populate('listing', 'name zipcode', 'Listing').exec();
const guestStays = Stay.find({guestId: ObjectId(userId)}).populate('listing', 'name zipcode', 'Listing').exec();
return Promise.all([hostStays, guestStays]).then(([hostStays, guestStays]) => {
let stays = [];
hostStays.forEach(stay => stays.push(Object.assign({role: 'host'}, stay.toObject())));
guestStays.forEach(stay => stays.push(Object.assign({role: 'guest'}, stay.toObject())));
res.status(200).json(stays);
});
}
}).catch(err => {
console.log('Server error: ', err);
res.status(500).send('Oops! Server error.');
});
});
app.post('/api/stays', jwtAuth, (req, res) => {
const { email: guestEmail } = req.tokenPayload;
const { listingId, startDate, endDate } = req.body;
if (!listingId || !startDate || !endDate ) { res.status(400).send('bad request'); return; }
let hostEmail = '';
let listing = Listing.findById(listingId).exec();
let guest = User.findOne({email: guestEmail}).exec();
const newStay = Promise.all([listing, guest]).then(([listing, guest]) => {
if (!listing || !guest) {
return res.status(400).send('Listing or guest not found.');
} else {
const days = Math.max(moment(endDate).diff(moment(startDate), 'days'), 1);
hostEmail = listing.email;
let newStay = new Stay({
listing: listingId,
hostId: listing.userId,
guestId: guest._id,
startDate: startDate,
endDate: endDate,
status: 'pending',
pricePer: listing.cost,
totalPrice: Math.floor(listing.cost*(days)),
});
return newStay.save();
}
}).then(stay => {
return new Chat({stay: stay._id, host: stay.hostId, guest: stay.guestId }).save();
}).then(chat => {
if (hostEmail) { sendStayRequestMail(hostEmail, guestEmail, startDate, endDate); }
res.status(201).json({message: 'stay created', stayId: chat.stay});
}).catch(err => {
console.log('Server error: ', err);
res.status(500).send('Oops! Server error.');
});
});
//TODO: send email notifications on stay update
app.put('/api/stay/cancel/:stayId', jwtAuth, (req, res) => {
Stay.findById(req.params.stayId).exec().then(stay => {
if (!stay) { throw new Error('Stay not found'); }
if (stay.status === 'complete') { throw new Error('Cannot modify a complete stay'); }
if (stay.status === 'rejected') { throw new Error('Cannot cancel a rejected stay'); }
return stay.update({status: 'cancelled'}).exec();
}).then(() => res.status(200).json({stayId: req.params.stayId}))
.catch(err => res.status(400).send(err.message));
});
app.put('/api/stay/approve/:stayId', jwtAuth, (req, res) => {
const stayId = req.params.stayId;
const user = User.findOne({email: req.tokenPayload.email}).exec();
const stay = Stay.findById(stayId).exec();
Promise.all([user, stay]).then(([user, stay]) => {
if (!stay) { throw new Error('Stay not found'); }
if (!user) { throw new Error('User not found'); }
if (stay.status === 'complete') { throw new Error('Cannot modify a complete stay'); }
if (!stay.hostId.equals(user._id)) { throw new Error('Only host may approve or reject a stay'); }
return stay.update({status: 'approved'}).exec();
}).then(() => res.status(200).json({stayId: req.params.stayId}))
.catch(err => res.status(400).send(err.message));
});
app.put('/api/stay/reject/:stayId', jwtAuth, (req, res) => {
const stayId = req.params.stayId;
const user = User.findOne({email: req.tokenPayload.email}).exec();
const stay = Stay.findById(stayId).exec();
Promise.all([user, stay]).then(([user, stay]) => {
if (!stay) { throw new Error('Stay not found'); }
if (!user) { throw new Error('User not found'); }
if (stay.status === 'complete' || stay.status === 'approved') {
throw new Error('Cannot reject an approved or complete stay');
}
if (!stay.hostId.equals(user._id)) { throw new Error('Only host may approve or reject a stay'); }
return stay.update({status: 'rejected'}).exec();
}).then(() => res.status(200).json({stayId: req.params.stayId}))
.catch(err => res.status(400).send(err.message));
});
app.put('/api/stay/rating/:stayId', jwtAuth, (req, res) => {
const stayId = req.params.stayId;
const role = req.body.role;
const rating = req.body.rating;
const user = User.findOne({email: req.tokenPayload.email}).exec();
const stay = Stay.findById(stayId).exec();
Promise.all([user, stay]).then(([user, stay]) => {
if (!stay) { throw new Error('Stay not found'); }
if (!user) { throw new Error('User not found'); }
// if (stay.status !== 'complete') { throw new Error('Cannot rate an open stay'); }
if (stay.guestId.equals(user._id)) { return stay.update({listingRating: rating}) }
if (stay.hostId.equals(user._id)) { return stay.update({guestRating: rating}) }
}).then(() => res.status(200).json({rating: req.body.rating}))
.catch(err => res.status(400).send(err.message));
});
app.get('/api/stay/rating/host/:userId', jwtAuth, (req, res) => {
const userId = req.params.userId;
let name;
User.findById(userId).then((user) => {
name = user.name;
}).then(() => {
return Stay.find({hostId: ObjectId(userId)}).exec();
}).then((stays) => {
let ratingTotal = 0;
let ratingCount = 0;
stays.forEach((stay) => {
if (stay.listingRating) {
ratingTotal += stay.listingRating;
ratingCount++;
}
});
let avgRating = (ratingTotal / ratingCount).toFixed(1);
res.status(200).json({name: name, rating: avgRating > 0 ? avgRating : 0})
}).catch((err) => {
res.status(400).send(err.message);
});
});
app.get('/api/stay/rating/guest/:userId', jwtAuth, (req, res) => {
const userId = req.params.userId;
let name;
User.findById(userId).then((user) => {
name = user.name;
}).then(() => {
return Stay.find({guestId: ObjectId(userId)}).exec();
}).then((stays) => {
let ratingTotal = 0;
let ratingCount = 0;
stays.forEach((stay) => {
if (stay.guestRating) {
ratingTotal += stay.guestRating;
ratingCount++;
}
});
let avgRating = (ratingTotal / ratingCount).toFixed(1);
res.status(200).json({name: name, rating: avgRating > 0 ? avgRating : 0})
}).catch((err) => {
res.status(400).send(err.message);
});
});
app.get('/api/messages/:stayId', jwtAuth, (req, res) => {
let userId;
const user = User.findOne({email: req.tokenPayload.email}).exec().then(user => {
if (!user) { throw new Error('User not found'); }
userId = user._id;
}).then(() => Stay.findById(req.params.stayId).exec()).then(stay => {
if (!(userId.equals(stay.hostId) || userId.equals(stay.guestId))) {
throw new Error('Only host or guest may participate in chat');
}
return Chat.findOne({stay: req.params.stayId}).exec();
}).then(chat => {
let msg = Msg.find({chatId: chat._id});
if (req.query.before) { msg = msg.where('createdAt').lt(req.query.before); }
if (req.query.after) { msg = msg.where('createdAt').gt(req.query.after); }
msg.sort('-createdAt').limit(20).populate('user', '_id name').exec().then(msgs => {
res.status(200).json(msgs);
});
}).catch(err => res.status(400).send(err.message));
});
app.get('/api/chat/:stayId', jwtAuth, (req, res) => {
let user = req.tokenPayload;
let resp = {stay: {id: req.params.stayId}};
User.findOne({email: req.tokenPayload.email}).exec().then(foundUser => {
if (!foundUser) { throw new Error('User not found'); }
user.id = foundUser._id;
}).then(() => Stay.findById(resp.stay.id).populate('listing', 'name', 'Listing').exec())
.then(stay => {
if (!(user.id.equals(stay.hostId) || user.id.equals(stay.guestId))) {
throw new Error('Only host or guest may participate in chat');
}
resp.stay.listing = stay.listing;
return Chat.findOne({stay: req.params.stayId}).exec();
}).then(chat => {
resp.user = {id: user.id, name: user.name }
resp.chatId = chat._id;
if (user.id.equals(chat.host)) { // user is host
return User.findById(chat.guest).then(guest => {
resp.user.role = 'host';
resp.other = {id: guest._id, name: guest.name, role: 'guest'};
});
} else { // user is guest so go find the host
return User.findById(chat.host).then(host => {
resp.user.role = 'guest';
resp.other = {id: host._id, name: host.name, role: 'host'};
});
}
}).then(() => res.status(200).json(resp))
.catch(err => res.status(400).send(err.message));
});
app.post('/api/messages/:stayId', jwtAuth, (req, res) => {
const user = User.findOne({email: req.tokenPayload.email}).exec();
const chat = Chat.findOne({stay: req.params.stayId}).exec();
Promise.all([user, chat]).then(([user, chat]) => {
if (!user) { throw new Error('User not found'); }
if (!chat) { throw new Error('Chat not found'); }
if (!(user._id.equals(chat.guest) || user._id.equals(chat.host))) {
throw new Error('Only host or guest may participate in chat');
}
let newMsg = new Msg({ text: req.body.text, user: user._id, chatId: chat._id});
return newMsg.save();
}).then(() => {
res.status(201).send('Message created');
}).catch(err => res.status(400).send(err.message));
});
app.get('*', (req, res) => {
res.sendFile(__dirname + '/src/public/index.html');
})
const server = app.listen(httpPort, () => {
console.log('Listening on localhost on ', httpPort);
});
const io = socket(server);
socketChat(io);
module.exports = app;