Skip to content

Commit

Permalink
added message sending
Browse files Browse the repository at this point in the history
  • Loading branch information
harikrishnan-git committed Dec 26, 2024
1 parent 77201c5 commit e0128c2
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions backend/controllers/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,32 @@ export const sendMsg = async (req, res) => {
recieverId,
message,
});
msg.save().then((res) => {
const conv = Conversation.findOne({
participants: { $all: [senderId, recieverId] },
msg.save();
const conv = await Conversation.findOne({
participants: { $all: [senderId, recieverId] },
});
if (!conv) {
const con = new Conversation({
participants: [senderId, recieverId],
messages: [msg._id],
});
if (!conv) {
const con = new Conversation({
participants: [senderId, recieverId],
message: [msg._id],
con
.save()
.then(() => {
console.log("Conversation created");
})
.catch((err) => {
console.log("problem while creating conversation");
});
con
.save()
.then(() => {
console.log("Conversation created");
})
.catch((err) => {
console.log("problem while creating conversation");
});
} else {
conv.message.push(msg._id);
}
});
res.send(con);
res.end();
} else {
conv.messages.push(msg._id);
conv.save().then(() => {
res.send(conv);
res.end();
});
}
} catch (err) {
console.log("error in controller of sendMsg", err.message);
res.status(500).json({ error: "error while sending message" });
Expand Down

0 comments on commit e0128c2

Please sign in to comment.