| @@ -1,4 +1,4 @@ | |||
| 'use strict'; | |||
| "use strict"; | |||
| /** | |||
| * Read the documentation (https://strapi.io/documentation/developer-docs/latest/concepts/controllers.html#core-controllers) | |||
| @@ -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}`); | |||
| // } | |||
| // }, | |||
| // }, | |||
| }; | |||
| @@ -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, | |||
| @@ -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", | |||
| // } | |||
| // ); | |||
| // } | |||
| } | |||
| } | |||
| } | |||
| @@ -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 }; | |||