e55dc7d4444764a40a9afe4fcdf7ebba360321a3
[motion2.git] / server / api / cmHopper / cmHopper.rpc.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 _0x9095=['error','message','info','GetCmHopper','CmHopper','findAll','options','raw','where','attributes','limit','include','map','model','catch','ShowCmHopper','find','CreateCmHopper','body','update','UpdateCmHopper','DestroyCmHopper','GetContactDialer','CmContact','describe','sequelize','transaction','LOCK','UPDATE','Contact','User','name','voicePause','includes','UserId','push','MoveContactManagedDialer','destroy','CountReScheduled','COUNT','literal','`scheduledat`\x20>=\x20','format','\x22%s\x22','YYYY-MM-DD\x20HH:mm:ss','\x20,\x201,\x20null','contactsRescheduled','GetContactRecallMe','length','keys','lodash','util','bluebird','randomstring','../../mysqldb','../../config/utils','../../config/logger','../../config/environment','jayson/promise','client','http','then','CmHopper,\x20%s,\x20%s','request\x20sent','debug','CmHopper,\x20%s,\x20%s,\x20%s','stringify'];(function(_0xdab10,_0x173024){var _0x4644af=function(_0x20be8b){while(--_0x20be8b){_0xdab10['push'](_0xdab10['shift']());}};_0x4644af(++_0x173024);}(_0x9095,0x181));var _0x5909=function(_0x1344d6,_0x2181e6){_0x1344d6=_0x1344d6-0x0;var _0xf45b24=_0x9095[_0x1344d6];return _0xf45b24;};'use strict';var _=require(_0x5909('0x0'));var util=require(_0x5909('0x1'));var moment=require('moment');var BPromise=require(_0x5909('0x2'));var rs=require(_0x5909('0x3'));var fs=require('fs');var Redis=require('ioredis');var db=require(_0x5909('0x4'))['db'];var utils=require(_0x5909('0x5'));var logger=require(_0x5909('0x6'))('rpc');var config=require(_0x5909('0x7'));var jayson=require(_0x5909('0x8'));var client=jayson[_0x5909('0x9')][_0x5909('0xa')]({'port':0x232a});function respondWithRpcPromise(_0x1d6482,_0xa457f5,_0x12512c){return new BPromise(function(_0x5eedde,_0x480895){return client['request'](_0x1d6482,_0x12512c)[_0x5909('0xb')](function(_0x1143ce){logger['info'](_0x5909('0xc'),_0xa457f5,_0x5909('0xd'));logger[_0x5909('0xe')](_0x5909('0xf'),_0xa457f5,'request\x20sent',JSON[_0x5909('0x10')](_0x1143ce));if(_0x1143ce[_0x5909('0x11')]){if(_0x1143ce[_0x5909('0x11')]['code']===0x1f4){logger[_0x5909('0x11')](_0x5909('0xc'),_0xa457f5,_0x1143ce[_0x5909('0x11')]['message']);return _0x480895(_0x1143ce['error'][_0x5909('0x12')]);}logger['error'](_0x5909('0xc'),_0xa457f5,_0x1143ce['error'][_0x5909('0x12')]);return _0x5eedde(_0x1143ce['error'][_0x5909('0x12')]);}else{logger[_0x5909('0x13')](_0x5909('0xc'),_0xa457f5,'request\x20sent');_0x5eedde(_0x1143ce['result'][_0x5909('0x12')]);}})['catch'](function(_0x465f68){logger['error'](_0x5909('0xc'),_0xa457f5,_0x465f68);_0x480895(_0x465f68);});});}exports[_0x5909('0x14')]=function(_0x5f4b15){var _0x7b7fda=this;return new Promise(function(_0x4ca60c,_0x98268d){return db[_0x5909('0x15')][_0x5909('0x16')]({'raw':_0x5f4b15[_0x5909('0x17')]?_0x5f4b15[_0x5909('0x17')][_0x5909('0x18')]===undefined?!![]:![]:!![],'where':_0x5f4b15[_0x5909('0x17')]?_0x5f4b15[_0x5909('0x17')][_0x5909('0x19')]||null:null,'attributes':_0x5f4b15[_0x5909('0x17')]?_0x5f4b15[_0x5909('0x17')][_0x5909('0x1a')]||null:null,'limit':_0x5f4b15[_0x5909('0x17')]?_0x5f4b15[_0x5909('0x17')][_0x5909('0x1b')]||null:null,'include':_0x5f4b15[_0x5909('0x17')]?_0x5f4b15[_0x5909('0x17')][_0x5909('0x1c')]?_[_0x5909('0x1d')](_0x5f4b15[_0x5909('0x17')][_0x5909('0x1c')],function(_0x5a8b88){return{'model':db[_0x5a8b88[_0x5909('0x1e')]],'as':_0x5a8b88['as'],'attributes':_0x5a8b88[_0x5909('0x1a')],'include':_0x5a8b88[_0x5909('0x1c')]?_[_0x5909('0x1d')](_0x5a8b88[_0x5909('0x1c')],function(_0x58b189){return{'model':db[_0x58b189['model']],'as':_0x58b189['as'],'attributes':_0x58b189['attributes'],'include':_0x58b189[_0x5909('0x1c')]?_[_0x5909('0x1d')](_0x58b189[_0x5909('0x1c')],function(_0x11675d){return{'model':db[_0x11675d[_0x5909('0x1e')]],'as':_0x11675d['as'],'attributes':_0x11675d['attributes']};}):[]};}):[]};}):[]:[]})[_0x5909('0xb')](function(_0x391a31){logger['info']('GetCmHopper',_0x5f4b15);logger['debug']('GetCmHopper',_0x5f4b15,JSON['stringify'](_0x391a31));_0x4ca60c(_0x391a31);})[_0x5909('0x1f')](function(_0x5b5e9c){logger[_0x5909('0x11')](_0x5909('0x14'),_0x5b5e9c[_0x5909('0x12')],_0x5f4b15);_0x98268d(_0x7b7fda['error'](0x1f4,_0x5b5e9c[_0x5909('0x12')]));});});};exports[_0x5909('0x20')]=function(_0x2a9b8e){var _0x2883c5=this;return new Promise(function(_0x48fde9,_0x20b9b6){return db[_0x5909('0x15')][_0x5909('0x21')]({'raw':_0x2a9b8e['options']?_0x2a9b8e[_0x5909('0x17')]['raw']===undefined?!![]:![]:!![],'where':_0x2a9b8e['options']?_0x2a9b8e[_0x5909('0x17')]['where']||null:null,'attributes':_0x2a9b8e[_0x5909('0x17')]?_0x2a9b8e[_0x5909('0x17')]['attributes']||null:null,'include':_0x2a9b8e['options']?_0x2a9b8e[_0x5909('0x17')]['include']?_[_0x5909('0x1d')](_0x2a9b8e[_0x5909('0x17')]['include'],function(_0x173746){return{'model':db[_0x173746[_0x5909('0x1e')]],'as':_0x173746['as'],'attributes':_0x173746[_0x5909('0x1a')],'include':_0x173746['include']?_[_0x5909('0x1d')](_0x173746[_0x5909('0x1c')],function(_0x271117){return{'model':db[_0x271117[_0x5909('0x1e')]],'as':_0x271117['as'],'attributes':_0x271117[_0x5909('0x1a')],'include':_0x271117[_0x5909('0x1c')]?_[_0x5909('0x1d')](_0x271117['include'],function(_0x86f0d7){return{'model':db[_0x86f0d7['model']],'as':_0x86f0d7['as'],'attributes':_0x86f0d7[_0x5909('0x1a')]};}):[]};}):[]};}):[]:[]})[_0x5909('0xb')](function(_0x31efb3){logger[_0x5909('0x13')](_0x5909('0x20'),_0x2a9b8e);logger[_0x5909('0xe')]('ShowCmHopper',_0x2a9b8e,JSON[_0x5909('0x10')](_0x31efb3));_0x48fde9(_0x31efb3);})[_0x5909('0x1f')](function(_0x4ee822){logger[_0x5909('0x11')]('ShowCmHopper',_0x4ee822[_0x5909('0x12')],_0x2a9b8e);_0x20b9b6(_0x2883c5[_0x5909('0x11')](0x1f4,_0x4ee822['message']));});});};exports[_0x5909('0x22')]=function(_0x511b36){var _0x2486a2=this;return new Promise(function(_0x34b7e0,_0x1c8c0d){return db[_0x5909('0x15')]['create'](_0x511b36[_0x5909('0x23')],{'raw':_0x511b36['options']?_0x511b36[_0x5909('0x17')]['raw']===undefined?!![]:![]:!![]})[_0x5909('0xb')](function(_0x31edce){logger[_0x5909('0x13')](_0x5909('0x22'),_0x511b36);logger[_0x5909('0xe')](_0x5909('0x22'),_0x511b36,JSON[_0x5909('0x10')](_0x31edce));_0x34b7e0(_0x31edce);})[_0x5909('0x1f')](function(_0x2d751e){logger['error'](_0x5909('0x22'),_0x2d751e['message'],_0x511b36);_0x1c8c0d(_0x2486a2[_0x5909('0x11')](0x1f4,_0x2d751e[_0x5909('0x12')]));});});};exports['UpdateCmHopper']=function(_0x43afc1){var _0x3f7bc0=this;return new Promise(function(_0x2c5012,_0x4f0f05){return db[_0x5909('0x15')][_0x5909('0x24')](_0x43afc1['body'],{'raw':_0x43afc1[_0x5909('0x17')]?_0x43afc1[_0x5909('0x17')]['raw']===undefined?!![]:![]:!![],'individualHooks':!![],'where':_0x43afc1[_0x5909('0x17')]?_0x43afc1[_0x5909('0x17')][_0x5909('0x19')]||null:null,'attributes':_0x43afc1['options']?_0x43afc1[_0x5909('0x17')][_0x5909('0x1a')]||null:null,'limit':_0x43afc1[_0x5909('0x17')]?_0x43afc1[_0x5909('0x17')]['limit']||null:null})[_0x5909('0xb')](function(_0x237a47){logger[_0x5909('0x13')](_0x5909('0x25'),_0x43afc1);logger['debug']('UpdateCmHopper',_0x43afc1,JSON[_0x5909('0x10')](_0x237a47));_0x2c5012(_0x237a47);})[_0x5909('0x1f')](function(_0x5b4760){logger[_0x5909('0x11')]('UpdateCmHopper',_0x5b4760[_0x5909('0x12')],_0x43afc1);_0x4f0f05(_0x3f7bc0[_0x5909('0x11')](0x1f4,_0x5b4760[_0x5909('0x12')]));});});};exports[_0x5909('0x26')]=function(_0x25ebff){var _0x327dd0=this;return new Promise(function(_0x5bc346,_0x594b81){return db[_0x5909('0x15')]['destroy']({'where':_0x25ebff[_0x5909('0x17')]?_0x25ebff[_0x5909('0x17')]['where']||null:null})['then'](function(_0x498cf1){logger[_0x5909('0x13')](_0x5909('0x26'),_0x25ebff);logger[_0x5909('0xe')]('DestroyCmHopper',_0x25ebff,JSON[_0x5909('0x10')](_0x498cf1));_0x5bc346(_0x498cf1);})[_0x5909('0x1f')](function(_0x3c2fae){logger['error'](_0x5909('0x26'),_0x3c2fae['message'],_0x25ebff);_0x594b81(_0x327dd0[_0x5909('0x11')](0x1f4,_0x3c2fae[_0x5909('0x12')]));});});};exports[_0x5909('0x27')]=function(_0x46906c){var _0x563c7e=this;return new Promise(function(_0x259c1d,_0x390fb3){return db[_0x5909('0x28')][_0x5909('0x29')]()[_0x5909('0xb')](function(_0x5d9d17){return db[_0x5909('0x2a')][_0x5909('0x2b')](function(_0x35a4d0){return db['CmHopper']['findAll']({'where':_0x46906c['options']?_0x46906c[_0x5909('0x17')][_0x5909('0x19')]||null:null,'attributes':_0x46906c['options']?_0x46906c[_0x5909('0x17')][_0x5909('0x1a')]||null:null,'limit':_0x46906c[_0x5909('0x17')]?_0x46906c['options'][_0x5909('0x1b')]||null:null,'order':_0x46906c[_0x5909('0x17')]?_0x46906c[_0x5909('0x17')]['order']||null:null,'transaction':_0x35a4d0,'lock':_0x35a4d0[_0x5909('0x2c')][_0x5909('0x2d')],'include':[{'model':db['CmContact'],'as':_0x5909('0x2e'),'attributes':_['keys'](_0x5d9d17)},{'model':db[_0x5909('0x2f')],'as':'Agent','attributes':['id',_0x5909('0x30'),_0x5909('0x31')]}]})[_0x5909('0xb')](function(_0x46c3e6){var _0x4c8585=[];var _0x2bc96b=[];for(var _0x1d6597=0x0;_0x1d6597<_0x46c3e6['length'];_0x1d6597+=0x1){if(!_0x2bc96b[_0x5909('0x32')](_0x46c3e6[_0x1d6597][_0x5909('0x33')])){_0x2bc96b[_0x5909('0x34')](_0x46c3e6[_0x1d6597]['UserId']);_0x4c8585['push'](_0x46c3e6[_0x1d6597][_0x5909('0x24')](_0x46906c[_0x5909('0x23')],{'transaction':_0x35a4d0}));}}return BPromise['all'](_0x4c8585);});})['then'](function(_0x4b666a){logger[_0x5909('0x13')](_0x5909('0x27'),_0x46906c);logger[_0x5909('0xe')]('GetContactDialer',_0x46906c,JSON[_0x5909('0x10')](_0x4b666a));_0x259c1d(_0x4b666a);})['catch'](function(_0x5906a7){logger['error']('GetContactDialer',_0x5906a7[_0x5909('0x12')],_0x46906c);_0x390fb3(_0x563c7e[_0x5909('0x11')](0x1f4,_0x5906a7['message']));});});});};exports[_0x5909('0x35')]=function(_0x189a6c){var _0x1c33f1=this;return new Promise(function(_0x51c899,_0x374736){return db[_0x5909('0x2a')]['transaction'](function(_0xbe88f4){return db['CmHopperFinal']['create'](_0x189a6c[_0x5909('0x23')],{'raw':_0x189a6c[_0x5909('0x17')]?_0x189a6c[_0x5909('0x17')][_0x5909('0x18')]===undefined?!![]:![]:!![],'transaction':_0xbe88f4})['then'](function(){return db['CmHopper'][_0x5909('0x36')]({'where':_0x189a6c[_0x5909('0x17')]?_0x189a6c[_0x5909('0x17')][_0x5909('0x19')]||null:null,'transaction':_0xbe88f4});});})[_0x5909('0xb')](function(_0x2ffc1c){logger['info'](_0x5909('0x35'),_0x189a6c);logger[_0x5909('0xe')](_0x5909('0x35'),_0x189a6c,JSON[_0x5909('0x10')](_0x2ffc1c));_0x51c899(_0x2ffc1c);})[_0x5909('0x1f')](function(_0x55108b){logger['error'](_0x5909('0x35'),_0x55108b[_0x5909('0x12')],_0x189a6c);_0x374736(_0x1c33f1[_0x5909('0x11')](0x1f4,_0x55108b[_0x5909('0x12')]));});});};exports[_0x5909('0x37')]=function(_0xe258df){var _0x35781b=this;return new Promise(function(_0x51ed5a,_0x2d5089){return db[_0x5909('0x15')][_0x5909('0x16')]({'raw':_0xe258df[_0x5909('0x17')]?_0xe258df[_0x5909('0x17')][_0x5909('0x18')]===undefined?!![]:![]:!![],'where':_0xe258df['options']?_0xe258df['options']['where']||null:null,'attributes':[[db[_0x5909('0x2a')]['fn'](_0x5909('0x38'),db[_0x5909('0x2a')]['fn']('IF',db[_0x5909('0x2a')][_0x5909('0x39')](_0x5909('0x3a')+util[_0x5909('0x3b')](_0x5909('0x3c'),moment()['format'](_0x5909('0x3d')))+_0x5909('0x3e')))),_0x5909('0x3f')]]})[_0x5909('0xb')](function(_0x1361ff){logger['info']('CountReScheduled',_0xe258df);logger[_0x5909('0xe')](_0x5909('0x37'),_0xe258df,JSON[_0x5909('0x10')](_0x1361ff));_0x51ed5a(_0x1361ff);})['catch'](function(_0x11c3bd){logger[_0x5909('0x11')](_0x5909('0x37'),_0x11c3bd['message'],_0xe258df);_0x2d5089(_0x35781b[_0x5909('0x11')](0x1f4,_0x11c3bd['message']));});});};exports[_0x5909('0x40')]=function(_0x1baaf7){var _0x2c569f=this;return new Promise(function(_0x1d8d94,_0x5f03d9){return db[_0x5909('0x28')]['describe']()[_0x5909('0xb')](function(_0x3708d1){return db[_0x5909('0x2a')]['transaction'](function(_0x370ec5){return db[_0x5909('0x15')][_0x5909('0x16')]({'where':_0x1baaf7[_0x5909('0x17')]?_0x1baaf7[_0x5909('0x17')][_0x5909('0x19')]||null:null,'attributes':_0x1baaf7[_0x5909('0x17')]?_0x1baaf7['options'][_0x5909('0x1a')]||null:null,'limit':0x1,'order':_0x1baaf7['options']?_0x1baaf7[_0x5909('0x17')]['order']||null:null,'group':db[_0x5909('0x15')][_0x5909('0x33')],'transaction':_0x370ec5,'lock':_0x370ec5[_0x5909('0x2c')][_0x5909('0x2d')],'include':_0x1baaf7[_0x5909('0x17')]?_0x1baaf7[_0x5909('0x17')][_0x5909('0x1c')]?_[_0x5909('0x1d')](_0x1baaf7['options'][_0x5909('0x1c')],function(_0xe84c63){return{'model':db[_0xe84c63['model']],'as':_0xe84c63['as'],'attributes':_0xe84c63[_0x5909('0x1a')],'include':_0xe84c63[_0x5909('0x1c')]?_[_0x5909('0x1d')](_0xe84c63[_0x5909('0x1c')],function(_0x334ec8){return{'model':db[_0x334ec8[_0x5909('0x1e')]],'as':_0x334ec8['as'],'attributes':_0x334ec8[_0x5909('0x1a')],'include':_0x334ec8['include']?_[_0x5909('0x1d')](_0x334ec8[_0x5909('0x1c')],function(_0x499ab7){return{'model':db[_0x499ab7[_0x5909('0x1e')]],'as':_0x499ab7['as'],'attributes':_0x499ab7['attributes']};}):[]};}):[]};}):[]:[]})[_0x5909('0xb')](function(_0x568f63){var _0x3e5419=[];for(var _0x51a879=0x0;_0x51a879<_0x568f63[_0x5909('0x41')];_0x51a879+=0x1){_0x3e5419[_0x5909('0x34')](_0x568f63[_0x51a879][_0x5909('0x24')](_0x1baaf7[_0x5909('0x23')],{'transaction':_0x370ec5}));}return BPromise['all'](_0x3e5419);});})[_0x5909('0xb')](function(_0x267f2e){logger['info'](_0x5909('0x40'),_0x1baaf7);logger[_0x5909('0xe')]('GetContactRecallMe',_0x1baaf7,JSON[_0x5909('0x10')](_0x267f2e));_0x1d8d94(_0x267f2e);})['catch'](function(_0x578aea){logger['error'](_0x5909('0x40'),_0x578aea[_0x5909('0x12')],_0x1baaf7);_0x5f03d9(_0x2c569f[_0x5909('0x11')](0x1f4,_0x578aea[_0x5909('0x12')]));});});});};exports['GetCmHopperPreview']=function(_0x11d276){var _0x47477a=this;return new Promise(function(_0x27a2f6,_0x567fdb){db[_0x5909('0x28')][_0x5909('0x29')]()['then'](function(_0x41e6fb){return db[_0x5909('0x15')]['findAll']({'raw':_0x11d276[_0x5909('0x17')]?_0x11d276[_0x5909('0x17')]['raw']===undefined?!![]:![]:!![],'where':_0x11d276[_0x5909('0x17')]?_0x11d276[_0x5909('0x17')][_0x5909('0x19')]||null:null,'attributes':_0x11d276['options']?_0x11d276['options']['attributes']||null:null,'limit':_0x11d276[_0x5909('0x17')]?_0x11d276[_0x5909('0x17')]['limit']||null:null,'include':_0x11d276['options']?_0x11d276[_0x5909('0x17')][_0x5909('0x1c')]?_[_0x5909('0x1d')](_0x11d276[_0x5909('0x17')][_0x5909('0x1c')],function(_0x31ad98){return{'model':db[_0x31ad98[_0x5909('0x1e')]],'as':_0x31ad98['as'],'attributes':_0x31ad98['as']==='Contact'?_[_0x5909('0x42')](_0x41e6fb):_0x31ad98[_0x5909('0x1a')],'include':_0x31ad98[_0x5909('0x1c')]?_[_0x5909('0x1d')](_0x31ad98[_0x5909('0x1c')],function(_0x3272ad){return{'model':db[_0x3272ad[_0x5909('0x1e')]],'as':_0x3272ad['as'],'attributes':_0x3272ad['attributes'],'include':_0x3272ad['include']?_[_0x5909('0x1d')](_0x3272ad[_0x5909('0x1c')],function(_0x4e6242){return{'model':db[_0x4e6242[_0x5909('0x1e')]],'as':_0x4e6242['as'],'attributes':_0x4e6242[_0x5909('0x1a')]};}):[]};}):[]};}):[]:[]})['then'](function(_0x41922e){logger[_0x5909('0x13')](_0x5909('0x14'),_0x11d276);logger[_0x5909('0xe')](_0x5909('0x14'),_0x11d276,JSON[_0x5909('0x10')](_0x41922e));_0x27a2f6(_0x41922e);})['catch'](function(_0x1f7272){logger[_0x5909('0x11')](_0x5909('0x14'),_0x1f7272[_0x5909('0x12')],_0x11d276);_0x567fdb(_0x47477a[_0x5909('0x11')](0x1f4,_0x1f7272['message']));});});});};