0732f3cb36693b6a24bca6860fa627f1ec9e1463
[motion2.git] / server / api / whatsappMessage / whatsappMessage.model.js
1 // *************************************************************************
2 // *                                                                       *
3 // * xCALLY Motion -  The Omnichannel Contact Center                       *
4 // * Copyright (c) Xenialab s.r.l. All Rights Reserved                     *
5 // *                                                                       *
6 // *************************************************************************
7 // *                                                                       *
8 // * Email: info@xcally.com                                                *
9 // * Website: https://www.xcally.com                                       *
10 // *                                                                       *
11 // *************************************************************************
12 // *                                                                       *
13 // * The SOFTWARE PRODUCT is protected by copyright laws and international *
14 // * copyright treaties, as well as other intellectual property laws and   *
15 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                         *
16 // *                                                                       *
17 // *************************************************************************
18 var _0x085f=['text','POST','application/json','statuses','message_id','err','finally','utf8mb4_unicode_ci','lodash','../../config/logger','request-promise','node-fetch','path','rimraf','../../config/environment','./whatsappMessage.attributes','exports','define','WhatsappMessage','whatsapp_messages','messageId','fti_whatsapp_messages','FULLTEXT','models','direction','toLowerCase','secret','isNil','UserId','findOne','WhatsappInteractionId','then','update','createdAt','catch','error','out','find','type','twilio','authToken','phone','accountSid','messages','whatsapp:','remote','token','sid','WhatsappMessage,\x20%s,\x20%s,\x20%s','afterCreate','inspect','Missing\x20parameters:\x20phone,\x20accountSid\x20or\x20authToken','/whatsapp/v1/','/messages/'];(function(_0xe22173,_0x3e82ec){var _0x22db1a=function(_0x3199f7){while(--_0x3199f7){_0xe22173['push'](_0xe22173['shift']());}};_0x22db1a(++_0x3e82ec);}(_0x085f,0x74));var _0xf085=function(_0x459cdb,_0x45c950){_0x459cdb=_0x459cdb-0x0;var _0x58e331=_0x085f[_0x459cdb];return _0x58e331;};'use strict';var _=require(_0xf085('0x0'));var util=require('util');var logger=require(_0xf085('0x1'))('api');var moment=require('moment');var BPromise=require('bluebird');var rp=require(_0xf085('0x2'));var fetch=require(_0xf085('0x3'));var fs=require('fs');var path=require(_0xf085('0x4'));var rimraf=require(_0xf085('0x5'));var config=require(_0xf085('0x6'));var attributes=require(_0xf085('0x7'));module[_0xf085('0x8')]=function(_0x38ae48,_0x1f28d0){return _0x38ae48[_0xf085('0x9')](_0xf085('0xa'),attributes,{'tableName':_0xf085('0xb'),'paranoid':![],'indexes':[{'name':_0xf085('0xc'),'fields':[_0xf085('0xc')]},{'name':_0xf085('0xd'),'fields':['body'],'type':_0xf085('0xe')}],'timestamps':!![],'hooks':{'afterCreate':function(_0x51a6b8,_0x6c4bdd,_0x50a765){var _0x3a8b36=_0x38ae48[_0xf085('0xf')];var _0x2d33bc=_0x51a6b8['get']({'plain':!![]});if(_0x2d33bc[_0xf085('0x10')][_0xf085('0x11')]()==='in'||_0x2d33bc[_0xf085('0x10')][_0xf085('0x11')]()==='out'&&!_0x2d33bc[_0xf085('0x12')]&&!_[_0xf085('0x13')](_0x2d33bc[_0xf085('0x14')])){_0x3a8b36['WhatsappInteraction'][_0xf085('0x15')]({'where':{'id':_0x2d33bc[_0xf085('0x16')]}})[_0xf085('0x17')](function(_0x2e59c1){if(_0x2e59c1){_0x2e59c1[_0xf085('0x18')]({'lastMsgAt':_0x2d33bc[_0xf085('0x19')],'lastMsgDirection':_0x2d33bc[_0xf085('0x10')][_0xf085('0x11')]()});}})[_0xf085('0x1a')](function(_0x48dda8){console[_0xf085('0x1b')](_0x48dda8);});}if(_0x2d33bc[_0xf085('0x10')]===_0xf085('0x1c')&&!_0x2d33bc['secret']){var _0x136c75;_0x3a8b36['WhatsappAccount'][_0xf085('0x1d')]({'where':{'id':_0x2d33bc['WhatsappAccountId']},'raw':!![]})['then'](function(_0x376d75){_0x136c75=_0x376d75;if(_0x136c75&&_0x136c75['type']){switch(_0x136c75[_0xf085('0x1e')]){case _0xf085('0x1f'):var _0x7ff2e8=require('twilio');if(_0x136c75['accountSid']&&_0x136c75[_0xf085('0x20')]&&_0x136c75[_0xf085('0x21')]){var _0x6acb44=new _0x7ff2e8(_0x136c75[_0xf085('0x22')],_0x136c75[_0xf085('0x20')]);_0x6acb44[_0xf085('0x23')]['create']({'body':_0x2d33bc['body'],'to':_0xf085('0x24')+_0x2d33bc[_0xf085('0x21')],'from':_0xf085('0x24')+_0x136c75[_0xf085('0x21')],'statusCallback':_0x136c75[_0xf085('0x25')]+'/api/whatsapp/messages/'+_0x2d33bc['id']+'/status?token='+_0x136c75[_0xf085('0x26')]})[_0xf085('0x17')](function(_0x4c90e8){if(_0x4c90e8[_0xf085('0x27')]){return _0x51a6b8[_0xf085('0x18')]({'messageId':_0x4c90e8[_0xf085('0x27')]});}})[_0xf085('0x1a')](function(_0x44f283){logger['error'](_0xf085('0x28'),_0xf085('0x29'),_0xf085('0x1f'),util[_0xf085('0x2a')](_0x44f283,{'showHidden':![],'depth':null}));});}else{logger[_0xf085('0x1b')](_0xf085('0x28'),_0xf085('0x29'),'twilio',_0xf085('0x2b'));}break;case'sinch':var _0x296cd1=_0x136c75['accountSid'];var _0x5631d7=_0x136c75[_0xf085('0x20')];var _0x4d3050=_0x136c75['baseUrl']+_0xf085('0x2c')+_0x296cd1+_0xf085('0x2d');var _0x42e2d4={'to':[_0x2d33bc['phone']],'message':{'type':_0xf085('0x2e'),'text':_0x2d33bc['body']}};var _0x315d3c={'method':_0xf085('0x2f'),'uri':_0x4d3050,'headers':{'Authorization':'Bearer\x20'+_0x5631d7,'Content-Type':_0xf085('0x30')},'body':_0x42e2d4,'json':!![]};rp(_0x315d3c)[_0xf085('0x17')](function(_0x527fa8){if(_0x527fa8[_0xf085('0x31')][0x0]['message_id']){return _0x51a6b8[_0xf085('0x18')]({'messageId':_0x527fa8[_0xf085('0x31')][0x0][_0xf085('0x32')]});}})[_0xf085('0x1a')](function(_0x4af22d){logger[_0xf085('0x33')]('WhatsappMessage,\x20%s',_0x4af22d);});break;}}})[_0xf085('0x34')](function(){_0x50a765();});}else{_0x50a765();}}},'charset':'utf8mb4','collate':_0xf085('0x35')});};