Kaynağa Gözat

finished caaLL

unknown 2 yıl önce
ebeveyn
işleme
699b985b01

+ 43 - 3
controllers/messages.js

@@ -464,7 +464,7 @@ const sentMessage = async (req, res, next) => {
 
 const callMessage = async (req, res, next) => {
 	try {
-		const { id, reject, duration } = req.body;
+		const { id, duration } = req.body;
 		const idTime = Math.round(Date.now() / 1000);
 		const user = req.user;
 		const userId = user.id;
@@ -475,7 +475,6 @@ const callMessage = async (req, res, next) => {
 		if (companion && isChat && isCompanionChat) {
 			const newMessage = await MessageModel.add({
 				initiator: true,
-				reject,
 				duration,
 				message: 'Outgoing Call',
 				name,
@@ -490,7 +489,6 @@ const callMessage = async (req, res, next) => {
 				owner: userId,
 			});
 			await MessageModel.add({
-				reject,
 				duration,
 				message: 'Incoming Call',
 				name: isCompanionChat.name,
@@ -536,6 +534,47 @@ const callMessage = async (req, res, next) => {
 	}
 };
 
+const updateMessageCall = async (req, res, next) => {
+	try {
+		const { id, duration } = req.body;
+		const userId = req.user.id;
+		const userMessage = await MessageModel.getById(id, userId);
+		const isChat = await ChatModel.getByField(userMessage.companionId, userId);
+		const isCompanionChat = await ChatModel.getByField(
+			userId,
+			userMessage.companionId
+		);
+		const messageOne = duration ? 'Outgoing Call' : 'Canceled Call';
+		const messageTwo = duration ? 'Incoming Call' : 'Missed Call';
+		await MessageModel.updateMessage(id, userId, {
+			message: messageOne,
+			duration,
+		});
+		await MessageModel.findByFieldsAndUpdate(
+			userId,
+			userMessage.idTime,
+			userMessage.companionId,
+			{
+				message: messageTwo,
+				duration,
+			}
+		);
+		await ChatModel.update(isChat._id, userId, {
+			lastMessage: messageOne,
+		});
+		await ChatModel.update(isCompanionChat._id, userMessage.companionId, {
+			lastMessage: messageTwo,
+		});
+		return res.status(200).json({
+			status: 'success',
+			code: 200,
+			data: {},
+		});
+	} catch (e) {
+		next(e);
+	}
+};
+
 const sentMessageReply = async (req, res, next) => {
 	try {
 		const { id, message, caption } = req.body;
@@ -1107,6 +1146,7 @@ module.exports = {
 	listMessagesById,
 	sentMessage,
 	callMessage,
+	updateMessageCall,
 	sentMessageReply,
 	sentMessageForward,
 	imageMessage,

+ 0 - 4
model/schemas/message.js

@@ -114,10 +114,6 @@ const messageSchema = new Schema(
 			type: Boolean,
 			default: false,
 		},
-		reject: {
-			type: Boolean,
-			default: false,
-		},
 		initiator: {
 			type: Boolean,
 			default: false,

+ 6 - 0
routes/messages.js

@@ -23,6 +23,12 @@ router
 		validation.removeSelected,
 		controllers.removeSelected
 	)
+	.patch(
+		'/call/end',
+		guard,
+		validation.callMessage,
+		controllers.updateMessageCall
+	)
 	.patch('/unpin/all/', guard, validation.unpinAll, controllers.unpinAllMessage)
 	.post(
 		'/image/:companionIdAndCaption',

+ 0 - 1
validation/message.js

@@ -10,7 +10,6 @@ const schemaSentMessage = Joi.object({
 
 const schemaCallMessage = Joi.object({
 	id: Joi.string().required(),
-	reject: Joi.boolean().required(),
 	duration: Joi.number().required(),
 });