forked from kaloraat/nodeapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.js
More file actions
192 lines (173 loc) · 5.6 KB
/
user.js
File metadata and controls
192 lines (173 loc) · 5.6 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
const _ = require('lodash');
const User = require('../models/user');
const formidable = require('formidable');
const fs = require('fs');
exports.userById = (req, res, next, id) => {
User.findById(id)
// populate followers and following users array
.populate('following', '_id name')
.populate('followers', '_id name')
.exec((err, user) => {
if (err || !user) {
return res.status(400).json({
error: 'User not found'
});
}
req.profile = user; // adds profile object in req with user info
next();
});
};
exports.hasAuthorization = (req, res, next) => {
let sameUser = req.profile && req.auth && req.profile._id == req.auth._id;
let adminUser = req.profile && req.auth && req.auth.role === 'admin';
const authorized = sameUser || adminUser;
// console.log("req.profile ", req.profile, " req.auth ", req.auth);
// console.log("SAMEUSER", sameUser, "ADMINUSER", adminUser);
if (!authorized) {
return res.status(403).json({
error: 'User is not authorized to perform this action'
});
}
next();
};
exports.allUsers = (req, res) => {
User.find((err, users) => {
if (err) {
return res.status(400).json({
error: err
});
}
res.json(users);
}).select('name email updated created role');
};
exports.getUser = (req, res) => {
req.profile.hashed_password = undefined;
req.profile.salt = undefined;
return res.json(req.profile);
};
// exports.updateUser = (req, res, next) => {
// let user = req.profile;
// user = _.extend(user, req.body); // extend - mutate the source object
// user.updated = Date.now();
// user.save(err => {
// if (err) {
// return res.status(400).json({
// error: "You are not authorized to perform this action"
// });
// }
// user.hashed_password = undefined;
// user.salt = undefined;
// res.json({ user });
// });
// };
exports.updateUser = (req, res, next) => {
let form = new formidable.IncomingForm();
// console.log("incoming form data: ", form);
form.keepExtensions = true;
form.parse(req, (err, fields, files) => {
if (err) {
return res.status(400).json({
error: 'Photo could not be uploaded'
});
}
// save user
let user = req.profile;
// console.log("user in update: ", user);
user = _.extend(user, fields);
user.updated = Date.now();
// console.log("USER FORM DATA UPDATE: ", user);
if (files.photo) {
user.photo.data = fs.readFileSync(files.photo.path);
user.photo.contentType = files.photo.type;
}
user.save((err, result) => {
if (err) {
return res.status(400).json({
error: err
});
}
user.hashed_password = undefined;
user.salt = undefined;
// console.log("user after update with formdata: ", user);
res.json(user);
});
});
};
exports.userPhoto = (req, res, next) => {
if (req.profile.photo.data) {
res.set(('Content-Type', req.profile.photo.contentType));
return res.send(req.profile.photo.data);
}
next();
};
exports.deleteUser = (req, res, next) => {
let user = req.profile;
user.remove((err, user) => {
if (err) {
return res.status(400).json({
error: err
});
}
res.json({ message: 'User deleted successfully' });
});
};
// follow unfollow
exports.addFollowing = (req, res, next) => {
User.findByIdAndUpdate(req.body.userId, { $push: { following: req.body.followId } }, (err, result) => {
if (err) {
return res.status(400).json({ error: err });
}
next();
});
};
exports.addFollower = (req, res) => {
User.findByIdAndUpdate(req.body.followId, { $push: { followers: req.body.userId } }, { new: true })
.populate('following', '_id name')
.populate('followers', '_id name')
.exec((err, result) => {
if (err) {
return res.status(400).json({
error: err
});
}
result.hashed_password = undefined;
result.salt = undefined;
res.json(result);
});
};
// remove follow unfollow
exports.removeFollowing = (req, res, next) => {
User.findByIdAndUpdate(req.body.userId, { $pull: { following: req.body.unfollowId } }, (err, result) => {
if (err) {
return res.status(400).json({ error: err });
}
next();
});
};
exports.removeFollower = (req, res) => {
User.findByIdAndUpdate(req.body.unfollowId, { $pull: { followers: req.body.userId } }, { new: true })
.populate('following', '_id name')
.populate('followers', '_id name')
.exec((err, result) => {
if (err) {
return res.status(400).json({
error: err
});
}
result.hashed_password = undefined;
result.salt = undefined;
res.json(result);
});
};
exports.findPeople = (req, res) => {
let following = req.profile.following;
following.push(req.profile._id);
User.find({ _id: { $nin: following } }, (err, users) => {
if (err) {
return res.status(400).json({
error: err
});
}
res.json(users);
}).select('name');
};