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 = {
|
module.exports = {
|
||||||
registerUser: async (req, res, next) => {
|
registerUser: async (req, res, next) => {
|
||||||
const { email, fullName, password } = req.body;
|
const { email, fullName, password, createdBy } = req.body;
|
||||||
try {
|
try {
|
||||||
|
let createdByUser;
|
||||||
|
if (createdBy && mongoose.isValidObjectId(createdBy)) {
|
||||||
|
createdByUser = await User.findById(createdBy);
|
||||||
|
}
|
||||||
const salt = await bcrypt.genSalt();
|
const salt = await bcrypt.genSalt();
|
||||||
const newUser = {
|
const newUser = {
|
||||||
email: email,
|
email: email,
|
||||||
@@ -42,6 +46,10 @@ module.exports = {
|
|||||||
password: await bcrypt.hash(password, salt),
|
password: await bcrypt.hash(password, salt),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (createdByUser) {
|
||||||
|
newUser["createdBy"] = createdByUser;
|
||||||
|
}
|
||||||
|
|
||||||
const user = await User.create(newUser);
|
const user = await User.create(newUser);
|
||||||
console.log({ msg: "new user created", user });
|
console.log({ msg: "new user created", user });
|
||||||
|
|
||||||
@@ -156,11 +164,9 @@ module.exports = {
|
|||||||
page = page ? parseInt(page) : 0;
|
page = page ? parseInt(page) : 0;
|
||||||
perPage = perPage ? parseInt(perPage) : 10;
|
perPage = perPage ? parseInt(perPage) : 10;
|
||||||
|
|
||||||
const result = await User.find(
|
const result = await User.find()
|
||||||
{},
|
.skip(page * perPage)
|
||||||
{ id: 1, fullName: 1, email: 1, roles: 1, permissions: 1, createdBy: 1 },
|
.limit(perPage)
|
||||||
{ skip: page * perPage, limit: perPage }
|
|
||||||
)
|
|
||||||
.populate({ path: "roles", populate: "permissions" })
|
.populate({ path: "roles", populate: "permissions" })
|
||||||
.populate("permissions")
|
.populate("permissions")
|
||||||
.populate("createdBy");
|
.populate("createdBy");
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ module.exports = {
|
|||||||
throw new Error("Not Authenticated user!");
|
throw new Error("Not Authenticated user!");
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error(error.message);
|
||||||
res.status(401).send({
|
res.status(401).send({
|
||||||
success: false,
|
success: false,
|
||||||
error: constants.AUTHENTICATION_FAILURE_ERROR_MESSAGE,
|
error: constants.AUTHENTICATION_FAILURE_ERROR_MESSAGE,
|
||||||
|
|||||||
Reference in New Issue
Block a user