-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuserController.js
More file actions
84 lines (67 loc) · 2.42 KB
/
userController.js
File metadata and controls
84 lines (67 loc) · 2.42 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
const asyncHandler = require("express-async-handler");
const userModel = require("../db/models/userModel");
const ApiError = require("../middlewares/apiError");
const { BadRequestError , NotFoundError } = require("../middlewares/apiError");
const {
SuccessMsgResponse,
SuccessResponse,
SuccessMsgDataResponse,
SuccessResponsePagination,
} = require("../middlewares/apiResponse");
const UserRepo = require("../db/repositories/userRepo");
exports.getUser = asyncHandler(async (req, res) => {
const user = await UserRepo.findById(req.params.id);
if (!user) {
throw new NotFoundError("No user found with that id");
}
return new SuccessResponse(user).send(res);
});
exports.createUser = asyncHandler(async (req, res) => {
const checkUser = await UserRepo.findOneByObj({ email: req.body.email });
if (checkUser) {
throw new BadRequestError("A user with this email already exists");
}
let user = await UserRepo.create(req.body);
let { password, ...data } = user._doc;
return new SuccessMsgDataResponse(data, "User created successfully").send(
res
);
});
exports.getUsers = asyncHandler(async (req, res) => {
const { page, perPage } = req.query;
const options = {
page: parseInt(page, 10) || 1,
limit: parseInt(perPage, 10) || 10,
};
const users = await UserRepo.findByObjPaginate({}, options, req.query);
if (!users) {
return new SuccessMsgResponse("No users found").send(res);
}
const { docs, ...meta } = users;
return new SuccessResponsePagination(docs, meta).send(res);
});
exports.deleteUser = asyncHandler(async (req, res) => {
const user = await UserRepo.findOneByObj({ _id: req.params.id, deletedAt: null});
if (!user) {
throw new NotFoundError("User not registered or deleted");
}
let deletedUser = await UserRepo.deleteUser(user);
return new SuccessMsgDataResponse(deletedUser, "User deleted successfully").send(
res
);
});
exports.updateUser = asyncHandler(async (req, res) => {
if(req.body.email){
const checkUser = await UserRepo.findOneByObj({_id: { $ne: req.params.id }, email: req.body.email });
if (checkUser) {
throw new BadRequestError("A user with this email already exists");
}
}
const user = await UserRepo.findByIdAndUpdate( req.params.id, req.body);
if (!user) {
throw new NotFoundError("No user found with that id");
}
return new SuccessMsgDataResponse(user, "User updated successfully").send(
res
);
});