-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
385 lines (338 loc) · 10.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
// Npm packages
const express = require('express');
const bodyParser = require('body-parser');
const fileUpload = require('express-fileupload');
const cloudinary = require('cloudinary');
const jwt = require('express-jwt');
const jwks = require('jwks-rsa');
const cors = require('cors');
const axios = require('axios');
// Local files
const Message = require('./models/Message');
const User = require('./models/User');
const Pet = require('./models/Pet');
const Photo = require('./models/Photo');
const Activity = require('./models/Activity');
const ToDoList = require('./models/ToDoList');
const Review = require('./models/Review');
const google = require('./config/googlemaps.api');
const cloud = require('./config/cloudinary.api');
const config = ('./config/');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const { NODE_ENV } = process.env;
// Check for environment variables, set port accordingly
const port = NODE_ENV === 'development' ? 9000 : 80;
// Need this to serve our bundled index.html
app.use(express.static(`${__dirname}/dist`));
// Need this to serve the logo picture
app.use(express.static(`${__dirname}/client/assets`));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(fileUpload());
app.use(cors());
cloudinary.config({
cloud_name: 'derywsrky',
api_key: cloud.token,
api_secret: cloud.secret,
});
const authCheck = jwt({
secret: jwks.expressJwtSecret({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
jwksUri: 'https://findo.auth0.com/.well-known/jwks.json',
}),
audience: config.url,
issuer: 'https://findo.auth0.com/',
algorithms: ['RS256'],
});
// /////////////////////////////////////////////////////////////////////////
// socket io
io.on('connection', (socket) => {
console.log('User connected');
console.log(socket.id);
socket.on('disconnect', () => {
console.log('User disconnected');
});
socket.on('save-message', (data) => {
console.log(data);
io.emit('new-message', { message: data });
});
});
// //////////////////////////////////////////////////////////////////////
// Takes in information about the pet, puts it in a pet table with a link to the user
app.post('/petSignup', (req, res) => {
const userEmail = req.body.profile.email;
const {
kind,
name,
characteristics,
place,
} = req.body;
Pet.createPet(name, kind, characteristics, place, userEmail, (err, pet) => {
if (err) {
res.status(500).send(err);
} else {
res.status(201).send(pet);
}
});
});
// Creates a new user
app.post('/personSignup', (req, res) => {
const { email } = req.body.profile;
const {
name,
address,
city,
state,
zip,
extra,
} = req.body;
User.createUser(name, email, address, city, state, zip, extra, (err, response) => {
if (err) {
res.status(500).send(err);
} else {
res.status(201).send(response);
}
});
});
// Updates profile information
app.put('/profile', (req, res) => {
const userEmail = req.body.profile.email;
const { updateKey, updateValue } = req.headers;
User.updateUser(userEmail, updateKey, updateValue, (err, updated) => {
if (err) {
res.status(404).send(err);
} else {
res.status(201).send(updated);
}
});
});
// Deletes a profile
app.delete('/profile', (req, res) => {
const userEmail = req.body.profile.email;
User.deleteUser(userEmail, (err, result) => {
if (err) {
res.status(404).send(err);
} else {
res.status(202).send(result);
}
});
});
// Saves chat messages to the database
app.post('/chat', (req, res) => {
const userEmail = req.body.profile.email;
const { text, room } = req.body;
// Store message in databse
Message.createMessage(text, room, userEmail, (err, result) => {
// Send message to both users, using socket.io
if (err) {
res.status(500).send(err);
} else {
res.send(result);
}
});
});
app.get('/chat/:email/:room', (req, res) => {
const { email, room } = req.params;
Message.findUserMessages(email, room, (err, messages) => {
if (err) {
res.status(404).send(err);
} else {
res.status(200).send(messages);
}
});
});
// Saves reviews to the database
app.post('/review', (req, res) => {
const userEmail = req.body.profile.email;
const {
punctuality,
friendliness,
overall,
comments,
} = req.body;
Review.createReview(userEmail, punctuality, friendliness, overall, comments, (err, review) => {
if (err) {
console.error(err);
res.status(404).send(err);
} else {
res.status(201).send(review);
}
});
});
// Write route to get activities at a certain time for the schedule component
app.get('/activities/:date', (req, res) => {
const time = req.params.date;
Activity.getActivitiesByTime(time, (err, activities) => {
if (err) {
res.status(404).send(err);
} else {
res.status(200).send(activities);
}
});
});
// Adds an activity to the database
app.post('/activities', (req, res) => {
const userEmail = req.body.profile.email;
const {
location,
time,
date,
} = req.body;
Activity.createActivity(userEmail, location, time, date, (err, result) => {
if (err) {
res.status(500).send(err);
} else {
res.status(201).send(result);
}
});
});
// Adds a to do to the database
app.post('/todo', (req, res) => {
console.log(req.body);
// Update this one a lot
const userEmail = req.body.profile.email;
const {
location,
time,
date,
emailPerson,
} = req.body;
ToDoList.createToDoList(userEmail, location, time, date, emailPerson, (err, result) => {
if (err) {
res.status(500).send(err);
} else {
res.status(201).send(result);
}
});
});
app.get('/todo/:id', (req, res) => {
const email = req.params.id;
ToDoList.getUserToDoList(email, (err, todolist) => {
if (err) {
res.status(404).send(err);
} else {
res.status(200).send(todolist);
}
});
});
// Sends information to fill out the individual dashboard person dashboard and pet dashboard
app.get('/dashboard/:email', (req, res) => {
const userEmail = req.params.email;
// Gets user information based on email
User.getUser(userEmail, (err, userInfo) => {
if (err) console.error(err);
// Gets pet information based on returned user id
Pet.getPet(userEmail, (error, petInfo) => {
if (error) console.error(error);
// Gets activities for that user
Activity.getUserActivities(userEmail, (er, activities) => {
if (er) console.error(er);
// Puts them in object and sends to user
const result = { userInfo, petInfo, activities };
res.status(200).send(result);
});
});
});
});
// Populate the person dashboard
app.get('/PersonDashboard/:email', (req, res) => {
const userEmail = req.params.email;
User.getUser(userEmail, (err, userInfo) => {
if (err) console.error(err);
ToDoList.getUserToDoList(userEmail, (er, todolist) => {
if (er) console.error(er);
Activity.getUserActivities(userEmail, (error, activities) => {
if (error) console.error(error);
const result = { userInfo, todolist, activities };
res.status(200).send(result);
});
});
});
});
// Populate the user profile page
app.get('/userProfile/:email', (req, res) => {
const userEmail = req.params.email;
// Gets user information based on email
User.getUser(userEmail, (err, userInfo) => {
if (err) console.error(err);
// Gets pet information based on returned user id
Pet.getPet(userEmail, (error, petInfo) => {
if (error) console.error(error);
// Gets photos for that user
Photo.findUserPhotos(userEmail, (er, photos) => {
if (er) console.error(er);
// Puts them in object and sends to user
const result = { userInfo, petInfo, photos };
res.status(200).send(result);
});
});
});
});
// Populate the pet dashboard
app.get('/petDashboard/:id', (req, res) => {
const petId = req.params.id;
Pet.getPetId(petId, (err, pet) => {
if (err) {
res.status(404).send(err);
} else {
res.status(200).send(pet);
}
});
});
// Recieves a file upload, adds it to cloudinary, then adds to the database
app.post('/photos*', (req, res) => {
// The format of the incoming file is a bit weird, has to be encoded to base64
const newPhoto = req.files['uploads[]'].data.toString('base64');
const type = req.files['uploads[]'].mimetype;
const userEmail = req.params[0];
// Uploads to cloudinary
cloudinary.v2.uploader.upload(`data:${type};base64,${newPhoto}`, (err, photo) => {
if (err) {
res.status(400).send(err);
} else {
// The url in Cloudinary's response is put into the database for use
Photo.addPhoto(photo.url, userEmail, (error, response) => {
if (error) {
res.status(500).send(error);
} else {
// The new photo entry is sent back to the client to be rendered
res.status(201).send(response);
}
});
}
});
});
// Gets user photos for their profile page
app.get('/photos/:email', (req, res) => {
const userEmail = req.params.email;
Photo.findUserPhotos(userEmail, (er, photos) => {
if (er) console.error(er);
// Gets activities for that user
Activity.getUserActivities(userEmail, (error, activities) => {
if (error) console.error(error);
// Puts them in object and sends to user
const result = { photos, activities };
res.status(200).send(result);
});
});
});
// Gets the lat/long location for the map
app.post('/map', (req, res) => {
const { location } = req.body;
axios.get(`https://maps.googleapis.com/maps/api/geocode/json?address=${location},+New+Orleans,+LA&key=${google.token}`)
.then(response => res.status(200).send(response.data.results[0].geometry.location))
.catch(err => res.status(500).send(err));
});
if (NODE_ENV === 'production') {
app.get('/*', (req, res) => {
res.sendFile(express.static(`${__dirname}/dist/index.html`));
});
}
// Open our connection
http.listen(port, () => {
console.log(`App is listening on ${port}`);
});