Jelajahi Sumber

work on call message

unknown 2 tahun lalu
induk
melakukan
e74c1f6bea
4 mengubah file dengan 98 tambahan dan 0 penghapusan
  1. 75 0
      controllers/messages.js
  2. 12 0
      model/schemas/message.js
  3. 1 0
      routes/messages.js
  4. 10 0
      validation/message.js

+ 75 - 0
controllers/messages.js

@@ -462,6 +462,80 @@ const sentMessage = async (req, res, next) => {
 	}
 };
 
+const callMessage = async (req, res, next) => {
+	try {
+		const { id, reject, duration } = req.body;
+		const idTime = Math.round(Date.now() / 1000);
+		const user = req.user;
+		const userId = user.id;
+		const companion = await UserModel.findById(id);
+		const isChat = await ChatModel.getByField(id, userId);
+		const isCompanionChat = await ChatModel.getByField(userId, id);
+		const { name, lastName, avatarUrl, color, number } = user;
+		if (companion && isChat && isCompanionChat) {
+			const newMessage = await MessageModel.add({
+				initiator: true,
+				reject,
+				duration,
+				message: 'Outgoing Call',
+				name,
+				lastName,
+				avatarUrl,
+				color,
+				number,
+				type: 'text',
+				idTime,
+				companionIdFlow: userId,
+				companionId: id,
+				owner: userId,
+			});
+			await MessageModel.add({
+				reject,
+				duration,
+				message: 'Incoming Call',
+				name: isCompanionChat.name,
+				lastName: isCompanionChat.lastName,
+				avatarUrl,
+				color,
+				number,
+				type: 'text',
+				idTime,
+				companionIdFlow: userId,
+				companionId: userId,
+				owner: id,
+			});
+			const { total } = await MessageModel.getList(
+				{ owner: userId, companionId: id },
+				{}
+			);
+			await ChatModel.update(isChat._id, userId, {
+				total,
+				seen: total,
+				watched: false,
+				lastMessage: 'Outgoing Call',
+				lastMessageCreatedAt: newMessage.createdAt,
+			});
+			const { total: Total } = await MessageModel.getList(
+				{ owner: id, companionId: userId },
+				{}
+			);
+			await ChatModel.update(isCompanionChat._id, id, {
+				total: Total,
+				seenCompanion: Total,
+				lastMessage: 'Incoming Call',
+				lastMessageCreatedAt: newMessage.createdAt,
+			});
+			return res.status(201).json({
+				status: 'success',
+				code: 201,
+				data: newMessage,
+			});
+		}
+	} catch (e) {
+		next(e);
+	}
+};
+
 const sentMessageReply = async (req, res, next) => {
 	try {
 		const { id, message, caption } = req.body;
@@ -1032,6 +1106,7 @@ module.exports = {
 	unpinAllMessage,
 	listMessagesById,
 	sentMessage,
+	callMessage,
 	sentMessageReply,
 	sentMessageForward,
 	imageMessage,

+ 12 - 0
model/schemas/message.js

@@ -114,6 +114,18 @@ const messageSchema = new Schema(
 			type: Boolean,
 			default: false,
 		},
+		reject: {
+			type: Boolean,
+			default: false,
+		},
+		initiator: {
+			type: Boolean,
+			default: false,
+		},
+		duration: {
+			type: Number,
+			default: null,
+		},
 		owner: {
 			type: SchemaTypes.ObjectId,
 			ref: 'user',

+ 1 - 0
routes/messages.js

@@ -8,6 +8,7 @@ const upload = require('../helpers/upload');
 router
 	.get('/', guard, controllers.listMessages)
 	.post('/', guard, validation.sentMessage, controllers.sentMessage)
+	.post('/call/', guard, validation.callMessage, controllers.callMessage)
 	.post('/reply/', guard, validation.sentMessage, controllers.sentMessageReply)
 	.post('/edit/', guard, validation.editMessage, controllers.editMessage)
 	.post(

+ 10 - 0
validation/message.js

@@ -8,6 +8,12 @@ const schemaSentMessage = Joi.object({
 	caption: Joi.any().optional(),
 });
 
+const schemaCallMessage = Joi.object({
+	id: Joi.string().required(),
+	reject: Joi.boolean().required(),
+	duration: Joi.number().required(),
+});
+
 const schemaEditMessage = Joi.object({
 	id: Joi.string().required(),
 	message: Joi.any().optional(),
@@ -34,6 +40,10 @@ module.exports.sentMessage = (req, _res, next) => {
 	return validate(schemaSentMessage, req.body, next);
 };
 
+module.exports.callMessage = (req, _res, next) => {
+	return validate(schemaCallMessage, req.body, next);
+};
+
 module.exports.editMessage = (req, _res, next) => {
 	return validate(schemaEditMessage, req.body, next);
 };