-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
489 lines (451 loc) · 14.9 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
'use strict'
//first we import our dependencies...
var express = require('express');
var mongoose = require('mongoose');
//var fs = require('fs');
//var grid = require('grid');
var nodeMailer = require('nodemailer');
var bodyParser = require('body-parser');
var User = require('./model/users');
var Comment = require('./model/comments')
var Group = require('./model/groups');
const jwt = require('express-jwt');
const jwks = require('jwks-rsa');
const cors = require('cors');
//and create our instances
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
var router = express.Router();
//set our port to either a predetermined port number if you have set it up, or 3001
var port = process.env.API_PORT || 3001;
//authorization method
const authCheck = jwt({
secret: jwks.expressJwtSecret({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
// YOUR-AUTH0-DOMAIN name e.g prosper.auth0.com
jwksUri: "https://tied.auth0.com/.well-known/jwks.json"
}),
// This is the identifier we set when we created the API
aud: 'https://tied.auth0.com/api/v2',
issuer: 'tied.auth0.com',
algorithms: ['RS256']
});
//db config
var mongoDB = 'mongodb://Jab123:[email protected]:25113/test1';
mongoose.connect(mongoDB)
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
//now we should configure the API to use bodyParser and look for JSON data in the request body
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
//To prevent errors from Cross Origin Resource Sharing, we will set our headers to allow CORS with middleware like so:
app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Credentials', 'true');
res.setHeader('Access-Control-Allow-Methods', 'GET,HEAD,OPTIONS,POST,PUT,DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers');
//and remove cacheing so we get the most recent comments
res.setHeader('Cache-Control', 'no-cache');
next();
});
//now we can set the route path & initialize the API
router.get('/', function(req, res) {
res.json({ message: 'API Initialized!'});
});
//adding the /comments router to our /api router
router.route('/comments')
//retrieve all comments from the database
.get(function(req, res) {
//looks at our comment schema
Comment.find(function(err, comments) {
if (err)
res.send(err);
//responds with a json object of our database comments.
res.json(comments)
});
})
//post new comments to the database
.post(function(req, res) {
var comment = new Comment();
//body parser lets us use the req.body
comment.author = req.body.author;
comment.text = req.body.text;
comment.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Comment successfully added!' });
});
});
router.route('/comments/:comment_id')
//The put method gives us the chance to update our comment based on
//the ID passed to the route
.put(function(req, res) {
Comment.findById(req.params.comment_id, function(err, comment) {
if (err)
res.send(err);
//setting the new author and text to whatever was changed. If
//nothing was changed we will not alter the field.
(req.body.author) ? comment.author = req.body.author : null;
(req.body.text) ? comment.text = req.body.text : null;
//save comment
comment.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Comment has been updated' });
});
});
})
//delete method for removing a comment from our database
.delete(function(req, res) {
//selects the comment by its ID, then removes it.
Comment.remove({ _id: req.params.comment_id }, function(err, comment) {
if (err)
res.send(err);
res.json({ message: 'Comment has been deleted' })
})
});
router.route('/users')
//retrieve all users from the database
.get(function(req, res) {
//looks at our user schema
User.find(function(err, users) {
if (err)
res.send(err);
//responds with a json object of our database users.
res.json(users)
});
},authCheck)
//post new users to the database
.post(function(req, res) {
var user = new User();
//body parser lets us use the req.body
user.user = req.body.user;
user.password = req.body.password;
user.firstname = req.body.firstname;
user.lastname = req.body.lastname;
user.email = req.body.email;
user.uniqueId = req.body.uniqueId;
user.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'User successfully added!' });
});
},authCheck);
router.route('/users/:user_id')
.get(function(req, res) {
var user_id = String(req.params.user_id);
var userFound = null;
User.find({uniqueId: user_id}, function(err, user) {
if (err)
res.send(err)
res.json(user[0]);
});
})
.post(function(req,res){
var user_id = Number(req.params.user_id);
var user = new User();
user._id = user_id;
user.save(function(err) {
if(err)
res.send(err);
});
})
.put(function(req,res){
User.find({uniqueId: req.params.user_id}, function(err, user){
if(err)
res.send(err);
(req.body.firstname) ? user[0].firstname = req.body.firstname : null;
(req.body.lastname) ? user[0].lastname = req.body.lastname : null;
(req.body.email) ? user[0].email = req.body.email : null;
user[0].save(function(err) {
if (err)
res.send(err);
res.json({ message: 'User successfully updated' });
})
})
});
router.route('/users/:user_id/rsvp')
.put(function(req, res) {
User.find({uniqueId: req.params.user_id}, function(err, user) {
if (err)
res.send(err);
if(user[0].rsvps.length > 0){
var groupIndex = user[0].rsvps.findIndex(x => x.group_id == req.body.groupId);
if(groupIndex > -1){
user[0].rsvps[groupIndex].events.unshift(req.body.eventId);
}
else{
user[0].rsvps.unshift({group_id: req.body.groupId, events:[req.body.eventId]});
}
}
else{
user[0].rsvps.unshift({group_id: req.body.groupId, events: [req.body.eventId]});
}
user[0].save(function(err) {
if (err)
res.send(err);
res.json({ message: 'User RSVP updated' })
})
})
})
router.route('/users/:user_id/unrsvp')
.put(function(req,res){
User.find({uniqueId:req.params.user_id}, function(err, user) {
if(err)
res.send(err);
if(user[0].rsvps.length > 0){
var index = user[0].rsvps.findIndex(x => x.group_id == req.body.groupId);
if(index > -1){
if(user[0].rsvps[index].events.length > -1){
var eventIndex = user[0].rsvps[index].events.indexOf(req.body.eventId);
user[0].rsvps[index].events.splice(eventIndex,1);
if(user[0].rsvps[index].events.length === 0){
user[0].rsvps.splice(index,1);
}
}
}
}
user[0].save(function(err){
if (err)
res.send(err);
res.json({ message: 'User RSVP updated'})
})
})
})
router.route('/users/:user_id/groupdeleteunrsvp')
.put(function(req,res){
User.find({uniqueId:req.params.user_id}, function(err, user) {
if(err)
res.send(err);
if(user[0].rsvps.length > 0){
var index = user[0].rsvps.findIndex(x => x.group_id == req.body.groupId);
if(index > -1){
user[0].rsvps.splice(index,1);
}
}
user[0].save(function(err){
if(err)
res.send(err);
res.json({ message: 'User RSVP updated'})
})
})
})
router.route('/groups')
//retrieve all groups from the database
.get((req,res) => {
//look at the group schema
Group.find(function(err, groups) {
if (err)
res.send(err);
res.json(groups)
});
},authCheck)
//post new group to the database
.post(function(req, res) {
var group = new Group();
group.name = req.body.name;
group.description = req.body.description;
group.img = req.body.img;
group.admins = req.body.admins;
group.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Group successfully added'});
});
},authCheck);
router.route('/groups/:group_id')
//retrieve a groups from the database
.get(function(req, res) {
//look at the group schema
Group.findById(req.params.group_id, function(err, group) {
if (err)
res.send(err);
res.json(group)
});
})
//The put method gives us the chance to add an event based on
//the ID passed to the route
.put(function(req, res) {
Group.findById(req.params.group_id, function(err, group) {
if (err)
res.send(err);
group.events.unshift(req.body);
group.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Event has been added' });
});
});
})
//delete method for removing a group from our database
.delete(function(req, res) {
//selects the group by its ID, then removes it.
Group.remove({ _id: req.params.group_id }, function(err, group) {
if (err)
res.send(err);
res.json({ message: 'Group has been deleted' })
})
});
router.route('/groups/:group_id/requestAdmin')
.put(function(req, res){
Group.findById(req.params.group_id, function(err, group){
if(err)
res.send(err);
if(group.rqadmins.indexOf(req.body.userId) < 0){
group.rqadmins.unshift(req.body.userId);
}
group.save(function(err){
if(err)
res.send(err);
res.json({ message: 'Request sent'})
});
});
});
router.route('/groups/:group_id/approveAdmin')
.put(function(req, res){
Group.findById(req.params.group_id, function(err, group){
if(err)
res.send(err);
if(group.rqadmins.indexOf(req.body.userId) > -1) {
group.rqadmins.splice(group.rqadmins.indexOf(req.body.userId),1);
if(group.admins.indexOf(req.body.userId) < 0) {
group.admins.unshift(req.body.userId);
}
}
group.save(function(err){
if(err)
res.send(err);
res.json({ message: 'User approved for admin'})
});
});
});
router.route('/groups/:group_id/rejectAdmin')
.put(function(req, res){
Group.findById(req.params.group_id, function(err, group){
if(err)
res.send(err);
if(group.rqadmins.indexOf(req.body.userId) > -1){
group.rqadmins.splice(group.rqadmins.indexOf(req.body.userId),1);
}
group.save(function(err){
if(err)
res.send(err);
res.json({ message: 'User rejected for admin'})
});
});
});
router.route('/groups/:group_id/edit')
//for editing groups
.put(function(req, res) {
Group.findById(req.params.group_id, function(err, group) {
if (err)
res.send(err);
(req.body.name) ? group.name = req.body.name : null;
(req.body.description) ? group.description = req.body.description : null;
group.save(function(err) {
if (err)
res.send(err)
res.json({ message: 'Group Updated'})
})
})
});
router.route('/groups/:group_id/Message')
//messaging to clients
.get(function(req,res) {
res.render('forms/MessageForm');
})
.post(function(req,res) {
let transporter = nodeMailer.createTransport({
host: 'smtp.gmail.com',
port:465,
secure: true,
auth: {
user:'[email protected]',
pass:'kirtSnig'
}
});
let mailOptions = {
from: '"NO REPLY-TIED." <[email protected]>',
to:'[email protected]',
bcc:req.body.to,
subject:req.body.subject,
html:req.body.body
};
transporter.sendMail(mailOptions, (error, info) => {
if(error) {
return console.log(Error);
}
console.log('Message %s sent: %s', info.messageId, info.response);
res.render('forms/MessageForm');
});
});
router.route('/groups/:group_id/:event_id')
//retrieve an event from a group in the database
.get(function(req, res) {
Group.findById(req.params.group_id, function(err, group) {
if (err)
res.send(err);
var index = group.events.findIndex(x => x._id == req.params.event_id);
res.json(group.events[index])
})
})
.put(function(req, res) {
Group.findById(req.params.group_id, function(err, group) {
if (err)
res.send(err);
var index = group.events.findIndex(x => x._id == req.params.event_id);
(req.body.name) ? group.events[index].name = req.body.name : null;
(req.body.description) ? group.events[index].description = req.body.description : null;
(req.body.date) ? group.events[index].date = req.body.date : null;
(req.body.time) ? group.events[index].time = req.body.time : null;
(req.body.loc) ? group.events[index].loc = req.body.loc : null;
(req.body.userId) ? group.events[index].attendees.unshift(req.body.userId) : null;
(req.body.img) ? group.events[index].img = req.body.img : null;
group.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Event has been updated' })
})
})
})
.delete(function(req, res) {
//selects the event by its ID, then removes it.
Group.findById(req.params.group_id, function(err, group) {
if (err)
res.send(err);
var index = group.events.findIndex(x => x._id == req.params.event_id);
group.events.splice(index, 1);
group.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Event has been deleted' })
})
})
});
router.route('/groups/:group_id/:event_id/unrsvp')
.put(function(req,res){
Group.findById(req.params.group_id, function(err, group) {
if(err)
res.send(err);
var index = group.events.findIndex(x => x._id == req.params.event_id);
var userIndex = group.events[index].attendees.indexOf(req.body.userId);
if(userIndex > -1){
group.events[index].attendees.splice(userIndex,1);
}
group.save(function(err){
if(err)
res.send(err);
res.json({ message: 'RSVP removed'})
})
})
});
//Use our router configuration when we call /api
app.use('/api', router);
//starts the server and listens for requests
app.listen(port, function() {
console.log(`api running on port ${port}`);
});