Browse Source

add few small menues

unknown 2 years ago
parent
commit
70a29876f9
6 changed files with 27 additions and 48 deletions
  1. 10 7
      controllers/chats.js
  2. 1 1
      controllers/contacts.js
  3. 10 10
      controllers/messages.js
  4. 5 7
      controllers/user.js
  5. 1 7
      model/chat.js
  6. 0 16
      model/user.js

+ 10 - 7
controllers/chats.js

@@ -42,13 +42,13 @@ const startChat = async (req, res, next) => {
 			number: Number,
 		} = user;
 		if (companion && isUser && isCompanion) {
-			await ChatModel.update(isUser._id, {
+			await ChatModel.update(isUser._id, userId, {
 				avatarUrl,
 				color,
 				online,
 				number,
 			});
-			await ChatModel.update(isCompanion._id, {
+			await ChatModel.update(isCompanion._id, id, {
 				avatarUrl: AvatarUrl,
 				color: Color,
 				online: Online,
@@ -133,7 +133,7 @@ const muteChat = async (req, res, next) => {
 		const isChat = await ChatModel.getByField(id, userId);
 		if (isChat) {
 			const { _id, mute } = isChat;
-			await ChatModel.update(_id, { mute: !mute });
+			await ChatModel.update(_id, userId, { mute: !mute });
 			return res.status(200).json({
 				status: 'success',
 				code: 200,
@@ -152,7 +152,7 @@ const sortChat = async (req, res, next) => {
 		const isChat = await ChatModel.getByField(id, userId);
 		if (isChat) {
 			const { _id, sort } = isChat;
-			await ChatModel.update(_id, { sort: !sort });
+			await ChatModel.update(_id, userId, { sort: !sort });
 			return res.status(200).json({
 				status: 'success',
 				code: 200,
@@ -176,8 +176,11 @@ const seenChat = async (req, res, next) => {
 		);
 		if (isChat && isMessages && isCompanionChat) {
 			const { total } = isMessages;
-			await ChatModel.update(isChat._id, { seen: total, watched: false });
-			await ChatModel.update(isCompanionChat._id, { watched: true });
+			await ChatModel.update(isChat._id, userId, {
+				seen: total,
+				watched: false,
+			});
+			await ChatModel.update(isCompanionChat._id, id, { watched: true });
 			return res.status(200).json({
 				status: 'success',
 				code: 200,
@@ -195,7 +198,7 @@ const typingChat = async (req, res, next) => {
 		const userId = req.user.id;
 		const isCompanionChat = await ChatModel.getByField(userId, id);
 		if (isCompanionChat) {
-			await ChatModel.update(isCompanionChat._id, { typing });
+			await ChatModel.update(isCompanionChat._id, id, { typing });
 			return res.status(200).json({
 				status: 'success',
 				code: 200,

+ 1 - 1
controllers/contacts.js

@@ -83,7 +83,7 @@ const updateContact = async (req, res, next) => {
 		const chat = await ChatModel.update(_id, userId, { name, lastName });
 		if (contact && chat) {
 			return res.status(200).json({
-				data: { _id },
+				data: {},
 			});
 		} else {
 			return res.status(404).json({

+ 10 - 10
controllers/messages.js

@@ -75,7 +75,7 @@ const sentMessage = async (req, res, next) => {
 				{ owner: userId, companionId: id },
 				{}
 			);
-			await ChatModel.update(isChat._id, {
+			await ChatModel.update(isChat._id, userId, {
 				total,
 				seen: total,
 				watched: false,
@@ -86,7 +86,7 @@ const sentMessage = async (req, res, next) => {
 				{ owner: id, companionId: userId },
 				{}
 			);
-			await ChatModel.update(isCompanionChat._id, {
+			await ChatModel.update(isCompanionChat._id, id, {
 				total: Total,
 				lastMessage: message,
 				lastMessageCreatedAt: newMessage.createdAt,
@@ -146,7 +146,7 @@ const imageMessage = async (req, res, next) => {
 				{ owner: userId, companionId: id },
 				{}
 			);
-			await ChatModel.update(isChat._id, {
+			await ChatModel.update(isChat._id, userId, {
 				total,
 				seen: total,
 				watched: false,
@@ -157,7 +157,7 @@ const imageMessage = async (req, res, next) => {
 				{ owner: id, companionId: userId },
 				{}
 			);
-			await ChatModel.update(isCompanionChat._id, {
+			await ChatModel.update(isCompanionChat._id, id, {
 				total: Total,
 				lastMessage: imgUrl,
 				lastMessageCreatedAt: newMessage.createdAt,
@@ -216,7 +216,7 @@ const audioMessage = async (req, res, next) => {
 				{ owner: userId, companionId: id },
 				{}
 			);
-			await ChatModel.update(isChat._id, {
+			await ChatModel.update(isChat._id, userId, {
 				total,
 				seen: total,
 				watched: false,
@@ -227,7 +227,7 @@ const audioMessage = async (req, res, next) => {
 				{ owner: id, companionId: userId },
 				{}
 			);
-			await ChatModel.update(isCompanionChat._id, {
+			await ChatModel.update(isCompanionChat._id, id, {
 				total: Total,
 				lastMessage: audioUrl,
 				lastMessageCreatedAt: newMessage.createdAt,
@@ -286,7 +286,7 @@ const videoMessage = async (req, res, next) => {
 				{ owner: userId, companionId: id },
 				{}
 			);
-			await ChatModel.update(isChat._id, {
+			await ChatModel.update(isChat._id, userId, {
 				total,
 				seen: total,
 				watched: false,
@@ -297,7 +297,7 @@ const videoMessage = async (req, res, next) => {
 				{ owner: id, companionId: userId },
 				{}
 			);
-			await ChatModel.update(isCompanionChat._id, {
+			await ChatModel.update(isCompanionChat._id, id, {
 				total: Total,
 				lastMessage: videoUrl,
 				lastMessageCreatedAt: newMessage.createdAt,
@@ -370,7 +370,7 @@ const fileMessage = async (req, res, next) => {
 				{ owner: userId, companionId: id },
 				{}
 			);
-			await ChatModel.update(isChat._id, {
+			await ChatModel.update(isChat._id, userId, {
 				total,
 				seen: total,
 				watched: false,
@@ -381,7 +381,7 @@ const fileMessage = async (req, res, next) => {
 				{ owner: id, companionId: userId },
 				{}
 			);
-			await ChatModel.update(isCompanionChat._id, {
+			await ChatModel.update(isCompanionChat._id, id, {
 				total: Total,
 				lastMessage: fileUrl,
 				lastMessageCreatedAt: newMessage.createdAt,

+ 5 - 7
controllers/user.js

@@ -17,7 +17,7 @@ const createNewUser = async (req, res, next) => {
 		const { number, country } = req.body;
 		const isExist = await UserModel.findByNumber(number);
 		if (isExist) {
-			await UserModel.updateCode(isExist._id, code);
+			await UserModel.updateUser(isExist._id, { code });
 		} else {
 			await UserModel.createUser({ number, country, color, code });
 		}
@@ -135,12 +135,10 @@ const getCurrent = async (req, res, next) => {
 
 const updateCredentials = async (req, res, next) => {
 	try {
-		const { id, token } = req.user;
-		await UserModel.updateCredentials(id, req.body);
+		const { id } = req.user;
+		await UserModel.updateUser(id, req.body);
 		return res.status(200).json({
-			data: {
-				token,
-			},
+			data: {},
 		});
 	} catch (e) {
 		next(e);
@@ -166,7 +164,7 @@ const updateAvatar = async (req, res, next) => {
 		await createFolderIsExist(path.join(DIR_IMAGES, userId));
 		await fs.rename(pathToFile, path.join(DIR_IMAGES, userId, newNameAvatar));
 		const avatarUrl = path.normalize(path.join(userId, newNameAvatar));
-		await UserModel.updateAvatar(userId, avatarUrl);
+		await UserModel.updateUser(userId, { avatarUrl });
 		return res.status(200).json({
 			status: 'success',
 			code: 200,

+ 1 - 7
model/chat.js

@@ -44,12 +44,7 @@ const remove = async (id, userId) => {
 	return removedChat;
 };
 
-const update = async (id, obj) => {
-	const chat = await Chat.updateOne({ _id: id }, { ...obj });
-	return chat;
-};
-
-const updateByOwner = async (id, userId, obj) => {
+const update = async (id, userId, obj) => {
 	const chat = await Chat.updateOne({ _id: id, owner: userId }, { ...obj });
 	return chat;
 };
@@ -65,6 +60,5 @@ module.exports = {
 	add,
 	remove,
 	update,
-	updateByOwner,
 	updateCompanionsChat,
 };

+ 0 - 16
model/user.js

@@ -10,29 +10,13 @@ const createUser = async (body) => {
 	const user = new User({ ...body });
 	return await user.save();
 };
-const updateCode = async (id, code) => {
-	return await User.updateOne({ _id: id }, { code });
-};
-const updateToken = async (id, token) => {
-	return await User.updateOne({ _id: id }, { token });
-};
 const updateUser = async (id, obj) => {
 	return await User.updateOne({ _id: id }, { ...obj });
 };
-const updateCredentials = async (id, body) => {
-	return await User.findByIdAndUpdate({ _id: id }, { ...body }, { new: true });
-};
-const updateAvatar = async (id, avatarUrl) => {
-	return await User.updateOne({ _id: id }, { avatarUrl });
-};
 
 module.exports = {
 	findByNumber,
 	createUser,
-	updateCode,
-	updateToken,
 	updateUser,
-	updateCredentials,
-	updateAvatar,
 	findById,
 };