Ver código fonte

work on delet

unknown 2 anos atrás
pai
commit
31b75c82b2
3 arquivos alterados com 73 adições e 39 exclusões
  1. 67 33
      controllers/messages.js
  2. 2 6
      model/message.js
  3. 4 0
      model/schemas/message.js

+ 67 - 33
controllers/messages.js

@@ -122,14 +122,22 @@ const removeMessage = async (req, res, next) => {
 		const userId = req.user.id;
 		const userMessage = await MessageModel.remove(id, userId);
 		await MessageModel.findByFieldsAndUpdateMany(
-			userMessage.companionId,
-			id,
-			userId,
 			{
+				oldId: userMessage._id,
+			},
+			{
+				deleted: true,
 				replyMessage: null,
 				replyName: null,
 				replyLastName: null,
 				replyCaption: null,
+				forwardMessage: null,
+				forwardMessage: null,
+				forwardName: null,
+				forwardLastName: null,
+				forwardCaption: null,
+				fullType: null,
+				companionIdForwardToAndFrom: null,
 				oldId: null,
 			}
 		);
@@ -138,23 +146,32 @@ const removeMessage = async (req, res, next) => {
 			userMessage.idTime,
 			userMessage.companionId
 		);
-		await MessageModel.removeByFields(
-			userId,
-			userMessage.idTime,
-			userMessage.companionId
-		);
 		await MessageModel.findByFieldsAndUpdateMany(
-			userId,
-			companionMessage._id,
-			userMessage.companionId,
 			{
+				oldId: companionMessage._id,
+			},
+			{
+				deleted: true,
 				replyMessage: null,
 				replyName: null,
 				replyLastName: null,
 				replyCaption: null,
+				forwardMessage: null,
+				forwardMessage: null,
+				forwardName: null,
+				forwardLastName: null,
+				forwardCaption: null,
+				fullType: null,
+				companionIdForwardToAndFrom: null,
 				oldId: null,
 			}
 		);
+		await MessageModel.removeByFields(
+			userId,
+			userMessage.idTime,
+			userMessage.companionId
+		);
+
 		if (userMessage.type !== 'text') {
 			const params = {
 				Bucket: AWS_BUCKET_NAME,
@@ -224,27 +241,48 @@ const removeSelected = async (req, res, next) => {
 		const { selectedArr, companionId } = req.body;
 		const toDeleteMessage = async (id) => {
 			const userMessage = await MessageModel.remove(id, userId);
-			await MessageModel.findByFieldsAndUpdateMany(companionId, id, userId, {
-				replyMessage: null,
-				replyName: null,
-				replyLastName: null,
-				replyCaption: null,
-				oldId: null,
-			});
+			await MessageModel.findByFieldsAndUpdateMany(
+				{
+					oldId: userMessage._id,
+				},
+				{
+					deleted: true,
+					replyMessage: null,
+					replyName: null,
+					replyLastName: null,
+					replyCaption: null,
+					forwardMessage: null,
+					forwardMessage: null,
+					forwardName: null,
+					forwardLastName: null,
+					forwardCaption: null,
+					fullType: null,
+					companionIdForwardToAndFrom: null,
+					oldId: null,
+				}
+			);
 			const companionMessage = await MessageModel.findByFields(
 				userId,
 				userMessage.idTime,
 				companionId
 			);
 			await MessageModel.findByFieldsAndUpdateMany(
-				userId,
-				companionMessage._id,
-				companionId,
 				{
+					oldId: companionMessage._id,
+				},
+				{
+					deleted: true,
 					replyMessage: null,
 					replyName: null,
 					replyLastName: null,
 					replyCaption: null,
+					forwardMessage: null,
+					forwardMessage: null,
+					forwardName: null,
+					forwardLastName: null,
+					forwardCaption: null,
+					fullType: null,
+					companionIdForwardToAndFrom: null,
 					oldId: null,
 				}
 			);
@@ -493,10 +531,6 @@ const sentMessageForward = async (req, res, next) => {
 			userMessage.companionId
 		);
 		const isCompanionUser = await UserModel.findById(userMessage.companionId);
-		const isForwardHasChatWithCompanion = await ChatModel.getByField(
-			userMessage.companionId,
-			companionIdForwardToAndFrom
-		);
 		const isForwardChat = await ChatModel.getByField(
 			userId,
 			companionIdForwardToAndFrom
@@ -652,7 +686,7 @@ const imageMessage = async (req, res, next) => {
 				total,
 				seen: total,
 				watched: false,
-				lastMessage: imgUrl,
+				lastMessage: fullType,
 				lastMessageCreatedAt: newMessage.createdAt,
 			});
 			const { total: Total } = await MessageModel.getList(
@@ -662,7 +696,7 @@ const imageMessage = async (req, res, next) => {
 			await ChatModel.update(isCompanionChat._id, id, {
 				total: Total,
 				seenCompanion: Total,
-				lastMessage: imgUrl,
+				lastMessage: fullType,
 				lastMessageCreatedAt: newMessage.createdAt,
 			});
 			return res.status(201).json({
@@ -739,7 +773,7 @@ const audioMessage = async (req, res, next) => {
 				total,
 				seen: total,
 				watched: false,
-				lastMessage: audioUrl,
+				lastMessage: fullType,
 				lastMessageCreatedAt: newMessage.createdAt,
 			});
 			const { total: Total } = await MessageModel.getList(
@@ -749,7 +783,7 @@ const audioMessage = async (req, res, next) => {
 			await ChatModel.update(isCompanionChat._id, id, {
 				total: Total,
 				seenCompanion: Total,
-				lastMessage: audioUrl,
+				lastMessage: fullType,
 				lastMessageCreatedAt: newMessage.createdAt,
 			});
 			return res.status(201).json({
@@ -826,7 +860,7 @@ const videoMessage = async (req, res, next) => {
 				total,
 				seen: total,
 				watched: false,
-				lastMessage: videoUrl,
+				lastMessage: fullType,
 				lastMessageCreatedAt: newMessage.createdAt,
 			});
 			const { total: Total } = await MessageModel.getList(
@@ -836,7 +870,7 @@ const videoMessage = async (req, res, next) => {
 			await ChatModel.update(isCompanionChat._id, id, {
 				total: Total,
 				seenCompanion: Total,
-				lastMessage: videoUrl,
+				lastMessage: fullType,
 				lastMessageCreatedAt: newMessage.createdAt,
 			});
 			return res.status(201).json({
@@ -926,7 +960,7 @@ const fileMessage = async (req, res, next) => {
 				total,
 				seen: total,
 				watched: false,
-				lastMessage: fileUrl,
+				lastMessage: fullType,
 				lastMessageCreatedAt: newMessage.createdAt,
 			});
 			const { total: Total } = await MessageModel.getList(
@@ -936,7 +970,7 @@ const fileMessage = async (req, res, next) => {
 			await ChatModel.update(isCompanionChat._id, id, {
 				total: Total,
 				seenCompanion: Total,
-				lastMessage: fileUrl,
+				lastMessage: fullType,
 				lastMessageCreatedAt: newMessage.createdAt,
 			});
 			return res.status(201).json({

+ 2 - 6
model/message.js

@@ -69,12 +69,8 @@ const findByFieldsAndUpdate = async (companionId, idTime, userId, body) => {
 	return message;
 };
 
-const findByFieldsAndUpdateMany = async (companionId, oldId, userId, body) => {
-	const messages = await Message.find({
-		companionId,
-		oldId,
-		owner: userId,
-	}).updateMany({ ...body });
+const findByFieldsAndUpdateMany = async (obj, body) => {
+	const messages = await Message.find({ ...obj }).updateMany({ ...body });
 	return messages;
 };
 

+ 4 - 0
model/schemas/message.js

@@ -106,6 +106,10 @@ const messageSchema = new Schema(
 			type: Boolean,
 			default: false,
 		},
+		deleted: {
+			type: Boolean,
+			default: false,
+		},
 		edited: {
 			type: Boolean,
 			default: false,