const { Op } = require('sequelize'); async function check_personal_chat(req, res){ const timestamp = 1739755456; const max_timestamp = 1739754376; const company_id = 20; const session = 'Dp14ODv8FR2LepuLwjdqOYlKavLQUbN92Pymn3jEddk='; const chat_data = await db.chats.findAll({ //where like date where: { session: session, messageTimestamp: { [Op.gt]: timestamp, [Op.lt]: max_timestamp }, fromMe: true }, }); length = chat_data.length; console.log('length', length); //then from chat_data get message_id and search in the chat_sender_details table let chat_not_exist = []; for (let i = 0; i < chat_data.length; i++) { const chat = chat_data[i]; const message_id = chat.message_id; const chat_sender_details = await db.chat_sender_details.findOne({ where: { message_id: message_id, ses } }); if(!chat_sender_details){ chat_not_exist.push(chat); } } let chat_not_exist_in_reply = []; //then looping the chat_not_exist and compare if the messageText from chats is not same with auto_replies.reply then return data for (let i = 0; i < chat_not_exist.length; i++) { const chat = chat_not_exist[i]; const messageText = chat.messageText; const auto_replies = await db.auto_replies.findOne({ where: { reply: messageText, company_id: company_id } }); if(!auto_replies){ chat_not_exist_in_reply.push(chat); } } res.status(200).json({ chat_not_exist_in_reply }); } app.get('/check_personal_chat', check_personal_chat);