|
|
@@ -8,6 +8,7 @@ const refreshChat = async function (client, sessionsId) { |
|
|
|
if (client && sessionsId) { |
|
|
|
var chatId; |
|
|
|
var result = await messageModel.getAllChats(client); |
|
|
|
// console.log(result["DATA"]); |
|
|
|
if (result["STATUS"] == 1) { |
|
|
|
for (var i = 0; i < result["DATA"].length; i++) { |
|
|
|
var chatsExist = await strapi.api["chats"].services[ |
|
|
@@ -17,22 +18,34 @@ const refreshChat = async function (client, sessionsId) { |
|
|
|
phoneNumber: result["DATA"][i]["id"].user, |
|
|
|
}); |
|
|
|
if (!chatsExist) { |
|
|
|
await strapi.query("chats").create({ |
|
|
|
session_key: sessionsId, |
|
|
|
name: result["DATA"][i]["name"], |
|
|
|
unreadCount: result["DATA"][i]["unreadCount"], |
|
|
|
timestamp: moment |
|
|
|
.unix(result["DATA"][i]["timestamp"]) |
|
|
|
.toDate(), |
|
|
|
chatId: result["DATA"][i]["id"], |
|
|
|
phoneNumber: result["DATA"][i]["id"].user, |
|
|
|
}); |
|
|
|
//check again with last active session |
|
|
|
chatsExist = await strapi.api["chats"].services[ |
|
|
|
"chats" |
|
|
|
].findOne({ |
|
|
|
session_key: sessionsId, |
|
|
|
last_sessionId: client.info.me._serialized, |
|
|
|
phoneNumber: result["DATA"][i]["id"].user, |
|
|
|
}); |
|
|
|
if (!chatsExist) { |
|
|
|
await strapi.query("chats").create({ |
|
|
|
session_key: sessionsId, |
|
|
|
name: result["DATA"][i]["name"], |
|
|
|
unreadCount: result["DATA"][i]["unreadCount"], |
|
|
|
timestamp: moment |
|
|
|
.unix(result["DATA"][i]["timestamp"]) |
|
|
|
.toDate(), |
|
|
|
chatId: result["DATA"][i]["id"], |
|
|
|
phoneNumber: result["DATA"][i]["id"].user, |
|
|
|
last_sessionId: client.info.me._serialized, |
|
|
|
}); |
|
|
|
} else { |
|
|
|
let id = chatsExist.id; |
|
|
|
await strapi.query("chats").update( |
|
|
|
{ id }, |
|
|
|
{ |
|
|
|
session_key: sessionsId, |
|
|
|
} |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
var messages = await messageModel.getMessagebyId( |
|
|
|
client, |
|
|
|