Commit cfd37a4b authored by Balasuriya D.A.M.'s avatar Balasuriya D.A.M.

Fetch User Chats API

parent 5e664de7
......@@ -52,4 +52,25 @@ const accessChat = asyncHandler(async (req, res) => {
}
});
module.exports = { accessChat };
\ No newline at end of file
const fetchChats = asyncHandler(async (req, res) => {
try {
Chat.find({ users: { $elemMatch: { $eq: req.user._id } } })
.populate("users", "-password")
.populate("groupAdmin", "-password")
.populate("latestMessage")
.sort({ updatedAt: -1 })
.then(async (results) => {
results = await User.populate(results, {
path: "latestMessage.sender",
select: "name pic email",
});
res.status(200).send(results);
});
} catch (error) {
res.status(400);
throw new Error(error.message);
}
});
module.exports = { accessChat, fetchChats };
\ No newline at end of file
const express = require("express");
const { accessChat } = require("../controllers/chatControllers");
const { accessChat, fetchChats } = require("../controllers/chatControllers");
const { protect } = require("../middleware/authMiddleware");
......@@ -9,8 +9,8 @@ const router = express.Router();
//only login user can access this route.
router.route("/").post(protect, accessChat);
//router.route("/").get(protect, fetchChats);
router.route("/").post(protect, accessChat); //API route for one on one chat
router.route("/").get(protect, fetchChats);
//router.route("/group").post(protect, createGroupChat); //create group
//router.route("/rename").put(protect, renameGroup); //update group
//router.route("/groupremove").put(protect, removeFromGroup); //remove from group
......
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