diff --git a/src/controller/user.controller.js b/src/controller/user.controller.js index 5097a82..4ba6fae 100644 --- a/src/controller/user.controller.js +++ b/src/controller/user.controller.js @@ -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"); diff --git a/src/controller/utils/authorize.js b/src/controller/utils/authorize.js index 8c98129..d799a70 100644 --- a/src/controller/utils/authorize.js +++ b/src/controller/utils/authorize.js @@ -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,