|
@@ -5,11 +5,27 @@ const MessageModel = require('../model/message');
|
|
|
const listChats = async (req, res, next) => {
|
|
|
try {
|
|
|
const userId = req.user.id;
|
|
|
- const chats = await ChatModel.getList(userId, req.query);
|
|
|
+ const { total, limit, page, chats } = await ChatModel.getList(
|
|
|
+ userId,
|
|
|
+ req.query
|
|
|
+ );
|
|
|
+
|
|
|
const messages = await MessageModel.getLastMessagesList(userId);
|
|
|
+
|
|
|
+ const lastOnline = await chats.reduce(async (acc, { companionId }) => {
|
|
|
+ const { _id, online, updatedAt } = await UserModel.findById(companionId);
|
|
|
+ acc.push({ _id, online, updatedAt });
|
|
|
+ return acc;
|
|
|
+ }, []);
|
|
|
+
|
|
|
const lastMessages = await messages.reduce(
|
|
|
- (acc, { message, companionId }) => {
|
|
|
- const obj = { message, companionId };
|
|
|
+ (acc, { message, companionId, createdAt, updatedAt }) => {
|
|
|
+ const obj = {
|
|
|
+ message,
|
|
|
+ companionId,
|
|
|
+ createdAt,
|
|
|
+ updatedAt,
|
|
|
+ };
|
|
|
if (acc.length === 0) {
|
|
|
acc.push(obj);
|
|
|
return acc;
|
|
@@ -31,8 +47,12 @@ const listChats = async (req, res, next) => {
|
|
|
status: 'success',
|
|
|
code: 200,
|
|
|
data: {
|
|
|
- ...chats,
|
|
|
+ total,
|
|
|
+ limit,
|
|
|
+ page,
|
|
|
+ chats,
|
|
|
lastMessages,
|
|
|
+ lastOnline,
|
|
|
},
|
|
|
});
|
|
|
} catch (e) {
|
|
@@ -114,7 +134,74 @@ const startChat = async (req, res, next) => {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
+const muteChat = async (req, res, next) => {
|
|
|
+ try {
|
|
|
+ const id = req.body.id;
|
|
|
+ const userId = req.user.id;
|
|
|
+ const isChat = await ChatModel.getByField(id, userId);
|
|
|
+ if (isChat) {
|
|
|
+ const { _id, mute } = isChat;
|
|
|
+ await ChatModel.update(_id, { mute: !mute });
|
|
|
+ return res.status(200).json({
|
|
|
+ status: 'success',
|
|
|
+ code: 200,
|
|
|
+ data: {},
|
|
|
+ });
|
|
|
+ }
|
|
|
+ } catch (e) {
|
|
|
+ next(e);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+const chatById = async (req, res, next) => {
|
|
|
+ try {
|
|
|
+ const userId = req.user.id;
|
|
|
+ const companionId = req.params.companionId;
|
|
|
+ const { online } = await UserModel.findById(companionId);
|
|
|
+ const isChat = await ChatModel.getByField(companionId, userId);
|
|
|
+ if (isChat) {
|
|
|
+ const {
|
|
|
+ name,
|
|
|
+ lastName,
|
|
|
+ avatarUrl,
|
|
|
+ color,
|
|
|
+ mute,
|
|
|
+ number,
|
|
|
+ _id,
|
|
|
+ companionId,
|
|
|
+ owner,
|
|
|
+ createdAt,
|
|
|
+ updatedAt,
|
|
|
+ __v,
|
|
|
+ } = isChat;
|
|
|
+ return res.json({
|
|
|
+ status: 'success',
|
|
|
+ code: 200,
|
|
|
+ data: {
|
|
|
+ name,
|
|
|
+ lastName,
|
|
|
+ avatarUrl,
|
|
|
+ color,
|
|
|
+ online,
|
|
|
+ mute,
|
|
|
+ number,
|
|
|
+ _id,
|
|
|
+ companionId,
|
|
|
+ owner,
|
|
|
+ createdAt,
|
|
|
+ updatedAt,
|
|
|
+ __v,
|
|
|
+ },
|
|
|
+ });
|
|
|
+ }
|
|
|
+ } catch (e) {
|
|
|
+ next(e);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
module.exports = {
|
|
|
listChats,
|
|
|
startChat,
|
|
|
+ muteChat,
|
|
|
+ chatById,
|
|
|
};
|