|
- const messageModel = require("../../../model/messageModel");
- const { parseMultipartData, sanitizeEntity } = require("strapi-utils");
- const refreshChat = require("../../../util/refreshChat");
- /**
- * Read the documentation (https://strapi.io/documentation/developer-docs/latest/concepts/controllers.html#core-controllers)
- * to customize this controller
- */
-
- module.exports = {
- initiate: async (ctx) => {
- ctx.respond = false;
- const { req, res } = ctx;
- res.setHeader("Content-Type", "text/html; charset=UTF-8");
- res.setHeader("Transfer-Encoding", "chunked");
- res.writeHead(200);
-
- messageModel.createNewClient(async function (data) {
- // console.log(data);
- let clientInfo;
- if (data["STATUS"] == 1 && data["TYPE"] == "READY") {
- // console.log(data["DATA"]);
- clientInfo = data["DATA"];
- let checkExist = await strapi
- .query("session-data")
- .findOne({ sessionId: clientInfo.wid._serialized });
- if (checkExist) {
- // messageModel.loadClient(async function(data){
- // let state = data.parse(data);
- // if (state["STATUS"] == 1 && state["TYPE"] == "READY"){
- // let loaded = new Client();
- // loaded = state["DATA"];
- // await loaded.destroy();
- // }
- // });
- let id = checkExist.id;
- await strapi.services["session-data"].update(
- { id },
- {
- name: clientInfo.pushname,
- device_manufacturer:
- clientInfo.phone.device_manufacturer,
- device_model: clientInfo.phone.device_model,
- platform: clientInfo.platform,
- os_version: clientInfo.phone.os_version,
- phonenumber: clientInfo.wid.user,
- session_data: clientInfo.sessionData,
- sessionId: clientInfo.wid._serialized,
- }
- );
- } else {
- await strapi.services["session-data"].create({
- name: clientInfo.pushname,
- device_manufacturer:
- clientInfo.phone.device_manufacturer,
- device_model: clientInfo.phone.device_model,
- platform: clientInfo.platform,
- os_version: clientInfo.phone.os_version,
- phonenumber: clientInfo.wid.user,
- session_data: clientInfo.sessionData,
- sessionId: clientInfo.wid._serialized,
- });
- checkExist = await strapi
- .query("session-data")
- .findOne({ sessionId: clientInfo.wid._serialized });
- // console.log(entry);
- }
- // await refreshChat(data["DATA"], checkExist.id);
- // await data["DATA"].destroy();
- // data["DATA"] = clientInfo;
- }
- res.write(JSON.stringify(data));
- if (
- data["STATUS"] == 0 ||
- (data["STATUS"] == 1 && data["TYPE"] == "READY")
- ) {
- return res.end();
- }
- });
- },
- };
|