-
Notifications
You must be signed in to change notification settings - Fork 2
/
users.js
206 lines (199 loc) · 6.36 KB
/
users.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
var express = require('express');
var router = express.Router();
const usersModel = require('../../db/models/users');
const tablesModel = require('../../db/models/tables.js');
const cardsModel = require('../../db/models/cards.js');
const bcrypt = require('bcrypt');
const saltRounds = 12;
const jwtChecker = require('../middleware/jwtChecker.js');
const auth = require('../middleware/auth.js');
const tryCatch = require('../utils/tryCatch');
// router.use(jwtChecker.checkToken);
router.get('/:id', (req, res) => {
const id = req.params.id;
usersModel
.getUserByID(id)
.then(user => {
res.status(200).send(user);
})
.catch(err => {
res
.status(402)
.send('Unexpected error occurred @routes/users.js in getting user');
});
});
router.post(
'/',
(req, res, next) => {
let tableId = req.headers.cookie ? req.cookies.tableId ? req.cookies.tableId : null : null;
const { email, password, name } = req.body;
//post user to usersModel if she doesn't already exist
usersModel
.getUserInfoByEmail(email)
.then(user => {
if (!user) {
//if email does not exist create user
bcrypt
.hash(password, saltRounds)
.then(hashedPassword => {
usersModel
.createNewUser({ name, hashedPassword, email })
.then(userCreated => {
if(tableId) {
usersModel.getUserInfoByEmail(email)
.then((newUser) => {
return tablesModel.addUserToTable(tableId, newUser.id)
})
.then((addedTableToNewUser) => {
res.clearCookie('tableId')
next();
})
.catch((err) => {
console.log('error happened during adding invited user to table', err);
res.status(403).json({
success: false,
message: 'Error in adding you to invited table.'
})
})
} else {
console.log('THIS IS NOT GOOD');
next()
}
})
.catch(error => {
console.log('creating new user failed', error);
res.status(403).json({
success: false,
message: 'Unexpected Error Occurred Try Later.',
});
});
})
.catch(error => {
console.log('creating new user password has failed', error);
res.status(403).json({
success: false,
message: 'Unexpected Error Occurred Try Later.',
});
});
} else {
//if email already exists, send message
res
.status(400)
.json({ success: false, message: 'Username already exists' });
}
})
.catch(err => {
console.log('Error getting user info @users.js line 37', err);
res.status(404).send('error getting user');
});
},
auth.auth
);
router.put('/:id', (req, res) => {
const userId = req.user;
const id = req.params.id;
const userObj = req.body;
userObj.id = id;
//@TODO: turn this back on
// if (userId !== id) {
// return res.status(401).send({ message: 'Unauthorized' });
// }
usersModel
.getUserByID(id)
.then(user => {
if (!user) {
//if user does not exist, send message
res
.status(400)
.json({ success: false, message: 'Invalid credentials' });
} else {
//if user exists, update user
bcrypt
.hash(userObj.password, saltRounds)
.then(hashedPassword => {
userObj.password = hashedPassword;
usersModel
.updateUser(userObj)
.then(updatedUser => {
updatedUser.password = '';
res.status(201).send(updatedUser);
})
.catch(err => {
console.log('Error updating user @users.js', err);
res.status(403).json({
success: false,
message: 'Unexpected Error Occurred Try Later.',
});
});
})
.catch(error => {
console.log('error creating hash password', error);
res.status(403).json({
success: false,
message: 'Unexpected Error Occurred Try Later.',
});
});
}
})
.catch(err => {
console.log('Error getting user info @users.js', err);
res.status(404).send(JSON.stringify(id));
});
});
router.delete('/:id', (req, res) => {
const id = req.params.id;
const userId = req.user;
//@TODO: turn this back on
// if (userId !== id) {
// return res.status(401).send({ message: 'Unauthorized' });
// }
usersModel
.getUserByID(id)
.then(user => {
if (user) {
usersModel
.deleteUser(id)
.then(() => {
res
.status(201)
.json({ success: true, message: 'Account Deleted!' });
})
.catch(err => {
console.log('Error deleting user @users.js', err);
res.status(403).json({
success: false,
message: 'Unexpected Error Occurred Try Later.',
});
});
} else {
res
.status(400)
.json({ success: false, message: 'User does not exist' });
}
})
.catch(err => {
console.log('Error getting user @users.js', err);
res.status(404).json({ success: false, message: 'Error getting user' });
});
});
router.get('/:id/cards/', async (req, res) => {
const userId = req.params.id;
tryCatch(async () => {
const user = await usersModel.getUserByID(userId);
if (user){
const cards = await cardsModel.getCardsByUserID(userId);
console.log(cards);
res.status(201).send(cards)
}else{
res.status(404).json({ success: false, message: 'Error getting user' });
}
}, res);
})
router.get('/:userId/table/:tableId/cards', async (req, res) => {
const {userId, tableId} = req.params;
tryCatch(async () => {
const count = await cardsModel.countUserCardsByTable(userId, tableId);
res.status(201).send(count);
})
})
module.exports = router;