Browse Source

add ability change name and surename

unknown 2 years ago
parent
commit
64c73be093

+ 11 - 14
controllers/contacts.js

@@ -1,6 +1,7 @@
 const ContactModel = require('../model/contact');
 const UserModel = require('../model/user');
 const ChatModel = require('../model/chat');
+const MessageModel = require('../model/message');
 
 const listContacts = async (req, res, next) => {
 	try {
@@ -78,21 +79,17 @@ const addContact = async (req, res, next) => {
 
 const updateContact = async (req, res, next) => {
 	try {
-		const { id, _id, name, lastName } = req.body;
+		const { id, _id, name, lastName, companionId } = req.body;
 		const userId = req.user.id;
-		const contact = await ContactModel.update(id, userId, { name, lastName });
-		const chat = await ChatModel.update(_id, userId, { name, lastName });
-		if (contact && chat) {
-			return res.status(200).json({
-				data: {},
-			});
-		} else {
-			return res.status(404).json({
-				status: 'error',
-				code: 404,
-				data: 'Not Found',
-			});
-		}
+		await ContactModel.update(id, userId, { name, lastName });
+		await ChatModel.update(_id, userId, { name, lastName });
+		await MessageModel.updateOwnerMessages(
+			{ companionId, companionIdFlow: { $ne: userId }, owner: userId },
+			{ name, lastName }
+		);
+		return res.status(200).json({
+			data: {},
+		});
 	} catch (e) {
 		next(e);
 	}

+ 10 - 0
controllers/messages.js

@@ -108,6 +108,7 @@ const sentMessage = async (req, res, next) => {
 				number,
 				type: 'text',
 				idTime,
+				companionIdFlow: userId,
 				companionId: id,
 				owner: userId,
 			});
@@ -120,6 +121,7 @@ const sentMessage = async (req, res, next) => {
 				number,
 				type: 'text',
 				idTime,
+				companionIdFlow: userId,
 				companionId: userId,
 				owner: id,
 			});
@@ -182,6 +184,7 @@ const imageMessage = async (req, res, next) => {
 				type: 'image',
 				fullType,
 				idTime,
+				companionIdFlow: userId,
 				companionId: id,
 				owner: userId,
 			});
@@ -195,6 +198,7 @@ const imageMessage = async (req, res, next) => {
 				type: 'image',
 				fullType,
 				idTime,
+				companionIdFlow: userId,
 				companionId: userId,
 				owner: id,
 			});
@@ -256,6 +260,7 @@ const audioMessage = async (req, res, next) => {
 				type: 'audio',
 				fullType,
 				idTime,
+				companionIdFlow: userId,
 				companionId: id,
 				owner: userId,
 			});
@@ -269,6 +274,7 @@ const audioMessage = async (req, res, next) => {
 				type: 'audio',
 				fullType,
 				idTime,
+				companionIdFlow: userId,
 				companionId: userId,
 				owner: id,
 			});
@@ -330,6 +336,7 @@ const videoMessage = async (req, res, next) => {
 				type: 'video',
 				fullType,
 				idTime,
+				companionIdFlow: userId,
 				companionId: id,
 				owner: userId,
 			});
@@ -343,6 +350,7 @@ const videoMessage = async (req, res, next) => {
 				type: 'video',
 				fullType,
 				idTime,
+				companionIdFlow: userId,
 				companionId: userId,
 				owner: id,
 			});
@@ -417,6 +425,7 @@ const fileMessage = async (req, res, next) => {
 				type,
 				fullType,
 				idTime,
+				companionIdFlow: userId,
 				companionId: id,
 				owner: userId,
 			});
@@ -430,6 +439,7 @@ const fileMessage = async (req, res, next) => {
 				type,
 				fullType,
 				idTime,
+				companionIdFlow: userId,
 				companionId: userId,
 				owner: id,
 			});

+ 61 - 4
controllers/user.js

@@ -1,5 +1,6 @@
 const UserModel = require('../model/user');
 const ChatModel = require('../model/chat');
+const MessageModel = require('../model/message');
 const fs = require('fs').promises;
 const path = require('path');
 const Jimp = require('jimp');
