Browse Source

add abilyty to delet chat

unknown 2 years ago
parent
commit
c0b7b8c065

BIN
audios/62275e151f60894d30cce0ca/audioMessage1649337587773.mp3


+ 114 - 55
controllers/chats.js

@@ -10,38 +10,38 @@ const listChats = async (req, res, next) => {
 			req.query
 		);
 
-		const messages = await MessageModel.getLastMessagesList(userId);
+		// 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 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, createdAt, updatedAt }) => {
-				const obj = {
-					message,
-					companionId,
-					createdAt,
-					updatedAt,
-				};
-				if (acc.length === 0) {
-					acc.push(obj);
-					return acc;
-				}
-				if (acc.some((el) => el.companionId === companionId)) {
-					const i = acc.findIndex((el) => {
-						return el.companionId === companionId;
-					});
-					acc[i] = obj;
-					return acc;
-				}
-				acc.push(obj);
-				return acc;
-			},
-			[]
-		);
+		// const lastMessages = await messages.reduce(
+		// 	(acc, { message, companionId, createdAt, updatedAt }) => {
+		// 		const obj = {
+		// 			message,
+		// 			companionId,
+		// 			createdAt,
+		// 			updatedAt,
+		// 		};
+		// 		if (acc.length === 0) {
+		// 			acc.push(obj);
+		// 			return acc;
+		// 		}
+		// 		if (acc.some((el) => el.companionId === companionId)) {
+		// 			const i = acc.findIndex((el) => {
+		// 				return el.companionId === companionId;
+		// 			});
+		// 			acc[i] = obj;
+		// 			return acc;
+		// 		}
+		// 		acc.push(obj);
+		// 		return acc;
+		// 	},
+		// 	[]
+		// );
 
 		return res.json({
 			status: 'success',
@@ -51,8 +51,8 @@ const listChats = async (req, res, next) => {
 				limit,
 				page,
 				chats,
-				lastMessages,
-				lastOnline,
+				lastMessages: [{ message: 'fff' }, { message: 'ddd' }],
+				lastOnline: [new Date(), new Date()],
 			},
 		});
 	} catch (e) {
@@ -77,7 +77,7 @@ const startChat = async (req, res, next) => {
 			online: Online,
 			number: Number,
 		} = user;
-		if (companion && (isUser || isCompanion)) {
+		if (companion && isUser && isCompanion) {
 			await ChatModel.update(isUser._id, {
 				avatarUrl,
 				color,
@@ -98,31 +98,88 @@ const startChat = async (req, res, next) => {
 			});
 		}
 
-		if (companion && !isUser && !isCompanion) {
-			const newChat = await ChatModel.add({
-				name,
-				lastName,
-				avatarUrl,
-				color,
-				online,
-				number,
-				companionId: id,
-				owner: userId,
-			});
-			await ChatModel.add({
-				name: Name,
-				lastName: LastName,
-				avatarUrl: AvatarUrl,
-				color: Color,
-				online: Online,
-				number: Number,
-				companionId: userId,
-				owner: id,
-			});
+		if (companion) {
+			const newChat =
+				!isUser &&
+				(await ChatModel.add({
+					name,
+					lastName,
+					avatarUrl,
+					color,
+					online,
+					number,
+					companionId: id,
+					owner: userId,
+				}));
+			!isCompanion &&
+				(await ChatModel.add({
+					name: Name,
+					lastName: LastName,
+					avatarUrl: AvatarUrl,
+					color: Color,
+					online: Online,
+					number: Number,
+					companionId: userId,
+					owner: id,
+				}));
 			return res.status(201).json({
 				status: 'success',
 				code: 201,
-				data: newChat,
+				data: newChat ? newChat : isUser,
+			});
+		}
+	} catch (e) {
+		next(e);
+	}
+};
+
+const removeChatForBoth = async (req, res, next) => {
+	try {
+		const companionId = req.params.id;
+		const userId = req.user.id;
+		const isUserChat = await ChatModel.getByField(companionId, userId);
+		const isCompanionChat = await ChatModel.getByField(userId, companionId);
+		if (isUserChat || isCompanionChat) {
+			isUserChat && (await MessageModel.removeAll(userId));
+			isCompanionChat && (await MessageModel.removeAll(companionId));
+			isUserChat && (await ChatModel.remove(isUserChat._id, userId));
+			isCompanionChat &&
+				(await ChatModel.remove(isCompanionChat._id, companionId));
+			return res.json({
+				status: 'success',
+				code: 200,
+				data: {},
+			});
+		} else {
+			return res.status(404).json({
+				status: 'error',
+				code: 404,
+				data: 'Not Found',
+			});
+		}
+	} catch (e) {
+		next(e);
+	}
+};
+
+const removeChatForMe = async (req, res, next) => {
+	try {
+		const companionId = req.params.id;
+		const userId = req.user.id;
+		const isUserChat = await ChatModel.getByField(companionId, userId);
+		if (isUserChat) {
+			await MessageModel.removeAll(userId);
+			await ChatModel.remove(isUserChat._id, userId);
+			return res.json({
+				status: 'success',
+				code: 200,
+				data: {},
+			});
+		} else {
+			return res.status(404).json({
+				status: 'error',
+				code: 404,
+				data: 'Not Found',
 			});
 		}
 	} catch (e) {
@@ -267,6 +324,8 @@ const chatById = async (req, res, next) => {
 module.exports = {
 	listChats,
 	startChat,
+	removeChatForBoth,
+	removeChatForMe,
 	muteChat,
 	sortChat,
 	seenChat,

BIN
images/624eeb58f5448755b08ec0a2/download.jpg


+ 9 - 0
model/chat.js

@@ -36,6 +36,14 @@ const add = async (obj) => {
 	return chat;
 };
 
+const remove = async (id, userId) => {
+	const removedChat = await Chat.findByIdAndRemove({
+		_id: id,
+		owner: userId,
+	});
+	return removedChat;
+};
+
 const update = async (id, obj) => {
 	const chat = await Chat.updateOne({ _id: id }, { ...obj });
 	return chat;
@@ -50,6 +58,7 @@ module.exports = {
 	getList,
 	getByField,
 	add,
+	remove,
 	update,
 	updateByOwner,
 };

+ 8 - 0
model/message.js

@@ -65,6 +65,13 @@ const remove = async (id, userId) => {
 	return removedMessage;
 };
 
+const removeAll = async (userId) => {
+	const removedAllMessages = await Message.remove({
+		owner: userId,
+	});
+	return removedAllMessages;
+};
+
 module.exports = {
 	getList,
 	getListById,
@@ -73,4 +80,5 @@ module.exports = {
 	getLastMessagesList,
 	add,
 	remove,
+	removeAll,
 };

+ 3 - 1
routes/chats.js

@@ -11,6 +11,8 @@ router
 	.patch('/mute/', guard, controllers.muteChat)
 	.patch('/sort/', guard, controllers.sortChat)
 	.patch('/seen/', guard, controllers.seenChat)
-	.patch('/typing/', guard, validation.typingChat, controllers.typingChat);
+	.patch('/typing/', guard, validation.typingChat, controllers.typingChat)
+	.delete('/both/:id', guard, controllers.removeChatForBoth)
+	.delete('/me/:id', guard, controllers.removeChatForMe);
 
 module.exports = router;

BIN
videos/62275dbc1f60894d30cce0c9/videoMessage1649337883561.mp4


BIN
videos/62275e151f60894d30cce0ca/videoMessage1649337602093.mp4