Built motion from commit d5e4af8c.|2.6.23
[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 _0x3228=['application/json','statuses','message_id','err','WhatsappMessage,\x20%s','utf8mb4','utf8mb4_unicode_ci','lodash','api','moment','bluebird','request-promise','node-fetch','path','rimraf','../../config/environment','./whatsappMessage.attributes','WhatsappMessage','whatsapp_messages','messageId','fti_whatsapp_messages','body','FULLTEXT','get','direction','toLowerCase','out','secret','isNil','UserId','WhatsappInteraction','findOne','WhatsappInteractionId','then','update','createdAt','catch','error','WhatsappAccount','find','WhatsappAccountId','type','twilio','accountSid','authToken','phone','messages','create','whatsapp:','remote','/api/whatsapp/messages/','/status?token=','token','sid','WhatsappMessage,\x20%s,\x20%s,\x20%s','afterCreate','inspect','sinch','/whatsapp/v1/','/messages/','text','POST','Bearer\x20'];(function(_0x565ed7,_0x398228){var _0x183ad7=function(_0x5a43d7){while(--_0x5a43d7){_0x565ed7['push'](_0x565ed7['shift']());}};_0x183ad7(++_0x398228);}(_0x3228,0x181));var _0x8322=function(_0x1a5433,_0x4b01ff){_0x1a5433=_0x1a5433-0x0;var _0x4fec8c=_0x3228[_0x1a5433];return _0x4fec8c;};'use strict';var _=require(_0x8322('0x0'));var util=require('util');var logger=require('../../config/logger')(_0x8322('0x1'));var moment=require(_0x8322('0x2'));var BPromise=require(_0x8322('0x3'));var rp=require(_0x8322('0x4'));var fetch=require(_0x8322('0x5'));var fs=require('fs');var path=require(_0x8322('0x6'));var rimraf=require(_0x8322('0x7'));var config=require(_0x8322('0x8'));var attributes=require(_0x8322('0x9'));module['exports']=function(_0x2d3f72,_0x1d7870){return _0x2d3f72['define'](_0x8322('0xa'),attributes,{'tableName':_0x8322('0xb'),'paranoid':![],'indexes':[{'name':_0x8322('0xc'),'fields':[_0x8322('0xc')]},{'name':_0x8322('0xd'),'fields':[_0x8322('0xe')],'type':_0x8322('0xf')}],'timestamps':!![],'hooks':{'afterCreate':function(_0x15c187,_0x35a94a,_0x2a3369){var _0xa58401=_0x2d3f72['models'];var _0x5a159f=_0x15c187[_0x8322('0x10')]({'plain':!![]});if(_0x5a159f[_0x8322('0x11')][_0x8322('0x12')]()==='in'||_0x5a159f[_0x8322('0x11')][_0x8322('0x12')]()===_0x8322('0x13')&&!_0x5a159f[_0x8322('0x14')]&&!_[_0x8322('0x15')](_0x5a159f[_0x8322('0x16')])){_0xa58401[_0x8322('0x17')][_0x8322('0x18')]({'where':{'id':_0x5a159f[_0x8322('0x19')]}})[_0x8322('0x1a')](function(_0x250164){if(_0x250164){_0x250164[_0x8322('0x1b')]({'lastMsgAt':_0x5a159f[_0x8322('0x1c')],'lastMsgDirection':_0x5a159f[_0x8322('0x11')][_0x8322('0x12')]()});}})[_0x8322('0x1d')](function(_0x54950c){console[_0x8322('0x1e')](_0x54950c);});}if(_0x5a159f['direction']===_0x8322('0x13')&&!_0x5a159f[_0x8322('0x14')]){var _0xebace9;_0xa58401[_0x8322('0x1f')][_0x8322('0x20')]({'where':{'id':_0x5a159f[_0x8322('0x21')]},'raw':!![]})[_0x8322('0x1a')](function(_0xab35b3){_0xebace9=_0xab35b3;if(_0xebace9&&_0xebace9['type']){switch(_0xebace9[_0x8322('0x22')]){case _0x8322('0x23'):var _0xc6020=require(_0x8322('0x23'));if(_0xebace9[_0x8322('0x24')]&&_0xebace9[_0x8322('0x25')]&&_0xebace9[_0x8322('0x26')]){var _0x4f5285=new _0xc6020(_0xebace9['accountSid'],_0xebace9['authToken']);_0x4f5285[_0x8322('0x27')][_0x8322('0x28')]({'body':_0x5a159f[_0x8322('0xe')],'to':_0x8322('0x29')+_0x5a159f[_0x8322('0x26')],'from':_0x8322('0x29')+_0xebace9[_0x8322('0x26')],'statusCallback':_0xebace9[_0x8322('0x2a')]+_0x8322('0x2b')+_0x5a159f['id']+_0x8322('0x2c')+_0xebace9[_0x8322('0x2d')]})[_0x8322('0x1a')](function(_0x3cbd64){if(_0x3cbd64[_0x8322('0x2e')]){return _0x15c187['update']({'messageId':_0x3cbd64[_0x8322('0x2e')]});}})[_0x8322('0x1d')](function(_0x1d6c94){logger[_0x8322('0x1e')](_0x8322('0x2f'),_0x8322('0x30'),_0x8322('0x23'),util[_0x8322('0x31')](_0x1d6c94,{'showHidden':![],'depth':null}));});}else{logger[_0x8322('0x1e')](_0x8322('0x2f'),_0x8322('0x30'),'twilio','Missing\x20parameters:\x20phone,\x20accountSid\x20or\x20authToken');}break;case _0x8322('0x32'):var _0x4d3b67=_0xebace9[_0x8322('0x24')];var _0x516e85=_0xebace9[_0x8322('0x25')];var _0x2f8ded=_0xebace9['baseUrl']+_0x8322('0x33')+_0x4d3b67+_0x8322('0x34');var _0xe6d895={'to':[_0x5a159f[_0x8322('0x26')]],'message':{'type':_0x8322('0x35'),'text':_0x5a159f[_0x8322('0xe')]}};var _0xae1974={'method':_0x8322('0x36'),'uri':_0x2f8ded,'headers':{'Authorization':_0x8322('0x37')+_0x516e85,'Content-Type':_0x8322('0x38')},'body':_0xe6d895,'json':!![]};rp(_0xae1974)[_0x8322('0x1a')](function(_0x338359){if(_0x338359[_0x8322('0x39')][0x0][_0x8322('0x3a')]){return _0x15c187[_0x8322('0x1b')]({'messageId':_0x338359[_0x8322('0x39')][0x0]['message_id']});}})[_0x8322('0x1d')](function(_0x503faa){logger[_0x8322('0x3b')](_0x8322('0x3c'),_0x503faa);});break;}}})['finally'](function(){_0x2a3369();});}else{_0x2a3369();}}},'charset':_0x8322('0x3d'),'collate':_0x8322('0x3e')});};