From bb5f2998f7bd65c1d00a472af8d9a4490d2571b8 Mon Sep 17 00:00:00 2001 From: jefry Date: Mon, 17 May 2021 10:35:04 +0700 Subject: [PATCH] buat push --- api/chats/controllers/chats.js | 2 +- api/chats/models/chats.js | 28 ++++++++++++++++++-- api/message/services/message.js | 18 ++++++------- config/functions/cron.js | 32 +++++++++++------------ util/refreshChat.js | 58 ++++++++++++++++++++--------------------- 5 files changed, 80 insertions(+), 58 deletions(-) diff --git a/api/chats/controllers/chats.js b/api/chats/controllers/chats.js index 556f4fc..8651e9a 100644 --- a/api/chats/controllers/chats.js +++ b/api/chats/controllers/chats.js @@ -1,4 +1,4 @@ -'use strict'; +"use strict"; /** * Read the documentation (https://strapi.io/documentation/developer-docs/latest/concepts/controllers.html#core-controllers) diff --git a/api/chats/models/chats.js b/api/chats/models/chats.js index dd847ef..0db0f2e 100644 --- a/api/chats/models/chats.js +++ b/api/chats/models/chats.js @@ -1,8 +1,32 @@ -'use strict'; +const messageModel = require("../../../model/messageModel"); +("use strict"); /** * Read the documentation (https://strapi.io/documentation/developer-docs/latest/concepts/models.html#lifecycle-hooks) * to customize this model */ -module.exports = {}; +module.exports = { + // lifecycles: { + // async beforeCreate(data) { + // var checkData = await strapi + // .query("chats") + // .find({ + // session_key: data.session_key, + // phoneNumber: data.phoneNumber, + // }); + // if (checkData) { + // throw strapi.errors.badRequest("Chats already exist"); + // } + // var data = await messageModel + // .asyncLoadClient(data.session_key) + // .catch((message) => { + // throw strapi.errors.badRequest(`error loading clients ${message}`); + // }); + // if (data["TYPE"] == "READY") { + // } else if (data["TYPE"] == "AUTH_FAILURE") { + // throw strapi.errors.badRequest(`error loading clients ${message}`); + // } + // }, + // }, +}; diff --git a/api/message/services/message.js b/api/message/services/message.js index 68ae794..2d6e64e 100644 --- a/api/message/services/message.js +++ b/api/message/services/message.js @@ -6,15 +6,15 @@ */ module.exports = { - distinctbyKey: async (params) => { - const results = await strapi.query("message").find(params); - let arrKeys = []; - results.forEach((i) => { - if (!arrKeys.includes(i["session_datum_key"])) - arrKeys.push(i["session_datum_key"]); - }); - return arrKeys; - }, + // distinctbyKey: async (params) => { + // const results = await strapi.query("message").find(params); + // let arrKeys = []; + // results.forEach((i) => { + // if (!arrKeys.includes(i["session_datum_key"])) + // arrKeys.push(i["session_datum_key"]); + // }); + // return arrKeys; + // }, getQueueMessage: async (sessionId) => { var chat = await strapi.api["chats"].services["chats"].find({ session_key: sessionId, diff --git a/config/functions/cron.js b/config/functions/cron.js index ff5600d..38a1b6e 100644 --- a/config/functions/cron.js +++ b/config/functions/cron.js @@ -43,22 +43,22 @@ module.exports = { await strapi.api["session-data"].services[ "session-data" ].delete({ id }); - const messages = await strapi.api.message.services.message.find( - { - _sort: "session_datum_key:desc", - state: "QUEUE", - session_datum_key: i["id"], - } - ); - for (const message of messages) { - await strapi.api.message.services.message.update( - { id: message.id }, - { - state: "ERROR", - keterangan: "sessions no longer valid", - } - ); - } + // const messages = await strapi.api.message.services.message.find( + // { + // _sort: "session_datum_key:desc", + // state: "QUEUE", + // session_datum_key: i["id"], + // } + // ); + // for (const message of messages) { + // await strapi.api.message.services.message.update( + // { id: message.id }, + // { + // state: "ERROR", + // keterangan: "sessions no longer valid", + // } + // ); + // } } } } diff --git a/util/refreshChat.js b/util/refreshChat.js index f20c7f7..acc4824 100644 --- a/util/refreshChat.js +++ b/util/refreshChat.js @@ -6,11 +6,29 @@ const refreshMessage = require("./refreshMessage"); const refreshChat = async function (client, sessionsId) { try { if (client && sessionsId) { + var oldChats = await strapi.api["chats"].services["chats"].find({ + last_sessionId: client.info.me._serialized, + }); + oldChats = oldChats.filter( + (item) => Object.keys(item.session_key).length === 0 + ); + for (var i = 0; i < oldChats.length; i++) { + var oldChatId = oldChats[i].id; + // console.log(oldChats[i]); + await strapi.query("chats").update( + { id: oldChatId }, + { + session_key: sessionsId, + } + ); + console.log(i); + } + 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++) { + console.log("next", i); var chatsExist = await strapi.api["chats"].services[ "chats" ].findOne({ @@ -18,35 +36,17 @@ const refreshChat = async function (client, sessionsId) { phoneNumber: result["DATA"][i]["id"].user, }); if (!chatsExist) { - //check again with last active session - chatsExist = await strapi.api["chats"].services[ - "chats" - ].findOne({ - last_sessionId: client.info.me._serialized, + 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, }); - 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 { - console.log("updating old chat"); - let id = chatsExist.id; - strapi.query("chats").update( - { id }, - { - session_key: sessionsId, - } - ); - } } var messages = await messageModel.getMessagebyId( client, @@ -65,8 +65,6 @@ const refreshChat = async function (client, sessionsId) { throw { message: result["DATA"] }; } } - - // console.log("done populating chats"); } catch (e) { console.log("error", e.message || e); // return { STATUS: 0, DATA: e.message || e };