44a2d6d932903b2261ccbf7bd37cc11c1ff9f917
[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 _0x2bfc=['intersection','attributes','fields','length','hasOwnProperty','nolimit','order','sort','where','filters','merge','type','VIRTUAL','filter','includeAll','findAll','rows','catch','show','params','include','options','create','body','update','find','pick','getConfigurations','findOne','getSubjects','channel','getDescriptions','DeskConfiguration','DeskField','Subjects','Descriptions','getFields','priority','remoteUri','username','password','consumerKey','token','tokenSecret','lastIndexOf','GET','format','authType','basic','headers','Authorization','%s:%s','concat','_embedded','label','error','stringify','test','Wrong\x20credentials','statusCode','eml-format','rimraf','zip-dir','request-promise','moment','bluebird','mustache','util','path','sox','ejs','fs-extra','squel','crypto','jsforce','desk.js','to-csv','querystring','papaparse','../../components/auth/service','../../components/parsers/qs','api','../../config/environment','../../config/license/util','../../mysqldb','../../components/integrations/configuration','sendStatus','status','json','offset','limit','undefined','count','set','Content-Range','save','then','destroy','end','stack','name','send','map','DeskAccount','rawAttributes','fieldName','key','model','query','keys'];(function(_0x59f904,_0x1f7075){var _0x98fc40=function(_0x33df50){while(--_0x33df50){_0x59f904['push'](_0x59f904['shift']());}};_0x98fc40(++_0x1f7075);}(_0x2bfc,0x118));var _0xc2bf=function(_0x5bc694,_0x568ae1){_0x5bc694=_0x5bc694-0x0;var _0x22f607=_0x2bfc[_0x5bc694];return _0x22f607;};'use strict';var emlformat=require(_0xc2bf('0x0'));var rimraf=require(_0xc2bf('0x1'));var zipdir=require(_0xc2bf('0x2'));var jsonpatch=require('fast-json-patch');var rp=require(_0xc2bf('0x3'));var moment=require(_0xc2bf('0x4'));var BPromise=require(_0xc2bf('0x5'));var Mustache=require(_0xc2bf('0x6'));var util=require(_0xc2bf('0x7'));var path=require(_0xc2bf('0x8'));var sox=require(_0xc2bf('0x9'));var csv=require('to-csv');var ejs=require(_0xc2bf('0xa'));var fs=require('fs');var fs_extra=require(_0xc2bf('0xb'));var _=require('lodash');var squel=require(_0xc2bf('0xc'));var crypto=require(_0xc2bf('0xd'));var jsforce=require(_0xc2bf('0xe'));var deskjs=require(_0xc2bf('0xf'));var toCsv=require(_0xc2bf('0x10'));var querystring=require(_0xc2bf('0x11'));var Papa=require(_0xc2bf('0x12'));var Redis=require('ioredis');var authService=require(_0xc2bf('0x13'));var qs=require(_0xc2bf('0x14'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require('../../config/license/hardware');var logger=require('../../config/logger')(_0xc2bf('0x15'));var utils=require('../../config/utils');var config=require(_0xc2bf('0x16'));var licenseUtil=require(_0xc2bf('0x17'));var db=require(_0xc2bf('0x18'))['db'];var integrations=require(_0xc2bf('0x19'));function respondWithStatusCode(_0x301d4c,_0x4a3a56){_0x4a3a56=_0x4a3a56||0xcc;return function(_0x49d0a5){if(_0x49d0a5){return _0x301d4c[_0xc2bf('0x1a')](_0x4a3a56);}return _0x301d4c[_0xc2bf('0x1b')](_0x4a3a56)['end']();};}function respondWithResult(_0x3c9542,_0x253906){_0x253906=_0x253906||0xc8;return function(_0x2410e8){if(_0x2410e8){return _0x3c9542[_0xc2bf('0x1b')](_0x253906)[_0xc2bf('0x1c')](_0x2410e8);}};}function respondWithFilteredResult(_0x1b2e54,_0x4cdae2){return function(_0x1525f3){if(_0x1525f3){var _0xd8e647=typeof _0x4cdae2[_0xc2bf('0x1d')]==='undefined'&&typeof _0x4cdae2[_0xc2bf('0x1e')]===_0xc2bf('0x1f');var _0x1cf683=_0x1525f3['count'];var _0x5d4678=_0xd8e647?0x0:_0x4cdae2[_0xc2bf('0x1d')];var _0x4eee97=_0xd8e647?_0x1525f3[_0xc2bf('0x20')]:_0x4cdae2[_0xc2bf('0x1d')]+_0x4cdae2[_0xc2bf('0x1e')];var _0x16efcc;if(_0x4eee97>=_0x1cf683){_0x4eee97=_0x1cf683;_0x16efcc=0xc8;}else{_0x16efcc=0xce;}_0x1b2e54[_0xc2bf('0x1b')](_0x16efcc);return _0x1b2e54[_0xc2bf('0x21')](_0xc2bf('0x22'),_0x5d4678+'-'+_0x4eee97+'/'+_0x1cf683)[_0xc2bf('0x1c')](_0x1525f3);}return null;};}function patchUpdates(_0x1e94a2){return function(_0x6bc1ce){try{jsonpatch['apply'](_0x6bc1ce,_0x1e94a2,!![]);}catch(_0xfffae2){return BPromise['reject'](_0xfffae2);}return _0x6bc1ce[_0xc2bf('0x23')]();};}function saveUpdates(_0x2e89ad,_0x1a95f5){return function(_0x450db6){if(_0x450db6){return _0x450db6['update'](_0x2e89ad)[_0xc2bf('0x24')](function(_0x154351){return _0x154351;});}return null;};}function removeEntity(_0x5e7625,_0x57d764){return function(_0x1344a3){if(_0x1344a3){return _0x1344a3[_0xc2bf('0x25')]()[_0xc2bf('0x24')](function(){_0x5e7625['status'](0xcc)[_0xc2bf('0x26')]();});}};}function handleEntityNotFound(_0x266540,_0x3476d1){return function(_0x2cb400){if(!_0x2cb400){_0x266540[_0xc2bf('0x1a')](0x194);}return _0x2cb400;};}function handleError(_0x10d252,_0x1e5877){_0x1e5877=_0x1e5877||0x1f4;return function(_0x4a37c8){logger['error'](_0x4a37c8[_0xc2bf('0x27')]);if(_0x4a37c8[_0xc2bf('0x28')]){delete _0x4a37c8[_0xc2bf('0x28')];}_0x10d252['status'](_0x1e5877)[_0xc2bf('0x29')](_0x4a37c8);};}exports['index']=function(_0x3eeb8e,_0x582105){var _0x51c6e1={},_0x2aa3e5={},_0x9d5867={'count':0x0,'rows':[]};var _0x39fc84=_[_0xc2bf('0x2a')](db[_0xc2bf('0x2b')][_0xc2bf('0x2c')],function(_0xc375a4){return{'name':_0xc375a4[_0xc2bf('0x2d')],'type':_0xc375a4['type'][_0xc2bf('0x2e')]};});_0x2aa3e5[_0xc2bf('0x2f')]=_[_0xc2bf('0x2a')](_0x39fc84,'name');_0x2aa3e5[_0xc2bf('0x30')]=_[_0xc2bf('0x31')](_0x3eeb8e['query']);_0x2aa3e5['filters']=_[_0xc2bf('0x32')](_0x2aa3e5[_0xc2bf('0x2f')],_0x2aa3e5['query']);_0x51c6e1[_0xc2bf('0x33')]=_['intersection'](_0x2aa3e5[_0xc2bf('0x2f')],qs[_0xc2bf('0x34')](_0x3eeb8e[_0xc2bf('0x30')][_0xc2bf('0x34')]));_0x51c6e1['attributes']=_0x51c6e1['attributes'][_0xc2bf('0x35')]?_0x51c6e1[_0xc2bf('0x33')]:_0x2aa3e5['model'];if(!_0x3eeb8e[_0xc2bf('0x30')][_0xc2bf('0x36')](_0xc2bf('0x37'))){_0x51c6e1['limit']=qs[_0xc2bf('0x1e')](_0x3eeb8e[_0xc2bf('0x30')]['limit']);_0x51c6e1['offset']=qs[_0xc2bf('0x1d')](_0x3eeb8e[_0xc2bf('0x30')][_0xc2bf('0x1d')]);}_0x51c6e1[_0xc2bf('0x38')]=qs[_0xc2bf('0x39')](_0x3eeb8e[_0xc2bf('0x30')][_0xc2bf('0x39')]);_0x51c6e1[_0xc2bf('0x3a')]=qs[_0xc2bf('0x3b')](_['pick'](_0x3eeb8e['query'],_0x2aa3e5[_0xc2bf('0x3b')]),_0x39fc84);if(_0x3eeb8e['query']['filter']){_0x51c6e1['where']=_[_0xc2bf('0x3c')](_0x51c6e1[_0xc2bf('0x3a')],{'$or':_[_0xc2bf('0x2a')](_0x39fc84,function(_0x309706){if(_0x309706[_0xc2bf('0x3d')]!==_0xc2bf('0x3e')){var _0x35eef9={};_0x35eef9[_0x309706[_0xc2bf('0x28')]]={'$like':'%'+_0x3eeb8e['query'][_0xc2bf('0x3f')]+'%'};return _0x35eef9;}})});}_0x51c6e1=_[_0xc2bf('0x3c')]({},_0x51c6e1,_0x3eeb8e['options']);var _0x441cd1={'where':_0x51c6e1['where']};return db['DeskAccount'][_0xc2bf('0x20')](_0x441cd1)[_0xc2bf('0x24')](function(_0x2286eb){_0x9d5867[_0xc2bf('0x20')]=_0x2286eb;if(_0x3eeb8e[_0xc2bf('0x30')][_0xc2bf('0x40')]){_0x51c6e1['include']=[{'all':!![]}];}return db[_0xc2bf('0x2b')][_0xc2bf('0x41')](_0x51c6e1);})[_0xc2bf('0x24')](function(_0x5f424c){_0x9d5867[_0xc2bf('0x42')]=_0x5f424c;return _0x9d5867;})[_0xc2bf('0x24')](respondWithFilteredResult(_0x582105,_0x51c6e1))[_0xc2bf('0x43')](handleError(_0x582105,null));};exports[_0xc2bf('0x44')]=function(_0x4d0e38,_0xa75a5f){var _0x1ed111={'raw':![],'where':{'id':_0x4d0e38[_0xc2bf('0x45')]['id']}},_0x225d4f={};_0x225d4f[_0xc2bf('0x2f')]=_[_0xc2bf('0x31')](db[_0xc2bf('0x2b')]['rawAttributes']);_0x225d4f[_0xc2bf('0x30')]=_[_0xc2bf('0x31')](_0x4d0e38[_0xc2bf('0x30')]);_0x225d4f['filters']=_[_0xc2bf('0x32')](_0x225d4f[_0xc2bf('0x2f')],_0x225d4f[_0xc2bf('0x30')]);_0x1ed111[_0xc2bf('0x33')]=_['intersection'](_0x225d4f[_0xc2bf('0x2f')],qs['fields'](_0x4d0e38[_0xc2bf('0x30')][_0xc2bf('0x34')]));_0x1ed111['attributes']=_0x1ed111[_0xc2bf('0x33')][_0xc2bf('0x35')]?_0x1ed111[_0xc2bf('0x33')]:_0x225d4f[_0xc2bf('0x2f')];if(_0x4d0e38[_0xc2bf('0x30')][_0xc2bf('0x40')]){_0x1ed111[_0xc2bf('0x46')]=[{'all':!![]}];}_0x1ed111=_['merge']({},_0x1ed111,_0x4d0e38[_0xc2bf('0x47')]);return db[_0xc2bf('0x2b')]['find'](_0x1ed111)[_0xc2bf('0x24')](handleEntityNotFound(_0xa75a5f,null))[_0xc2bf('0x24')](respondWithResult(_0xa75a5f,null))[_0xc2bf('0x43')](handleError(_0xa75a5f,null));};exports[_0xc2bf('0x48')]=function(_0x647ab9,_0x2010cc){return db[_0xc2bf('0x2b')][_0xc2bf('0x48')](_0x647ab9[_0xc2bf('0x49')],{})[_0xc2bf('0x24')](respondWithResult(_0x2010cc,0xc9))[_0xc2bf('0x43')](handleError(_0x2010cc,null));};exports[_0xc2bf('0x4a')]=function(_0x18f258,_0x2fbc1a){if(_0x18f258['body']['id']){delete _0x18f258[_0xc2bf('0x49')]['id'];}return db['DeskAccount'][_0xc2bf('0x4b')]({'where':{'id':_0x18f258['params']['id']}})[_0xc2bf('0x24')](handleEntityNotFound(_0x2fbc1a,null))[_0xc2bf('0x24')](saveUpdates(_0x18f258[_0xc2bf('0x49')],null))[_0xc2bf('0x24')](respondWithResult(_0x2fbc1a,null))[_0xc2bf('0x43')](handleError(_0x2fbc1a,null));};exports[_0xc2bf('0x25')]=function(_0x1ce623,_0x1b40e0){return db[_0xc2bf('0x2b')][_0xc2bf('0x4b')]({'where':{'id':_0x1ce623[_0xc2bf('0x45')]['id']}})[_0xc2bf('0x24')](handleEntityNotFound(_0x1b40e0,null))[_0xc2bf('0x24')](removeEntity(_0x1b40e0,null))[_0xc2bf('0x43')](handleError(_0x1b40e0,null));};exports['getConfigurations']=function(_0x50a814,_0x44a9f7,_0x283a6e){var _0x172a3a={};var _0x44b450={};var _0x4ebf51;var _0x182674;return db[_0xc2bf('0x2b')]['findOne']({'where':{'id':_0x50a814[_0xc2bf('0x45')]['id']}})[_0xc2bf('0x24')](handleEntityNotFound(_0x44a9f7,null))['then'](function(_0x442e0c){if(_0x442e0c){_0x4ebf51=_0x442e0c;_0x44b450[_0xc2bf('0x2f')]=_[_0xc2bf('0x31')](db['DeskConfiguration']['rawAttributes']);_0x44b450[_0xc2bf('0x30')]=_[_0xc2bf('0x31')](_0x50a814[_0xc2bf('0x30')]);_0x44b450[_0xc2bf('0x3b')]=_[_0xc2bf('0x32')](_0x44b450[_0xc2bf('0x2f')],_0x44b450['query']);_0x172a3a[_0xc2bf('0x33')]=_[_0xc2bf('0x32')](_0x44b450[_0xc2bf('0x2f')],qs['fields'](_0x50a814[_0xc2bf('0x30')][_0xc2bf('0x34')]));_0x172a3a[_0xc2bf('0x33')]=_0x172a3a[_0xc2bf('0x33')][_0xc2bf('0x35')]?_0x172a3a['attributes']:_0x44b450[_0xc2bf('0x2f')];_0x172a3a[_0xc2bf('0x38')]=qs[_0xc2bf('0x39')](_0x50a814[_0xc2bf('0x30')]['sort']);_0x172a3a[_0xc2bf('0x3a')]=qs[_0xc2bf('0x3b')](_[_0xc2bf('0x4c')](_0x50a814[_0xc2bf('0x30')],_0x44b450[_0xc2bf('0x3b')]));if(_0x50a814[_0xc2bf('0x30')][_0xc2bf('0x3f')]){_0x172a3a[_0xc2bf('0x3a')]=_['merge'](_0x172a3a[_0xc2bf('0x3a')],{'$or':_[_0xc2bf('0x2a')](_0x172a3a[_0xc2bf('0x33')],function(_0x3b97ec){var _0x3ef371={};_0x3ef371[_0x3b97ec]={'$like':'%'+_0x50a814[_0xc2bf('0x30')][_0xc2bf('0x3f')]+'%'};return _0x3ef371;})});}_0x172a3a=_[_0xc2bf('0x3c')]({},_0x172a3a,_0x50a814[_0xc2bf('0x47')]);return _0x4ebf51[_0xc2bf('0x4d')](_0x172a3a);}})[_0xc2bf('0x24')](function(_0x2e28f1){if(_0x2e28f1){_0x182674=_0x2e28f1[_0xc2bf('0x35')];if(!_0x50a814[_0xc2bf('0x30')]['hasOwnProperty']('nolimit')){_0x172a3a[_0xc2bf('0x1e')]=qs[_0xc2bf('0x1e')](_0x50a814['query'][_0xc2bf('0x1e')]);_0x172a3a[_0xc2bf('0x1d')]=qs['offset'](_0x50a814[_0xc2bf('0x30')][_0xc2bf('0x1d')]);}return _0x4ebf51[_0xc2bf('0x4d')](_0x172a3a);}})[_0xc2bf('0x24')](function(_0x272df6){if(_0x272df6){return _0x272df6?{'count':_0x182674,'rows':_0x272df6}:null;}})[_0xc2bf('0x24')](respondWithResult(_0x44a9f7,null))[_0xc2bf('0x43')](handleError(_0x44a9f7,null));};exports['addConfiguration']=function(_0xbc9a32,_0x3048a4,_0x95d47a){if(_0xbc9a32[_0xc2bf('0x49')]['id']){delete _0xbc9a32[_0xc2bf('0x49')]['id'];}return db[_0xc2bf('0x2b')][_0xc2bf('0x4e')]({'where':{'id':_0xbc9a32[_0xc2bf('0x45')]['id']}})[_0xc2bf('0x24')](handleEntityNotFound(_0x3048a4,null))['then'](function(_0x56769c){if(_0x56769c){_0xbc9a32['body']['AccountId']=_0x56769c['id'];_0xbc9a32[_0xc2bf('0x49')]['Subjects']=integrations[_0xc2bf('0x4f')](_0xbc9a32[_0xc2bf('0x49')][_0xc2bf('0x50')],_0xbc9a32[_0xc2bf('0x49')][_0xc2bf('0x3d')]);_0xbc9a32['body']['Descriptions']=integrations[_0xc2bf('0x51')](_0xbc9a32[_0xc2bf('0x49')][_0xc2bf('0x50')],_0xbc9a32[_0xc2bf('0x49')][_0xc2bf('0x3d')]);return db[_0xc2bf('0x52')]['create'](_0xbc9a32['body'],{'include':[{'model':db[_0xc2bf('0x53')],'as':_0xc2bf('0x54')},{'model':db[_0xc2bf('0x53')],'as':_0xc2bf('0x55')}]});}return null;})[_0xc2bf('0x24')](respondWithResult(_0x3048a4,null))[_0xc2bf('0x43')](handleError(_0x3048a4,null));};exports[_0xc2bf('0x56')]=function(_0x2207ee,_0x2c76a1,_0x458030){var _0x41e182=[{'name':_0xc2bf('0x57'),'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 _0x386dcc='';return db[_0xc2bf('0x2b')]['findOne']({'where':{'id':_0x2207ee[_0xc2bf('0x45')]['id']},'attributes':['id',_0xc2bf('0x58'),'authType',_0xc2bf('0x59'),_0xc2bf('0x5a'),_0xc2bf('0x5b'),'consumerSecret',_0xc2bf('0x5c'),_0xc2bf('0x5d')]})[_0xc2bf('0x24')](handleEntityNotFound(_0x2c76a1,null))[_0xc2bf('0x24')](function(_0x17f24a){if(_0x17f24a){_0x386dcc=_0x17f24a['remoteUri'];var _0x4a1836=_0x386dcc['slice'](-0x1);if(_0x4a1836==='/'){_0x386dcc=_0x386dcc['substring'](0x0,_0x386dcc[_0xc2bf('0x5e')](_0x4a1836));}var _0x3b4d44={'method':_0xc2bf('0x5f'),'uri':util[_0xc2bf('0x60')]('%s/custom_fields',_0x386dcc),'headers':{'Content-Type':'application/json','Accept':'application/json'},'json':!![]};if(_0x17f24a[_0xc2bf('0x61')]===_0xc2bf('0x62')){_0x3b4d44[_0xc2bf('0x63')][_0xc2bf('0x64')]=util[_0xc2bf('0x60')]('Basic\x20%s',new Buffer(util[_0xc2bf('0x60')](_0xc2bf('0x65'),_0x17f24a[_0xc2bf('0x59')],_0x17f24a[_0xc2bf('0x5a')]))['toString']('base64'));}return rp(_0x3b4d44)[_0xc2bf('0x24')](function(_0x55c7fc){return _0x41e182[_0xc2bf('0x66')](_[_0xc2bf('0x2a')](_0x55c7fc[_0xc2bf('0x67')]['entries'],function(_0x229b0d){return{'id':_0x229b0d[_0xc2bf('0x28')],'name':_0x229b0d[_0xc2bf('0x28')],'type':_0x229b0d[_0xc2bf('0x3d')],'active':_0x229b0d['active'],'label':_0x229b0d[_0xc2bf('0x68')],'custom':!![]};}));});}})['then'](function(_0x1e615b){return{'count':_0x1e615b[_0xc2bf('0x35')],'rows':_0x1e615b};})['then'](respondWithResult(_0x2c76a1,null))[_0xc2bf('0x43')](function(_0x1312b3){var _0x2a1608=_0x2207ee['query']['test']?0x1f4:_0x1312b3['statusCode']||0x1f4;logger[_0xc2bf('0x69')]('getFields,\x20%s,\x20%s,\x20%s','getFields',_0x2a1608,JSON[_0xc2bf('0x6a')](_0x1312b3));delete _0x1312b3['name'];if(_0x2a1608===0x191){_0x2a1608=0x190;}_0x2c76a1[_0xc2bf('0x1b')](_0x2a1608)[_0xc2bf('0x29')](_0x2207ee[_0xc2bf('0x30')][_0xc2bf('0x6b')]?{'message':_0xc2bf('0x6c'),'statusCode':_0x1312b3[_0xc2bf('0x6d')]}:_0x1312b3);});};