Built motion from commit bab6f49e.|2.6.17
[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 _0xf61f=['/messages/','text','POST','Bearer\x20','application/json','statuses','message_id','err','WhatsappMessage,\x20%s','utf8mb4','utf8mb4_unicode_ci','lodash','util','../../config/logger','api','moment','rimraf','../../config/environment','./whatsappMessage.attributes','exports','define','whatsapp_messages','messageId','fti_whatsapp_messages','body','FULLTEXT','get','direction','toLowerCase','out','secret','isNil','WhatsappInteraction','findOne','WhatsappInteractionId','then','update','createdAt','catch','WhatsappAccount','type','twilio','accountSid','authToken','phone','messages','create','remote','/api/whatsapp/messages/','sid','error','WhatsappMessage,\x20%s,\x20%s,\x20%s','afterCreate','inspect','sinch','baseUrl','/whatsapp/v1/'];(function(_0x4b1552,_0x43b0f5){var _0x2fc6f1=function(_0x4e4a46){while(--_0x4e4a46){_0x4b1552['push'](_0x4b1552['shift']());}};_0x2fc6f1(++_0x43b0f5);}(_0xf61f,0xb6));var _0xff61=function(_0x1ca882,_0x14f6e6){_0x1ca882=_0x1ca882-0x0;var _0x357d7e=_0xf61f[_0x1ca882];return _0x357d7e;};'use strict';var _=require(_0xff61('0x0'));var util=require(_0xff61('0x1'));var logger=require(_0xff61('0x2'))(_0xff61('0x3'));var moment=require(_0xff61('0x4'));var BPromise=require('bluebird');var rp=require('request-promise');var fetch=require('node-fetch');var fs=require('fs');var path=require('path');var rimraf=require(_0xff61('0x5'));var config=require(_0xff61('0x6'));var attributes=require(_0xff61('0x7'));module[_0xff61('0x8')]=function(_0xa5d6b,_0x3eb4c7){return _0xa5d6b[_0xff61('0x9')]('WhatsappMessage',attributes,{'tableName':_0xff61('0xa'),'paranoid':![],'indexes':[{'name':_0xff61('0xb'),'fields':['messageId']},{'name':_0xff61('0xc'),'fields':[_0xff61('0xd')],'type':_0xff61('0xe')}],'timestamps':!![],'hooks':{'afterCreate':function(_0x2945b0,_0x5c4543,_0x4c198a){var _0x21a806=_0xa5d6b['models'];var _0x30e21d=_0x2945b0[_0xff61('0xf')]({'plain':!![]});if(_0x30e21d[_0xff61('0x10')][_0xff61('0x11')]()==='in'||_0x30e21d[_0xff61('0x10')][_0xff61('0x11')]()===_0xff61('0x12')&&!_0x30e21d[_0xff61('0x13')]&&!_[_0xff61('0x14')](_0x30e21d['UserId'])){_0x21a806[_0xff61('0x15')][_0xff61('0x16')]({'where':{'id':_0x30e21d[_0xff61('0x17')]}})[_0xff61('0x18')](function(_0x2c827e){if(_0x2c827e){_0x2c827e[_0xff61('0x19')]({'lastMsgAt':_0x30e21d[_0xff61('0x1a')],'lastMsgDirection':_0x30e21d[_0xff61('0x10')][_0xff61('0x11')]()});}})[_0xff61('0x1b')](function(_0x20f7a6){console['error'](_0x20f7a6);});}if(_0x30e21d[_0xff61('0x10')]===_0xff61('0x12')&&!_0x30e21d['secret']){var _0x35b126;_0x21a806[_0xff61('0x1c')]['find']({'where':{'id':_0x30e21d['WhatsappAccountId']},'raw':!![]})['then'](function(_0x4d0d21){_0x35b126=_0x4d0d21;if(_0x35b126&&_0x35b126[_0xff61('0x1d')]){switch(_0x35b126[_0xff61('0x1d')]){case _0xff61('0x1e'):var _0x4ca107=require(_0xff61('0x1e'));if(_0x35b126[_0xff61('0x1f')]&&_0x35b126[_0xff61('0x20')]&&_0x35b126[_0xff61('0x21')]){var _0x4a1f2e=new _0x4ca107(_0x35b126[_0xff61('0x1f')],_0x35b126[_0xff61('0x20')]);_0x4a1f2e[_0xff61('0x22')][_0xff61('0x23')]({'body':_0x30e21d[_0xff61('0xd')],'to':'whatsapp:'+_0x30e21d['phone'],'from':'whatsapp:'+_0x35b126['phone'],'statusCallback':_0x35b126[_0xff61('0x24')]+_0xff61('0x25')+_0x30e21d['id']+'/status?token='+_0x35b126['token']})[_0xff61('0x18')](function(_0x26fbb4){if(_0x26fbb4[_0xff61('0x26')]){return _0x2945b0[_0xff61('0x19')]({'messageId':_0x26fbb4['sid']});}})[_0xff61('0x1b')](function(_0xfd5a3e){logger[_0xff61('0x27')](_0xff61('0x28'),_0xff61('0x29'),_0xff61('0x1e'),util[_0xff61('0x2a')](_0xfd5a3e,{'showHidden':![],'depth':null}));});}else{logger[_0xff61('0x27')]('WhatsappMessage,\x20%s,\x20%s,\x20%s',_0xff61('0x29'),_0xff61('0x1e'),'Missing\x20parameters:\x20phone,\x20accountSid\x20or\x20authToken');}break;case _0xff61('0x2b'):var _0x207d10=_0x35b126[_0xff61('0x1f')];var _0x3f24b2=_0x35b126[_0xff61('0x20')];var _0x538c61=_0x35b126[_0xff61('0x2c')]+_0xff61('0x2d')+_0x207d10+_0xff61('0x2e');var _0x580bfe={'to':[_0x30e21d[_0xff61('0x21')]],'message':{'type':_0xff61('0x2f'),'text':_0x30e21d[_0xff61('0xd')]}};var _0x5c59ab={'method':_0xff61('0x30'),'uri':_0x538c61,'headers':{'Authorization':_0xff61('0x31')+_0x3f24b2,'Content-Type':_0xff61('0x32')},'body':_0x580bfe,'json':!![]};rp(_0x5c59ab)[_0xff61('0x18')](function(_0x32fb33){if(_0x32fb33[_0xff61('0x33')][0x0][_0xff61('0x34')]){return _0x2945b0[_0xff61('0x19')]({'messageId':_0x32fb33['statuses'][0x0][_0xff61('0x34')]});}})[_0xff61('0x1b')](function(_0x24014b){logger[_0xff61('0x35')](_0xff61('0x36'),_0x24014b);});break;}}})['finally'](function(){_0x4c198a();});}else{_0x4c198a();}}},'charset':_0xff61('0x37'),'collate':_0xff61('0x38')});};