Commit 5bb926e8 authored by Balasuriya D.A.M.'s avatar Balasuriya D.A.M.

Add/Remove User to Group API

parent 0c544102
...@@ -132,4 +132,44 @@ const renameGroup = asyncHandler(async (req, res) => { ...@@ -132,4 +132,44 @@ const renameGroup = asyncHandler(async (req, res) => {
} }
}); });
module.exports = { accessChat, fetchChats, createGroupChat, renameGroup }; const addToGroup = asyncHandler(async (req, res) => {
\ No newline at end of file const { chatId, userId } = req.body;
const added = await Chat.findByIdAndUpdate(chatId, {
$push: { users: userId },
},
{ new: true }
)
.populate("users", "-password")
.populate("groupAdmin", "-password");
if (!added) {
res.status(404);
throw new Error("Organization Not Found");//chat not found
} else {
res.json(added);
}
});
const removeFromGroup = asyncHandler(async (req, res) => {
const { chatId, userId } = req.body;
const removed = await Chat.findByIdAndUpdate(chatId, {
$pull: { users: userId },
},
{ new: true }
)
.populate("users", "-password")
.populate("groupAdmin", "-password");
if (!removed) {
res.status(404);
throw new Error("Organization Not Found");//chat not found
} else {
res.json(removed);
}
});
module.exports = { accessChat, fetchChats, createGroupChat, renameGroup, addToGroup, removeFromGroup };
\ No newline at end of file
const express = require("express"); const express = require("express");
const { accessChat, fetchChats, createGroupChat, renameGroup} = require("../controllers/chatControllers"); const { accessChat, fetchChats, createGroupChat, renameGroup, addToGroup, removeFromGroup} = require("../controllers/chatControllers");
const { protect } = require("../middleware/authMiddleware"); const { protect } = require("../middleware/authMiddleware");
...@@ -13,7 +13,8 @@ router.route("/").post(protect, accessChat); //API route for one on one chat ...@@ -13,7 +13,8 @@ router.route("/").post(protect, accessChat); //API route for one on one chat
router.route("/").get(protect, fetchChats); router.route("/").get(protect, fetchChats);
router.route("/group").post(protect, createGroupChat); //create group router.route("/group").post(protect, createGroupChat); //create group
router.route("/rename").put(protect, renameGroup); //update group router.route("/rename").put(protect, renameGroup); //update group
//router.route("/groupremove").put(protect, removeFromGroup); //remove from group router.route("/groupadd").put(protect, addToGroup); //add someone to group
//router.route("/groupadd").put(protect, addToGroup); //add someone to group router.route("/groupremove").put(protect, removeFromGroup); //remove from group
module.exports = router; module.exports = router;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment