47bbca75a65f5ffa51fde3c7db7121daa8a86cf3
[motion2.git] / server / api / intDeskAccount / intDeskAccount.controller.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 _0x60e5=['status','end','undefined','limit','offset','count','set','json','apply','reject','save','then','error','stack','name','map','DeskAccount','rawAttributes','key','keys','filters','intersection','model','query','attributes','fields','length','order','sort','pick','filter','merge','where','type','findAll','catch','show','includeAll','include','options','create','body','destroy','find','params','getConfigurations','findOne','DeskConfiguration','hasOwnProperty','nolimit','AccountId','Subjects','Descriptions','getDescriptions','DeskField','getFields','priority','authType','username','password','consumerKey','consumerSecret','token','tokenSecret','remoteUri','slice','substring','lastIndexOf','format','%s/custom_fields','application/json','basic','headers','Authorization','Basic\x20%s','%s:%s','toString','base64','concat','_embedded','active','label','test','statusCode','getFields,\x20%s,\x20%s,\x20%s','Wrong\x20credentials','eml-format','rimraf','zip-dir','request-promise','moment','bluebird','mustache','util','path','sox','to-csv','fs-extra','lodash','squel','crypto','jsforce','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/environment','../../config/license/util','../../mysqldb','../../components/integrations/configuration','sendStatus'];(function(_0x2a13f3,_0x165bd7){var _0x3d251a=function(_0x53ddc7){while(--_0x53ddc7){_0x2a13f3['push'](_0x2a13f3['shift']());}};_0x3d251a(++_0x165bd7);}(_0x60e5,0x1b5));var _0x560e=function(_0x562f3b,_0x4c65ae){_0x562f3b=_0x562f3b-0x0;var _0x1edd55=_0x60e5[_0x562f3b];return _0x1edd55;};'use strict';var emlformat=require(_0x560e('0x0'));var rimraf=require(_0x560e('0x1'));var zipdir=require(_0x560e('0x2'));var jsonpatch=require('fast-json-patch');var rp=require(_0x560e('0x3'));var moment=require(_0x560e('0x4'));var BPromise=require(_0x560e('0x5'));var Mustache=require(_0x560e('0x6'));var util=require(_0x560e('0x7'));var path=require(_0x560e('0x8'));var sox=require(_0x560e('0x9'));var csv=require(_0x560e('0xa'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0x560e('0xb'));var _=require(_0x560e('0xc'));var squel=require(_0x560e('0xd'));var crypto=require(_0x560e('0xe'));var jsforce=require(_0x560e('0xf'));var deskjs=require(_0x560e('0x10'));var toCsv=require('to-csv');var querystring=require(_0x560e('0x11'));var Papa=require(_0x560e('0x12'));var Redis=require(_0x560e('0x13'));var authService=require(_0x560e('0x14'));var qs=require(_0x560e('0x15'));var as=require(_0x560e('0x16'));var hardwareService=require(_0x560e('0x17'));var logger=require(_0x560e('0x18'))(_0x560e('0x19'));var utils=require('../../config/utils');var config=require(_0x560e('0x1a'));var licenseUtil=require(_0x560e('0x1b'));var db=require(_0x560e('0x1c'))['db'];var integrations=require(_0x560e('0x1d'));function respondWithStatusCode(_0x56bb59,_0x2331c4){_0x2331c4=_0x2331c4||0xcc;return function(_0x37635d){if(_0x37635d){return _0x56bb59[_0x560e('0x1e')](_0x2331c4);}return _0x56bb59[_0x560e('0x1f')](_0x2331c4)[_0x560e('0x20')]();};}function respondWithResult(_0x45858b,_0x4ed1c1){_0x4ed1c1=_0x4ed1c1||0xc8;return function(_0x57f3b9){if(_0x57f3b9){return _0x45858b['status'](_0x4ed1c1)['json'](_0x57f3b9);}};}function respondWithFilteredResult(_0x16e226,_0x925a3b){return function(_0x35bb52){if(_0x35bb52){var _0x20b156=typeof _0x925a3b['offset']===_0x560e('0x21')&&typeof _0x925a3b[_0x560e('0x22')]===_0x560e('0x21');var _0x31d314=_0x35bb52['count'];var _0x23cc46=_0x20b156?0x0:_0x925a3b[_0x560e('0x23')];var _0x31a1a0=_0x20b156?_0x35bb52[_0x560e('0x24')]:_0x925a3b[_0x560e('0x23')]+_0x925a3b[_0x560e('0x22')];var _0x51557e;if(_0x31a1a0>=_0x31d314){_0x31a1a0=_0x31d314;_0x51557e=0xc8;}else{_0x51557e=0xce;}_0x16e226['status'](_0x51557e);return _0x16e226[_0x560e('0x25')]('Content-Range',_0x23cc46+'-'+_0x31a1a0+'/'+_0x31d314)[_0x560e('0x26')](_0x35bb52);}return null;};}function patchUpdates(_0x2a5c2d){return function(_0x359d2b){try{jsonpatch[_0x560e('0x27')](_0x359d2b,_0x2a5c2d,!![]);}catch(_0x42b480){return BPromise[_0x560e('0x28')](_0x42b480);}return _0x359d2b[_0x560e('0x29')]();};}function saveUpdates(_0x382316,_0x546865){return function(_0x3e27e7){if(_0x3e27e7){return _0x3e27e7['update'](_0x382316)[_0x560e('0x2a')](function(_0x3f3613){return _0x3f3613;});}return null;};}function removeEntity(_0x396a0a,_0x8510b7){return function(_0x5d37c2){if(_0x5d37c2){return _0x5d37c2['destroy']()['then'](function(){_0x396a0a[_0x560e('0x1f')](0xcc)[_0x560e('0x20')]();});}};}function handleEntityNotFound(_0x4867d1,_0x171e48){return function(_0x128047){if(!_0x128047){_0x4867d1[_0x560e('0x1e')](0x194);}return _0x128047;};}function handleError(_0x5eb1c8,_0x4ca49f){_0x4ca49f=_0x4ca49f||0x1f4;return function(_0x26d6f0){logger[_0x560e('0x2b')](_0x26d6f0[_0x560e('0x2c')]);if(_0x26d6f0[_0x560e('0x2d')]){delete _0x26d6f0[_0x560e('0x2d')];}_0x5eb1c8[_0x560e('0x1f')](_0x4ca49f)['send'](_0x26d6f0);};}exports['index']=function(_0x19f68a,_0xbdeecd){var _0x5debe0={},_0x315f19={},_0x279f79={'count':0x0,'rows':[]};var _0x2582d0=_[_0x560e('0x2e')](db[_0x560e('0x2f')][_0x560e('0x30')],function(_0x48efd7){return{'name':_0x48efd7['fieldName'],'type':_0x48efd7['type'][_0x560e('0x31')]};});_0x315f19['model']=_[_0x560e('0x2e')](_0x2582d0,_0x560e('0x2d'));_0x315f19['query']=_[_0x560e('0x32')](_0x19f68a['query']);_0x315f19[_0x560e('0x33')]=_[_0x560e('0x34')](_0x315f19[_0x560e('0x35')],_0x315f19[_0x560e('0x36')]);_0x5debe0[_0x560e('0x37')]=_[_0x560e('0x34')](_0x315f19['model'],qs[_0x560e('0x38')](_0x19f68a[_0x560e('0x36')][_0x560e('0x38')]));_0x5debe0['attributes']=_0x5debe0[_0x560e('0x37')][_0x560e('0x39')]?_0x5debe0[_0x560e('0x37')]:_0x315f19['model'];if(!_0x19f68a[_0x560e('0x36')]['hasOwnProperty']('nolimit')){_0x5debe0[_0x560e('0x22')]=qs[_0x560e('0x22')](_0x19f68a['query'][_0x560e('0x22')]);_0x5debe0['offset']=qs[_0x560e('0x23')](_0x19f68a['query'][_0x560e('0x23')]);}_0x5debe0[_0x560e('0x3a')]=qs[_0x560e('0x3b')](_0x19f68a['query'][_0x560e('0x3b')]);_0x5debe0['where']=qs[_0x560e('0x33')](_[_0x560e('0x3c')](_0x19f68a[_0x560e('0x36')],_0x315f19[_0x560e('0x33')]),_0x2582d0);if(_0x19f68a['query'][_0x560e('0x3d')]){_0x5debe0['where']=_[_0x560e('0x3e')](_0x5debe0[_0x560e('0x3f')],{'$or':_['map'](_0x2582d0,function(_0x25fe7b){if(_0x25fe7b[_0x560e('0x40')]!=='VIRTUAL'){var _0x4ca0d7={};_0x4ca0d7[_0x25fe7b[_0x560e('0x2d')]]={'$like':'%'+_0x19f68a[_0x560e('0x36')][_0x560e('0x3d')]+'%'};return _0x4ca0d7;}})});}_0x5debe0=_[_0x560e('0x3e')]({},_0x5debe0,_0x19f68a['options']);var _0x484af5={'where':_0x5debe0[_0x560e('0x3f')]};return db[_0x560e('0x2f')][_0x560e('0x24')](_0x484af5)[_0x560e('0x2a')](function(_0x2a952a){_0x279f79[_0x560e('0x24')]=_0x2a952a;if(_0x19f68a['query']['includeAll']){_0x5debe0['include']=[{'all':!![]}];}return db[_0x560e('0x2f')][_0x560e('0x41')](_0x5debe0);})[_0x560e('0x2a')](function(_0x2af18a){_0x279f79['rows']=_0x2af18a;return _0x279f79;})[_0x560e('0x2a')](respondWithFilteredResult(_0xbdeecd,_0x5debe0))[_0x560e('0x42')](handleError(_0xbdeecd,null));};exports[_0x560e('0x43')]=function(_0x3e7a1e,_0x3fcb6a){var _0x386246={'raw':![],'where':{'id':_0x3e7a1e['params']['id']}},_0x350745={};_0x350745[_0x560e('0x35')]=_[_0x560e('0x32')](db['DeskAccount'][_0x560e('0x30')]);_0x350745[_0x560e('0x36')]=_[_0x560e('0x32')](_0x3e7a1e[_0x560e('0x36')]);_0x350745[_0x560e('0x33')]=_[_0x560e('0x34')](_0x350745[_0x560e('0x35')],_0x350745['query']);_0x386246[_0x560e('0x37')]=_['intersection'](_0x350745[_0x560e('0x35')],qs['fields'](_0x3e7a1e[_0x560e('0x36')]['fields']));_0x386246['attributes']=_0x386246[_0x560e('0x37')][_0x560e('0x39')]?_0x386246[_0x560e('0x37')]:_0x350745[_0x560e('0x35')];if(_0x3e7a1e[_0x560e('0x36')][_0x560e('0x44')]){_0x386246[_0x560e('0x45')]=[{'all':!![]}];}_0x386246=_[_0x560e('0x3e')]({},_0x386246,_0x3e7a1e[_0x560e('0x46')]);return db[_0x560e('0x2f')]['find'](_0x386246)['then'](handleEntityNotFound(_0x3fcb6a,null))[_0x560e('0x2a')](respondWithResult(_0x3fcb6a,null))['catch'](handleError(_0x3fcb6a,null));};exports[_0x560e('0x47')]=function(_0x51af97,_0x2451b8){return db[_0x560e('0x2f')][_0x560e('0x47')](_0x51af97[_0x560e('0x48')],{})[_0x560e('0x2a')](respondWithResult(_0x2451b8,0xc9))['catch'](handleError(_0x2451b8,null));};exports['update']=function(_0x5ac8a4,_0x338497){if(_0x5ac8a4[_0x560e('0x48')]['id']){delete _0x5ac8a4['body']['id'];}return db[_0x560e('0x2f')]['find']({'where':{'id':_0x5ac8a4['params']['id']}})[_0x560e('0x2a')](handleEntityNotFound(_0x338497,null))[_0x560e('0x2a')](saveUpdates(_0x5ac8a4[_0x560e('0x48')],null))[_0x560e('0x2a')](respondWithResult(_0x338497,null))[_0x560e('0x42')](handleError(_0x338497,null));};exports[_0x560e('0x49')]=function(_0x5555e4,_0x1a1f9b){return db['DeskAccount'][_0x560e('0x4a')]({'where':{'id':_0x5555e4[_0x560e('0x4b')]['id']}})[_0x560e('0x2a')](handleEntityNotFound(_0x1a1f9b,null))[_0x560e('0x2a')](removeEntity(_0x1a1f9b,null))[_0x560e('0x42')](handleError(_0x1a1f9b,null));};exports[_0x560e('0x4c')]=function(_0x361c0b,_0x2ceedd,_0x28f5ee){var _0xfecadf={};var _0x50baa6={};var _0xa67670;var _0x310e5d;return db[_0x560e('0x2f')][_0x560e('0x4d')]({'where':{'id':_0x361c0b['params']['id']}})['then'](handleEntityNotFound(_0x2ceedd,null))[_0x560e('0x2a')](function(_0x4aa623){if(_0x4aa623){_0xa67670=_0x4aa623;_0x50baa6[_0x560e('0x35')]=_[_0x560e('0x32')](db[_0x560e('0x4e')]['rawAttributes']);_0x50baa6[_0x560e('0x36')]=_[_0x560e('0x32')](_0x361c0b[_0x560e('0x36')]);_0x50baa6[_0x560e('0x33')]=_[_0x560e('0x34')](_0x50baa6[_0x560e('0x35')],_0x50baa6[_0x560e('0x36')]);_0xfecadf[_0x560e('0x37')]=_['intersection'](_0x50baa6['model'],qs[_0x560e('0x38')](_0x361c0b[_0x560e('0x36')]['fields']));_0xfecadf[_0x560e('0x37')]=_0xfecadf[_0x560e('0x37')][_0x560e('0x39')]?_0xfecadf[_0x560e('0x37')]:_0x50baa6[_0x560e('0x35')];_0xfecadf[_0x560e('0x3a')]=qs[_0x560e('0x3b')](_0x361c0b['query']['sort']);_0xfecadf[_0x560e('0x3f')]=qs[_0x560e('0x33')](_[_0x560e('0x3c')](_0x361c0b['query'],_0x50baa6[_0x560e('0x33')]));if(_0x361c0b['query'][_0x560e('0x3d')]){_0xfecadf[_0x560e('0x3f')]=_[_0x560e('0x3e')](_0xfecadf['where'],{'$or':_['map'](_0xfecadf[_0x560e('0x37')],function(_0x18ebf7){var _0x481488={};_0x481488[_0x18ebf7]={'$like':'%'+_0x361c0b['query'][_0x560e('0x3d')]+'%'};return _0x481488;})});}_0xfecadf=_[_0x560e('0x3e')]({},_0xfecadf,_0x361c0b[_0x560e('0x46')]);return _0xa67670['getConfigurations'](_0xfecadf);}})[_0x560e('0x2a')](function(_0x43086f){if(_0x43086f){_0x310e5d=_0x43086f[_0x560e('0x39')];if(!_0x361c0b[_0x560e('0x36')][_0x560e('0x4f')](_0x560e('0x50'))){_0xfecadf[_0x560e('0x22')]=qs[_0x560e('0x22')](_0x361c0b['query']['limit']);_0xfecadf['offset']=qs['offset'](_0x361c0b['query']['offset']);}return _0xa67670['getConfigurations'](_0xfecadf);}})[_0x560e('0x2a')](function(_0x4871f8){if(_0x4871f8){return _0x4871f8?{'count':_0x310e5d,'rows':_0x4871f8}:null;}})[_0x560e('0x2a')](respondWithResult(_0x2ceedd,null))['catch'](handleError(_0x2ceedd,null));};exports['addConfiguration']=function(_0x4aae5a,_0x2548fa,_0x5dd176){if(_0x4aae5a[_0x560e('0x48')]['id']){delete _0x4aae5a[_0x560e('0x48')]['id'];}return db['DeskAccount'][_0x560e('0x4d')]({'where':{'id':_0x4aae5a[_0x560e('0x4b')]['id']}})[_0x560e('0x2a')](handleEntityNotFound(_0x2548fa,null))[_0x560e('0x2a')](function(_0x22baf9){if(_0x22baf9){_0x4aae5a[_0x560e('0x48')][_0x560e('0x51')]=_0x22baf9['id'];_0x4aae5a[_0x560e('0x48')][_0x560e('0x52')]=integrations['getSubjects'](_0x4aae5a[_0x560e('0x48')]['channel'],_0x4aae5a['body'][_0x560e('0x40')]);_0x4aae5a[_0x560e('0x48')][_0x560e('0x53')]=integrations[_0x560e('0x54')](_0x4aae5a[_0x560e('0x48')]['channel'],_0x4aae5a[_0x560e('0x48')]['type']);return db[_0x560e('0x4e')]['create'](_0x4aae5a[_0x560e('0x48')],{'include':[{'model':db[_0x560e('0x55')],'as':'Subjects'},{'model':db[_0x560e('0x55')],'as':_0x560e('0x53')}]});}return null;})[_0x560e('0x2a')](respondWithResult(_0x2548fa,null))[_0x560e('0x42')](handleError(_0x2548fa,null));};exports[_0x560e('0x56')]=function(_0xedf48a,_0x27bcbf,_0x595101){var _0x51d7ce=[{'name':'priority','label':_0x560e('0x57'),'custom':![],'options':[{'name':0x1,'value':0x1},{'name':0x2,'value':0x2},{'name':0x3,'value':0x3},{'name':0x4,'value':0x4},{'name':0x5,'value':0x5},{'name':0x6,'value':0x6},{'name':0x7,'value':0x7},{'name':0x8,'value':0x8},{'name':0x9,'value':0x9},{'name':0xa,'value':0xa}]}];var _0x190118='';return db[_0x560e('0x2f')]['findOne']({'where':{'id':_0xedf48a[_0x560e('0x4b')]['id']},'attributes':['id','remoteUri',_0x560e('0x58'),_0x560e('0x59'),_0x560e('0x5a'),_0x560e('0x5b'),_0x560e('0x5c'),_0x560e('0x5d'),_0x560e('0x5e')]})[_0x560e('0x2a')](handleEntityNotFound(_0x27bcbf,null))[_0x560e('0x2a')](function(_0x132f6f){if(_0x132f6f){_0x190118=_0x132f6f[_0x560e('0x5f')];var _0x29ba1f=_0x190118[_0x560e('0x60')](-0x1);if(_0x29ba1f==='/'){_0x190118=_0x190118[_0x560e('0x61')](0x0,_0x190118[_0x560e('0x62')](_0x29ba1f));}var _0x5c826e={'method':'GET','uri':util[_0x560e('0x63')](_0x560e('0x64'),_0x190118),'headers':{'Content-Type':_0x560e('0x65'),'Accept':'application/json'},'json':!![]};if(_0x132f6f[_0x560e('0x58')]===_0x560e('0x66')){_0x5c826e[_0x560e('0x67')][_0x560e('0x68')]=util[_0x560e('0x63')](_0x560e('0x69'),new Buffer(util['format'](_0x560e('0x6a'),_0x132f6f['username'],_0x132f6f[_0x560e('0x5a')]))[_0x560e('0x6b')](_0x560e('0x6c')));}return rp(_0x5c826e)[_0x560e('0x2a')](function(_0x190ecc){return _0x51d7ce[_0x560e('0x6d')](_['map'](_0x190ecc[_0x560e('0x6e')]['entries'],function(_0x377caa){return{'id':_0x377caa['name'],'name':_0x377caa[_0x560e('0x2d')],'type':_0x377caa[_0x560e('0x40')],'active':_0x377caa[_0x560e('0x6f')],'label':_0x377caa[_0x560e('0x70')],'custom':!![]};}));});}})[_0x560e('0x2a')](function(_0x2dc733){return{'count':_0x2dc733[_0x560e('0x39')],'rows':_0x2dc733};})[_0x560e('0x2a')](respondWithResult(_0x27bcbf,null))[_0x560e('0x42')](function(_0x2a5f5a){var _0xc0ae58=_0xedf48a[_0x560e('0x36')][_0x560e('0x71')]?0x1f4:_0x2a5f5a[_0x560e('0x72')]||0x1f4;logger[_0x560e('0x2b')](_0x560e('0x73'),_0x560e('0x56'),_0xc0ae58,JSON['stringify'](_0x2a5f5a));delete _0x2a5f5a[_0x560e('0x2d')];if(_0xc0ae58===0x191){_0xc0ae58=0x190;}_0x27bcbf['status'](_0xc0ae58)['send'](_0xedf48a['query']['test']?{'message':_0x560e('0x74'),'statusCode':_0x2a5f5a['statusCode']}:_0x2a5f5a);});};