Explorar el Código

make some fix with last message

unknown hace 2 años
padre
commit
b9ef34e8e3
Se han modificado 1 ficheros con 28 adiciones y 2 borrados
  1. 28 2
      controllers/messages.js

+ 28 - 2
controllers/messages.js

@@ -170,7 +170,7 @@ const removeMessage = async (req, res, next) => {
 			userId,
 			userMessage.companionId
 		);
-		const { total } = await MessageModel.getList(
+		const { total, messages } = await MessageModel.getList(
 			{ owner: userId, companionId: userMessage.companionId },
 			{}
 		);
@@ -185,15 +185,28 @@ const removeMessage = async (req, res, next) => {
 				return total;
 			}
 		};
+		const isLastMessages = messages.slice(-1)[0] ? messages.slice(-1)[0] : null;
 		await ChatModel.update(isChat._id, userId, {
 			total: total,
 			seen: seenCalculate(isChat.seen, total),
 			seenCompanion: seenCalculate(isChat.seenCompanion, total),
+			lastMessage: isLastMessages
+				? isLastMessages.type === 'text'
+					? isLastMessages.message
+					: isLastMessages.fullType
+				: null,
+			lastMessageCreatedAt: isLastMessages ? isLastMessages.createdAt : null,
 		});
 		await ChatModel.update(isCompanionChat._id, userMessage.companionId, {
 			total: Total,
 			seen: seenCalculate(isCompanionChat.seen, Total),
 			seenCompanion: seenCalculate(isCompanionChat.seenCompanion, Total),
+			lastMessage: isLastMessages
+				? isLastMessages.type === 'text'
+					? isLastMessages.message
+					: isLastMessages.fullType
+				: null,
+			lastMessageCreatedAt: isLastMessages ? isLastMessages.createdAt : null,
 		});
 		return res.json({
 			status: 'success',
@@ -253,7 +266,7 @@ const removeSelected = async (req, res, next) => {
 		await selectedArr.forEach(async (id) => await toDeleteMessage(id));
 		const isChat = await ChatModel.getByField(companionId, userId);
 		const isCompanionChat = await ChatModel.getByField(userId, companionId);
-		const { total } = await MessageModel.getList(
+		const { total, messages } = await MessageModel.getList(
 			{ owner: userId, companionId },
 			{}
 		);
@@ -268,15 +281,28 @@ const removeSelected = async (req, res, next) => {
 				return total;
 			}
 		};
+		const isLastMessages = messages.slice(-1)[0] ? messages.slice(-1)[0] : null;
 		await ChatModel.update(isChat._id, userId, {
 			total: total,
 			seen: seenCalculate(isChat.seen, total),
 			seenCompanion: seenCalculate(isChat.seenCompanion, total),
+			lastMessage: isLastMessages
+				? isLastMessages.type === 'text'
+					? isLastMessages.message
+					: isLastMessages.fullType
+				: null,
+			lastMessageCreatedAt: isLastMessages ? isLastMessages.createdAt : null,
 		});
 		await ChatModel.update(isCompanionChat._id, companionId, {
 			total: Total,
 			seen: seenCalculate(isCompanionChat.seen, Total),
 			seenCompanion: seenCalculate(isCompanionChat.seenCompanion, Total),
+			lastMessage: isLastMessages
+				? isLastMessages.type === 'text'
+					? isLastMessages.message
+					: isLastMessages.fullType
+				: null,
+			lastMessageCreatedAt: isLastMessages ? isLastMessages.createdAt : null,
 		});
 		return res.json({
 			status: 'success',