Merge pull request #23 from kfnawaz/feat/get-all-users-update

Feat: Get all users api update
This commit is contained in:
bluestreamlds
2022-02-10 21:59:38 +05:30
committed by GitHub
2 changed files with 13 additions and 6 deletions

View File

@@ -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");

View File

@@ -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,