5da05aecd1f2a1940774a29bc3e1af4fa4e24b70
[motion2.git] / server / api / smsMessage / smsMessage.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 _0x7f81=['POST','user_key','session_key','total_sent','internal_order_id','deliveryReport','read','Missing\x20parameters:\x20username,\x20password','connectel','senderString','https://smsapi.connectel.se/api/send/','requestid','Missing\x20parameters:\x20authToken','clicksend','xCALLY\x20Motion','format','password','https://rest.clicksend.com/v3/sms/send','Basic\x20%s','base64','http_code','data','queued_count','head','messages','message_id','clickSend','plivo','authId','remote','https://api.plivo.com/v1/Account/%s/Message/','toString','message_uuid','Missing\x20parameters:\x20authId,\x20authToken','baseUrl','/auth/1/session','/sms/2/text/advanced','/api/sms/messages/','IBSSO\x20','infobip','csc','crypto','apiKey','login','md5','digest','hex','signature=','GET','/external/get/send.php?','keys','id_sms','https://platform.clickatell.com/messages','UTF-8','apiMessageId','clickatell','Missing\x20parameters:\x20apiKey','https://api.intelepeer.com/_rest/v4/app/sms/send','Bearer\x20','application/json','code','message','refid','status','intelepeer','Token\x20not\x20received','finally','utf8mb4','lodash','util','../../config/logger','api','bluebird','path','rimraf','../../config/environment','./smsMessage.attributes','exports','define','SmsMessage','sms_messages','fti_sms_messages','body','FULLTEXT','models','get','direction','toLowerCase','out','isNil','UserId','SmsInteraction','SmsInteractionId','update','createdAt','catch','secret','SmsAccount','find','SmsAccountId','then','type','twilio','accountSid','authToken','phone','/status?token=','token','sid','error','SmsMessage,\x20%s,\x20%s,\x20%s','afterCreate','inspect','Missing\x20parameters:\x20phone,\x20accountSid\x20or\x20authToken','skebby','username','smsMethod','sender','login?username=','sms'];(function(_0x4afe31,_0x126fed){var _0x235433=function(_0x686e98){while(--_0x686e98){_0x4afe31['push'](_0x4afe31['shift']());}};_0x235433(++_0x126fed);}(_0x7f81,0x1ac));var _0x17f8=function(_0x1bf977,_0xbb8a6b){_0x1bf977=_0x1bf977-0x0;var _0x559680=_0x7f81[_0x1bf977];return _0x559680;};'use strict';var _=require(_0x17f8('0x0'));var util=require(_0x17f8('0x1'));var logger=require(_0x17f8('0x2'))(_0x17f8('0x3'));var moment=require('moment');var BPromise=require(_0x17f8('0x4'));var rp=require('request-promise');var fs=require('fs');var path=require(_0x17f8('0x5'));var rimraf=require(_0x17f8('0x6'));var config=require(_0x17f8('0x7'));var attributes=require(_0x17f8('0x8'));module[_0x17f8('0x9')]=function(_0x4066b2,_0x53438d){return _0x4066b2[_0x17f8('0xa')](_0x17f8('0xb'),attributes,{'tableName':_0x17f8('0xc'),'paranoid':![],'indexes':[{'name':'messageId','fields':['messageId']},{'name':_0x17f8('0xd'),'fields':[_0x17f8('0xe')],'type':_0x17f8('0xf')}],'timestamps':!![],'hooks':{'afterCreate':function(_0x36728e,_0x47ab34,_0x1642cf){var _0x5b92cc=_0x4066b2[_0x17f8('0x10')];var _0x3220f5=_0x36728e[_0x17f8('0x11')]({'plain':!![]});if(_0x3220f5[_0x17f8('0x12')][_0x17f8('0x13')]()==='in'||_0x3220f5[_0x17f8('0x12')][_0x17f8('0x13')]()===_0x17f8('0x14')&&!_0x3220f5['secret']&&!_[_0x17f8('0x15')](_0x3220f5[_0x17f8('0x16')])){_0x5b92cc[_0x17f8('0x17')]['findOne']({'where':{'id':_0x3220f5[_0x17f8('0x18')]}})['then'](function(_0x25d72f){if(_0x25d72f){_0x25d72f[_0x17f8('0x19')]({'lastMsgAt':_0x3220f5[_0x17f8('0x1a')],'lastMsgDirection':_0x3220f5[_0x17f8('0x12')][_0x17f8('0x13')]()});}})[_0x17f8('0x1b')](function(_0xe6be41){console['error'](_0xe6be41);});}if(_0x3220f5[_0x17f8('0x12')]===_0x17f8('0x14')&&!_0x3220f5[_0x17f8('0x1c')]){var _0x65b7c5;_0x5b92cc[_0x17f8('0x1d')][_0x17f8('0x1e')]({'where':{'id':_0x3220f5[_0x17f8('0x1f')]},'raw':!![]})[_0x17f8('0x20')](function(_0x2a1a53){_0x65b7c5=_0x2a1a53;if(_0x65b7c5&&_0x65b7c5[_0x17f8('0x21')]){switch(_0x65b7c5[_0x17f8('0x21')]){case _0x17f8('0x22'):var _0x52117d=require(_0x17f8('0x22'));if(_0x65b7c5[_0x17f8('0x23')]&&_0x65b7c5[_0x17f8('0x24')]&&_0x65b7c5[_0x17f8('0x25')]){var _0x1b9ba7=new _0x52117d(_0x65b7c5[_0x17f8('0x23')],_0x65b7c5[_0x17f8('0x24')]);_0x1b9ba7['messages']['create']({'body':_0x3220f5['body'],'to':_0x3220f5[_0x17f8('0x25')],'from':_0x65b7c5[_0x17f8('0x25')],'statusCallback':_0x65b7c5['remote']+'/api/sms/messages/'+_0x3220f5['id']+_0x17f8('0x26')+_0x65b7c5[_0x17f8('0x27')]})['then'](function(_0x3a63ce){if(_0x3a63ce[_0x17f8('0x28')]){return _0x36728e[_0x17f8('0x19')]({'messageId':_0x3a63ce[_0x17f8('0x28')]});}})[_0x17f8('0x1b')](function(_0x174ecf){logger[_0x17f8('0x29')](_0x17f8('0x2a'),_0x17f8('0x2b'),_0x17f8('0x22'),util[_0x17f8('0x2c')](_0x174ecf,{'showHidden':![],'depth':null}));});}else{logger['error'](_0x17f8('0x2a'),_0x17f8('0x2b'),_0x17f8('0x22'),_0x17f8('0x2d'));}break;case _0x17f8('0x2e'):var _0x5ed964='https://api.skebby.it/API/v1.0/REST/';if(_0x65b7c5[_0x17f8('0x2f')]&&_0x65b7c5['password']){var _0x40d610={'recipient':[_0x3220f5[_0x17f8('0x25')]],'message':_0x3220f5[_0x17f8('0xe')],'message_type':_0x65b7c5[_0x17f8('0x30')]};if(_0x65b7c5[_0x17f8('0x30')]&&_0x65b7c5[_0x17f8('0x30')]!=='SI'){_0x40d610[_0x17f8('0x31')]=_0x65b7c5['phone'];}var _0x1c255f={'url':_0x5ed964+_0x17f8('0x32')+_0x65b7c5[_0x17f8('0x2f')]+'&password='+_0x65b7c5['password'],'method':'GET'};rp(_0x1c255f)[_0x17f8('0x20')](function(_0x432a9d){var _0x224733=_0x432a9d['split'](';');var _0x23f83a={'user_key':_0x224733[0x0],'session_key':_0x224733[0x1]};_0x1c255f={'url':_0x5ed964+_0x17f8('0x33'),'method':_0x17f8('0x34'),'headers':{'user_key':_0x23f83a[_0x17f8('0x35')],'Session_key':_0x23f83a[_0x17f8('0x36')]},'json':!![],'body':_0x40d610};return rp(_0x1c255f);})[_0x17f8('0x20')](function(_0x294df8){if(_0x294df8[_0x17f8('0x37')]>=0x1){var _0x2954aa={'messageId':_0x294df8[_0x17f8('0x38')]};if(_0x65b7c5[_0x17f8('0x30')]!=='GP'||!_0x65b7c5[_0x17f8('0x39')]){_0x2954aa[_0x17f8('0x3a')]=!![];}return _0x36728e[_0x17f8('0x19')](_0x2954aa);}else{return logger['error'](_0x17f8('0x2a'),_0x17f8('0x2b'),'skebby',util[_0x17f8('0x2c')](_0x294df8,{'showHidden':![],'depth':null}));}})['catch'](function(_0x4e6653){return logger[_0x17f8('0x29')](_0x17f8('0x2a'),'afterCreate',_0x17f8('0x2e'),util[_0x17f8('0x2c')](_0x4e6653,{'showHidden':![],'depth':null}));});}else{logger[_0x17f8('0x29')](_0x17f8('0x2a'),_0x17f8('0x2b'),_0x17f8('0x2e'),_0x17f8('0x3b'));}break;case _0x17f8('0x3c'):if(_0x65b7c5[_0x17f8('0x24')]){var _0x148a44={'recipient':_0x3220f5[_0x17f8('0x25')],'message':_0x3220f5['body'],'sender':_0x65b7c5[_0x17f8('0x3d')]||_0x65b7c5[_0x17f8('0x25')],'authorization':_0x65b7c5['authToken']};var _0x47ab34={'method':_0x17f8('0x34'),'uri':_0x17f8('0x3e'),'body':_0x148a44,'json':!![]};rp(_0x47ab34)[_0x17f8('0x20')](function(_0x45bacf){var _0x182c64={'messageId':_0x45bacf[_0x17f8('0x3f')]};if(!_0x65b7c5[_0x17f8('0x39')]){_0x182c64[_0x17f8('0x3a')]=!![];}return _0x36728e['update'](_0x182c64);})[_0x17f8('0x1b')](function(_0x405365){logger['error'](_0x17f8('0x2a'),'afterCreate',_0x17f8('0x3c'),util[_0x17f8('0x2c')](_0x405365,{'showHidden':![],'depth':null}));});}else{logger[_0x17f8('0x29')](_0x17f8('0x2a'),_0x17f8('0x2b'),_0x17f8('0x3c'),_0x17f8('0x40'));}break;case _0x17f8('0x41'):if(_0x65b7c5[_0x17f8('0x2f')]&&_0x65b7c5['password']){var _0x148a44={'messages':[{'source':_0x17f8('0x42'),'to':_0x3220f5[_0x17f8('0x25')],'body':_0x3220f5['body'],'from':_0x65b7c5[_0x17f8('0x3d')]||_0x65b7c5[_0x17f8('0x25')]}]};var _0x206383=util[_0x17f8('0x43')]('%s:%s',_0x65b7c5['username'],_0x65b7c5[_0x17f8('0x44')]);var _0x47ab34={'method':'POST','uri':_0x17f8('0x45'),'body':_0x148a44,'headers':{'Authorization':util[_0x17f8('0x43')](_0x17f8('0x46'),new Buffer(_0x206383)['toString'](_0x17f8('0x47')))},'json':!![]};rp(_0x47ab34)[_0x17f8('0x20')](function(_0x141d31){if(_0x141d31['http_code']>=0xc8&&_0x141d31[_0x17f8('0x48')]<=0xce){if(_0x141d31['data']&&_0x141d31[_0x17f8('0x49')][_0x17f8('0x4a')]>0x0){var _0x3470f7=_[_0x17f8('0x4b')](_0x141d31['data'][_0x17f8('0x4c')]);var _0x1acef1={'messageId':_0x3470f7[_0x17f8('0x4d')]};if(!_0x65b7c5[_0x17f8('0x39')]){_0x1acef1[_0x17f8('0x3a')]=!![];}return _0x36728e[_0x17f8('0x19')](_0x1acef1);}else{logger[_0x17f8('0x29')](_0x17f8('0x2a'),'afterCreate',_0x17f8('0x4e'),util[_0x17f8('0x2c')](_0x141d31,{'showHidden':![],'depth':null}));}}else{logger[_0x17f8('0x29')](_0x17f8('0x2a'),_0x17f8('0x2b'),_0x17f8('0x4e'),util[_0x17f8('0x2c')](_0x141d31,{'showHidden':![],'depth':null}));}})['catch'](function(_0xb41210){logger[_0x17f8('0x29')](_0x17f8('0x2a'),_0x17f8('0x2b'),_0x17f8('0x4e'),util['inspect'](_0xb41210,{'showHidden':![],'depth':null}));});}else{logger['error'](_0x17f8('0x2a'),_0x17f8('0x2b'),'clickSend',_0x17f8('0x3b'));}break;case _0x17f8('0x4f'):if(_0x65b7c5[_0x17f8('0x50')]&&_0x65b7c5['authToken']){var _0x148a44={'dst':_0x3220f5[_0x17f8('0x25')],'text':_0x3220f5[_0x17f8('0xe')],'src':_0x65b7c5[_0x17f8('0x3d')]||_0x65b7c5['phone'],'url':_0x65b7c5[_0x17f8('0x51')]+'/api/sms/messages/'+_0x3220f5['id']+_0x17f8('0x26')+_0x65b7c5[_0x17f8('0x27')]};var _0x206383=util[_0x17f8('0x43')]('%s:%s',_0x65b7c5['authId'],_0x65b7c5[_0x17f8('0x24')]);var _0x47ab34={'method':_0x17f8('0x34'),'uri':util[_0x17f8('0x43')](_0x17f8('0x52'),_0x65b7c5['authId']),'body':_0x148a44,'headers':{'Authorization':util[_0x17f8('0x43')](_0x17f8('0x46'),new Buffer(_0x206383)[_0x17f8('0x53')](_0x17f8('0x47')))},'json':!![]};rp(_0x47ab34)['then'](function(_0x30258c){var _0x4cdbb1={'messageId':_0x30258c[_0x17f8('0x54')][0x0]};return _0x36728e['update'](_0x4cdbb1);})['catch'](function(_0x3e9544){logger[_0x17f8('0x29')]('SmsMessage,\x20%s,\x20%s,\x20%s',_0x17f8('0x2b'),_0x17f8('0x4f'),util['inspect'](_0x3e9544,{'showHidden':![],'depth':null}));});}else{logger[_0x17f8('0x29')]('SmsMessage,\x20%s,\x20%s,\x20%s',_0x17f8('0x2b'),_0x17f8('0x4f'),_0x17f8('0x55'));}break;case'infobip':if(_0x65b7c5[_0x17f8('0x2f')]&&_0x65b7c5[_0x17f8('0x44')]){if(_0x65b7c5[_0x17f8('0x56')]){var _0x32335c={'method':_0x17f8('0x34'),'uri':_0x65b7c5[_0x17f8('0x56')]+_0x17f8('0x57'),'body':{'username':_0x65b7c5[_0x17f8('0x2f')],'password':_0x65b7c5[_0x17f8('0x44')]},'json':!![]};return rp(_0x32335c)['then'](function(_0x376223){var _0x56f643={'method':'POST','uri':_0x65b7c5[_0x17f8('0x56')]+_0x17f8('0x58'),'body':{'messages':[{'from':_0x65b7c5[_0x17f8('0x25')],'destinations':[{'to':_0x3220f5[_0x17f8('0x25')]}],'text':_0x3220f5[_0x17f8('0xe')],'notifyUrl':_0x65b7c5[_0x17f8('0x51')]+_0x17f8('0x59')+_0x3220f5['id']+_0x17f8('0x26')+_0x65b7c5[_0x17f8('0x27')],'notifyContentType':'application/json','callbackData':_0x65b7c5[_0x17f8('0x25')]+';'+_0x3220f5[_0x17f8('0xe')]}]},'headers':{'Authorization':_0x17f8('0x5a')+_0x376223[_0x17f8('0x27')]},'json':!![]};return rp(_0x56f643)[_0x17f8('0x20')](function(_0x1d7499){if(_0x1d7499[_0x17f8('0x4c')]){var _0x351a80={'messageId':_0x1d7499['messages'][0x0]['messageId']};if(!_0x65b7c5[_0x17f8('0x39')]){_0x351a80[_0x17f8('0x3a')]=!![];}return _0x36728e[_0x17f8('0x19')](_0x351a80);}else{return logger['error'](_0x17f8('0x2a'),'afterCreate','infobip',util[_0x17f8('0x2c')](_0x1d7499,{'showHidden':![],'depth':null}));}});})[_0x17f8('0x1b')](function(_0x30c143){return logger[_0x17f8('0x29')](_0x17f8('0x2a'),_0x17f8('0x2b'),_0x17f8('0x5b'),util[_0x17f8('0x2c')](_0x30c143,{'showHidden':![],'depth':null}));});}else{logger[_0x17f8('0x29')](_0x17f8('0x2a'),_0x17f8('0x2b'),_0x17f8('0x5b'),'Missing\x20parameters:\x20base_url');}}else{logger[_0x17f8('0x29')]('SmsMessage,\x20%s,\x20%s,\x20%s','afterCreate',_0x17f8('0x5b'),_0x17f8('0x3b'));}break;case _0x17f8('0x5c'):var _0x10aa6f=require(_0x17f8('0x5d'));if(_0x65b7c5[_0x17f8('0x5e')]){var _0x452a73={};_0x452a73[_0x17f8('0x5f')]=_0x65b7c5['username'];_0x452a73[_0x17f8('0x25')]=_0x3220f5[_0x17f8('0x25')];_0x452a73['sender']=_0x65b7c5[_0x17f8('0x25')];_0x452a73['text']=_0x3220f5[_0x17f8('0xe')];_0x452a73['timestamp']=+new Date();var _0x21bb02='';var _0x34612e='';var _0x500bec=Object['keys'](_0x452a73);_0x500bec['sort']();for(var _0x56569d=0x0;_0x56569d<_0x500bec['length'];_0x56569d++){if(_0x21bb02!=''){_0x21bb02+='&';}_0x21bb02+=_0x500bec[_0x56569d]+'='+encodeURIComponent(_0x452a73[_0x500bec[_0x56569d]]);_0x34612e+=_0x452a73[_0x500bec[_0x56569d]];}_0x34612e+=_0x65b7c5[_0x17f8('0x5e')];_0x34612e=_0x10aa6f['createHash'](_0x17f8('0x60'))[_0x17f8('0x19')](_0x34612e)[_0x17f8('0x61')](_0x17f8('0x62'));if(_0x21bb02!==''){_0x21bb02+='&';}_0x21bb02+=_0x17f8('0x63')+_0x34612e;var _0x1c255f={'method':_0x17f8('0x64'),'uri':_0x65b7c5['baseUrl']+_0x17f8('0x65')+_0x21bb02,'json':!![]};return rp(_0x1c255f)[_0x17f8('0x20')](function(_0xcf0cf){var _0x3c94eb=Object[_0x17f8('0x66')](_0xcf0cf);if(_0xcf0cf[_0x3c94eb[0x0]]['error']&&parseInt(_0xcf0cf[_0x3c94eb[0x0]][_0x17f8('0x29')])===0x0){var _0x31d507={'messageId':_0xcf0cf[_0x3c94eb[0x0]][_0x17f8('0x67')]};if(!_0x65b7c5[_0x17f8('0x39')]){_0x31d507[_0x17f8('0x3a')]=!![];}return _0x36728e[_0x17f8('0x19')](_0x31d507);}else{return logger['error'](_0x17f8('0x2a'),_0x17f8('0x2b'),_0x17f8('0x5c'),util[_0x17f8('0x2c')](_0xcf0cf[_0x17f8('0x29')],{'showHidden':![],'depth':null}));}})[_0x17f8('0x1b')](function(_0x278a4f){return logger[_0x17f8('0x29')](_0x17f8('0x2a'),'afterCreate',_0x17f8('0x5c'),util[_0x17f8('0x2c')](_0x278a4f,{'showHidden':![],'depth':null}));});}else{logger[_0x17f8('0x29')](_0x17f8('0x2a'),_0x17f8('0x2b'),_0x17f8('0x5c'),'Missing\x20parameters:\x20apiKey');}break;case'clickatell':if(_0x65b7c5[_0x17f8('0x5e')]){var _0x385897={'method':_0x17f8('0x34'),'uri':_0x17f8('0x68'),'headers':{'Authorization':_0x65b7c5[_0x17f8('0x5e')]},'body':{'content':_0x3220f5[_0x17f8('0xe')],'to':[_0x3220f5[_0x17f8('0x25')]],'from':_0x65b7c5[_0x17f8('0x25')],'binary':![],'clientMessageId':'uuid','charset':_0x17f8('0x69')},'json':!![]};rp(_0x385897)[_0x17f8('0x20')](function(_0x472bc2){if(!_0x472bc2[_0x17f8('0x29')]){var _0x206eba={'messageId':_0x472bc2[_0x17f8('0x4c')][0x0][_0x17f8('0x6a')]};if(!_0x65b7c5[_0x17f8('0x39')]){_0x206eba[_0x17f8('0x3a')]=!![];}return _0x36728e['update'](_0x206eba);}else{return logger[_0x17f8('0x29')](_0x17f8('0x2a'),_0x17f8('0x2b'),_0x17f8('0x6b'),util[_0x17f8('0x2c')](_0x472bc2['error'],{'showHidden':![],'depth':null}));}})['catch'](function(_0x3e8714){return logger[_0x17f8('0x29')](_0x17f8('0x2a'),_0x17f8('0x2b'),_0x17f8('0x6b'),util[_0x17f8('0x2c')](_0x3e8714,{'showHidden':![],'depth':null}));});}else{logger[_0x17f8('0x29')](_0x17f8('0x2a'),_0x17f8('0x2b'),_0x17f8('0x6b'),_0x17f8('0x6c'));}break;case'intelepeer':if(_0x65b7c5[_0x17f8('0x5e')]){var _0x445644={};var _0x1c255f={'method':'POST','uri':_0x17f8('0x6d'),'headers':{'Authorization':_0x17f8('0x6e')+_0x65b7c5[_0x17f8('0x5e')],'Content-Type':_0x17f8('0x6f')},'body':{'from':_0x65b7c5[_0x17f8('0x25')],'to':_0x3220f5[_0x17f8('0x25')],'text':_0x3220f5[_0x17f8('0xe')]},'json':!![]};return rp(_0x1c255f)[_0x17f8('0x20')](function(_0x46938c){if(_0x46938c[_0x17f8('0x70')]==0xca){_0x445644[_0x17f8('0x71')]=_0x46938c;var _0x485e5b={'method':_0x17f8('0x34'),'uri':'https://api.intelepeer.com/_rest/v4/app/sms/mdr','headers':{'Authorization':_0x17f8('0x6e')+_0x65b7c5['apiKey'],'Content-Type':_0x17f8('0x6f')},'body':{'refid':_0x445644['message'][_0x17f8('0x72')]},'json':!![]};return rp(_0x485e5b)[_0x17f8('0x20')](function(_0x4e39a1){_0x445644[_0x17f8('0x73')]=_0x4e39a1;var _0x278681={'messageId':_0x445644[_0x17f8('0x71')]['refid'],'read':!![]};return _0x36728e[_0x17f8('0x19')](_0x278681);});}else{return logger[_0x17f8('0x29')](_0x17f8('0x2a'),'afterCreate',_0x17f8('0x74'),util[_0x17f8('0x2c')](_0x46938c,{'showHidden':![],'depth':null}));}})[_0x17f8('0x1b')](function(_0x59a4e1){return logger[_0x17f8('0x29')](_0x17f8('0x2a'),'afterCreate','intelepeer',util[_0x17f8('0x2c')](_0x59a4e1,{'showHidden':![],'depth':null}));});}else{return logger[_0x17f8('0x29')](_0x17f8('0x2a'),_0x17f8('0x2b'),_0x17f8('0x74'),_0x17f8('0x75'));}break;}}})[_0x17f8('0x76')](function(){_0x1642cf();});}else{_0x1642cf();}}},'charset':_0x17f8('0x77'),'collate':'utf8mb4_unicode_ci'});};