feat: added crud apis for user
This commit is contained in:
@@ -150,4 +150,85 @@ module.exports = {
|
||||
next(error);
|
||||
}
|
||||
},
|
||||
getAllUsers: async (req, res, next) => {
|
||||
try {
|
||||
let { page, perPage } = req.query;
|
||||
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 }
|
||||
)
|
||||
.populate({ path: "roles", populate: "permissions" })
|
||||
.populate("permissions")
|
||||
.populate("createdBy");
|
||||
res.send({ success: true, data: result });
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
},
|
||||
getUserById: async (req, res, next) => {
|
||||
const { id } = req.params;
|
||||
if (!mongoose.isValidObjectId(id)) {
|
||||
res.status(400).send({ success: false, error: "Invalid data for user ID" });
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await User.findOne({ _id: id }, { id: 1, fullName: 1, email: 1, roles: 1, permissions: 1, createdBy: 1 })
|
||||
.populate({ path: "roles", populate: "permissions" })
|
||||
.populate("permissions")
|
||||
.populate("createdBy");
|
||||
res.send({ success: true, data: result });
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
},
|
||||
createUser: async (req, res, next) => {
|
||||
const { email, fullName, password } = req.body;
|
||||
try {
|
||||
const salt = await bcrypt.genSalt();
|
||||
const newUser = {
|
||||
email: email,
|
||||
fullName: fullName,
|
||||
password: await bcrypt.hash(password, salt),
|
||||
createdBy: res.locals.user,
|
||||
};
|
||||
|
||||
const user = await User.create(newUser);
|
||||
console.log({ msg: "new user created", user });
|
||||
|
||||
res.send({ success: true, data: user });
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
next(err);
|
||||
}
|
||||
},
|
||||
updateUser: async (req, res, next) => {
|
||||
const { id } = req.params;
|
||||
if (!mongoose.isValidObjectId(id)) {
|
||||
res.status(400).send({ success: false, error: "Invalid data for user ID" });
|
||||
}
|
||||
|
||||
const { email, fullName, password } = req.body;
|
||||
try {
|
||||
const user = await User.findById(id);
|
||||
if (user) {
|
||||
res.status(404).send({ success: false, error: "User not found" });
|
||||
}
|
||||
const salt = await bcrypt.genSalt();
|
||||
|
||||
if (email) user.email = email;
|
||||
if (fullName) user.fullName = fullName;
|
||||
if (password) user.password = await bcrypt.hash(password, salt);
|
||||
|
||||
await user.save();
|
||||
|
||||
res.send({ success: true, data: user });
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
next(err);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
@@ -4,6 +4,12 @@ const { SuperAdminCheck, AuthenticateMiddleware } = require("./utils/authorize")
|
||||
|
||||
router.post("/register", controller.registerUser);
|
||||
router.post("/login", controller.loginUser);
|
||||
|
||||
router.get("/all", AuthenticateMiddleware, SuperAdminCheck, controller.getAllUsers);
|
||||
router.get("/:id", AuthenticateMiddleware, SuperAdminCheck, controller.getUserById);
|
||||
router.post("/create", AuthenticateMiddleware, SuperAdminCheck, controller.createUser);
|
||||
router.post("/:id", AuthenticateMiddleware, SuperAdminCheck, controller.updateUser);
|
||||
|
||||
router.post("/:user/add-access", AuthenticateMiddleware, SuperAdminCheck, controller.addUserAccessControl);
|
||||
router.post("/:user/remove-access", AuthenticateMiddleware, SuperAdminCheck, controller.removeUserAccessControl);
|
||||
router.get("/allowed-ui-modules", AuthenticateMiddleware, controller.getUIAccessControl);
|
||||
|
||||
@@ -48,6 +48,10 @@ const schema = new mongoose.Schema(
|
||||
ref: "UserPermission",
|
||||
},
|
||||
],
|
||||
createdBy: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
ref: "User",
|
||||
}
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
|
||||
Reference in New Issue
Block a user