-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusersController.js
217 lines (196 loc) · 6.21 KB
/
usersController.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
let model = require("./../models/userModel");
let Email = require("./../utils/email");
let JWT = require("jsonwebtoken");
let AsyncError = require("./../utils/catchAsync");
let sharp = require("sharp");
let multer = require("multer");
function generateToken(user) {
return JWT.sign({ id: user._id, cpa: "Techkey" }, process.env.TOKENSECRETE, {
expiresIn: process.env.TOKENDURATION,
});
}
function generateCookie(res, token, req) {
let options = {
expires: new Date(Date.now() + process.env.COOKIEDURATION + 24 + 60 + 60),
//secure: process.env.NODE_ENV == 'production' ? true : false,
secure: req.secure || req.headers["x-forwarded-proto"] === "https",
httpOnly: true,
};
res.cookie("JWT", token, options);
}
exports.getUsers = AsyncError(async function name(req, res, next) {
let users = await model.find();
res
.status(200)
.json({ status: "successs", results: users.length, data: users });
});
exports.addUser = AsyncError(async function (req, res) {
try {
let newDoc = await model.create(req.body);
let id = String(newDoc._id);
// new Email(newDoc, " ").sendEmailVerification(newDoc.verificationNumber);
// res.status(200).json({ status: "success", data: newDoc });
res
.status(200)
.json({ status: "success", message: "Successfully Registered", id });
} catch (err) {
res.json({ status: "failed", message: err.message });
}
});
exports.getOneUser = AsyncError(async function name(req, res, next) {
let user = await model.findById(req.params.id);
res.status(200).json({ status: "successs", user });
});
exports.deletUser = AsyncError(async function (req, res, next) {
let deleted = await model.findOneAndDelete({ _id: req.params.id });
res.status(204).json({ status: "success", data: null });
});
exports.updateUser = AsyncError(async function (req, res, next) {
let updated = await model.findOneAndUpdate({ _id: req.params.id }, req.body, {
new: true,
runvalidators: true,
});
res.status(200).json({ status: "success", data: updated });
});
exports.updateProfile = AsyncError(async function (req, res, next) {
let updated = await model.findOneAndUpdate({ _id: req.user._id }, req.body, {
new: true,
runvalidators: true,
});
if (updated) {
res
.status(200)
.json({ status: "success", message: "update received", data: updated });
} else {
res.status(200).json({ status: "failed", message: "update failed" });
}
});
exports.getme = AsyncError(async function (req, res, next) {
let doc = await model.find({ _id: req.user.id });
res.status(200).json({
status: "success",
data: doc,
});
});
exports.resendCode = AsyncError(async function (req, res, next) {
let user = await model.findById(req.body.url);
if (!user) {
res.status(200).json({
status: "failed",
message: "User not Found",
});
} else {
try {
// new Email(user, " ").sendEmailVerification(user.verificationNumber);
res.status(200).json({
status: "success",
message: "Verification Code Resent",
});
} catch (err) {
console.log(err);
res.status(200).json({
status: "failed",
});
// return next(
// new appError("There was an error sending the email try again later", 500)
// );
}
}
});
exports.usercontact = AsyncError(async function (req, res, next) {
let name = req.body.name;
let email = req.body.email;
let subject = req.body.subject;
let message = req.body.message;
try {
// new Email(" ", " ").sendContactForm(name, email, subject, message);
} catch (err) {
console.log("TC-1245", err);
res.status(200).json({ status: "Failed" });
}
// res.status(200).json({ status: "success", data: newDoc });
res.status(200).json({ status: "success" });
});
exports.verifyMe = AsyncError(async function (req, res, next) {
let user = await model.findById(req.body.id);
if (user) {
let userVerificationNumber = user.verificationNumber;
if (userVerificationNumber == req.body.code) {
user.isVerified = true;
await user.save({ validateBeforeSave: false });
let token = generateToken(user);
generateCookie(res, token, req);
res.locals.user = user;
try {
// new Email(user, " ").sendVerifiedEmail();
} catch (err) {
console.log("TC-09121", err);
}
res.status(200).json({
status: "success",
message: "User Verified",
token,
user: {
name: user.name,
phone: user.phone,
role: user.role,
photo: user.photo,
email: user.email,
},
});
} else {
res.status(200).json({
status: "failed",
message: "invalid Verification Code",
});
}
} else {
res.status(200).json({
status: "failed",
message: "User not registed",
});
}
});
let multerStorage = multer.memoryStorage();
let multerfilter = function (res, file, cb) {
if (file.mimetype.startsWith("image")) {
cb(null, true);
} else {
cb(new appError("File uploaded is not an image", 500));
}
};
// let upload = multer({ dest: 'public/img/' });
let upload = multer({ storage: multerStorage, fileFilter: multerfilter });
exports.uploadimage = upload.single("profilePhotoUpload");
exports.resizePhoto = function (req, res, next) {
if (!req.file) {
return next();
} else {
try {
req.file.filename = `${req.user.id}-${Date.now()}.jpeg`;
sharp(req.file.buffer)
.resize(500, 500)
.toFormat("jpeg")
.jpeg({ quality: 90 })
.toFile(`public/img/users/${req.file.filename}`);
} catch (err) {
console.log("TC-214", err);
}
next();
}
};
exports.uploadImage = AsyncError(async function (req, res, next) {
try {
let updated = await model.findOneAndUpdate(
{ _id: req.user.id },
{ photo: req.file.filename },
{
new: true,
runvalidators: true,
}
);
res.status(200).json({ status: "success" });
} catch (error) {
res.status(200).json({ status: "Failed" });
}
});