Browse Source

make forward

unknown 2 years ago
parent
commit
7179baddf0
4 changed files with 109 additions and 0 deletions
  1. 82 0
      controllers/messages.js
  2. 12 0
      model/schemas/message.js
  3. 6 0
      routes/messages.js
  4. 9 0
      validation/message.js

+ 82 - 0
controllers/messages.js

@@ -438,6 +438,87 @@ const sentMessageReply = async (req, res, next) => {
 	}
 };
 
+const sentMessageForward = async (req, res, next) => {
+	try {
+		const { id, companionIdForwardToAndFrom } = req.body;
+		const idTime = Math.round(Date.now() / 1000);
+		const user = req.user;
+		const userId = user.id;
+		const userMessage = await MessageModel.getById(id, userId);
+		const isChat = await ChatModel.getByField(
+			companionIdForwardToAndFrom,
+			userId
+		);
+		const isForwardChat = await ChatModel.getByField(
+			userId,
+			companionIdForwardToAndFrom
+		);
+		if (userMessage && isChat && isForwardChat) {
+			const newMessage = await MessageModel.add({
+				message: userMessage.message,
+				name: user.name,
+				lastName: user.lastName,
+				avatarUrl: user.avatarUrl,
+				color: user.color,
+				number: user.number,
+				type: 'text',
+				fullType: userMessage.fullType,
+				forwardName: userMessage.name,
+				forwardLastName: userMessage.lastMessage,
+				idTime,
+				companionIdFlow: userId,
+				companionIdForwardToAndFrom: userMessage.companionId,
+				companionId: companionIdForwardToAndFrom,
+				owner: userId,
+			});
+			await MessageModel.add({
+				message: userMessage.message,
+				name: isForwardChat.name,
+				lastName: isForwardChat.lastName,
+				avatarUrl: user.avatarUrl,
+				color: user.color,
+				number: user.number,
+				type: 'text',
+				fullType: userMessage.fullType,
+				forwardName: userMessage.name,
+				forwardLastName: userMessage.lastMessage,
+				idTime,
+				companionIdFlow: userId,
+				companionId: userId,
+				companionIdForwardToAndFrom: userMessage.companionId,
+				owner: companionIdForwardToAndFrom,
+			});
+			const { total } = await MessageModel.getList(
+				{ owner: userId, companionId: companionIdForwardToAndFrom },
+				{}
+			);
+			await ChatModel.update(isChat._id, userId, {
+				total,
+				seen: total,
+				watched: false,
+				lastMessage: userMessage.message,
+				lastMessageCreatedAt: newMessage.createdAt,
+			});
+			const { total: Total } = await MessageModel.getList(
+				{ owner: companionIdForwardToAndFrom, companionId: userId },
+				{}
+			);
+			await ChatModel.update(isForwardChat._id, companionIdForwardToAndFrom, {
+				total: Total,
+				lastMessage: userMessage.message,
+				lastMessageCreatedAt: newMessage.createdAt,
+			});
+			return res.status(201).json({
+				status: 'success',
+				code: 201,
+				data: newMessage,
+			});
+		}
+	} catch (e) {
+		next(e);
+	}
+};
+
 const imageMessage = async (req, res, next) => {
 	try {
 		const userId = req.user.id;
@@ -804,6 +885,7 @@ module.exports = {
 	listMessagesById,
 	sentMessage,
 	sentMessageReply,
+	sentMessageForward,
 	imageMessage,
 	audioMessage,
 	videoMessage,

+ 12 - 0
model/schemas/message.js

@@ -22,6 +22,10 @@ const messageSchema = new Schema(
 			type: String,
 			default: null,
 		},
+		companionIdForwardToAndFrom: {
+			type: String,
+			default: null,
+		},
 		name: {
 			type: String,
 			default: null,
@@ -38,6 +42,14 @@ const messageSchema = new Schema(
 			type: String,
 			default: null,
 		},
+		forwardName: {
+			type: String,
+			default: null,
+		},
+		forwardLastName: {
+			type: String,
+			default: null,
+		},
 		avatarUrl: {
 			type: String,
 			default: null,

+ 6 - 0
routes/messages.js

@@ -9,6 +9,12 @@ router
 	.get('/', guard, controllers.listMessages)
 	.post('/', guard, validation.sentMessage, controllers.sentMessage)
 	.post('/reply/', guard, validation.sentMessage, controllers.sentMessageReply)
+	.post(
+		'/forward/',
+		guard,
+		validation.sentForwardMessage,
+		controllers.sentMessageForward
+	)
 	.patch(
 		'/selected/',
 		guard,

+ 9 - 0
validation/message.js

@@ -8,6 +8,11 @@ const schemaSentMessage = Joi.object({
 	caption: Joi.any().optional(),
 });
 
+const schemaSentForwardMessage = Joi.object({
+	id: Joi.string().required(),
+	companionIdForwardToAndFrom: Joi.any().required(),
+});
+
 const schemaRemoveSelected = Joi.object({
 	companionId: Joi.string().required(),
 	selectedArr: Joi.array().items(Joi.string().min(5).max(50)).required(),
@@ -21,6 +26,10 @@ module.exports.sentMessage = (req, _res, next) => {
 	return validate(schemaSentMessage, req.body, next);
 };
 
+module.exports.sentForwardMessage = (req, _res, next) => {
+	return validate(schemaSentForwardMessage, req.body, next);
+};
+
 module.exports.removeSelected = (req, _res, next) => {
 	return validate(schemaRemoveSelected, req.body, next);
 };