-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
291 lines (226 loc) · 9.25 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
// Get the packages
var config = require('./config/main');
var express = require('express');
var mongoose = require ('mongoose');
var bodyParser = require('body-parser');
var twilio = require ('twilio')(config.TWILIO_ACCOUNT_SID, config.TWILIO_AUTH_TOKEN);
var stripe = require("stripe")(config.STRIPE_KEY);
var jwt = require("jsonwebtoken");
var passport = require('passport');
// Loading models
var User = require('./models/user');
var Address = require('./models/address');
var Order = require('./models/order');
// Loading controllers
var userController = require('./controllers/user.js');
var paymentController = require('./controllers/payment.js');
var addressController = require('./controllers/address.js');
var orderController = require('./controllers/order.js');
var cheesePrice = 3.49
var pepperoniPrice = 3.99
// Create express application
var app = express();
// Use environment defined port
var port = process.env.PORT;
// Create express router
var router = express.Router();
// Use body-parser package in application
app.use(bodyParser.urlencoded({
extended: true
}));
//initialize passport for use
app.use(passport.initialize());
// connect to mongoDB
mongoose.connect(config.MONGO_CONNECTION_URL);
// Bring in passport strategy
require('./config/passport')(passport);
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type, Authorization');
next();
});
// --------------------------------------- API ROUTES -----------------------------------
router.route('/users')
.post(userController.postUsers);
router.route('/users/login')
.post(userController.login);
router.route('/users/addEmail/:user_id')
.all(passport.authenticate('jwt', { session: false }))
.post(userController.addEmail);
router.route('/users/wantsReceipts/:user_id')
.all(passport.authenticate('jwt', { session: false }))
.post(userController.wantsReceipts);
router.route('/users/wantsConfirmation/:user_id')
.all(passport.authenticate('jwt', { session: false }))
.post(userController.wantsConfirmation);
router.route('/users/hasSeenTutorial/:user_id')
.all(passport.authenticate('jwt', { session: false }))
.post(userController.hasSeenTutorial);
router.route('/sendCode')
.post(userController.sendCode);
// Method: Generate and send a 6 digit code used for password reset
// Preconditions:
// Needs "phone" in body of request to look up user
// Postconditions:
// Returns data of Twilio text sent to phone number that was
// given IF that phone number exists for a user in the database.
// That phone # is texted a 6 digit code that is saved to the user's profile.
router.route('/sendPassCode')
.post(userController.sendPassCode);
// Method: Reset Password with 6 digit code (that was texted to user in /sendPassCode method)
// Preconditions:
// Must use /sendPassCode method before this method so that a 6 digit code is generated and saved to user's profile
// Needs "phone" in body of request to look up user
// Needs "code" in body of request to compare 6 digit code input by user to the code that was assigned to the user in /sendPassCode method
// Needs "password" in body of request to serve as the new password the user wants it reset to
// Postconditions:
// If the code matches the phone # then their password is changed to the password they input.
// If there is no user w/ the phone number they input, an err is returned.
// If the code does not match, JSON is returned saying {success: false, message: "Code does not match"}
router.route('/resetPass')
.post(userController.resetPass);
// Method: Authenticate a user with a JWT
// Preconditions:
// Needs "phone" in body of request that acts as username for user profile
// Needs "password" in body of request to authenticate user.
// Postconditions:
// If the phone # does not match one in the database, JSON is returned stating {success: false, message: 'Authentication failed. User not found.'}
// If the phone # does match a user but the password does not for that user, JSON is
// returned stating { success: false, message: 'Authentication failed. Passwords did not match.' }
// If the phone # and password match a user, a JWT for that specific user is generated & JSON is returned saying { success: true, token:"JWT " + token }
router.route('/users/authenticate')
.post(userController.authenticate);
// test authentification route.
router.get('/testauth', passport.authenticate('jwt', { session: false }), function (req, res){
res.json({'user id': req.user.id});
});
router.get('/isOpen/:user_id', function (req, res){
// 8pm EST = 20
// 4am EST = 4
User.findById(req.params.user_id, function (err, user){
if (err){
res.send(err);
} else {
if (user.school == "GEORGETOWN"){
res.json({open: false, closedMessage: "Open Wednesday, Thursday, and Friday 10pm - 3am."});
} else {
if (user.school == "COLUMBIA"){
res.json({open: true, closedMessage: "Open Thursday, Friday, and Saturday 10pm - 3am"});
}
}
}
});
});
router.post('/sendOpenText', function (req, res){
var numbers = []
for(i = 0; i < numbers.length-1; i++){
twilio.sendMessage({
to: numbers[i],
from: config.TWILIO_PHONE,
body: 'DoorSlice is now open! 10pm - 3am we are delivering fresh, hot pizza, by the slice, directly to your dorm room. Cheese: $2.99, Pepperoni: $3.49. Order now!'
}, function (err, data){
if (err){
res.send(err);
}
res.send(data);
});
}
});
// returns user's profile
router.route('/users/:user_id')
.all(passport.authenticate('jwt', { session: false }))
.get(userController.getUser);
// Create a new Stripe customer
router.route('/payments/newStripeUser/:user_id')
.all(passport.authenticate('jwt', { session: false }))
.post(paymentController.newStripeUser);
// Add a new credit card to a Stripe customer
router.route('/payments/newStripeCard/:user_id')
.all(passport.authenticate('jwt', { session: false }))
.post(paymentController.newStripeCard);
// Update the default credit card of a Stripe customer
router.route('/payments/updateDefaultCard/:user_id')
.all(passport.authenticate('jwt', { session: false }))
.post(paymentController.updateDefaultCard);
// Charge a user
router.route('/payments/charge/:user_id')
.all(passport.authenticate('jwt', { session: false }))
.post(paymentController.chargeUser);
// Update the default card of the user
router.route('/payments/updateDefaultCard/:user_id')
.all(passport.authenticate('jwt', { session: false }))
.post(paymentController.updateDefaultCard);
router.route('/payments/removeCard/:user_id')
.all(passport.authenticate('jwt', { session: false }))
.post(paymentController.removeCard);
// Add a new address to a user's profile
router.route('/address/:user_id')
.all(passport.authenticate('jwt', { session: false }))
.post(addressController.addAddress);
// Delete an address from a user's profile
router.route('/address/:user_id/:address_id')
.all(passport.authenticate('jwt', { session: false }))
.delete(addressController.deleteAddress);
// Method: Add an order to user's profile and order collection.
// Preconditions:
// Needs "cheese" in body of request .. denotes the number of cheese slices ordered *if none are ordered, set value as 0*
// Needs "pepperoni" in body of request .. denotes the number of pepperoni slices ordered *if none are ordered, set value as 0*
// Needs "cardUsed" in body of request ... value should be set as the cardID of the card used for payment
// if apple pay was used, "applePay" should be set as the value
// Postconditions:
// if there is an error, an error will be returned
// if there is no error, JSON will be returned with message: "Order Saved!" and return the data of the user
router.route('/orders/:user_id/:address_id')
.all(passport.authenticate('jwt', { session: false }))
.post(orderController.addOrder);
router.route('/rateOrder/:user_id')
.post(orderController.rateOrder);
var pricesRoute = router.route('/prices')
pricesRoute.get(function (req, res){
res.json({"Cheese": cheesePrice, "Pepperoni": pepperoniPrice});
});
var addressRoute = router.route('/addresses/dorms/:user_id')
addressRoute.get(function (req, res){
User.findById(req.params.user_id, function (err, user){
if (err){
res.send(err);
} else {
if (!user.school){
res.json({"Message": "This user doesn't have a school assigned to their profile"});
}
if (user.school == "COLUMBIA"){
var dorms = [
"CARMAN", /*"CLAREMONT", "WIEN HALL",
"48 CLAREMONT", "601 WEST 113TH STREET",
"BROADWAY HALL", "CARLTON ARMS",
"EAST CAMPUS", "FURNALD HALL",
"HARMONY HALL", "HARTLEY HALL",
"HOGAN HALL", "RIVER HALL",
"JUGGLES HALL", "SHAPIRO HALL",
"WALLACH HALL", "WATT HALL",
"WOODBRIDGE HALL"*/
]
res.json({"Dorms": dorms});
} else {
if (user.school == "GEORGETOWN"){
var dorms = [
"NEW SOUTH"
/*"/*VILLAGE C EAST","VILLAGE C WEST",
"NEW SOUTH", "KENNEDY HALL", "LXR",
"HARBIN HALL", "NORTH EAST HALL",
"COPLEY HALL", "REYNOLDS HALL",
"MCCARTHY HALL", "DARNALL HALL",
"HENLE VILLAGE", "VILLAGE A",
"VILLAGE B", "NEVILS",
"FREEDOM HALL"*/
]
res.json({"Dorms": dorms});
}
}
}
});
});
// register all routes with /api
app.use('/api', router);
// Start the server
app.listen(port);
console.log("On port: " + port);