forked from cs4241-21a/final_project
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
407 lines (355 loc) · 11.9 KB
/
server.js
File metadata and controls
407 lines (355 loc) · 11.9 KB
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
// Import dependencies for properly running the server
const express = require("express"),
bodyparser = require("body-parser"),
morgan = require('morgan'),
mongodb = require( 'mongodb' ),
mongoose = require('mongoose'),
UserEntry = require('./models/loginModel.js'),
EventEntry = require('./models/eventModel.js'),
CalendarEntry = require('./models/calendarModel.js'),
cookie = require('cookie-session'),
app = express(),
staticDir = "build",
moment = require('moment'),
port = 8080;
require('dotenv').config();
const { json } = require("body-parser");
const {response, request} = require("express");
const uri = 'mongodb+srv://'+process.env.ACCOUNT+':'+process.env.PASS+'@'+process.env.HOST
//const uri =`mongodb+srv://${USER}:${PASS}@${HOST}/myFirstDatabase?retryWrites=true&w=majority`;
mongoose.connect(uri, {useNewUrlParser: true, useUnifiedTopology: true})
.then(result => app.listen(process.env.PORT || port))
.catch(err => console.log(err));
//register view engine
app.set('view engine','ejs');
// helpful bois
app.use(morgan('dev'));
app.use(express.urlencoded({extended: true}));
// Custom middleware for outputting an error code if the MongoDB server is down
/*app.use((req,res,next) => {
if(collection !== null) {
next()
}else{
res.status(503).send()
}
})*/
// cookie setup
app.use( cookie({
name: 'session',
keys: ['key1', 'key2'],
username: 'username'
}))
app.post('/signUp', async (req, res) => {
const entry = new UserEntry({
username: req.body.username,
password: req.body.password
})
// check if username exists
if (await checkUsername(req.body.username)) {
entry.save()
.then(result => {
res.send(result);
})
.catch(err => {
console.log(err);
});
res.render('login')
}
return;
//return error warning that username is already taken
})
async function checkUsername(user,){
let array = [];
await UserEntry.find({username: {$eq: user}})
.then(result =>{
array = result;
})
if (array.length >= 1 ){
return false;
}
return true;
}
app.post('/login', async (req, res) => {
console.log(req.body)
let validated = await checkUsernamePassword(req.body.username, req.body.password);
if (validated) {
req.session.login = true;
req.session.username = req.body.username;
res.redirect('/index');
} else {
res.render('login');
}
})
app.post('/loadEvents', async (req, res) =>{
EventEntry.find({attendees: {$in: req.session.username}})
.then(dbresponse =>{
//console.log("aaaa",dbresponse)
res.json(dbresponse)
})
})
app.get('/pendingEvents', (req, res) => {
EventEntry.find({$and: [{attendees: req.session.username}]}) // {owner: {$ne: req.session.username}}, {chosenEventDate: null}
.then(result => {
res.json({
events: result,
username: req.session.username,
})
})
})
app.get('/ownedEvents', (req, res) => {
EventEntry.find({owner: req.session.username})
.then(result => {
res.json({
events: result,
username: req.session.username,
})
})
})
Date.prototype.addDays = function(days) {
let date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
}
function getDates(startDate, stopDate) {
let dateArray = new Array();
let currentDate = startDate;
while (currentDate <= stopDate) {
console.log("CURR DATE: " + currentDate)
dateArray.push(new Date (currentDate));
currentDate = currentDate.addDays(1);
}
return dateArray;
}
app.post('/editEvent', bodyparser.json(), async(req, res) => {
EventEntry.findByIdAndUpdate(req.body.eventID, {chosenEventDate: req.body.chosenEventDate, chosenStartTime: req.body.chosenStartTime, location: req.body.location, description: req.body.description, attendees: req.body.attendees})
.then(result =>{
console.log(result)
res.sendStatus(200)
})
.catch(result => {
console.log(result)
res.sendStatus(504)
})
})
app.post('/addUserAvail', bodyparser.json(), async (req, res) => {
let userAvailObject = {name: req.session.username, personalLoaded: true, availability: req.body.attendeesAvailArray};
let eventData = await EventEntry.findById(req.body.eventID)
let availability = eventData.attendeesAvailability
let notInAvail = true
for(let i = 0; i < availability.length; i++) {
let currName = availability[i].name
if(currName === req.session.username) {
notInAvail = false
availability[i] = userAvailObject
break
}
}
if(notInAvail) {
availability.push(userAvailObject)
}
EventEntry.findByIdAndUpdate(req.body.eventID, {attendeesAvailability: availability})
.then(result => {
res.sendStatus(200)
})
})
app.post('/deleteEvent', bodyparser.json(), async(req, res) =>{
EventEntry.findByIdAndDelete(req.body.eventID)
.then(result =>{
res.sendStatus(200)
})
})
app.post('/createEvent', bodyparser.json(), async (req,res) => {
console.log(req.body);
let startDate = new Date(req.body.startDate);
console.log("START RAW: " + req.body.startDate)
console.log("START DATE: " + startDate)
let endDate = new Date(req.body.endDate);
let dateRange = getDates(startDate,endDate);
let timeRangeArray = [];
for (let i = 0; i < dateRange.length; i++){
timeRangeArray.push(req.body.timeRange);
}
let fullAttendees = []
fullAttendees.push(req.session.username)
for(let count = 0; count<req.body.attendees.length; count++){
if(req.body.attendees[count] !== "")fullAttendees.push(req.body.attendees[count])
}
let attendeesAvailList = [];
const entry = new EventEntry({
owner: req.session.username,
eventName: req.body.title,
availableDates: dateRange,
availableTimes: timeRangeArray,
attendees: fullAttendees,
attendeesAvailability: attendeesAvailList,
meetingDuration: req.body.duration,
description: req.body.description,
chosenEventDate: null,
location: req.body.location
})
await entry.save()
let result = await EventEntry.find({})
res.render('events', {eventsList: result, sentUsername: req.session.username, title:"Events"})
})
app.post('/refreshpersonal', async(req,res) => {
CalendarEntry.find({username: {$eq: req.session.username}})
.then(dbresponse =>{
//console.log(dbresponse)
let jsonRes = []
for(let i = 0; i < dbresponse.length; i++){
let calItem = {}
calItem.start = dbresponse[i].startDateTime.toISOString()
calItem.end = dbresponse[i].endDateTime.toISOString()
calItem.title = dbresponse[i].eventName
calItem.location = dbresponse[i].location
calItem.description = dbresponse[i].description
calItem.id = dbresponse[i]._id.toString()
jsonRes.push(calItem)
}
//console.log(jsonRes)
res.json(jsonRes)
})
})
app.post('/addpersonal', async(req, res) => {
let dbEntry = new CalendarEntry({
username: req.session.username,
eventName: req.body.eventName,
recurring: false,
startDateTime: new Date(req.body.startDateTime),
endDateTime: new Date(req.body.endDateTime),
location: req.body.location,
description: req.body.description
})
await dbEntry.save()
res.redirect('/index')
})
app.post('/removepersonal', bodyparser.json(), async(req,res) => {
console.log(req.body)
CalendarEntry.deleteOne({_id: req.body.id})
.then(response => {
res.render('index')
})
})
app.post('/addToOthersPersonal', bodyparser.json(), async(req, res) => {
let dbEntry = new CalendarEntry({
username: req.body.attendeeName,
eventName: req.body.eventName,
recurring: false,
startDateTime: new Date(req.body.startDateTime),
endDateTime: new Date(req.body.endDateTime),
location: req.body.location,
description: req.body.description
})
await dbEntry.save()
res.render('index')
})
app.post('/getavailabilityfrompersonal', bodyparser.json(), async(req,res) => {
EventEntry.findById(req.body.eventID)
.then(async dbresponse => {
let dates = dbresponse.availableDates
console.log("dates: " + dates)
let times = dbresponse.availableTimes //need to use times[dateIndex][timeIndex] to access time list
console.log("times: " + times)
let duration = dbresponse.meetingDuration
console.log("duration: " + duration)
let attAv = dbresponse.attendeesAvailability
let availabilityArray = []
for(let dateIndex = 0; dateIndex<dates.length; dateIndex++){
for(let timeIndex = 0; timeIndex<times[dateIndex].length; timeIndex++){
console.log("dateIndex: " + dateIndex)
console.log("timeIndex: " + timeIndex)
console.log("times length:" + times.length)
let startDateTime = new Date(dates[dateIndex])
if(duration === 0.5){ //if duration is 0.5
startDateTime.setHours(Math.floor(times[dateIndex][timeIndex])) //need to get the floor to always get hour value only
console.log("modulo: " + times[dateIndex][timeIndex]%1)
if(times[dateIndex][timeIndex]%1 === 0){ //if we're on a whole hour
startDateTime.setMinutes(0)
}else{ //if we're on a half hour
startDateTime.setMinutes(30)
}
}else if(times[dateIndex][timeIndex]%1 === 0){ //or we're on a whole hour
startDateTime.setHours(times[dateIndex][timeIndex])
}
let endDateTime = new Date(startDateTime.getTime() + (duration*60*60*1000)) //lol janky math
await CalendarEntry.find({username: {$eq: req.session.username}, startDateTime: {$lt: endDateTime}, endDateTime: {$gte: startDateTime}})
.then(dbresponse => {
console.log(req.session.username + ", " + startDateTime + ", " + endDateTime)
if(dbresponse.length === 0){ //no results found, user is available
console.log("dbresponse" + dbresponse)
availabilityArray.push(startDateTime)
}
})
}
}
let newAvObj = {
name: req.session.username,
availability: availabilityArray,
personalLoaded: false,
}
attAv.push(newAvObj)
console.log(availabilityArray)
res.json(availabilityArray)
/* EventEntry.findByIdAndUpdate(req.body.eventID, {attendeesAvailability: attAv})
.then(result =>{
res.json(availabilityArray)
}) */
})
})
async function checkUsernamePassword(user, pass){
let array = [];
await UserEntry.find({username: {$eq: user}, password: {$eq:pass}})
.then(result =>{
array = result;
})
if (array.length >= 1 ){
return true;
}
return false;
}
app.use( function( req,res,next) {
if(req.url === '/signUpPage'){
res.render('signUpPage');
return;
}
if( req.session.login === true || req.url === '/css/style.css' || req.url === '/css/sakura.css') {
next()
} else {
res.render('login')
}
})
// Serve static files when necessary
app.use(express.static(staticDir));
app.use((req,res,next) => {
res.locals.path = req.path;
next();
});
// Setup handling of GET requests for homepage route
app.get('/', (req, res) => {
res.redirect('/index');
})
app.get('/index', (req, res) =>{
res.render('index');
})
app.get('/events', (req, res) => {
res.render('events', {sentUsername: req.session.username, title:"Events"});
})
app.get('/myEvents', (req, res) => {
res.render('myEvents', {sentUsername: req.session.username, title:"Events"});
})
app.get('/eventInvites', (req, res) => {
res.render('eventInvites', {sentUsername: req.session.username, title:"Events"});
})
app.get('/signUpPage', (req,res) =>{
res.render('signUpPage');
})
app.get('/login', (req,res) => {
res.render('login', {title:"Login Page"})
})
app.get('/addpersonal', (req,res) => {
res.render('addpersonal');
})
// 404 page
app.use((req,res) => {
res.status(404).render('404',{title: '404'})
})