wa_strapi
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

81 linhas
2.6 KiB

  1. const messageModel = require("../../../model/messageModel");
  2. const { parseMultipartData, sanitizeEntity } = require("strapi-utils");
  3. const refreshChat = require("../../../util/refreshChat");
  4. /**
  5. * Read the documentation (https://strapi.io/documentation/developer-docs/latest/concepts/controllers.html#core-controllers)
  6. * to customize this controller
  7. */
  8. module.exports = {
  9. initiate: async (ctx) => {
  10. ctx.respond = false;
  11. const { req, res } = ctx;
  12. res.setHeader("Content-Type", "text/html; charset=UTF-8");
  13. res.setHeader("Transfer-Encoding", "chunked");
  14. res.writeHead(200);
  15. messageModel.createNewClient(async function (data) {
  16. // console.log(data);
  17. let clientInfo;
  18. if (data["STATUS"] == 1 && data["TYPE"] == "READY") {
  19. // console.log(data["DATA"]);
  20. clientInfo = data["DATA"];
  21. let checkExist = await strapi
  22. .query("session-data")
  23. .findOne({ sessionId: clientInfo.wid._serialized });
  24. if (checkExist) {
  25. // messageModel.loadClient(async function(data){
  26. // let state = data.parse(data);
  27. // if (state["STATUS"] == 1 && state["TYPE"] == "READY"){
  28. // let loaded = new Client();
  29. // loaded = state["DATA"];
  30. // await loaded.destroy();
  31. // }
  32. // });
  33. let id = checkExist.id;
  34. await strapi.services["session-data"].update(
  35. { id },
  36. {
  37. name: clientInfo.pushname,
  38. device_manufacturer:
  39. clientInfo.phone.device_manufacturer,
  40. device_model: clientInfo.phone.device_model,
  41. platform: clientInfo.platform,
  42. os_version: clientInfo.phone.os_version,
  43. phonenumber: clientInfo.wid.user,
  44. session_data: clientInfo.sessionData,
  45. sessionId: clientInfo.wid._serialized,
  46. }
  47. );
  48. } else {
  49. await strapi.services["session-data"].create({
  50. name: clientInfo.pushname,
  51. device_manufacturer:
  52. clientInfo.phone.device_manufacturer,
  53. device_model: clientInfo.phone.device_model,
  54. platform: clientInfo.platform,
  55. os_version: clientInfo.phone.os_version,
  56. phonenumber: clientInfo.wid.user,
  57. session_data: clientInfo.sessionData,
  58. sessionId: clientInfo.wid._serialized,
  59. });
  60. checkExist = await strapi
  61. .query("session-data")
  62. .findOne({ sessionId: clientInfo.wid._serialized });
  63. // console.log(entry);
  64. }
  65. // await refreshChat(data["DATA"], checkExist.id);
  66. // await data["DATA"].destroy();
  67. // data["DATA"] = clientInfo;
  68. }
  69. res.write(JSON.stringify(data));
  70. if (
  71. data["STATUS"] == 0 ||
  72. (data["STATUS"] == 1 && data["TYPE"] == "READY")
  73. ) {
  74. return res.end();
  75. }
  76. });
  77. },
  78. };