Browse Source

make some fixes

unknown 2 years ago
parent
commit
374bc4941e
3 changed files with 29 additions and 43 deletions
  1. 17 38
      controllers/chats.js
  2. 12 5
      controllers/user.js
  3. BIN
      files/telegramReceive.mp3

+ 17 - 38
controllers/chats.js

@@ -34,32 +34,18 @@ const startChat = async (req, res, next) => {
 		const user = req.user;
 		const userId = user.id;
 		const companion = await UserModel.findById(id);
-		const isUser = await ChatModel.getByField(id, userId);
-		const isCompanion = await ChatModel.getByField(userId, id);
-		const {
-			name,
-			lastName,
-			originalName,
-			originalLastName,
-			avatarUrl,
-			avatarsArr,
-			color,
-			online,
-			number,
-		} = companion;
-		const {
-			name: Name,
-			lastName: LastName,
-			originalName: OriginalName,
-			originalLastName: OriginalLastName,
-			avatarUrl: AvatarUrl,
-			avatarsArr: AvatarsArr,
-			color: Color,
-			online: Online,
-			number: Number,
-		} = user;
-		if (companion && isUser && isCompanion) {
-			await ChatModel.update(isUser._id, userId, {
+		const userChat = await ChatModel.getByField(id, userId);
+		if (userChat) {
+			return res.json({
+				status: 'success',
+				code: 200,
+				data: userChat,
+			});
+		}
+		if (!userChat) {
+			const {
+				name,
+				lastName,
 				originalName,
 				originalLastName,
 				avatarUrl,
@@ -67,8 +53,10 @@ const startChat = async (req, res, next) => {
 				color,
 				online,
 				number,
-			});
-			await ChatModel.update(isCompanion._id, id, {
+			} = companion;
+			const {
+				name: Name,
+				lastName: LastName,
 				originalName: OriginalName,
 				originalLastName: OriginalLastName,
 				avatarUrl: AvatarUrl,
@@ -76,16 +64,7 @@ const startChat = async (req, res, next) => {
 				color: Color,
 				online: Online,
 				number: Number,
-			});
-			const updatedChat = await ChatModel.getByField(id, userId);
-			return res.status(200).json({
-				status: 'success',
-				code: 200,
-				data: updatedChat,
-			});
-		}
-
-		if (companion && !isUser && !isCompanion) {
+			} = user;
 			const newChat = await ChatModel.add({
 				name,
 				lastName,

+ 12 - 5
controllers/user.js

@@ -123,13 +123,20 @@ const getCurrent = async (req, res, next) => {
 			});
 		const id = req.user.id;
 		const online = true;
-		await UserModel.updateUser(id, { online });
-		await ChatModel.updateCompanionsChat(id, { online });
-		const updatedUser = await UserModel.findById(id);
-		return res.status(200).json({
+		if (user.online === String) {
+			await UserModel.updateUser(id, { online });
+			await ChatModel.updateCompanionsChat(id, { online });
+			const updatedUser = await UserModel.findById(id);
+			return res.status(200).json({
+				status: 'success',
+				code: 200,
+				data: updatedUser,
+			});
+		}
+		return res.json({
 			status: 'success',
 			code: 200,
-			data: updatedUser,
+			data: user,
 		});
 	} catch (e) {
 		next(e);

BIN
files/telegramReceive.mp3