@@ -54,7 +55,7 @@ const logIn = async (req, res, next) => {
 		if (!user.name || !user.lastName || !user.avatarUrl) registered = false;
 		const online = true;
 		await UserModel.updateUser(id, { token, code: '', online });
-		await ChatModel.updateCompanionsChat(id, online);
+		await ChatModel.updateCompanionsChat(id, { online });
 		return res.status(200).json({
 			status: 'success',
 			code: 200,
@@ -81,7 +82,7 @@ const logOut = async (req, res, next) => {
 				message: 'Invalid credentials',
 			});
 		await UserModel.updateUser(id, { token: null, online });
-		await ChatModel.updateCompanionsChat(id, online);
+		await ChatModel.updateCompanionsChat(id, { online });
 		return res.status(204).json({});
 	} catch (e) {
 		next(e);
@@ -100,7 +101,7 @@ const online = async (req, res, next) => {
 				data: 'UNAUTHORIZED',
 				message: 'Invalid credentials',
 			});
-		await ChatModel.updateCompanionsChat(id, online);
+		await ChatModel.updateCompanionsChat(id, { online });
 		await UserModel.updateUser(id, { online });
 		return res.status(204).json({});
 	} catch (e) {
@@ -121,7 +122,7 @@ const getCurrent = async (req, res, next) => {
 		const id = req.user.id;
 		const online = true;
 		await UserModel.updateUser(id, { online });
-		await ChatModel.updateCompanionsChat(id, online);
+		await ChatModel.updateCompanionsChat(id, { online });
 		const updatedUser = await UserModel.findById(id);
 		return res.status(200).json({
 			status: 'success',
@@ -136,7 +137,56 @@ const getCurrent = async (req, res, next) => {
 const updateCredentials = async (req, res, next) => {
 	try {
 		const { id } = req.user;
+		const { name, lastName, originalName, originalLastName } = req.body;
 		await UserModel.updateUser(id, req.body);
+		await ChatModel.updateCompanionsChat(id, {
+			originalName,
+			originalLastName,
+		});
+		await MessageModel.updateOwnerMessages(
+			{ companionIdFlow: { $eq: id }, owner: id },
+			{ name, lastName }
+		);
+		return res.status(200).json({
+			data: {},
+		});
+	} catch (e) {
+		next(e);
+	}
+};
+
+const updateUser = async (req, res, next) => {
+	try {
+		const { id } = req.user;
+		await UserModel.updateUser(id, req.body);
+		return res.status(200).json({
+			data: {},
+		});
+	} catch (e) {
+		next(e);
+	}
+};
+
+const removeAvatar = async (req, res, next) => {
+	try {
+		const { id, avatarsArr } = req.user;
+		const index = req.params.index;
+		const toDelete = avatarsArr[index];
+		const filteredAvatars = [...avatarsArr].filter(
+			({ avatarUrl }) => avatarUrl !== toDelete.avatarUrl
+		);
+		const DIR_IMAGES = process.env.DIR_IMAGES;
+		await fs.unlink(path.join(DIR_IMAGES, toDelete.avatarUrl));
+		const avatarUrl = filteredAvatars[0].avatarUrl;
+		await UserModel.updateUser(id, { avatarUrl, avatarsArr: filteredAvatars });
+		await ChatModel.updateCompanionsChat(id, {
+			avatarUrl,
+			avatarsArr: filteredAvatars,
+		});
+		await MessageModel.updateOwnerMessages(
+			{ companionIdFlow: { $eq: id } },
+			{ avatarUrl }
+		);
 		return res.status(200).json({
 			data: {},
 		});
@@ -170,6 +220,11 @@ const updateAvatar = async (req, res, next) => {
 			...req.user.avatarsArr,
 		];
 		await UserModel.updateUser(userId, { avatarUrl, avatarsArr });
+		await ChatModel.updateCompanionsChat(userId, { avatarUrl, avatarsArr });
+		await MessageModel.updateOwnerMessages(
+			{ companionIdFlow: { $eq: userId } },
+			{ avatarUrl }
+		);
 		return res.status(200).json({
 			status: 'success',
 			code: 200,
@@ -189,5 +244,7 @@ module.exports = {
 	online,
 	getCurrent,
 	updateCredentials,
+	updateUser,
+	removeAvatar,
 	updateAvatar,
 };

BIN
images/62697d28a015900cc431c597/1651080500image_jpeg.jpg


BIN
images/62697d28a015900cc431c597/1651080574videoPoster.png


images/62697d28a015900cc431c597/1651080560we.jpg → images/626a45efcd8b494250448c59/1651142863we.jpg


images/62697d28a015900cc431c597/1651080875download(2).png → images/626a71260508b6301021da29/1651145643download(1).png


BIN
images/626a71260508b6301021da29/1651146180we.jpg


BIN
images/626a714b0508b6301021da2a/1651143004download.jpg


images/62697d28a015900cc431c597/1651080880id.jpg → images/626a714b0508b6301021da2a/1651144010id.jpg


images/62697d28a015900cc431c597/1651080836путин.jpeg → images/626a71920508b6301021da2e/1651143072путин.jpeg


+ 2 - 2
model/chat.js

@@ -49,8 +49,8 @@ const update = async (id, userId, obj) => {
 	return chat;
 };
 
-const updateCompanionsChat = async (companionId, online) => {
-	const chats = await Chat.updateMany({ companionId }, { online });
+const updateCompanionsChat = async (companionId, obj) => {
+	const chats = await Chat.updateMany({ companionId }, { ...obj });
 	return chats;
 };
 

+ 12 - 0
model/message.js

@@ -68,6 +68,16 @@ const removeAll = async (companionId, userId) => {
 	return removedAllMessages;
 };
 
+const updateCompanionsMessages = async (companionId, obj) => {
+	const messages = await Message.updateMany({ companionId }, { ...obj });
+	return messages;
+};
+
+const updateOwnerMessages = async (options, obj) => {
+	const messages = await Message.updateMany({ ...options }, { ...obj });
+	return messages;
+};
+
 module.exports = {
 	getList,
 	getByField,
@@ -76,4 +86,6 @@ module.exports = {
 	remove,
 	removeByFields,
 	removeAll,
+	updateCompanionsMessages,
+	updateOwnerMessages,
 };

+ 4 - 0
model/schemas/message.js

@@ -14,6 +14,10 @@ const messageSchema = new Schema(
 			type: String,
 			default: null,
 		},
+		companionIdFlow: {
+			type: String,
+			default: null,
+		},
 		name: {
 			type: String,
 			default: null,

+ 9 - 3
routes/user.js

@@ -11,8 +11,8 @@ router
 	.patch('/auth/logout', guard, controllers.logOut)
 	.patch('/auth/online', guard, controllers.online)
 	.patch(
-		'/users/current',
-		[guard, validation.updateUser],
+		'/users/current/credentials',
+		[guard, validation.updateCredentials],
 		controllers.updateCredentials
 	)
 	.get('/users/current', guard, controllers.getCurrent)
@@ -20,5 +20,11 @@ router
 		'/users/avatars',
 		[guard, upload.uploadImage.single('avatar'), validation.validateUploadFile],
 		controllers.updateAvatar
-	);
+	)
+	.patch(
+		'/users/current/update',
+		[guard, validation.updateUser],
+		controllers.updateUser
+	)
+	.delete('/users/current/:index', guard, controllers.removeAvatar);
 module.exports = router;

+ 1 - 0
validation/contact.js

@@ -11,6 +11,7 @@ const schemaUpdateContact = Joi.object({
 	_id: Joi.string().required(),
 	name: Joi.string().min(1).max(30).optional().trim().optional(),
 	lastName: Joi.string().min(1).max(30).optional().trim().optional(),
+	companionId: Joi.string().min(1).optional(),
 }).min(2);
 
 module.exports.createContact = (req, _res, next) => {

+ 10 - 6
validation/user.js

@@ -7,13 +7,14 @@ const schemaCreateNewUser = Joi.object({
 	country: Joi.string().min(1).max(40).required(),
 }).min(2);
 
+const schemaUpdateCredentials = Joi.object({
+	name: Joi.string().min(1).max(30).trim().required(),
+	lastName: Joi.string().min(1).max(30).trim().required(),
+	originalName: Joi.string().min(1).max(30).trim().required(),
+	originalLastName: Joi.string().min(1).max(30).trim().required(),
+}).min(4);
+
 const schemaUpdateUser = Joi.object({
-	name: Joi.string().min(1).max(30).optional().trim().optional(),
-	lastName: Joi.string().min(1).max(30).optional().trim().optional(),
-	originalName: Joi.string().min(3).max(30).optional().trim().optional(),
-	originalLastName: Joi.string().min(3).max(30).optional().trim().optional(),
-	avatarUrl: Joi.string().optional(),
-	avatarsArr: Joi.array().optional(),
 	nightMode: Joi.boolean().optional(),
 	sort: Joi.boolean().optional(),
 }).min(1);
@@ -26,6 +27,9 @@ const schemaLogIn = Joi.object({
 module.exports.registration = (req, _res, next) => {
 	return validate(schemaCreateNewUser, req.body, next);
 };
+module.exports.updateCredentials = (req, _res, next) => {
+	return validate(schemaUpdateCredentials, req.body, next);
+};
 module.exports.updateUser = (req, _res, next) => {
 	return validate(schemaUpdateUser, req.body, next);
 };