Built motion from commit 0c5f406c.|2.6.29
[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 _0x545f=['fields','hasOwnProperty','nolimit','order','sort','where','pick','filter','merge','VIRTUAL','options','include','DeskAccount','findAll','rows','show','params','rawAttributes','length','includeAll','catch','create','body','find','getConfigurations','DeskConfiguration','findOne','Subjects','getSubjects','channel','type','Descriptions','getDescriptions','DeskField','priority','remoteUri','authType','username','password','consumerSecret','token','tokenSecret','slice','substring','lastIndexOf','GET','format','application/json','basic','headers','Basic\x20%s','toString','concat','_embedded','active','label','test','statusCode','getFields,\x20%s,\x20%s,\x20%s','getFields','Wrong\x20credentials','rimraf','zip-dir','moment','bluebird','mustache','util','path','sox','to-csv','ejs','fs-extra','lodash','squel','crypto','jsforce','desk.js','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','../../config/environment','../../config/license/util','sendStatus','status','end','json','offset','limit','undefined','count','set','Content-Range','apply','save','update','then','destroy','error','name','send','map','key','model','query','keys','filters','attributes','intersection'];(function(_0x4a5a5d,_0x146aa6){var _0xea850d=function(_0x11583b){while(--_0x11583b){_0x4a5a5d['push'](_0x4a5a5d['shift']());}};_0xea850d(++_0x146aa6);}(_0x545f,0xac));var _0xf545=function(_0x30faec,_0x18940b){_0x30faec=_0x30faec-0x0;var _0x29484f=_0x545f[_0x30faec];return _0x29484f;};'use strict';var emlformat=require('eml-format');var rimraf=require(_0xf545('0x0'));var zipdir=require(_0xf545('0x1'));var jsonpatch=require('fast-json-patch');var rp=require('request-promise');var moment=require(_0xf545('0x2'));var BPromise=require(_0xf545('0x3'));var Mustache=require(_0xf545('0x4'));var util=require(_0xf545('0x5'));var path=require(_0xf545('0x6'));var sox=require(_0xf545('0x7'));var csv=require(_0xf545('0x8'));var ejs=require(_0xf545('0x9'));var fs=require('fs');var fs_extra=require(_0xf545('0xa'));var _=require(_0xf545('0xb'));var squel=require(_0xf545('0xc'));var crypto=require(_0xf545('0xd'));var jsforce=require(_0xf545('0xe'));var deskjs=require(_0xf545('0xf'));var toCsv=require(_0xf545('0x8'));var querystring=require('querystring');var Papa=require('papaparse');var Redis=require(_0xf545('0x10'));var authService=require(_0xf545('0x11'));var qs=require(_0xf545('0x12'));var as=require(_0xf545('0x13'));var hardwareService=require(_0xf545('0x14'));var logger=require(_0xf545('0x15'))('api');var utils=require('../../config/utils');var config=require(_0xf545('0x16'));var licenseUtil=require(_0xf545('0x17'));var db=require('../../mysqldb')['db'];var integrations=require('../../components/integrations/configuration');function respondWithStatusCode(_0x14bee0,_0x179f8b){_0x179f8b=_0x179f8b||0xcc;return function(_0x5d73c2){if(_0x5d73c2){return _0x14bee0[_0xf545('0x18')](_0x179f8b);}return _0x14bee0[_0xf545('0x19')](_0x179f8b)[_0xf545('0x1a')]();};}function respondWithResult(_0x272a32,_0x1b64b7){_0x1b64b7=_0x1b64b7||0xc8;return function(_0x324e7d){if(_0x324e7d){return _0x272a32[_0xf545('0x19')](_0x1b64b7)[_0xf545('0x1b')](_0x324e7d);}};}function respondWithFilteredResult(_0x26a34a,_0x578e92){return function(_0x23ac57){if(_0x23ac57){var _0x21fedb=typeof _0x578e92[_0xf545('0x1c')]==='undefined'&&typeof _0x578e92[_0xf545('0x1d')]===_0xf545('0x1e');var _0x2bb17a=_0x23ac57[_0xf545('0x1f')];var _0x1a5d9b=_0x21fedb?0x0:_0x578e92[_0xf545('0x1c')];var _0x1fed33=_0x21fedb?_0x23ac57[_0xf545('0x1f')]:_0x578e92[_0xf545('0x1c')]+_0x578e92[_0xf545('0x1d')];var _0x4ac2c5;if(_0x1fed33>=_0x2bb17a){_0x1fed33=_0x2bb17a;_0x4ac2c5=0xc8;}else{_0x4ac2c5=0xce;}_0x26a34a['status'](_0x4ac2c5);return _0x26a34a[_0xf545('0x20')](_0xf545('0x21'),_0x1a5d9b+'-'+_0x1fed33+'/'+_0x2bb17a)['json'](_0x23ac57);}return null;};}function patchUpdates(_0x33be58){return function(_0xdff66b){try{jsonpatch[_0xf545('0x22')](_0xdff66b,_0x33be58,!![]);}catch(_0x39d6f5){return BPromise['reject'](_0x39d6f5);}return _0xdff66b[_0xf545('0x23')]();};}function saveUpdates(_0x14185d,_0x2b0003){return function(_0x4b4253){if(_0x4b4253){return _0x4b4253[_0xf545('0x24')](_0x14185d)[_0xf545('0x25')](function(_0x166b16){return _0x166b16;});}return null;};}function removeEntity(_0x86a2a2,_0x54c007){return function(_0x56a2f6){if(_0x56a2f6){return _0x56a2f6[_0xf545('0x26')]()['then'](function(){_0x86a2a2[_0xf545('0x19')](0xcc)[_0xf545('0x1a')]();});}};}function handleEntityNotFound(_0x545576,_0x124451){return function(_0x1320b0){if(!_0x1320b0){_0x545576[_0xf545('0x18')](0x194);}return _0x1320b0;};}function handleError(_0x9358ea,_0x1802c1){_0x1802c1=_0x1802c1||0x1f4;return function(_0x3a0345){logger[_0xf545('0x27')](_0x3a0345['stack']);if(_0x3a0345['name']){delete _0x3a0345[_0xf545('0x28')];}_0x9358ea[_0xf545('0x19')](_0x1802c1)[_0xf545('0x29')](_0x3a0345);};}exports['index']=function(_0x53801c,_0x281ea8){var _0x3d8002={},_0xc3ebd7={},_0x5414cf={'count':0x0,'rows':[]};var _0x1405eb=_[_0xf545('0x2a')](db['DeskAccount']['rawAttributes'],function(_0x2f9861){return{'name':_0x2f9861['fieldName'],'type':_0x2f9861['type'][_0xf545('0x2b')]};});_0xc3ebd7[_0xf545('0x2c')]=_['map'](_0x1405eb,'name');_0xc3ebd7[_0xf545('0x2d')]=_[_0xf545('0x2e')](_0x53801c[_0xf545('0x2d')]);_0xc3ebd7[_0xf545('0x2f')]=_['intersection'](_0xc3ebd7[_0xf545('0x2c')],_0xc3ebd7['query']);_0x3d8002[_0xf545('0x30')]=_[_0xf545('0x31')](_0xc3ebd7[_0xf545('0x2c')],qs[_0xf545('0x32')](_0x53801c[_0xf545('0x2d')][_0xf545('0x32')]));_0x3d8002['attributes']=_0x3d8002['attributes']['length']?_0x3d8002[_0xf545('0x30')]:_0xc3ebd7[_0xf545('0x2c')];if(!_0x53801c[_0xf545('0x2d')][_0xf545('0x33')](_0xf545('0x34'))){_0x3d8002[_0xf545('0x1d')]=qs[_0xf545('0x1d')](_0x53801c['query']['limit']);_0x3d8002['offset']=qs[_0xf545('0x1c')](_0x53801c[_0xf545('0x2d')][_0xf545('0x1c')]);}_0x3d8002[_0xf545('0x35')]=qs['sort'](_0x53801c[_0xf545('0x2d')][_0xf545('0x36')]);_0x3d8002[_0xf545('0x37')]=qs['filters'](_[_0xf545('0x38')](_0x53801c[_0xf545('0x2d')],_0xc3ebd7[_0xf545('0x2f')]),_0x1405eb);if(_0x53801c['query'][_0xf545('0x39')]){_0x3d8002[_0xf545('0x37')]=_[_0xf545('0x3a')](_0x3d8002[_0xf545('0x37')],{'$or':_[_0xf545('0x2a')](_0x1405eb,function(_0x2adee5){if(_0x2adee5['type']!==_0xf545('0x3b')){var _0x45afb2={};_0x45afb2[_0x2adee5[_0xf545('0x28')]]={'$like':'%'+_0x53801c[_0xf545('0x2d')][_0xf545('0x39')]+'%'};return _0x45afb2;}})});}_0x3d8002=_[_0xf545('0x3a')]({},_0x3d8002,_0x53801c[_0xf545('0x3c')]);var _0x3c72f9={'where':_0x3d8002[_0xf545('0x37')]};return db['DeskAccount'][_0xf545('0x1f')](_0x3c72f9)['then'](function(_0x16a209){_0x5414cf[_0xf545('0x1f')]=_0x16a209;if(_0x53801c['query']['includeAll']){_0x3d8002[_0xf545('0x3d')]=[{'all':!![]}];}return db[_0xf545('0x3e')][_0xf545('0x3f')](_0x3d8002);})[_0xf545('0x25')](function(_0x2045bd){_0x5414cf[_0xf545('0x40')]=_0x2045bd;return _0x5414cf;})[_0xf545('0x25')](respondWithFilteredResult(_0x281ea8,_0x3d8002))['catch'](handleError(_0x281ea8,null));};exports[_0xf545('0x41')]=function(_0x501ba4,_0x5d9119){var _0xc788b8={'raw':![],'where':{'id':_0x501ba4[_0xf545('0x42')]['id']}},_0x5d62e3={};_0x5d62e3[_0xf545('0x2c')]=_[_0xf545('0x2e')](db['DeskAccount'][_0xf545('0x43')]);_0x5d62e3[_0xf545('0x2d')]=_['keys'](_0x501ba4['query']);_0x5d62e3[_0xf545('0x2f')]=_['intersection'](_0x5d62e3[_0xf545('0x2c')],_0x5d62e3[_0xf545('0x2d')]);_0xc788b8['attributes']=_[_0xf545('0x31')](_0x5d62e3[_0xf545('0x2c')],qs[_0xf545('0x32')](_0x501ba4[_0xf545('0x2d')][_0xf545('0x32')]));_0xc788b8[_0xf545('0x30')]=_0xc788b8[_0xf545('0x30')][_0xf545('0x44')]?_0xc788b8[_0xf545('0x30')]:_0x5d62e3[_0xf545('0x2c')];if(_0x501ba4[_0xf545('0x2d')][_0xf545('0x45')]){_0xc788b8['include']=[{'all':!![]}];}_0xc788b8=_[_0xf545('0x3a')]({},_0xc788b8,_0x501ba4['options']);return db['DeskAccount']['find'](_0xc788b8)[_0xf545('0x25')](handleEntityNotFound(_0x5d9119,null))[_0xf545('0x25')](respondWithResult(_0x5d9119,null))[_0xf545('0x46')](handleError(_0x5d9119,null));};exports[_0xf545('0x47')]=function(_0x27f388,_0x251193){return db[_0xf545('0x3e')]['create'](_0x27f388[_0xf545('0x48')],{})['then'](respondWithResult(_0x251193,0xc9))[_0xf545('0x46')](handleError(_0x251193,null));};exports[_0xf545('0x24')]=function(_0x483b4a,_0x488f95){if(_0x483b4a['body']['id']){delete _0x483b4a[_0xf545('0x48')]['id'];}return db[_0xf545('0x3e')][_0xf545('0x49')]({'where':{'id':_0x483b4a[_0xf545('0x42')]['id']}})[_0xf545('0x25')](handleEntityNotFound(_0x488f95,null))[_0xf545('0x25')](saveUpdates(_0x483b4a[_0xf545('0x48')],null))[_0xf545('0x25')](respondWithResult(_0x488f95,null))[_0xf545('0x46')](handleError(_0x488f95,null));};exports[_0xf545('0x26')]=function(_0x1fa6f5,_0x5120dd){return db['DeskAccount']['find']({'where':{'id':_0x1fa6f5['params']['id']}})[_0xf545('0x25')](handleEntityNotFound(_0x5120dd,null))[_0xf545('0x25')](removeEntity(_0x5120dd,null))[_0xf545('0x46')](handleError(_0x5120dd,null));};exports[_0xf545('0x4a')]=function(_0x2c5118,_0x40aac1,_0x3b7b54){var _0x33f71d={};var _0x3bf6e7={};var _0x275ff5;var _0xc2af2c;return db[_0xf545('0x3e')]['findOne']({'where':{'id':_0x2c5118[_0xf545('0x42')]['id']}})[_0xf545('0x25')](handleEntityNotFound(_0x40aac1,null))[_0xf545('0x25')](function(_0x4e62aa){if(_0x4e62aa){_0x275ff5=_0x4e62aa;_0x3bf6e7['model']=_['keys'](db[_0xf545('0x4b')]['rawAttributes']);_0x3bf6e7['query']=_[_0xf545('0x2e')](_0x2c5118['query']);_0x3bf6e7[_0xf545('0x2f')]=_[_0xf545('0x31')](_0x3bf6e7[_0xf545('0x2c')],_0x3bf6e7[_0xf545('0x2d')]);_0x33f71d['attributes']=_[_0xf545('0x31')](_0x3bf6e7['model'],qs[_0xf545('0x32')](_0x2c5118[_0xf545('0x2d')]['fields']));_0x33f71d[_0xf545('0x30')]=_0x33f71d['attributes'][_0xf545('0x44')]?_0x33f71d['attributes']:_0x3bf6e7[_0xf545('0x2c')];_0x33f71d['order']=qs[_0xf545('0x36')](_0x2c5118[_0xf545('0x2d')][_0xf545('0x36')]);_0x33f71d['where']=qs['filters'](_[_0xf545('0x38')](_0x2c5118[_0xf545('0x2d')],_0x3bf6e7[_0xf545('0x2f')]));if(_0x2c5118[_0xf545('0x2d')][_0xf545('0x39')]){_0x33f71d['where']=_[_0xf545('0x3a')](_0x33f71d[_0xf545('0x37')],{'$or':_[_0xf545('0x2a')](_0x33f71d[_0xf545('0x30')],function(_0x41e1c){var _0x3397a4={};_0x3397a4[_0x41e1c]={'$like':'%'+_0x2c5118[_0xf545('0x2d')][_0xf545('0x39')]+'%'};return _0x3397a4;})});}_0x33f71d=_[_0xf545('0x3a')]({},_0x33f71d,_0x2c5118[_0xf545('0x3c')]);return _0x275ff5[_0xf545('0x4a')](_0x33f71d);}})[_0xf545('0x25')](function(_0x8aae49){if(_0x8aae49){_0xc2af2c=_0x8aae49[_0xf545('0x44')];if(!_0x2c5118['query']['hasOwnProperty'](_0xf545('0x34'))){_0x33f71d[_0xf545('0x1d')]=qs[_0xf545('0x1d')](_0x2c5118[_0xf545('0x2d')][_0xf545('0x1d')]);_0x33f71d[_0xf545('0x1c')]=qs[_0xf545('0x1c')](_0x2c5118[_0xf545('0x2d')][_0xf545('0x1c')]);}return _0x275ff5[_0xf545('0x4a')](_0x33f71d);}})[_0xf545('0x25')](function(_0x3e3307){if(_0x3e3307){return _0x3e3307?{'count':_0xc2af2c,'rows':_0x3e3307}:null;}})[_0xf545('0x25')](respondWithResult(_0x40aac1,null))['catch'](handleError(_0x40aac1,null));};exports['addConfiguration']=function(_0x100a71,_0x3215b2,_0x27c039){if(_0x100a71[_0xf545('0x48')]['id']){delete _0x100a71['body']['id'];}return db[_0xf545('0x3e')][_0xf545('0x4c')]({'where':{'id':_0x100a71[_0xf545('0x42')]['id']}})[_0xf545('0x25')](handleEntityNotFound(_0x3215b2,null))[_0xf545('0x25')](function(_0x286cd7){if(_0x286cd7){_0x100a71[_0xf545('0x48')]['AccountId']=_0x286cd7['id'];_0x100a71[_0xf545('0x48')][_0xf545('0x4d')]=integrations[_0xf545('0x4e')](_0x100a71['body'][_0xf545('0x4f')],_0x100a71[_0xf545('0x48')][_0xf545('0x50')]);_0x100a71[_0xf545('0x48')][_0xf545('0x51')]=integrations[_0xf545('0x52')](_0x100a71['body'][_0xf545('0x4f')],_0x100a71[_0xf545('0x48')][_0xf545('0x50')]);return db[_0xf545('0x4b')]['create'](_0x100a71[_0xf545('0x48')],{'include':[{'model':db[_0xf545('0x53')],'as':_0xf545('0x4d')},{'model':db[_0xf545('0x53')],'as':_0xf545('0x51')}]});}return null;})[_0xf545('0x25')](respondWithResult(_0x3215b2,null))[_0xf545('0x46')](handleError(_0x3215b2,null));};exports['getFields']=function(_0x2ebc81,_0x5a4d4a,_0x5c5d22){var _0x1ab75f=[{'name':_0xf545('0x54'),'label':_0xf545('0x54'),'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 _0xe08298='';return db['DeskAccount'][_0xf545('0x4c')]({'where':{'id':_0x2ebc81['params']['id']},'attributes':['id',_0xf545('0x55'),_0xf545('0x56'),_0xf545('0x57'),_0xf545('0x58'),'consumerKey',_0xf545('0x59'),_0xf545('0x5a'),_0xf545('0x5b')]})['then'](handleEntityNotFound(_0x5a4d4a,null))[_0xf545('0x25')](function(_0x52f631){if(_0x52f631){_0xe08298=_0x52f631[_0xf545('0x55')];var _0x1a03cc=_0xe08298[_0xf545('0x5c')](-0x1);if(_0x1a03cc==='/'){_0xe08298=_0xe08298[_0xf545('0x5d')](0x0,_0xe08298[_0xf545('0x5e')](_0x1a03cc));}var _0x223d75={'method':_0xf545('0x5f'),'uri':util[_0xf545('0x60')]('%s/custom_fields',_0xe08298),'headers':{'Content-Type':_0xf545('0x61'),'Accept':'application/json'},'json':!![]};if(_0x52f631[_0xf545('0x56')]===_0xf545('0x62')){_0x223d75[_0xf545('0x63')]['Authorization']=util['format'](_0xf545('0x64'),new Buffer(util['format']('%s:%s',_0x52f631[_0xf545('0x57')],_0x52f631[_0xf545('0x58')]))[_0xf545('0x65')]('base64'));}return rp(_0x223d75)[_0xf545('0x25')](function(_0x20c4d1){return _0x1ab75f[_0xf545('0x66')](_[_0xf545('0x2a')](_0x20c4d1[_0xf545('0x67')]['entries'],function(_0xd99de4){return{'id':_0xd99de4[_0xf545('0x28')],'name':_0xd99de4[_0xf545('0x28')],'type':_0xd99de4[_0xf545('0x50')],'active':_0xd99de4[_0xf545('0x68')],'label':_0xd99de4[_0xf545('0x69')],'custom':!![]};}));});}})['then'](function(_0x59270b){return{'count':_0x59270b[_0xf545('0x44')],'rows':_0x59270b};})[_0xf545('0x25')](respondWithResult(_0x5a4d4a,null))[_0xf545('0x46')](function(_0xe336fd){var _0x10796f=_0x2ebc81[_0xf545('0x2d')][_0xf545('0x6a')]?0x1f4:_0xe336fd[_0xf545('0x6b')]||0x1f4;logger[_0xf545('0x27')](_0xf545('0x6c'),_0xf545('0x6d'),_0x10796f,JSON['stringify'](_0xe336fd));delete _0xe336fd['name'];if(_0x10796f===0x191){_0x10796f=0x190;}_0x5a4d4a['status'](_0x10796f)[_0xf545('0x29')](_0x2ebc81[_0xf545('0x2d')]['test']?{'message':_0xf545('0x6e'),'statusCode':_0xe336fd[_0xf545('0x6b')]}:_0xe336fd);});};