Built motion from commit b5996064.|2.6.21
[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 _0x5f63=['status','end','json','offset','undefined','limit','count','set','Content-Range','reject','save','destroy','error','name','index','DeskAccount','rawAttributes','fieldName','type','map','query','keys','filters','intersection','model','attributes','fields','length','nolimit','order','sort','filter','where','VIRTUAL','merge','options','includeAll','include','findAll','then','catch','show','params','body','update','find','findOne','DeskConfiguration','getConfigurations','hasOwnProperty','getSubjects','channel','Descriptions','getDescriptions','Subjects','priority','remoteUri','authType','username','password','consumerKey','token','slice','lastIndexOf','GET','format','%s/custom_fields','application/json','basic','Authorization','Basic\x20%s','%s:%s','toString','entries','active','label','test','statusCode','getFields,\x20%s,\x20%s,\x20%s','getFields','stringify','Wrong\x20credentials','eml-format','rimraf','zip-dir','fast-json-patch','moment','bluebird','mustache','util','path','ejs','fs-extra','lodash','squel','desk.js','to-csv','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','../../config/license/util','../../components/integrations/configuration','sendStatus'];(function(_0x4b0290,_0x188c34){var _0x4bd9f4=function(_0x357f3f){while(--_0x357f3f){_0x4b0290['push'](_0x4b0290['shift']());}};_0x4bd9f4(++_0x188c34);}(_0x5f63,0x196));var _0x35f6=function(_0x2321d2,_0x5b9a61){_0x2321d2=_0x2321d2-0x0;var _0x193d16=_0x5f63[_0x2321d2];return _0x193d16;};'use strict';var emlformat=require(_0x35f6('0x0'));var rimraf=require(_0x35f6('0x1'));var zipdir=require(_0x35f6('0x2'));var jsonpatch=require(_0x35f6('0x3'));var rp=require('request-promise');var moment=require(_0x35f6('0x4'));var BPromise=require(_0x35f6('0x5'));var Mustache=require(_0x35f6('0x6'));var util=require(_0x35f6('0x7'));var path=require(_0x35f6('0x8'));var sox=require('sox');var csv=require('to-csv');var ejs=require(_0x35f6('0x9'));var fs=require('fs');var fs_extra=require(_0x35f6('0xa'));var _=require(_0x35f6('0xb'));var squel=require(_0x35f6('0xc'));var crypto=require('crypto');var jsforce=require('jsforce');var deskjs=require(_0x35f6('0xd'));var toCsv=require(_0x35f6('0xe'));var querystring=require(_0x35f6('0xf'));var Papa=require(_0x35f6('0x10'));var Redis=require(_0x35f6('0x11'));var authService=require(_0x35f6('0x12'));var qs=require(_0x35f6('0x13'));var as=require(_0x35f6('0x14'));var hardwareService=require(_0x35f6('0x15'));var logger=require(_0x35f6('0x16'))('api');var utils=require('../../config/utils');var config=require('../../config/environment');var licenseUtil=require(_0x35f6('0x17'));var db=require('../../mysqldb')['db'];var integrations=require(_0x35f6('0x18'));function respondWithStatusCode(_0x398a8d,_0x3ae14f){_0x3ae14f=_0x3ae14f||0xcc;return function(_0x2424de){if(_0x2424de){return _0x398a8d[_0x35f6('0x19')](_0x3ae14f);}return _0x398a8d[_0x35f6('0x1a')](_0x3ae14f)[_0x35f6('0x1b')]();};}function respondWithResult(_0x563099,_0x45a055){_0x45a055=_0x45a055||0xc8;return function(_0x1041a8){if(_0x1041a8){return _0x563099[_0x35f6('0x1a')](_0x45a055)[_0x35f6('0x1c')](_0x1041a8);}};}function respondWithFilteredResult(_0x179599,_0x405640){return function(_0x39629c){if(_0x39629c){var _0x5b041d=typeof _0x405640[_0x35f6('0x1d')]===_0x35f6('0x1e')&&typeof _0x405640[_0x35f6('0x1f')]==='undefined';var _0x20c95f=_0x39629c[_0x35f6('0x20')];var _0x4ff085=_0x5b041d?0x0:_0x405640[_0x35f6('0x1d')];var _0x49bfe5=_0x5b041d?_0x39629c[_0x35f6('0x20')]:_0x405640[_0x35f6('0x1d')]+_0x405640[_0x35f6('0x1f')];var _0x291621;if(_0x49bfe5>=_0x20c95f){_0x49bfe5=_0x20c95f;_0x291621=0xc8;}else{_0x291621=0xce;}_0x179599[_0x35f6('0x1a')](_0x291621);return _0x179599[_0x35f6('0x21')](_0x35f6('0x22'),_0x4ff085+'-'+_0x49bfe5+'/'+_0x20c95f)['json'](_0x39629c);}return null;};}function patchUpdates(_0x517d5b){return function(_0xd861cd){try{jsonpatch['apply'](_0xd861cd,_0x517d5b,!![]);}catch(_0x373e20){return BPromise[_0x35f6('0x23')](_0x373e20);}return _0xd861cd[_0x35f6('0x24')]();};}function saveUpdates(_0xd0b59,_0x14a164){return function(_0x401a9c){if(_0x401a9c){return _0x401a9c['update'](_0xd0b59)['then'](function(_0x1f445a){return _0x1f445a;});}return null;};}function removeEntity(_0x1384d2,_0x24ea42){return function(_0x293b61){if(_0x293b61){return _0x293b61[_0x35f6('0x25')]()['then'](function(){_0x1384d2[_0x35f6('0x1a')](0xcc)[_0x35f6('0x1b')]();});}};}function handleEntityNotFound(_0x994390,_0xc6e52e){return function(_0x39a006){if(!_0x39a006){_0x994390['sendStatus'](0x194);}return _0x39a006;};}function handleError(_0x291604,_0xa238dc){_0xa238dc=_0xa238dc||0x1f4;return function(_0x1a985f){logger[_0x35f6('0x26')](_0x1a985f['stack']);if(_0x1a985f[_0x35f6('0x27')]){delete _0x1a985f[_0x35f6('0x27')];}_0x291604[_0x35f6('0x1a')](_0xa238dc)['send'](_0x1a985f);};}exports[_0x35f6('0x28')]=function(_0x1e894a,_0x5ba902){var _0x12fefa={},_0xd1795b={},_0x6d3e0f={'count':0x0,'rows':[]};var _0x1a9c0c=_['map'](db[_0x35f6('0x29')][_0x35f6('0x2a')],function(_0x45e448){return{'name':_0x45e448[_0x35f6('0x2b')],'type':_0x45e448[_0x35f6('0x2c')]['key']};});_0xd1795b['model']=_[_0x35f6('0x2d')](_0x1a9c0c,_0x35f6('0x27'));_0xd1795b[_0x35f6('0x2e')]=_[_0x35f6('0x2f')](_0x1e894a[_0x35f6('0x2e')]);_0xd1795b[_0x35f6('0x30')]=_[_0x35f6('0x31')](_0xd1795b[_0x35f6('0x32')],_0xd1795b[_0x35f6('0x2e')]);_0x12fefa[_0x35f6('0x33')]=_[_0x35f6('0x31')](_0xd1795b['model'],qs[_0x35f6('0x34')](_0x1e894a[_0x35f6('0x2e')]['fields']));_0x12fefa['attributes']=_0x12fefa[_0x35f6('0x33')][_0x35f6('0x35')]?_0x12fefa[_0x35f6('0x33')]:_0xd1795b[_0x35f6('0x32')];if(!_0x1e894a[_0x35f6('0x2e')]['hasOwnProperty'](_0x35f6('0x36'))){_0x12fefa[_0x35f6('0x1f')]=qs['limit'](_0x1e894a['query'][_0x35f6('0x1f')]);_0x12fefa['offset']=qs[_0x35f6('0x1d')](_0x1e894a[_0x35f6('0x2e')]['offset']);}_0x12fefa[_0x35f6('0x37')]=qs[_0x35f6('0x38')](_0x1e894a[_0x35f6('0x2e')]['sort']);_0x12fefa['where']=qs[_0x35f6('0x30')](_['pick'](_0x1e894a[_0x35f6('0x2e')],_0xd1795b[_0x35f6('0x30')]),_0x1a9c0c);if(_0x1e894a['query'][_0x35f6('0x39')]){_0x12fefa['where']=_['merge'](_0x12fefa[_0x35f6('0x3a')],{'$or':_[_0x35f6('0x2d')](_0x1a9c0c,function(_0x156870){if(_0x156870[_0x35f6('0x2c')]!==_0x35f6('0x3b')){var _0x57297c={};_0x57297c[_0x156870[_0x35f6('0x27')]]={'$like':'%'+_0x1e894a[_0x35f6('0x2e')][_0x35f6('0x39')]+'%'};return _0x57297c;}})});}_0x12fefa=_[_0x35f6('0x3c')]({},_0x12fefa,_0x1e894a[_0x35f6('0x3d')]);var _0x523e2d={'where':_0x12fefa[_0x35f6('0x3a')]};return db[_0x35f6('0x29')]['count'](_0x523e2d)['then'](function(_0x2d8153){_0x6d3e0f[_0x35f6('0x20')]=_0x2d8153;if(_0x1e894a['query'][_0x35f6('0x3e')]){_0x12fefa[_0x35f6('0x3f')]=[{'all':!![]}];}return db[_0x35f6('0x29')][_0x35f6('0x40')](_0x12fefa);})[_0x35f6('0x41')](function(_0x9ecb0a){_0x6d3e0f['rows']=_0x9ecb0a;return _0x6d3e0f;})[_0x35f6('0x41')](respondWithFilteredResult(_0x5ba902,_0x12fefa))[_0x35f6('0x42')](handleError(_0x5ba902,null));};exports[_0x35f6('0x43')]=function(_0x3e4e8d,_0x5053a5){var _0x1bfb7a={'raw':![],'where':{'id':_0x3e4e8d[_0x35f6('0x44')]['id']}},_0x4181d1={};_0x4181d1['model']=_['keys'](db['DeskAccount'][_0x35f6('0x2a')]);_0x4181d1[_0x35f6('0x2e')]=_[_0x35f6('0x2f')](_0x3e4e8d[_0x35f6('0x2e')]);_0x4181d1[_0x35f6('0x30')]=_[_0x35f6('0x31')](_0x4181d1[_0x35f6('0x32')],_0x4181d1[_0x35f6('0x2e')]);_0x1bfb7a[_0x35f6('0x33')]=_[_0x35f6('0x31')](_0x4181d1[_0x35f6('0x32')],qs[_0x35f6('0x34')](_0x3e4e8d['query']['fields']));_0x1bfb7a[_0x35f6('0x33')]=_0x1bfb7a['attributes'][_0x35f6('0x35')]?_0x1bfb7a[_0x35f6('0x33')]:_0x4181d1[_0x35f6('0x32')];if(_0x3e4e8d[_0x35f6('0x2e')][_0x35f6('0x3e')]){_0x1bfb7a[_0x35f6('0x3f')]=[{'all':!![]}];}_0x1bfb7a=_[_0x35f6('0x3c')]({},_0x1bfb7a,_0x3e4e8d[_0x35f6('0x3d')]);return db[_0x35f6('0x29')]['find'](_0x1bfb7a)[_0x35f6('0x41')](handleEntityNotFound(_0x5053a5,null))['then'](respondWithResult(_0x5053a5,null))[_0x35f6('0x42')](handleError(_0x5053a5,null));};exports['create']=function(_0x61d9e9,_0x2b4c75){return db['DeskAccount']['create'](_0x61d9e9[_0x35f6('0x45')],{})[_0x35f6('0x41')](respondWithResult(_0x2b4c75,0xc9))['catch'](handleError(_0x2b4c75,null));};exports[_0x35f6('0x46')]=function(_0x5b64cb,_0x22f73f){if(_0x5b64cb[_0x35f6('0x45')]['id']){delete _0x5b64cb[_0x35f6('0x45')]['id'];}return db['DeskAccount'][_0x35f6('0x47')]({'where':{'id':_0x5b64cb[_0x35f6('0x44')]['id']}})[_0x35f6('0x41')](handleEntityNotFound(_0x22f73f,null))[_0x35f6('0x41')](saveUpdates(_0x5b64cb[_0x35f6('0x45')],null))[_0x35f6('0x41')](respondWithResult(_0x22f73f,null))[_0x35f6('0x42')](handleError(_0x22f73f,null));};exports[_0x35f6('0x25')]=function(_0x31fb88,_0x2a4d00){return db['DeskAccount'][_0x35f6('0x47')]({'where':{'id':_0x31fb88[_0x35f6('0x44')]['id']}})[_0x35f6('0x41')](handleEntityNotFound(_0x2a4d00,null))[_0x35f6('0x41')](removeEntity(_0x2a4d00,null))['catch'](handleError(_0x2a4d00,null));};exports['getConfigurations']=function(_0x5b74ea,_0x5046e4,_0xe279be){var _0x51f524={};var _0x2818db={};var _0x3e4214;var _0x29f0c0;return db[_0x35f6('0x29')][_0x35f6('0x48')]({'where':{'id':_0x5b74ea[_0x35f6('0x44')]['id']}})['then'](handleEntityNotFound(_0x5046e4,null))[_0x35f6('0x41')](function(_0x2d9658){if(_0x2d9658){_0x3e4214=_0x2d9658;_0x2818db['model']=_[_0x35f6('0x2f')](db[_0x35f6('0x49')][_0x35f6('0x2a')]);_0x2818db[_0x35f6('0x2e')]=_[_0x35f6('0x2f')](_0x5b74ea[_0x35f6('0x2e')]);_0x2818db[_0x35f6('0x30')]=_['intersection'](_0x2818db[_0x35f6('0x32')],_0x2818db['query']);_0x51f524[_0x35f6('0x33')]=_['intersection'](_0x2818db[_0x35f6('0x32')],qs[_0x35f6('0x34')](_0x5b74ea[_0x35f6('0x2e')][_0x35f6('0x34')]));_0x51f524[_0x35f6('0x33')]=_0x51f524[_0x35f6('0x33')][_0x35f6('0x35')]?_0x51f524[_0x35f6('0x33')]:_0x2818db['model'];_0x51f524[_0x35f6('0x37')]=qs[_0x35f6('0x38')](_0x5b74ea[_0x35f6('0x2e')][_0x35f6('0x38')]);_0x51f524[_0x35f6('0x3a')]=qs[_0x35f6('0x30')](_['pick'](_0x5b74ea[_0x35f6('0x2e')],_0x2818db[_0x35f6('0x30')]));if(_0x5b74ea[_0x35f6('0x2e')]['filter']){_0x51f524[_0x35f6('0x3a')]=_[_0x35f6('0x3c')](_0x51f524[_0x35f6('0x3a')],{'$or':_[_0x35f6('0x2d')](_0x51f524[_0x35f6('0x33')],function(_0x27481f){var _0x2d12d9={};_0x2d12d9[_0x27481f]={'$like':'%'+_0x5b74ea[_0x35f6('0x2e')][_0x35f6('0x39')]+'%'};return _0x2d12d9;})});}_0x51f524=_[_0x35f6('0x3c')]({},_0x51f524,_0x5b74ea[_0x35f6('0x3d')]);return _0x3e4214[_0x35f6('0x4a')](_0x51f524);}})['then'](function(_0x4b8458){if(_0x4b8458){_0x29f0c0=_0x4b8458[_0x35f6('0x35')];if(!_0x5b74ea['query'][_0x35f6('0x4b')](_0x35f6('0x36'))){_0x51f524[_0x35f6('0x1f')]=qs[_0x35f6('0x1f')](_0x5b74ea['query'][_0x35f6('0x1f')]);_0x51f524[_0x35f6('0x1d')]=qs[_0x35f6('0x1d')](_0x5b74ea[_0x35f6('0x2e')][_0x35f6('0x1d')]);}return _0x3e4214[_0x35f6('0x4a')](_0x51f524);}})[_0x35f6('0x41')](function(_0x1a2592){if(_0x1a2592){return _0x1a2592?{'count':_0x29f0c0,'rows':_0x1a2592}:null;}})[_0x35f6('0x41')](respondWithResult(_0x5046e4,null))['catch'](handleError(_0x5046e4,null));};exports['addConfiguration']=function(_0x519741,_0xad3751,_0x1861b5){if(_0x519741[_0x35f6('0x45')]['id']){delete _0x519741[_0x35f6('0x45')]['id'];}return db[_0x35f6('0x29')][_0x35f6('0x48')]({'where':{'id':_0x519741[_0x35f6('0x44')]['id']}})[_0x35f6('0x41')](handleEntityNotFound(_0xad3751,null))[_0x35f6('0x41')](function(_0xe4a0fc){if(_0xe4a0fc){_0x519741[_0x35f6('0x45')]['AccountId']=_0xe4a0fc['id'];_0x519741[_0x35f6('0x45')]['Subjects']=integrations[_0x35f6('0x4c')](_0x519741['body'][_0x35f6('0x4d')],_0x519741['body'][_0x35f6('0x2c')]);_0x519741['body'][_0x35f6('0x4e')]=integrations[_0x35f6('0x4f')](_0x519741['body'][_0x35f6('0x4d')],_0x519741[_0x35f6('0x45')][_0x35f6('0x2c')]);return db[_0x35f6('0x49')]['create'](_0x519741[_0x35f6('0x45')],{'include':[{'model':db['DeskField'],'as':_0x35f6('0x50')},{'model':db['DeskField'],'as':_0x35f6('0x4e')}]});}return null;})[_0x35f6('0x41')](respondWithResult(_0xad3751,null))[_0x35f6('0x42')](handleError(_0xad3751,null));};exports['getFields']=function(_0x3617ba,_0x50977d,_0x452204){var _0x5413f9=[{'name':_0x35f6('0x51'),'label':'priority','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 _0x382d81='';return db[_0x35f6('0x29')][_0x35f6('0x48')]({'where':{'id':_0x3617ba['params']['id']},'attributes':['id',_0x35f6('0x52'),_0x35f6('0x53'),_0x35f6('0x54'),_0x35f6('0x55'),_0x35f6('0x56'),'consumerSecret',_0x35f6('0x57'),'tokenSecret']})['then'](handleEntityNotFound(_0x50977d,null))[_0x35f6('0x41')](function(_0x42689c){if(_0x42689c){_0x382d81=_0x42689c[_0x35f6('0x52')];var _0x1ec742=_0x382d81[_0x35f6('0x58')](-0x1);if(_0x1ec742==='/'){_0x382d81=_0x382d81['substring'](0x0,_0x382d81[_0x35f6('0x59')](_0x1ec742));}var _0x89da4f={'method':_0x35f6('0x5a'),'uri':util[_0x35f6('0x5b')](_0x35f6('0x5c'),_0x382d81),'headers':{'Content-Type':'application/json','Accept':_0x35f6('0x5d')},'json':!![]};if(_0x42689c[_0x35f6('0x53')]===_0x35f6('0x5e')){_0x89da4f['headers'][_0x35f6('0x5f')]=util[_0x35f6('0x5b')](_0x35f6('0x60'),new Buffer(util[_0x35f6('0x5b')](_0x35f6('0x61'),_0x42689c['username'],_0x42689c[_0x35f6('0x55')]))[_0x35f6('0x62')]('base64'));}return rp(_0x89da4f)[_0x35f6('0x41')](function(_0x11377d){return _0x5413f9['concat'](_[_0x35f6('0x2d')](_0x11377d['_embedded'][_0x35f6('0x63')],function(_0x32f366){return{'id':_0x32f366['name'],'name':_0x32f366[_0x35f6('0x27')],'type':_0x32f366[_0x35f6('0x2c')],'active':_0x32f366[_0x35f6('0x64')],'label':_0x32f366[_0x35f6('0x65')],'custom':!![]};}));});}})[_0x35f6('0x41')](function(_0x39196b){return{'count':_0x39196b[_0x35f6('0x35')],'rows':_0x39196b};})['then'](respondWithResult(_0x50977d,null))['catch'](function(_0x258482){var _0x467df5=_0x3617ba[_0x35f6('0x2e')][_0x35f6('0x66')]?0x1f4:_0x258482[_0x35f6('0x67')]||0x1f4;logger[_0x35f6('0x26')](_0x35f6('0x68'),_0x35f6('0x69'),_0x467df5,JSON[_0x35f6('0x6a')](_0x258482));delete _0x258482[_0x35f6('0x27')];if(_0x467df5===0x191){_0x467df5=0x190;}_0x50977d[_0x35f6('0x1a')](_0x467df5)['send'](_0x3617ba[_0x35f6('0x2e')]['test']?{'message':_0x35f6('0x6b'),'statusCode':_0x258482[_0x35f6('0x67')]}:_0x258482);});};