Merge pull request #23 from kfnawaz/feat/get-all-users-update
Feat: Get all users api update
This commit is contained in:
@@ -33,8 +33,12 @@ const getValidIds = async (ids, model) => {
|
||||
|
||||
module.exports = {
|
||||
registerUser: async (req, res, next) => {
|
||||
const { email, fullName, password } = req.body;
|
||||
const { email, fullName, password, createdBy } = req.body;
|
||||
try {
|
||||
let createdByUser;
|
||||
if (createdBy && mongoose.isValidObjectId(createdBy)) {
|
||||
createdByUser = await User.findById(createdBy);
|
||||
}
|
||||
const salt = await bcrypt.genSalt();
|
||||
const newUser = {
|
||||
email: email,
|
||||
@@ -42,6 +46,10 @@ module.exports = {
|
||||
password: await bcrypt.hash(password, salt),
|
||||
};
|
||||
|
||||
if (createdByUser) {
|
||||
newUser["createdBy"] = createdByUser;
|
||||
}
|
||||
|
||||
const user = await User.create(newUser);
|
||||
console.log({ msg: "new user created", user });
|
||||
|
||||
@@ -156,11 +164,9 @@ module.exports = {
|
||||
page = page ? parseInt(page) : 0;
|
||||
perPage = perPage ? parseInt(perPage) : 10;
|
||||
|
||||
const result = await User.find(
|
||||
{},
|
||||
{ id: 1, fullName: 1, email: 1, roles: 1, permissions: 1, createdBy: 1 },
|
||||
{ skip: page * perPage, limit: perPage }
|
||||
)
|
||||
const result = await User.find()
|
||||
.skip(page * perPage)
|
||||
.limit(perPage)
|
||||
.populate({ path: "roles", populate: "permissions" })
|
||||
.populate("permissions")
|
||||
.populate("createdBy");
|
||||
|
||||
@@ -43,6 +43,7 @@ module.exports = {
|
||||
throw new Error("Not Authenticated user!");
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error.message);
|
||||
res.status(401).send({
|
||||
success: false,
|
||||
error: constants.AUTHENTICATION_FAILURE_ERROR_MESSAGE,
|
||||
|
||||
Reference in New Issue
Block a user