Built motion from commit 994c64e1.|2.6.1
[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 _0xf0c9=['map','rawAttributes','fieldName','type','key','model','query','keys','filters','attributes','fields','length','hasOwnProperty','nolimit','order','sort','pick','filter','where','VIRTUAL','merge','options','DeskAccount','count','includeAll','include','findAll','catch','show','params','intersection','find','create','body','getConfigurations','findOne','DeskConfiguration','addConfiguration','AccountId','Subjects','channel','Descriptions','DeskField','getFields','priority','remoteUri','authType','consumerKey','consumerSecret','token','tokenSecret','slice','substring','GET','%s/custom_fields','application/json','basic','format','%s:%s','base64','concat','_embedded','entries','active','label','test','getFields,\x20%s,\x20%s,\x20%s','stringify','Wrong\x20credentials','statusCode','eml-format','rimraf','fast-json-patch','moment','bluebird','mustache','util','fs-extra','lodash','squel','crypto','jsforce','desk.js','to-csv','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','../../components/integrations/configuration','status','end','offset','undefined','limit','set','json','reject','save','update','then','destroy','sendStatus','error','stack','name','send'];(function(_0x23ccce,_0xa7fc36){var _0x52a318=function(_0x87ccc){while(--_0x87ccc){_0x23ccce['push'](_0x23ccce['shift']());}};_0x52a318(++_0xa7fc36);}(_0xf0c9,0xb8));var _0x9f0c=function(_0x492535,_0x46524a){_0x492535=_0x492535-0x0;var _0x256620=_0xf0c9[_0x492535];return _0x256620;};'use strict';var emlformat=require(_0x9f0c('0x0'));var rimraf=require(_0x9f0c('0x1'));var zipdir=require('zip-dir');var jsonpatch=require(_0x9f0c('0x2'));var rp=require('request-promise');var moment=require(_0x9f0c('0x3'));var BPromise=require(_0x9f0c('0x4'));var Mustache=require(_0x9f0c('0x5'));var util=require(_0x9f0c('0x6'));var path=require('path');var sox=require('sox');var csv=require('to-csv');var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0x9f0c('0x7'));var _=require(_0x9f0c('0x8'));var squel=require(_0x9f0c('0x9'));var crypto=require(_0x9f0c('0xa'));var jsforce=require(_0x9f0c('0xb'));var deskjs=require(_0x9f0c('0xc'));var toCsv=require(_0x9f0c('0xd'));var querystring=require(_0x9f0c('0xe'));var Papa=require(_0x9f0c('0xf'));var Redis=require(_0x9f0c('0x10'));var authService=require(_0x9f0c('0x11'));var qs=require(_0x9f0c('0x12'));var as=require(_0x9f0c('0x13'));var hardwareService=require(_0x9f0c('0x14'));var logger=require(_0x9f0c('0x15'))('api');var utils=require(_0x9f0c('0x16'));var config=require(_0x9f0c('0x17'));var licenseUtil=require(_0x9f0c('0x18'));var db=require(_0x9f0c('0x19'))['db'];var integrations=require(_0x9f0c('0x1a'));function respondWithStatusCode(_0x456b95,_0xbd653f){_0xbd653f=_0xbd653f||0xcc;return function(_0x9c2e0d){if(_0x9c2e0d){return _0x456b95['sendStatus'](_0xbd653f);}return _0x456b95[_0x9f0c('0x1b')](_0xbd653f)[_0x9f0c('0x1c')]();};}function respondWithResult(_0x1ab6ee,_0x2f2fef){_0x2f2fef=_0x2f2fef||0xc8;return function(_0x5e414f){if(_0x5e414f){return _0x1ab6ee['status'](_0x2f2fef)['json'](_0x5e414f);}};}function respondWithFilteredResult(_0x542a33,_0x55c4a7){return function(_0x51788e){if(_0x51788e){var _0x5809cc=typeof _0x55c4a7[_0x9f0c('0x1d')]===_0x9f0c('0x1e')&&typeof _0x55c4a7['limit']===_0x9f0c('0x1e');var _0x57bd8c=_0x51788e['count'];var _0x5177cd=_0x5809cc?0x0:_0x55c4a7[_0x9f0c('0x1d')];var _0x40709e=_0x5809cc?_0x51788e['count']:_0x55c4a7['offset']+_0x55c4a7[_0x9f0c('0x1f')];var _0x4300e0;if(_0x40709e>=_0x57bd8c){_0x40709e=_0x57bd8c;_0x4300e0=0xc8;}else{_0x4300e0=0xce;}_0x542a33[_0x9f0c('0x1b')](_0x4300e0);return _0x542a33[_0x9f0c('0x20')]('Content-Range',_0x5177cd+'-'+_0x40709e+'/'+_0x57bd8c)[_0x9f0c('0x21')](_0x51788e);}return null;};}function patchUpdates(_0x1862f1){return function(_0x13cf04){try{jsonpatch['apply'](_0x13cf04,_0x1862f1,!![]);}catch(_0x60bad0){return BPromise[_0x9f0c('0x22')](_0x60bad0);}return _0x13cf04[_0x9f0c('0x23')]();};}function saveUpdates(_0x136323,_0x55149b){return function(_0x5e08fa){if(_0x5e08fa){return _0x5e08fa[_0x9f0c('0x24')](_0x136323)[_0x9f0c('0x25')](function(_0x458ea9){return _0x458ea9;});}return null;};}function removeEntity(_0x4ecd11,_0x48cdd6){return function(_0x405e0d){if(_0x405e0d){return _0x405e0d[_0x9f0c('0x26')]()[_0x9f0c('0x25')](function(){_0x4ecd11[_0x9f0c('0x1b')](0xcc)[_0x9f0c('0x1c')]();});}};}function handleEntityNotFound(_0x3dc846,_0x591dfd){return function(_0x146f99){if(!_0x146f99){_0x3dc846[_0x9f0c('0x27')](0x194);}return _0x146f99;};}function handleError(_0x2db1f4,_0x8b9984){_0x8b9984=_0x8b9984||0x1f4;return function(_0x10fc2f){logger[_0x9f0c('0x28')](_0x10fc2f[_0x9f0c('0x29')]);if(_0x10fc2f[_0x9f0c('0x2a')]){delete _0x10fc2f[_0x9f0c('0x2a')];}_0x2db1f4[_0x9f0c('0x1b')](_0x8b9984)[_0x9f0c('0x2b')](_0x10fc2f);};}exports['index']=function(_0x266179,_0x4c27b3){var _0x28b9ce={},_0x4f03c1={},_0x2e7933={'count':0x0,'rows':[]};var _0x5b0205=_[_0x9f0c('0x2c')](db['DeskAccount'][_0x9f0c('0x2d')],function(_0x596249){return{'name':_0x596249[_0x9f0c('0x2e')],'type':_0x596249[_0x9f0c('0x2f')][_0x9f0c('0x30')]};});_0x4f03c1[_0x9f0c('0x31')]=_[_0x9f0c('0x2c')](_0x5b0205,_0x9f0c('0x2a'));_0x4f03c1[_0x9f0c('0x32')]=_[_0x9f0c('0x33')](_0x266179['query']);_0x4f03c1[_0x9f0c('0x34')]=_['intersection'](_0x4f03c1['model'],_0x4f03c1[_0x9f0c('0x32')]);_0x28b9ce[_0x9f0c('0x35')]=_['intersection'](_0x4f03c1['model'],qs[_0x9f0c('0x36')](_0x266179[_0x9f0c('0x32')]['fields']));_0x28b9ce[_0x9f0c('0x35')]=_0x28b9ce[_0x9f0c('0x35')][_0x9f0c('0x37')]?_0x28b9ce[_0x9f0c('0x35')]:_0x4f03c1[_0x9f0c('0x31')];if(!_0x266179[_0x9f0c('0x32')][_0x9f0c('0x38')](_0x9f0c('0x39'))){_0x28b9ce[_0x9f0c('0x1f')]=qs[_0x9f0c('0x1f')](_0x266179['query'][_0x9f0c('0x1f')]);_0x28b9ce[_0x9f0c('0x1d')]=qs[_0x9f0c('0x1d')](_0x266179[_0x9f0c('0x32')][_0x9f0c('0x1d')]);}_0x28b9ce[_0x9f0c('0x3a')]=qs[_0x9f0c('0x3b')](_0x266179[_0x9f0c('0x32')]['sort']);_0x28b9ce['where']=qs['filters'](_[_0x9f0c('0x3c')](_0x266179[_0x9f0c('0x32')],_0x4f03c1[_0x9f0c('0x34')]),_0x5b0205);if(_0x266179[_0x9f0c('0x32')][_0x9f0c('0x3d')]){_0x28b9ce[_0x9f0c('0x3e')]=_['merge'](_0x28b9ce['where'],{'$or':_['map'](_0x5b0205,function(_0x29e936){if(_0x29e936['type']!==_0x9f0c('0x3f')){var _0x333985={};_0x333985[_0x29e936[_0x9f0c('0x2a')]]={'$like':'%'+_0x266179[_0x9f0c('0x32')][_0x9f0c('0x3d')]+'%'};return _0x333985;}})});}_0x28b9ce=_[_0x9f0c('0x40')]({},_0x28b9ce,_0x266179[_0x9f0c('0x41')]);var _0x451c65={'where':_0x28b9ce[_0x9f0c('0x3e')]};return db[_0x9f0c('0x42')][_0x9f0c('0x43')](_0x451c65)[_0x9f0c('0x25')](function(_0x54caf3){_0x2e7933[_0x9f0c('0x43')]=_0x54caf3;if(_0x266179['query'][_0x9f0c('0x44')]){_0x28b9ce[_0x9f0c('0x45')]=[{'all':!![]}];}return db[_0x9f0c('0x42')][_0x9f0c('0x46')](_0x28b9ce);})[_0x9f0c('0x25')](function(_0x409a77){_0x2e7933['rows']=_0x409a77;return _0x2e7933;})[_0x9f0c('0x25')](respondWithFilteredResult(_0x4c27b3,_0x28b9ce))[_0x9f0c('0x47')](handleError(_0x4c27b3,null));};exports[_0x9f0c('0x48')]=function(_0x31a535,_0x1e2488){var _0x561d0b={'raw':![],'where':{'id':_0x31a535[_0x9f0c('0x49')]['id']}},_0x58778c={};_0x58778c['model']=_[_0x9f0c('0x33')](db[_0x9f0c('0x42')][_0x9f0c('0x2d')]);_0x58778c['query']=_[_0x9f0c('0x33')](_0x31a535['query']);_0x58778c[_0x9f0c('0x34')]=_['intersection'](_0x58778c[_0x9f0c('0x31')],_0x58778c[_0x9f0c('0x32')]);_0x561d0b[_0x9f0c('0x35')]=_[_0x9f0c('0x4a')](_0x58778c[_0x9f0c('0x31')],qs['fields'](_0x31a535[_0x9f0c('0x32')][_0x9f0c('0x36')]));_0x561d0b[_0x9f0c('0x35')]=_0x561d0b[_0x9f0c('0x35')][_0x9f0c('0x37')]?_0x561d0b[_0x9f0c('0x35')]:_0x58778c['model'];if(_0x31a535[_0x9f0c('0x32')][_0x9f0c('0x44')]){_0x561d0b[_0x9f0c('0x45')]=[{'all':!![]}];}_0x561d0b=_[_0x9f0c('0x40')]({},_0x561d0b,_0x31a535[_0x9f0c('0x41')]);return db[_0x9f0c('0x42')][_0x9f0c('0x4b')](_0x561d0b)[_0x9f0c('0x25')](handleEntityNotFound(_0x1e2488,null))[_0x9f0c('0x25')](respondWithResult(_0x1e2488,null))['catch'](handleError(_0x1e2488,null));};exports[_0x9f0c('0x4c')]=function(_0x3c6160,_0x5e5076){return db[_0x9f0c('0x42')][_0x9f0c('0x4c')](_0x3c6160[_0x9f0c('0x4d')],{})['then'](respondWithResult(_0x5e5076,0xc9))['catch'](handleError(_0x5e5076,null));};exports['update']=function(_0x3ff5bf,_0x12e33b){if(_0x3ff5bf[_0x9f0c('0x4d')]['id']){delete _0x3ff5bf[_0x9f0c('0x4d')]['id'];}return db[_0x9f0c('0x42')][_0x9f0c('0x4b')]({'where':{'id':_0x3ff5bf['params']['id']}})[_0x9f0c('0x25')](handleEntityNotFound(_0x12e33b,null))[_0x9f0c('0x25')](saveUpdates(_0x3ff5bf['body'],null))[_0x9f0c('0x25')](respondWithResult(_0x12e33b,null))[_0x9f0c('0x47')](handleError(_0x12e33b,null));};exports[_0x9f0c('0x26')]=function(_0x231b84,_0x343eaa){return db[_0x9f0c('0x42')][_0x9f0c('0x4b')]({'where':{'id':_0x231b84[_0x9f0c('0x49')]['id']}})[_0x9f0c('0x25')](handleEntityNotFound(_0x343eaa,null))['then'](removeEntity(_0x343eaa,null))['catch'](handleError(_0x343eaa,null));};exports[_0x9f0c('0x4e')]=function(_0x42586a,_0x31f2ef,_0x270f41){var _0x5ee7de={};var _0x212fb8={};var _0x2c9818;var _0x5b9520;return db[_0x9f0c('0x42')][_0x9f0c('0x4f')]({'where':{'id':_0x42586a['params']['id']}})[_0x9f0c('0x25')](handleEntityNotFound(_0x31f2ef,null))[_0x9f0c('0x25')](function(_0x1a0a81){if(_0x1a0a81){_0x2c9818=_0x1a0a81;_0x212fb8[_0x9f0c('0x31')]=_[_0x9f0c('0x33')](db[_0x9f0c('0x50')][_0x9f0c('0x2d')]);_0x212fb8[_0x9f0c('0x32')]=_[_0x9f0c('0x33')](_0x42586a['query']);_0x212fb8[_0x9f0c('0x34')]=_[_0x9f0c('0x4a')](_0x212fb8['model'],_0x212fb8['query']);_0x5ee7de['attributes']=_['intersection'](_0x212fb8[_0x9f0c('0x31')],qs[_0x9f0c('0x36')](_0x42586a[_0x9f0c('0x32')][_0x9f0c('0x36')]));_0x5ee7de['attributes']=_0x5ee7de[_0x9f0c('0x35')]['length']?_0x5ee7de[_0x9f0c('0x35')]:_0x212fb8[_0x9f0c('0x31')];_0x5ee7de[_0x9f0c('0x3a')]=qs[_0x9f0c('0x3b')](_0x42586a['query'][_0x9f0c('0x3b')]);_0x5ee7de['where']=qs[_0x9f0c('0x34')](_[_0x9f0c('0x3c')](_0x42586a[_0x9f0c('0x32')],_0x212fb8[_0x9f0c('0x34')]));if(_0x42586a[_0x9f0c('0x32')][_0x9f0c('0x3d')]){_0x5ee7de[_0x9f0c('0x3e')]=_[_0x9f0c('0x40')](_0x5ee7de[_0x9f0c('0x3e')],{'$or':_['map'](_0x5ee7de['attributes'],function(_0x49448b){var _0x145dfd={};_0x145dfd[_0x49448b]={'$like':'%'+_0x42586a[_0x9f0c('0x32')]['filter']+'%'};return _0x145dfd;})});}_0x5ee7de=_[_0x9f0c('0x40')]({},_0x5ee7de,_0x42586a['options']);return _0x2c9818[_0x9f0c('0x4e')](_0x5ee7de);}})['then'](function(_0x3561ad){if(_0x3561ad){_0x5b9520=_0x3561ad['length'];if(!_0x42586a['query'][_0x9f0c('0x38')](_0x9f0c('0x39'))){_0x5ee7de['limit']=qs[_0x9f0c('0x1f')](_0x42586a[_0x9f0c('0x32')]['limit']);_0x5ee7de[_0x9f0c('0x1d')]=qs[_0x9f0c('0x1d')](_0x42586a[_0x9f0c('0x32')][_0x9f0c('0x1d')]);}return _0x2c9818['getConfigurations'](_0x5ee7de);}})['then'](function(_0x335e13){if(_0x335e13){return _0x335e13?{'count':_0x5b9520,'rows':_0x335e13}:null;}})[_0x9f0c('0x25')](respondWithResult(_0x31f2ef,null))[_0x9f0c('0x47')](handleError(_0x31f2ef,null));};exports[_0x9f0c('0x51')]=function(_0x31f8cc,_0xfb53ac,_0x5eb93d){if(_0x31f8cc[_0x9f0c('0x4d')]['id']){delete _0x31f8cc[_0x9f0c('0x4d')]['id'];}return db[_0x9f0c('0x42')][_0x9f0c('0x4f')]({'where':{'id':_0x31f8cc[_0x9f0c('0x49')]['id']}})[_0x9f0c('0x25')](handleEntityNotFound(_0xfb53ac,null))[_0x9f0c('0x25')](function(_0x931079){if(_0x931079){_0x31f8cc['body'][_0x9f0c('0x52')]=_0x931079['id'];_0x31f8cc[_0x9f0c('0x4d')][_0x9f0c('0x53')]=integrations['getSubjects'](_0x31f8cc['body'][_0x9f0c('0x54')],_0x31f8cc['body']['type']);_0x31f8cc[_0x9f0c('0x4d')][_0x9f0c('0x55')]=integrations['getDescriptions'](_0x31f8cc[_0x9f0c('0x4d')][_0x9f0c('0x54')],_0x31f8cc[_0x9f0c('0x4d')][_0x9f0c('0x2f')]);return db['DeskConfiguration'][_0x9f0c('0x4c')](_0x31f8cc[_0x9f0c('0x4d')],{'include':[{'model':db[_0x9f0c('0x56')],'as':_0x9f0c('0x53')},{'model':db[_0x9f0c('0x56')],'as':_0x9f0c('0x55')}]});}return null;})[_0x9f0c('0x25')](respondWithResult(_0xfb53ac,null))['catch'](handleError(_0xfb53ac,null));};exports[_0x9f0c('0x57')]=function(_0xa02383,_0x3b016f,_0x4c11bb){var _0x4d68e0=[{'name':_0x9f0c('0x58'),'label':_0x9f0c('0x58'),'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 _0x499c3d='';return db[_0x9f0c('0x42')][_0x9f0c('0x4f')]({'where':{'id':_0xa02383[_0x9f0c('0x49')]['id']},'attributes':['id',_0x9f0c('0x59'),_0x9f0c('0x5a'),'username','password',_0x9f0c('0x5b'),_0x9f0c('0x5c'),_0x9f0c('0x5d'),_0x9f0c('0x5e')]})[_0x9f0c('0x25')](handleEntityNotFound(_0x3b016f,null))['then'](function(_0x17efd5){if(_0x17efd5){_0x499c3d=_0x17efd5[_0x9f0c('0x59')];var _0x25e425=_0x499c3d[_0x9f0c('0x5f')](-0x1);if(_0x25e425==='/'){_0x499c3d=_0x499c3d[_0x9f0c('0x60')](0x0,_0x499c3d['lastIndexOf'](_0x25e425));}var _0x32c891={'method':_0x9f0c('0x61'),'uri':util['format'](_0x9f0c('0x62'),_0x499c3d),'headers':{'Content-Type':'application/json','Accept':_0x9f0c('0x63')},'json':!![]};if(_0x17efd5[_0x9f0c('0x5a')]===_0x9f0c('0x64')){_0x32c891['headers']['Authorization']=util[_0x9f0c('0x65')]('Basic\x20%s',new Buffer(util[_0x9f0c('0x65')](_0x9f0c('0x66'),_0x17efd5['username'],_0x17efd5['password']))['toString'](_0x9f0c('0x67')));}return rp(_0x32c891)[_0x9f0c('0x25')](function(_0x5ecc7e){return _0x4d68e0[_0x9f0c('0x68')](_[_0x9f0c('0x2c')](_0x5ecc7e[_0x9f0c('0x69')][_0x9f0c('0x6a')],function(_0x13e9f1){return{'id':_0x13e9f1[_0x9f0c('0x2a')],'name':_0x13e9f1['name'],'type':_0x13e9f1['type'],'active':_0x13e9f1[_0x9f0c('0x6b')],'label':_0x13e9f1[_0x9f0c('0x6c')],'custom':!![]};}));});}})[_0x9f0c('0x25')](function(_0x3ab3fe){return{'count':_0x3ab3fe[_0x9f0c('0x37')],'rows':_0x3ab3fe};})[_0x9f0c('0x25')](respondWithResult(_0x3b016f,null))[_0x9f0c('0x47')](function(_0x326cd2){var _0x5239b5=_0xa02383['query'][_0x9f0c('0x6d')]?0x1f4:_0x326cd2['statusCode']||0x1f4;logger[_0x9f0c('0x28')](_0x9f0c('0x6e'),_0x9f0c('0x57'),_0x5239b5,JSON[_0x9f0c('0x6f')](_0x326cd2));delete _0x326cd2[_0x9f0c('0x2a')];if(_0x5239b5===0x191){_0x5239b5=0x190;}_0x3b016f['status'](_0x5239b5)[_0x9f0c('0x2b')](_0xa02383[_0x9f0c('0x32')][_0x9f0c('0x6d')]?{'message':_0x9f0c('0x70'),'statusCode':_0x326cd2[_0x9f0c('0x71')]}:_0x326cd2);});};