Pārlūkot izejas kodu

finished with watched

unknown 2 gadi atpakaļ
vecāks
revīzija
f4dc0d6960
2 mainītis faili ar 55 papildinājumiem un 26 dzēšanām
  1. 39 26
      controllers/messages.js
  2. 16 0
      controllers/user.js

+ 39 - 26
controllers/messages.js

@@ -178,19 +178,22 @@ const removeMessage = async (req, res, next) => {
 			{ owner: userMessage.companionId, companionId: userId },
 			{}
 		);
+		const seenCalculate = (seen, total) => {
+			if (seen <= total) {
+				return seen;
+			} else {
+				return total;
+			}
+		};
 		await ChatModel.update(isChat._id, userId, {
 			total: total,
-			seen: total - isChat.seen > 0 ? isChat.seen : total,
-			seenCompanion:
-				total - isChat.seenCompanion > 0 ? isChat.seenCompanion : total,
+			seen: seenCalculate(isChat.seen, total),
+			seenCompanion: seenCalculate(isChat.seenCompanion, total),
 		});
 		await ChatModel.update(isCompanionChat._id, userMessage.companionId, {
 			total: Total,
-			seen: Total - isCompanionChat.seen > 0 ? isCompanionChat.seen : Total,
-			seenCompanion:
-				total - isCompanionChat.seenCompanion > 0
-					? isCompanionChat.seenCompanion
-					: total,
+			seen: seenCalculate(isCompanionChat.seen, Total),
+			seenCompanion: seenCalculate(isCompanionChat.seenCompanion, Total),
 		});
 		return res.json({
 			status: 'success',
@@ -206,7 +209,6 @@ const removeSelected = async (req, res, next) => {
 	try {
 		const userId = req.user.id;
 		const { selectedArr, companionId } = req.body;
-		const totalDeleted = selectedArr.length;
 		const toDeleteMessage = async (id) => {
 			const userMessage = await MessageModel.remove(id, userId);
 			await MessageModel.findByFieldsAndUpdateMany(companionId, id, userId, {
@@ -255,19 +257,26 @@ const removeSelected = async (req, res, next) => {
 			{ owner: userId, companionId },
 			{}
 		);
+		const { total: Total } = await MessageModel.getList(
+			{ owner: companionId, companionId: userId },
+			{}
+		);
+		const seenCalculate = (seen, total) => {
+			if (seen <= total) {
+				return seen;
+			} else {
+				return total;
+			}
+		};
 		await ChatModel.update(isChat._id, userId, {
 			total: total,
-			seen: total - isChat.seen > 0 ? total - isChat.seen : 0,
-			seenCompanion:
-				total - isChat.seenCompanion > 0 ? total - isChat.seenCompanion : 0,
+			seen: seenCalculate(isChat.seen, total),
+			seenCompanion: seenCalculate(isChat.seenCompanion, total),
 		});
 		await ChatModel.update(isCompanionChat._id, companionId, {
-			total: total,
-			seen: total - isCompanionChat.seen > 0 ? total - isCompanionChat.seen : 0,
-			seenCompanion:
-				total - isCompanionChat.seenCompanion > 0
-					? total - isCompanionChat.seenCompanion
-					: 0,
+			total: Total,
+			seen: seenCalculate(isCompanionChat.seen, Total),
+			seenCompanion: seenCalculate(isCompanionChat.seenCompanion, Total),
 		});
 		return res.json({
 			status: 'success',
@@ -485,8 +494,12 @@ const sentMessageForward = async (req, res, next) => {
 				number: user.number,
 				type: 'text',
 				fullType: userMessage.fullType,
-				forwardName: userMessage.name,
-				forwardLastName: userMessage.lastName,
+				forwardName:
+					user.number === userMessage.number ? user.name : isCompanionUser.name,
+				forwardLastName:
+					user.number === userMessage.number
+						? user.lastName
+						: isCompanionUser.lastName,
 				oldId: userMessage._id,
 				idTime,
 				companionIdFlow: userId,
@@ -506,12 +519,12 @@ const sentMessageForward = async (req, res, next) => {
 				number: user.number,
 				type: 'text',
 				fullType: userMessage.fullType,
-				forwardName: isForwardHasChatWithCompanion
-					? isForwardHasChatWithCompanion.name
-					: isCompanionUser.name,
-				forwardLastName: isForwardHasChatWithCompanion
-					? isForwardHasChatWithCompanion.lastName
-					: isCompanionUser.lastName,
+				forwardName:
+					user.number === userMessage.number ? user.name : isCompanionUser.name,
+				forwardLastName:
+					user.number === userMessage.number
+						? user.lastName
+						: isCompanionUser.lastName,
 				oldId: companionMessage._id,
 				idTime,
 				companionIdFlow: userId,

+ 16 - 0
controllers/user.js

@@ -159,6 +159,22 @@ const updateCredentials = async (req, res, next) => {
 			},
 			{ replyName: name, replyLastName: lastName }
 		);
+		await MessageModel.updateOwnerMessages(
+			{
+				companionIdForwardToAndFrom: { $eq: user._id },
+				forwardName: { $eq: user.name },
+				forwardLastName: { $eq: user.lastName },
+			},
+			{ forwardName: name, forwardLastName: lastName }
+		);
+		await MessageModel.updateOwnerMessages(
+			{
+				number: { $eq: user.number },
+				forwardName: { $eq: user.name },
+				forwardLastName: { $eq: user.lastName },
+			},
+			{ forwardName: name, forwardLastName: lastName }
+		);
 		return res.status(200).json({
 			data: {
 				token,