Browse Source

done features with status online complitly and added mute ability

unknown 2 years ago
parent
commit
eb020cbb18
3 changed files with 99 additions and 5 deletions
  1. 91 4
      controllers/chats.js
  2. 5 0
      model/schemas/chat.js
  3. 3 1
      routes/chats.js

+ 91 - 4
controllers/chats.js

@@ -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,
 };

+ 5 - 0
model/schemas/chat.js

@@ -1,3 +1,4 @@
+const { boolean } = require('joi');
 const mongoose = require('mongoose');
 const { Schema, model, SchemaTypes } = mongoose;
 const mongoosePaginate = require('mongoose-paginate-v2');
@@ -29,6 +30,10 @@ const chatSchema = new Schema(
 			type: String,
 			default: null,
 		},
+		mute: {
+			type: Boolean,
+			default: false,
+		},
 		number: {
 			type: String,
 			min: 8,

+ 3 - 1
routes/chats.js

@@ -6,6 +6,8 @@ const validation = require('../validation/chat');
 
 router
 	.get('/', guard, controllers.listChats)
-	.post('/', guard, validation.startChat, controllers.startChat);
+	.get('/:companionId', guard, controllers.chatById)
+	.post('/', guard, validation.startChat, controllers.startChat)
+	.post('/mute/', guard, controllers.muteChat);
 
 module.exports = router;