Built motion from commit (unavailable).|2.0.79
[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 _0x1df7=['get','direction','toLowerCase','out','secret','isNil','SmsInteraction','then','update','body','catch','find','SmsAccountId','type','accountSid','authToken','phone','create','remote','/api/sms/messages/','/status?token=','sid','error','SmsMessage,\x20%s,\x20%s,\x20%s','afterCreate','twilio','inspect','Missing\x20parameters:\x20phone,\x20accountSid\x20or\x20authToken','skebby','querystring','username','password','UTF-8','basic','method','send_sms_basic','send_sms_classic','senderString','sender_string','classic+','send_sms_classic_report','sender_number','POST','https://gateway.skebby.it/api/send/smseasy/advanced/http.php','application/x-www-form-urlencoded','length','utf8','parse','status','success','smsMethod','deliveryReport','read','connectel','https://smsapi.connectel.se/api/send/','requestid','Missing\x20parameters:\x20authToken','clicksend','xCALLY\x20Motion','format','Basic\x20%s','toString','base64','http_code','data','queued_count','head','messages','message_id','clickSend','plivo','authId','token','%s:%s','message_uuid','finally','utf8mb4','lodash','util','../../config/logger','moment','bluebird','./smsMessage.attributes','exports','define','messageId'];(function(_0x4067ac,_0x363603){var _0x53d74d=function(_0x492ad4){while(--_0x492ad4){_0x4067ac['push'](_0x4067ac['shift']());}};_0x53d74d(++_0x363603);}(_0x1df7,0xa3));var _0x71df=function(_0x48b06e,_0x32a60a){_0x48b06e=_0x48b06e-0x0;var _0x5bf0a=_0x1df7[_0x48b06e];return _0x5bf0a;};'use strict';var _=require(_0x71df('0x0'));var util=require(_0x71df('0x1'));var logger=require(_0x71df('0x2'))('api');var moment=require(_0x71df('0x3'));var BPromise=require(_0x71df('0x4'));var rp=require('request-promise');var attributes=require(_0x71df('0x5'));module[_0x71df('0x6')]=function(_0x42d48e,_0x5b1e35){return _0x42d48e[_0x71df('0x7')]('SmsMessage',attributes,{'tableName':'sms_messages','paranoid':![],'indexes':[{'name':_0x71df('0x8'),'fields':['messageId']}],'timestamps':!![],'hooks':{'afterCreate':function(_0x1175a3,_0x24170b,_0x238d4a){var _0x1ab1c0=_0x42d48e['models'];var _0x5064f8=_0x1175a3[_0x71df('0x9')]({'plain':!![]});if(_0x5064f8[_0x71df('0xa')]['toLowerCase']()==='in'||_0x5064f8[_0x71df('0xa')][_0x71df('0xb')]()===_0x71df('0xc')&&!_0x5064f8[_0x71df('0xd')]&&!_[_0x71df('0xe')](_0x5064f8['UserId'])){_0x1ab1c0[_0x71df('0xf')]['findOne']({'where':{'id':_0x5064f8['SmsInteractionId']}})[_0x71df('0x10')](function(_0x2636fb){if(_0x2636fb){_0x2636fb[_0x71df('0x11')]({'lastMsgAt':_0x5064f8['createdAt'],'lastMsgDirection':_0x5064f8[_0x71df('0xa')]['toLowerCase'](),'lastMsgBody':_0x5064f8[_0x71df('0x12')]});}})[_0x71df('0x13')](function(_0x9978d1){console['error'](_0x9978d1);});}if(_0x5064f8['direction']==='out'&&!_0x5064f8['secret']){var _0x2a8997;_0x1ab1c0['SmsAccount'][_0x71df('0x14')]({'where':{'id':_0x5064f8[_0x71df('0x15')]},'raw':!![]})[_0x71df('0x10')](function(_0x1ea187){_0x2a8997=_0x1ea187;if(_0x2a8997&&_0x2a8997[_0x71df('0x16')]){switch(_0x2a8997[_0x71df('0x16')]){case'twilio':var _0x552dab=require('twilio');if(_0x2a8997[_0x71df('0x17')]&&_0x2a8997[_0x71df('0x18')]&&_0x2a8997[_0x71df('0x19')]){var _0x37047b=new _0x552dab(_0x2a8997[_0x71df('0x17')],_0x2a8997[_0x71df('0x18')]);_0x37047b['messages'][_0x71df('0x1a')]({'body':_0x5064f8[_0x71df('0x12')],'to':_0x5064f8['phone'],'from':_0x2a8997['phone'],'statusCallback':_0x2a8997[_0x71df('0x1b')]+_0x71df('0x1c')+_0x5064f8['id']+_0x71df('0x1d')+_0x2a8997['token']})['then'](function(_0x349fea){if(_0x349fea['sid']){return _0x1175a3['update']({'messageId':_0x349fea[_0x71df('0x1e')]});}})[_0x71df('0x13')](function(_0x5bcbef){logger[_0x71df('0x1f')](_0x71df('0x20'),_0x71df('0x21'),_0x71df('0x22'),util[_0x71df('0x23')](_0x5bcbef,{'showHidden':![],'depth':null}));});}else{logger[_0x71df('0x1f')]('SmsMessage,\x20%s,\x20%s,\x20%s',_0x71df('0x21'),'twilio',_0x71df('0x24'));}break;case _0x71df('0x25'):var _0x469c79=require(_0x71df('0x26'));if(_0x2a8997[_0x71df('0x27')]&&_0x2a8997[_0x71df('0x28')]){var _0x54c13b={'username':_0x2a8997['username'],'password':_0x2a8997[_0x71df('0x28')],'recipients[]':[_0x5064f8[_0x71df('0x19')]],'text':_0x5064f8[_0x71df('0x12')],'charset':_0x71df('0x29')};switch(_0x2a8997['smsMethod']){case _0x71df('0x2a'):_0x54c13b[_0x71df('0x2b')]=_0x71df('0x2c');break;case'classic':_0x54c13b[_0x71df('0x2b')]=_0x71df('0x2d');_0x54c13b['sender_number']=_0x2a8997[_0x71df('0x19')];if(_0x2a8997[_0x71df('0x2e')]){_0x54c13b[_0x71df('0x2f')]=_0x2a8997[_0x71df('0x2e')];}else if(_0x2a8997['phone']){_0x54c13b['sender_number']=_0x2a8997['phone'];}break;case _0x71df('0x30'):_0x54c13b[_0x71df('0x2b')]=_0x71df('0x31');_0x54c13b[_0x71df('0x32')]=_0x2a8997[_0x71df('0x19')];if(_0x2a8997[_0x71df('0x2e')]){_0x54c13b['sender_string']=_0x2a8997[_0x71df('0x2e')];}else if(_0x2a8997[_0x71df('0x19')]){_0x54c13b[_0x71df('0x32')]=_0x2a8997[_0x71df('0x19')];}break;}var _0x125405=_0x469c79['stringify'](_0x54c13b);var _0x24170b={'method':_0x71df('0x33'),'uri':_0x71df('0x34'),'body':_0x125405,'headers':{'Content-Type':_0x71df('0x35'),'Content-Length':_0x125405[_0x71df('0x36')],'Content-Encoding':_0x71df('0x37')}};rp(_0x24170b)[_0x71df('0x10')](function(_0x51ecf5){var _0x35faf5=_0x469c79[_0x71df('0x38')](_0x51ecf5);if(_0x35faf5[_0x71df('0x39')]==_0x71df('0x3a')){var _0x3de897={'messageId':_0x35faf5['id']};if(_0x2a8997[_0x71df('0x3b')]!==_0x71df('0x30')||!_0x2a8997[_0x71df('0x3c')]){_0x3de897[_0x71df('0x3d')]=!![];}return _0x1175a3[_0x71df('0x11')](_0x3de897);}else{logger[_0x71df('0x1f')](_0x71df('0x20'),_0x71df('0x21'),_0x71df('0x25'),util[_0x71df('0x23')](_0x35faf5,{'showHidden':![],'depth':null}));}})['catch'](function(_0x19dc3c){logger[_0x71df('0x1f')](_0x71df('0x20'),'afterCreate',_0x71df('0x25'),util[_0x71df('0x23')](_0x19dc3c,{'showHidden':![],'depth':null}));});}else{logger[_0x71df('0x1f')](_0x71df('0x20'),_0x71df('0x21'),_0x71df('0x25'),'Missing\x20parameters:\x20username,\x20password');}break;case _0x71df('0x3e'):if(_0x2a8997[_0x71df('0x18')]){var _0x54c13b={'recipient':_0x5064f8[_0x71df('0x19')],'message':_0x5064f8[_0x71df('0x12')],'sender':_0x2a8997['senderString']||_0x2a8997[_0x71df('0x19')],'authorization':_0x2a8997['authToken']};var _0x24170b={'method':_0x71df('0x33'),'uri':_0x71df('0x3f'),'body':_0x54c13b,'json':!![]};rp(_0x24170b)['then'](function(_0x45cc3b){var _0x96a6f2={'messageId':_0x45cc3b[_0x71df('0x40')]};if(!_0x2a8997[_0x71df('0x3c')]){_0x96a6f2['read']=!![];}return _0x1175a3[_0x71df('0x11')](_0x96a6f2);})[_0x71df('0x13')](function(_0x456808){logger[_0x71df('0x1f')](_0x71df('0x20'),_0x71df('0x21'),'connectel',util[_0x71df('0x23')](_0x456808,{'showHidden':![],'depth':null}));});}else{logger['error']('SmsMessage,\x20%s,\x20%s,\x20%s','afterCreate',_0x71df('0x3e'),_0x71df('0x41'));}break;case _0x71df('0x42'):if(_0x2a8997['username']&&_0x2a8997['password']){var _0x54c13b={'messages':[{'source':_0x71df('0x43'),'to':_0x5064f8[_0x71df('0x19')],'body':_0x5064f8[_0x71df('0x12')],'from':_0x2a8997[_0x71df('0x2e')]||_0x2a8997['phone']}]};var _0x2b52e5=util[_0x71df('0x44')]('%s:%s',_0x2a8997[_0x71df('0x27')],_0x2a8997[_0x71df('0x28')]);var _0x24170b={'method':_0x71df('0x33'),'uri':'https://rest.clicksend.com/v3/sms/send','body':_0x54c13b,'headers':{'Authorization':util[_0x71df('0x44')](_0x71df('0x45'),new Buffer(_0x2b52e5)[_0x71df('0x46')](_0x71df('0x47')))},'json':!![]};rp(_0x24170b)['then'](function(_0x21b19e){if(_0x21b19e[_0x71df('0x48')]>=0xc8&&_0x21b19e[_0x71df('0x48')]<=0xce){if(_0x21b19e[_0x71df('0x49')]&&_0x21b19e[_0x71df('0x49')][_0x71df('0x4a')]>0x0){var _0x1fb201=_[_0x71df('0x4b')](_0x21b19e[_0x71df('0x49')][_0x71df('0x4c')]);var _0x4944a7={'messageId':_0x1fb201[_0x71df('0x4d')]};if(!_0x2a8997[_0x71df('0x3c')]){_0x4944a7[_0x71df('0x3d')]=!![];}return _0x1175a3[_0x71df('0x11')](_0x4944a7);}else{logger[_0x71df('0x1f')](_0x71df('0x20'),_0x71df('0x21'),'clickSend',util[_0x71df('0x23')](_0x21b19e,{'showHidden':![],'depth':null}));}}else{logger[_0x71df('0x1f')]('SmsMessage,\x20%s,\x20%s,\x20%s',_0x71df('0x21'),_0x71df('0x4e'),util[_0x71df('0x23')](_0x21b19e,{'showHidden':![],'depth':null}));}})[_0x71df('0x13')](function(_0x39146f){logger[_0x71df('0x1f')]('SmsMessage,\x20%s,\x20%s,\x20%s',_0x71df('0x21'),_0x71df('0x4e'),util[_0x71df('0x23')](_0x39146f,{'showHidden':![],'depth':null}));});}else{logger[_0x71df('0x1f')]('SmsMessage,\x20%s,\x20%s,\x20%s',_0x71df('0x21'),_0x71df('0x4e'),'Missing\x20parameters:\x20username,\x20password');}break;case _0x71df('0x4f'):if(_0x2a8997[_0x71df('0x50')]&&_0x2a8997[_0x71df('0x18')]){var _0x54c13b={'dst':_0x5064f8[_0x71df('0x19')],'text':_0x5064f8['body'],'src':_0x2a8997[_0x71df('0x2e')]||_0x2a8997[_0x71df('0x19')],'url':_0x2a8997[_0x71df('0x1b')]+_0x71df('0x1c')+_0x5064f8['id']+_0x71df('0x1d')+_0x2a8997[_0x71df('0x51')]};var _0x2b52e5=util['format'](_0x71df('0x52'),_0x2a8997[_0x71df('0x50')],_0x2a8997[_0x71df('0x18')]);var _0x24170b={'method':_0x71df('0x33'),'uri':util[_0x71df('0x44')]('https://api.plivo.com/v1/Account/%s/Message/',_0x2a8997[_0x71df('0x50')]),'body':_0x54c13b,'headers':{'Authorization':util['format'](_0x71df('0x45'),new Buffer(_0x2b52e5)[_0x71df('0x46')](_0x71df('0x47')))},'json':!![]};rp(_0x24170b)[_0x71df('0x10')](function(_0x1e35da){var _0x1004ca={'messageId':_0x1e35da[_0x71df('0x53')][0x0]};return _0x1175a3[_0x71df('0x11')](_0x1004ca);})[_0x71df('0x13')](function(_0x107a86){logger[_0x71df('0x1f')](_0x71df('0x20'),_0x71df('0x21'),'plivo',util[_0x71df('0x23')](_0x107a86,{'showHidden':![],'depth':null}));});}else{logger[_0x71df('0x1f')](_0x71df('0x20'),_0x71df('0x21'),_0x71df('0x4f'),'Missing\x20parameters:\x20authId,\x20authToken');}break;}}})[_0x71df('0x54')](function(){_0x238d4a();});}else{_0x238d4a();}}},'charset':_0x71df('0x55'),'collate':'utf8mb4_unicode_ci'});};