|
- const messageModel = require("../../../model/messageModel");
- const { parseMultipartData, sanitizeEntity } = require("strapi-utils");
- const { Client } = require("whatsapp-web.js");
- /**
- * 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);
- var json = JSON.parse(data);
- if (json["STATUS"] == 1 && json["TYPE"] == "READY") {
- // console.log(json["DATA"]);
- let checkExist = await strapi
- .query("session-data")
- .findOne({ phonenumber: json["DATA"].wid.user });
- if (checkExist) {
- // messageModel.loadClient(async function(data){
- // let state = JSON.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:json["DATA"].pushname,
- device_manufacturer:json["DATA"].phone.device_manufacturer,
- device_model:json["DATA"].phone.device_model,
- platform:json["DATA"].platform,
- os_version:json["DATA"].phone.os_version,
- phonenumber:json["DATA"].wid.user,
- session_data:json["DATA"].sessionData,
- });
- }
- else {
- await strapi.services["session-data"].create({
- name:json["DATA"].pushname,
- device_manufacturer:json["DATA"].phone.device_manufacturer,
- device_model:json["DATA"].phone.device_model,
- platform:json["DATA"].platform,
- os_version:json["DATA"].phone.os_version,
- phonenumber:json["DATA"].wid.user,
- session_data:json["DATA"].sessionData,
- });
- // console.log(entry);
- }
- }
- res.write(data);
- if (
- json["STATUS"] == 0 ||
- (json["STATUS"] == 1 && json["TYPE"] == "READY")
- ) {
- return res.end();
- }
- });
- },
- };
|