Skip to content

Commit

Permalink
Add: sendChatHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
chlehdwon committed May 30, 2023
1 parent 5c40faa commit a9c3b01
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions src/services/chats.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
const { chatModel, userModel, roomModel } = require("../modules/stores/mongo");
const { chatPopulateOption } = require("../modules/populates/chats");
const awsS3 = require("../modules/stores/awsS3");
const { transformChatsForRoom, emitChatEvent } = require("../modules/socket");
const {
transformChatsForRoom,
emitChatEvent,
emitUpdateEvent,
} = require("../modules/socket");
const {
roomPopulateOption,
formatSettlement,
} = require("../modules/populates/rooms");

const chatCount = 60;

Expand Down Expand Up @@ -178,15 +186,40 @@ const updateChatHandler = async (req, res) => {
if (!io) {
return res.status(403).send("Chat/update : socket did not connected");
}

const isPart = await isUserInRoom(userId, roomId);
if (!isPart) {
return res
.status(403)
.send("Chat/send : user did not participated in the room");
.send("Chat/update : user did not participated in the room");
}

const roomObject = await roomModel
.findOneAndUpdate(
{
_id: roomId,
part: {
$elemMatch: {
user: user._id,
},
},
},
{
$set: { "part.$[updater].readAt": lastMsgDate },
},
{
new: true,
arrayFilters: [{ "updater.user": { $eq: user._id } }],
}
)
.lean()
.populate(roomPopulateOption);
if (!roomObject) {
return res.status(404).send("Chat/update : cannot find room info");
}

/* TODO */
/* TODO: Return Formatting */
if (await emitUpdateEvent(io)) res.json({ part: roomObject.part });
else res.status(500).send("Chat/update : internal server error");
} catch (e) {
res.status(500).send("Chat/update : internal server error");
}
Expand Down

0 comments on commit a9c3b01

Please sign in to comment.