7b7bdc93f49f2e6b0a7512e3ec5757ba3877c315
[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 _0x261f=['moment','bluebird','mustache','util','path','sox','fs-extra','lodash','squel','crypto','jsforce','desk.js','to-csv','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','../../components/integrations/configuration','sendStatus','status','end','json','undefined','count','offset','limit','set','apply','reject','save','update','then','destroy','error','stack','name','send','map','DeskAccount','type','key','model','filters','query','attributes','intersection','fields','length','sort','where','pick','filter','merge','VIRTUAL','options','includeAll','findAll','rows','catch','params','keys','include','find','create','body','getConfigurations','findOne','rawAttributes','order','hasOwnProperty','nolimit','AccountId','getSubjects','Descriptions','getDescriptions','channel','DeskConfiguration','DeskField','Subjects','getFields','priority','username','password','consumerKey','consumerSecret','token','tokenSecret','remoteUri','slice','substring','lastIndexOf','format','%s/custom_fields','application/json','authType','basic','headers','Authorization','Basic\x20%s','%s:%s','toString','base64','concat','_embedded','entries','active','label','statusCode','getFields,\x20%s,\x20%s,\x20%s','stringify','Wrong\x20credentials','eml-format','rimraf','zip-dir','fast-json-patch','request-promise'];(function(_0x3dd15e,_0x1f0015){var _0x231fd0=function(_0x4f680a){while(--_0x4f680a){_0x3dd15e['push'](_0x3dd15e['shift']());}};_0x231fd0(++_0x1f0015);}(_0x261f,0x16c));var _0xf261=function(_0xa29d43,_0x28870d){_0xa29d43=_0xa29d43-0x0;var _0x4ce7ec=_0x261f[_0xa29d43];return _0x4ce7ec;};'use strict';var emlformat=require(_0xf261('0x0'));var rimraf=require(_0xf261('0x1'));var zipdir=require(_0xf261('0x2'));var jsonpatch=require(_0xf261('0x3'));var rp=require(_0xf261('0x4'));var moment=require(_0xf261('0x5'));var BPromise=require(_0xf261('0x6'));var Mustache=require(_0xf261('0x7'));var util=require(_0xf261('0x8'));var path=require(_0xf261('0x9'));var sox=require(_0xf261('0xa'));var csv=require('to-csv');var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0xf261('0xb'));var _=require(_0xf261('0xc'));var squel=require(_0xf261('0xd'));var crypto=require(_0xf261('0xe'));var jsforce=require(_0xf261('0xf'));var deskjs=require(_0xf261('0x10'));var toCsv=require(_0xf261('0x11'));var querystring=require('querystring');var Papa=require(_0xf261('0x12'));var Redis=require(_0xf261('0x13'));var authService=require(_0xf261('0x14'));var qs=require(_0xf261('0x15'));var as=require(_0xf261('0x16'));var hardwareService=require('../../config/license/hardware');var logger=require(_0xf261('0x17'))(_0xf261('0x18'));var utils=require(_0xf261('0x19'));var config=require(_0xf261('0x1a'));var licenseUtil=require(_0xf261('0x1b'));var db=require(_0xf261('0x1c'))['db'];var integrations=require(_0xf261('0x1d'));function respondWithStatusCode(_0xd44e61,_0x281c06){_0x281c06=_0x281c06||0xcc;return function(_0x4ad0cd){if(_0x4ad0cd){return _0xd44e61[_0xf261('0x1e')](_0x281c06);}return _0xd44e61[_0xf261('0x1f')](_0x281c06)[_0xf261('0x20')]();};}function respondWithResult(_0x346e49,_0x58883c){_0x58883c=_0x58883c||0xc8;return function(_0x1a0310){if(_0x1a0310){return _0x346e49['status'](_0x58883c)[_0xf261('0x21')](_0x1a0310);}};}function respondWithFilteredResult(_0x937ac6,_0x1e7bca){return function(_0xb541a2){if(_0xb541a2){var _0xbbd804=typeof _0x1e7bca['offset']===_0xf261('0x22')&&typeof _0x1e7bca['limit']===_0xf261('0x22');var _0x438808=_0xb541a2[_0xf261('0x23')];var _0x2549c8=_0xbbd804?0x0:_0x1e7bca[_0xf261('0x24')];var _0x4d7ed6=_0xbbd804?_0xb541a2[_0xf261('0x23')]:_0x1e7bca[_0xf261('0x24')]+_0x1e7bca[_0xf261('0x25')];var _0x15d1e0;if(_0x4d7ed6>=_0x438808){_0x4d7ed6=_0x438808;_0x15d1e0=0xc8;}else{_0x15d1e0=0xce;}_0x937ac6[_0xf261('0x1f')](_0x15d1e0);return _0x937ac6[_0xf261('0x26')]('Content-Range',_0x2549c8+'-'+_0x4d7ed6+'/'+_0x438808)[_0xf261('0x21')](_0xb541a2);}return null;};}function patchUpdates(_0x483d70){return function(_0x569ea1){try{jsonpatch[_0xf261('0x27')](_0x569ea1,_0x483d70,!![]);}catch(_0x2e4e20){return BPromise[_0xf261('0x28')](_0x2e4e20);}return _0x569ea1[_0xf261('0x29')]();};}function saveUpdates(_0x1a3c23,_0xbb37b){return function(_0x523c1a){if(_0x523c1a){return _0x523c1a[_0xf261('0x2a')](_0x1a3c23)[_0xf261('0x2b')](function(_0x482993){return _0x482993;});}return null;};}function removeEntity(_0x53c8ad,_0x183b9a){return function(_0x29f6aa){if(_0x29f6aa){return _0x29f6aa[_0xf261('0x2c')]()[_0xf261('0x2b')](function(){_0x53c8ad[_0xf261('0x1f')](0xcc)[_0xf261('0x20')]();});}};}function handleEntityNotFound(_0x1d402c,_0x29335d){return function(_0x1f961b){if(!_0x1f961b){_0x1d402c['sendStatus'](0x194);}return _0x1f961b;};}function handleError(_0x542cc1,_0x22e0c1){_0x22e0c1=_0x22e0c1||0x1f4;return function(_0x33992c){logger[_0xf261('0x2d')](_0x33992c[_0xf261('0x2e')]);if(_0x33992c[_0xf261('0x2f')]){delete _0x33992c[_0xf261('0x2f')];}_0x542cc1[_0xf261('0x1f')](_0x22e0c1)[_0xf261('0x30')](_0x33992c);};}exports['index']=function(_0x32d303,_0x3552ec){var _0x54b07a={},_0x3a60a8={},_0x2a7710={'count':0x0,'rows':[]};var _0x20e2f6=_[_0xf261('0x31')](db[_0xf261('0x32')]['rawAttributes'],function(_0x2a25d1){return{'name':_0x2a25d1['fieldName'],'type':_0x2a25d1[_0xf261('0x33')][_0xf261('0x34')]};});_0x3a60a8[_0xf261('0x35')]=_[_0xf261('0x31')](_0x20e2f6,_0xf261('0x2f'));_0x3a60a8['query']=_['keys'](_0x32d303['query']);_0x3a60a8[_0xf261('0x36')]=_['intersection'](_0x3a60a8['model'],_0x3a60a8[_0xf261('0x37')]);_0x54b07a[_0xf261('0x38')]=_[_0xf261('0x39')](_0x3a60a8['model'],qs[_0xf261('0x3a')](_0x32d303[_0xf261('0x37')][_0xf261('0x3a')]));_0x54b07a['attributes']=_0x54b07a[_0xf261('0x38')][_0xf261('0x3b')]?_0x54b07a[_0xf261('0x38')]:_0x3a60a8['model'];if(!_0x32d303[_0xf261('0x37')]['hasOwnProperty']('nolimit')){_0x54b07a[_0xf261('0x25')]=qs[_0xf261('0x25')](_0x32d303['query'][_0xf261('0x25')]);_0x54b07a[_0xf261('0x24')]=qs[_0xf261('0x24')](_0x32d303[_0xf261('0x37')][_0xf261('0x24')]);}_0x54b07a['order']=qs[_0xf261('0x3c')](_0x32d303[_0xf261('0x37')][_0xf261('0x3c')]);_0x54b07a[_0xf261('0x3d')]=qs[_0xf261('0x36')](_[_0xf261('0x3e')](_0x32d303[_0xf261('0x37')],_0x3a60a8[_0xf261('0x36')]),_0x20e2f6);if(_0x32d303[_0xf261('0x37')][_0xf261('0x3f')]){_0x54b07a['where']=_[_0xf261('0x40')](_0x54b07a['where'],{'$or':_[_0xf261('0x31')](_0x20e2f6,function(_0x49a533){if(_0x49a533['type']!==_0xf261('0x41')){var _0x2f0364={};_0x2f0364[_0x49a533[_0xf261('0x2f')]]={'$like':'%'+_0x32d303[_0xf261('0x37')][_0xf261('0x3f')]+'%'};return _0x2f0364;}})});}_0x54b07a=_[_0xf261('0x40')]({},_0x54b07a,_0x32d303[_0xf261('0x42')]);var _0x471c96={'where':_0x54b07a['where']};return db[_0xf261('0x32')][_0xf261('0x23')](_0x471c96)[_0xf261('0x2b')](function(_0x4e4c35){_0x2a7710[_0xf261('0x23')]=_0x4e4c35;if(_0x32d303[_0xf261('0x37')][_0xf261('0x43')]){_0x54b07a['include']=[{'all':!![]}];}return db[_0xf261('0x32')][_0xf261('0x44')](_0x54b07a);})['then'](function(_0x580dd2){_0x2a7710[_0xf261('0x45')]=_0x580dd2;return _0x2a7710;})[_0xf261('0x2b')](respondWithFilteredResult(_0x3552ec,_0x54b07a))[_0xf261('0x46')](handleError(_0x3552ec,null));};exports['show']=function(_0x117b98,_0x3b3320){var _0x55cb55={'raw':![],'where':{'id':_0x117b98[_0xf261('0x47')]['id']}},_0x82b83c={};_0x82b83c[_0xf261('0x35')]=_['keys'](db[_0xf261('0x32')]['rawAttributes']);_0x82b83c[_0xf261('0x37')]=_[_0xf261('0x48')](_0x117b98['query']);_0x82b83c[_0xf261('0x36')]=_[_0xf261('0x39')](_0x82b83c[_0xf261('0x35')],_0x82b83c[_0xf261('0x37')]);_0x55cb55[_0xf261('0x38')]=_[_0xf261('0x39')](_0x82b83c['model'],qs[_0xf261('0x3a')](_0x117b98[_0xf261('0x37')]['fields']));_0x55cb55['attributes']=_0x55cb55['attributes'][_0xf261('0x3b')]?_0x55cb55[_0xf261('0x38')]:_0x82b83c[_0xf261('0x35')];if(_0x117b98[_0xf261('0x37')][_0xf261('0x43')]){_0x55cb55[_0xf261('0x49')]=[{'all':!![]}];}_0x55cb55=_['merge']({},_0x55cb55,_0x117b98['options']);return db[_0xf261('0x32')][_0xf261('0x4a')](_0x55cb55)[_0xf261('0x2b')](handleEntityNotFound(_0x3b3320,null))[_0xf261('0x2b')](respondWithResult(_0x3b3320,null))['catch'](handleError(_0x3b3320,null));};exports[_0xf261('0x4b')]=function(_0x391d2f,_0x1f2995){return db[_0xf261('0x32')][_0xf261('0x4b')](_0x391d2f[_0xf261('0x4c')],{})[_0xf261('0x2b')](respondWithResult(_0x1f2995,0xc9))[_0xf261('0x46')](handleError(_0x1f2995,null));};exports[_0xf261('0x2a')]=function(_0x6db107,_0x4a4f7e){if(_0x6db107[_0xf261('0x4c')]['id']){delete _0x6db107[_0xf261('0x4c')]['id'];}return db[_0xf261('0x32')]['find']({'where':{'id':_0x6db107['params']['id']}})[_0xf261('0x2b')](handleEntityNotFound(_0x4a4f7e,null))[_0xf261('0x2b')](saveUpdates(_0x6db107[_0xf261('0x4c')],null))[_0xf261('0x2b')](respondWithResult(_0x4a4f7e,null))[_0xf261('0x46')](handleError(_0x4a4f7e,null));};exports['destroy']=function(_0x157c60,_0x46c72c){return db[_0xf261('0x32')][_0xf261('0x4a')]({'where':{'id':_0x157c60[_0xf261('0x47')]['id']}})[_0xf261('0x2b')](handleEntityNotFound(_0x46c72c,null))[_0xf261('0x2b')](removeEntity(_0x46c72c,null))[_0xf261('0x46')](handleError(_0x46c72c,null));};exports[_0xf261('0x4d')]=function(_0x2ae079,_0x390cc2,_0x13dd1f){var _0x54b457={};var _0x4cb060={};var _0x18c6bc;var _0x309912;return db[_0xf261('0x32')][_0xf261('0x4e')]({'where':{'id':_0x2ae079[_0xf261('0x47')]['id']}})[_0xf261('0x2b')](handleEntityNotFound(_0x390cc2,null))[_0xf261('0x2b')](function(_0x4f54a4){if(_0x4f54a4){_0x18c6bc=_0x4f54a4;_0x4cb060[_0xf261('0x35')]=_[_0xf261('0x48')](db['DeskConfiguration'][_0xf261('0x4f')]);_0x4cb060['query']=_[_0xf261('0x48')](_0x2ae079[_0xf261('0x37')]);_0x4cb060[_0xf261('0x36')]=_[_0xf261('0x39')](_0x4cb060[_0xf261('0x35')],_0x4cb060['query']);_0x54b457['attributes']=_[_0xf261('0x39')](_0x4cb060[_0xf261('0x35')],qs[_0xf261('0x3a')](_0x2ae079[_0xf261('0x37')]['fields']));_0x54b457[_0xf261('0x38')]=_0x54b457[_0xf261('0x38')]['length']?_0x54b457[_0xf261('0x38')]:_0x4cb060['model'];_0x54b457[_0xf261('0x50')]=qs[_0xf261('0x3c')](_0x2ae079['query'][_0xf261('0x3c')]);_0x54b457[_0xf261('0x3d')]=qs[_0xf261('0x36')](_['pick'](_0x2ae079['query'],_0x4cb060[_0xf261('0x36')]));if(_0x2ae079['query'][_0xf261('0x3f')]){_0x54b457[_0xf261('0x3d')]=_[_0xf261('0x40')](_0x54b457[_0xf261('0x3d')],{'$or':_[_0xf261('0x31')](_0x54b457['attributes'],function(_0x3c8b76){var _0x85e204={};_0x85e204[_0x3c8b76]={'$like':'%'+_0x2ae079['query'][_0xf261('0x3f')]+'%'};return _0x85e204;})});}_0x54b457=_[_0xf261('0x40')]({},_0x54b457,_0x2ae079[_0xf261('0x42')]);return _0x18c6bc[_0xf261('0x4d')](_0x54b457);}})[_0xf261('0x2b')](function(_0x28f0e8){if(_0x28f0e8){_0x309912=_0x28f0e8['length'];if(!_0x2ae079[_0xf261('0x37')][_0xf261('0x51')](_0xf261('0x52'))){_0x54b457[_0xf261('0x25')]=qs[_0xf261('0x25')](_0x2ae079[_0xf261('0x37')][_0xf261('0x25')]);_0x54b457[_0xf261('0x24')]=qs[_0xf261('0x24')](_0x2ae079[_0xf261('0x37')][_0xf261('0x24')]);}return _0x18c6bc[_0xf261('0x4d')](_0x54b457);}})[_0xf261('0x2b')](function(_0x47c354){if(_0x47c354){return _0x47c354?{'count':_0x309912,'rows':_0x47c354}:null;}})[_0xf261('0x2b')](respondWithResult(_0x390cc2,null))['catch'](handleError(_0x390cc2,null));};exports['addConfiguration']=function(_0x1853c0,_0xd5dd65,_0x555bf5){if(_0x1853c0[_0xf261('0x4c')]['id']){delete _0x1853c0[_0xf261('0x4c')]['id'];}return db[_0xf261('0x32')][_0xf261('0x4e')]({'where':{'id':_0x1853c0[_0xf261('0x47')]['id']}})[_0xf261('0x2b')](handleEntityNotFound(_0xd5dd65,null))[_0xf261('0x2b')](function(_0x29ca97){if(_0x29ca97){_0x1853c0[_0xf261('0x4c')][_0xf261('0x53')]=_0x29ca97['id'];_0x1853c0[_0xf261('0x4c')]['Subjects']=integrations[_0xf261('0x54')](_0x1853c0[_0xf261('0x4c')]['channel'],_0x1853c0[_0xf261('0x4c')][_0xf261('0x33')]);_0x1853c0['body'][_0xf261('0x55')]=integrations[_0xf261('0x56')](_0x1853c0[_0xf261('0x4c')][_0xf261('0x57')],_0x1853c0[_0xf261('0x4c')]['type']);return db[_0xf261('0x58')][_0xf261('0x4b')](_0x1853c0[_0xf261('0x4c')],{'include':[{'model':db[_0xf261('0x59')],'as':_0xf261('0x5a')},{'model':db['DeskField'],'as':_0xf261('0x55')}]});}return null;})['then'](respondWithResult(_0xd5dd65,null))[_0xf261('0x46')](handleError(_0xd5dd65,null));};exports[_0xf261('0x5b')]=function(_0x5537e9,_0x167134,_0x3e1ed6){var _0x3e662d=[{'name':'priority','label':_0xf261('0x5c'),'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 _0x3eadba='';return db[_0xf261('0x32')][_0xf261('0x4e')]({'where':{'id':_0x5537e9['params']['id']},'attributes':['id','remoteUri','authType',_0xf261('0x5d'),_0xf261('0x5e'),_0xf261('0x5f'),_0xf261('0x60'),_0xf261('0x61'),_0xf261('0x62')]})[_0xf261('0x2b')](handleEntityNotFound(_0x167134,null))[_0xf261('0x2b')](function(_0x174555){if(_0x174555){_0x3eadba=_0x174555[_0xf261('0x63')];var _0x13e629=_0x3eadba[_0xf261('0x64')](-0x1);if(_0x13e629==='/'){_0x3eadba=_0x3eadba[_0xf261('0x65')](0x0,_0x3eadba[_0xf261('0x66')](_0x13e629));}var _0x842f64={'method':'GET','uri':util[_0xf261('0x67')](_0xf261('0x68'),_0x3eadba),'headers':{'Content-Type':_0xf261('0x69'),'Accept':'application/json'},'json':!![]};if(_0x174555[_0xf261('0x6a')]===_0xf261('0x6b')){_0x842f64[_0xf261('0x6c')][_0xf261('0x6d')]=util[_0xf261('0x67')](_0xf261('0x6e'),new Buffer(util[_0xf261('0x67')](_0xf261('0x6f'),_0x174555[_0xf261('0x5d')],_0x174555[_0xf261('0x5e')]))[_0xf261('0x70')](_0xf261('0x71')));}return rp(_0x842f64)['then'](function(_0x1ee01b){return _0x3e662d[_0xf261('0x72')](_['map'](_0x1ee01b[_0xf261('0x73')][_0xf261('0x74')],function(_0x52aa4d){return{'id':_0x52aa4d[_0xf261('0x2f')],'name':_0x52aa4d[_0xf261('0x2f')],'type':_0x52aa4d['type'],'active':_0x52aa4d[_0xf261('0x75')],'label':_0x52aa4d[_0xf261('0x76')],'custom':!![]};}));});}})[_0xf261('0x2b')](function(_0x14489c){return{'count':_0x14489c[_0xf261('0x3b')],'rows':_0x14489c};})[_0xf261('0x2b')](respondWithResult(_0x167134,null))['catch'](function(_0x473a4b){var _0x3d9bdc=_0x5537e9[_0xf261('0x37')]['test']?0x1f4:_0x473a4b[_0xf261('0x77')]||0x1f4;logger[_0xf261('0x2d')](_0xf261('0x78'),'getFields',_0x3d9bdc,JSON[_0xf261('0x79')](_0x473a4b));delete _0x473a4b['name'];if(_0x3d9bdc===0x191){_0x3d9bdc=0x190;}_0x167134['status'](_0x3d9bdc)['send'](_0x5537e9[_0xf261('0x37')]['test']?{'message':_0xf261('0x7a'),'statusCode':_0x473a4b[_0xf261('0x77')]}:_0x473a4b);});};