unknown 2 years ago
parent
commit
d703dd65c5
6 changed files with 86 additions and 59 deletions
  1. 7 43
      controllers/chats.js
  2. 55 10
      controllers/messages.js
  3. 13 4
      controllers/user.js
  4. 6 0
      model/chat.js
  5. 1 2
      model/message.js
  6. 4 0
      model/schemas/chat.js

+ 7 - 43
controllers/chats.js

@@ -170,9 +170,12 @@ const seenChat = async (req, res, next) => {
 		const userId = req.user.id;
 		const isChat = await ChatModel.getByField(id, userId);
 		const isCompanionChat = await ChatModel.getByField(userId, id);
-		const isMessage = await MessageModel.getList(userId, {});
-		if (isChat && isMessage && isCompanionChat) {
-			const { total } = isMessage;
+		const isMessages = await MessageModel.getList(
+			{ owner: userId, companionId: id },
+			{}
+		);
+		if (isChat && isMessages && isCompanionChat) {
+			const { total } = isMessages;
 			await ChatModel.update(isChat._id, { seen: total, watched: false });
 			await ChatModel.update(isCompanionChat._id, { watched: true });
 			return res.status(200).json({
@@ -208,52 +211,13 @@ 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,
-				lastMessage,
-				mute,
-				sort,
-				seen,
-				total,
-				watched,
-				typing,
-				number,
-				_id,
-				companionId,
-				owner,
-				createdAt,
-				updatedAt,
-				__v,
-			} = isChat;
 			return res.json({
 				status: 'success',
 				code: 200,
 				data: {
-					name,
-					lastName,
-					avatarUrl,
-					color,
-					online,
-					lastMessage,
-					mute,
-					sort,
-					seen,
-					total,
-					watched,
-					typing,
-					number,
-					_id,
-					companionId,
-					owner,
-					createdAt,
-					updatedAt,
-					__v,
+					...isChat,
 				},
 			});
 		}

+ 55 - 10
controllers/messages.js

@@ -71,16 +71,25 @@ const sentMessage = async (req, res, next) => {
 				companionId: userId,
 				owner: id,
 			});
-			const { total } = await MessageModel.getList(userId, {});
+			const { total } = await MessageModel.getList(
+				{ owner: userId, companionId: id },
+				{}
+			);
 			await ChatModel.update(isChat._id, {
 				total,
 				seen: total,
+				watched: false,
 				lastMessage: message,
+				lastMessageCreatedAt: newMessage.createdAt,
 			});
-			const { total: Total } = await MessageModel.getList(id, {});
+			const { total: Total } = await MessageModel.getList(
+				{ owner: id, companionId: userId },
+				{}
+			);
 			await ChatModel.update(isCompanionChat._id, {
 				total: Total,
 				lastMessage: message,
+				lastMessageCreatedAt: newMessage.createdAt,
 			});
 			return res.status(201).json({
 				status: 'success',
@@ -133,16 +142,25 @@ const imageMessage = async (req, res, next) => {
 				companionId: userId,
 				owner: id,
 			});
-			const { total } = await MessageModel.getList(userId, {});
+			const { total } = await MessageModel.getList(
+				{ owner: userId, companionId: id },
+				{}
+			);
 			await ChatModel.update(isChat._id, {
 				total,
 				seen: total,
+				watched: false,
 				lastMessage: imgUrl,
+				lastMessageCreatedAt: newMessage.createdAt,
 			});
-			const { total: Total } = await MessageModel.getList(id, {});
+			const { total: Total } = await MessageModel.getList(
+				{ owner: id, companionId: userId },
+				{}
+			);
 			await ChatModel.update(isCompanionChat._id, {
 				total: Total,
 				lastMessage: imgUrl,
+				lastMessageCreatedAt: newMessage.createdAt,
 			});
 			return res.status(201).json({
 				status: 'success',
@@ -194,16 +212,25 @@ const audioMessage = async (req, res, next) => {
 				companionId: userId,
 				owner: id,
 			});
-			const { total } = await MessageModel.getList(userId, {});
+			const { total } = await MessageModel.getList(
+				{ owner: userId, companionId: id },
+				{}
+			);
 			await ChatModel.update(isChat._id, {
 				total,
 				seen: total,
+				watched: false,
 				lastMessage: audioUrl,
+				lastMessageCreatedAt: newMessage.createdAt,
 			});
-			const { total: Total } = await MessageModel.getList(id, {});
+			const { total: Total } = await MessageModel.getList(
+				{ owner: id, companionId: userId },
+				{}
+			);
 			await ChatModel.update(isCompanionChat._id, {
 				total: Total,
 				lastMessage: audioUrl,
+				lastMessageCreatedAt: newMessage.createdAt,
 			});
 			return res.status(201).json({
 				status: 'success',
@@ -255,16 +282,25 @@ const videoMessage = async (req, res, next) => {
 				companionId: userId,
 				owner: id,
 			});
-			const { total } = await MessageModel.getList(userId, {});
+			const { total } = await MessageModel.getList(
+				{ owner: userId, companionId: id },
+				{}
+			);
 			await ChatModel.update(isChat._id, {
 				total,
 				seen: total,
+				watched: false,
 				lastMessage: videoUrl,
+				lastMessageCreatedAt: newMessage.createdAt,
 			});
-			const { total: Total } = await MessageModel.getList(id, {});
+			const { total: Total } = await MessageModel.getList(
+				{ owner: id, companionId: userId },
+				{}
+			);
 			await ChatModel.update(isCompanionChat._id, {
 				total: Total,
 				lastMessage: videoUrl,
+				lastMessageCreatedAt: newMessage.createdAt,
 			});
 			return res.status(201).json({
 				status: 'success',
@@ -330,16 +366,25 @@ const fileMessage = async (req, res, next) => {
 				companionId: userId,
 				owner: id,
 			});
-			const { total } = await MessageModel.getList(userId, {});
+			const { total } = await MessageModel.getList(
+				{ owner: userId, companionId: id },
+				{}
+			);
 			await ChatModel.update(isChat._id, {
 				total,
 				seen: total,
+				watched: false,
 				lastMessage: fileUrl,
+				lastMessageCreatedAt: newMessage.createdAt,
 			});
-			const { total: Total } = await MessageModel.getList(id, {});
+			const { total: Total } = await MessageModel.getList(
+				{ owner: id, companionId: userId },
+				{}
+			);
 			await ChatModel.update(isCompanionChat._id, {
 				total: Total,
 				lastMessage: fileUrl,
+				lastMessageCreatedAt: newMessage.createdAt,
 			});
 			return res.status(201).json({
 				status: 'success',

+ 13 - 4
controllers/user.js

@@ -1,4 +1,5 @@
 const UserModel = require('../model/user');
+const ChatModel = require('../model/chat');
 const fs = require('fs').promises;
 const path = require('path');
 const Jimp = require('jimp');
@@ -51,7 +52,9 @@ const logIn = async (req, res, next) => {
 		const token = jwt.sign(payload, SECRET_KEY, { expiresIn: '24h' });
 		let registered = true;
 		if (!user.name || !user.lastName || !user.avatarUrl) registered = false;
-		await UserModel.updateUser(id, { token, code: '', online: true });
+		const online = true;
+		await UserModel.updateUser(id, { token, code: '', online });
+		await ChatModel.updateCompanionsChat(id, online);
 		return res.status(200).json({
 			status: 'success',
 			code: 200,
@@ -69,6 +72,7 @@ const logOut = async (req, res, next) => {
 	try {
 		const id = req.user.id;
 		const user = await UserModel.findById(id);
+		const online = new Date();
 		if (!user)
 			return res.status(401).json({
 				status: 'error',
@@ -76,7 +80,8 @@ const logOut = async (req, res, next) => {
 				data: 'UNAUTHORIZED',
 				message: 'Invalid credentials',
 			});
-		await UserModel.updateUser(id, { token: null, online: new Date() });
+		await UserModel.updateUser(id, { token: null, online });
+		await ChatModel.updateCompanionsChat(id, online);
 		return res.status(204).json({});
 	} catch (e) {
 		next(e);
@@ -87,6 +92,8 @@ const online = async (req, res, next) => {
 	try {
 		const id = req.user.id;
 		const user = await UserModel.findById(id);
+		const online = new Date();
+		await ChatModel.updateCompanionsChat(id, online);
 		if (!user)
 			return res.status(401).json({
 				status: 'error',
@@ -94,7 +101,7 @@ const online = async (req, res, next) => {
 				data: 'UNAUTHORIZED',
 				message: 'Invalid credentials',
 			});
-		await UserModel.updateUser(id, { online: new Date() });
+		await UserModel.updateUser(id, { online });
 		return res.status(204).json({});
 	} catch (e) {
 		next(e);
@@ -112,7 +119,9 @@ const getCurrent = async (req, res, next) => {
 				message: 'Invalid credentials',
 			});
 		const id = req.user.id;
-		await UserModel.updateUser(id, { online: true });
+		const online = true;
+		await UserModel.updateUser(id, { online });
+		await ChatModel.updateCompanionsChat(id, online);
 		const updatedUser = await UserModel.findById(id);
 		return res.status(200).json({
 			status: 'success',

+ 6 - 0
model/chat.js

@@ -54,6 +54,11 @@ const updateByOwner = async (id, userId, obj) => {
 	return chat;
 };
 
+const updateCompanionsChat = async (companionId, online) => {
+	const chats = await Chat.updateMany({ companionId }, { online });
+	return chats;
+};
+
 module.exports = {
 	getList,
 	getByField,
@@ -61,4 +66,5 @@ module.exports = {
 	remove,
 	update,
 	updateByOwner,
+	updateCompanionsChat,
 };

+ 1 - 2
model/message.js

@@ -1,10 +1,9 @@
 const Message = require('./schemas/message');
 
 const getList = async (
-	userId,
+	options,
 	{ sortBy, sortByDesc, filter, limit = '500', page = '1', sub }
 ) => {
-	const options = { owner: userId };
 	if (sub) options.subscription = { $all: [sub] };
 	const results = await Message.paginate(options, {
 		limit,

+ 4 - 0
model/schemas/chat.js

@@ -34,6 +34,10 @@ const chatSchema = new Schema(
 			type: String,
 			default: null,
 		},
+		lastMessageCreatedAt: {
+			type: String,
+			default: null,
+		},
 		mute: {
 			type: Boolean,
 			default: false,