86bf770aa5bbff80925d45b902b480492d4ca057
[motion2.git] / server / api / intSugarcrmConfiguration / intSugarcrmConfiguration.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 _0x48c5=['sort','pick','merge','where','filter','includeAll','findAll','rows','show','keys','rawAttributes','include','find','catch','create','body','update','params','getFields','findOne','SugarcrmField','order','options','getSubjects','getDescriptions','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','sox','to-csv','ejs','fs-extra','lodash','desk.js','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','../../config/environment','sendStatus','status','offset','undefined','count','limit','Content-Range','json','apply','reject','save','then','destroy','end','error','stack','name','send','index','map','SugarcrmConfiguration','key','model','query','filters','intersection','attributes','fields','length','hasOwnProperty','nolimit'];(function(_0x1324f0,_0x2e6ce7){var _0x4ca48d=function(_0x154eb5){while(--_0x154eb5){_0x1324f0['push'](_0x1324f0['shift']());}};_0x4ca48d(++_0x2e6ce7);}(_0x48c5,0x151));var _0x548c=function(_0x4b4e18,_0x192190){_0x4b4e18=_0x4b4e18-0x0;var _0x323ea5=_0x48c5[_0x4b4e18];return _0x323ea5;};'use strict';var emlformat=require(_0x548c('0x0'));var rimraf=require(_0x548c('0x1'));var zipdir=require(_0x548c('0x2'));var jsonpatch=require(_0x548c('0x3'));var rp=require(_0x548c('0x4'));var moment=require(_0x548c('0x5'));var BPromise=require(_0x548c('0x6'));var Mustache=require(_0x548c('0x7'));var util=require(_0x548c('0x8'));var path=require(_0x548c('0x9'));var sox=require(_0x548c('0xa'));var csv=require(_0x548c('0xb'));var ejs=require(_0x548c('0xc'));var fs=require('fs');var fs_extra=require(_0x548c('0xd'));var _=require(_0x548c('0xe'));var squel=require('squel');var crypto=require('crypto');var jsforce=require('jsforce');var deskjs=require(_0x548c('0xf'));var toCsv=require(_0x548c('0xb'));var querystring=require('querystring');var Papa=require('papaparse');var Redis=require('ioredis');var authService=require(_0x548c('0x10'));var qs=require(_0x548c('0x11'));var as=require(_0x548c('0x12'));var hardwareService=require(_0x548c('0x13'));var logger=require(_0x548c('0x14'))('api');var utils=require('../../config/utils');var config=require(_0x548c('0x15'));var licenseUtil=require('../../config/license/util');var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x315ad4,_0x12deb7){_0x12deb7=_0x12deb7||0xcc;return function(_0x369831){if(_0x369831){return _0x315ad4[_0x548c('0x16')](_0x12deb7);}return _0x315ad4[_0x548c('0x17')](_0x12deb7)['end']();};}function respondWithResult(_0x1af87a,_0x83308b){_0x83308b=_0x83308b||0xc8;return function(_0x108ed5){if(_0x108ed5){return _0x1af87a['status'](_0x83308b)['json'](_0x108ed5);}};}function respondWithFilteredResult(_0x16f9f8,_0x3e7dc3){return function(_0x11bf32){if(_0x11bf32){var _0x6a25e5=typeof _0x3e7dc3[_0x548c('0x18')]===_0x548c('0x19')&&typeof _0x3e7dc3['limit']==='undefined';var _0x2fbe55=_0x11bf32[_0x548c('0x1a')];var _0x11548b=_0x6a25e5?0x0:_0x3e7dc3[_0x548c('0x18')];var _0x44b3c4=_0x6a25e5?_0x11bf32[_0x548c('0x1a')]:_0x3e7dc3[_0x548c('0x18')]+_0x3e7dc3[_0x548c('0x1b')];var _0x281919;if(_0x44b3c4>=_0x2fbe55){_0x44b3c4=_0x2fbe55;_0x281919=0xc8;}else{_0x281919=0xce;}_0x16f9f8['status'](_0x281919);return _0x16f9f8['set'](_0x548c('0x1c'),_0x11548b+'-'+_0x44b3c4+'/'+_0x2fbe55)[_0x548c('0x1d')](_0x11bf32);}return null;};}function patchUpdates(_0x58298c){return function(_0x5b97fb){try{jsonpatch[_0x548c('0x1e')](_0x5b97fb,_0x58298c,!![]);}catch(_0x13f980){return BPromise[_0x548c('0x1f')](_0x13f980);}return _0x5b97fb[_0x548c('0x20')]();};}function saveUpdates(_0x398414,_0x573e59){return function(_0x15e116){if(_0x15e116){return _0x15e116['update'](_0x398414)[_0x548c('0x21')](function(_0x1797fe){return _0x1797fe;});}return null;};}function removeEntity(_0x4c0367,_0x17481b){return function(_0x256e3b){if(_0x256e3b){return _0x256e3b[_0x548c('0x22')]()[_0x548c('0x21')](function(){_0x4c0367[_0x548c('0x17')](0xcc)[_0x548c('0x23')]();});}};}function handleEntityNotFound(_0x372f6f,_0x2d54d2){return function(_0x234a71){if(!_0x234a71){_0x372f6f[_0x548c('0x16')](0x194);}return _0x234a71;};}function handleError(_0x5a4a08,_0x5bceac){_0x5bceac=_0x5bceac||0x1f4;return function(_0x40c457){logger[_0x548c('0x24')](_0x40c457[_0x548c('0x25')]);if(_0x40c457[_0x548c('0x26')]){delete _0x40c457[_0x548c('0x26')];}_0x5a4a08[_0x548c('0x17')](_0x5bceac)[_0x548c('0x27')](_0x40c457);};}exports[_0x548c('0x28')]=function(_0x4df279,_0x2628a3){var _0x4ba8d1={},_0x5648b4={},_0x240f1c={'count':0x0,'rows':[]};var _0x3dd655=_[_0x548c('0x29')](db[_0x548c('0x2a')]['rawAttributes'],function(_0x2b2577){return{'name':_0x2b2577['fieldName'],'type':_0x2b2577['type'][_0x548c('0x2b')]};});_0x5648b4[_0x548c('0x2c')]=_['map'](_0x3dd655,_0x548c('0x26'));_0x5648b4[_0x548c('0x2d')]=_['keys'](_0x4df279[_0x548c('0x2d')]);_0x5648b4[_0x548c('0x2e')]=_[_0x548c('0x2f')](_0x5648b4['model'],_0x5648b4['query']);_0x4ba8d1[_0x548c('0x30')]=_[_0x548c('0x2f')](_0x5648b4[_0x548c('0x2c')],qs[_0x548c('0x31')](_0x4df279[_0x548c('0x2d')][_0x548c('0x31')]));_0x4ba8d1['attributes']=_0x4ba8d1['attributes'][_0x548c('0x32')]?_0x4ba8d1[_0x548c('0x30')]:_0x5648b4[_0x548c('0x2c')];if(!_0x4df279[_0x548c('0x2d')][_0x548c('0x33')](_0x548c('0x34'))){_0x4ba8d1['limit']=qs['limit'](_0x4df279[_0x548c('0x2d')][_0x548c('0x1b')]);_0x4ba8d1[_0x548c('0x18')]=qs[_0x548c('0x18')](_0x4df279['query'][_0x548c('0x18')]);}_0x4ba8d1['order']=qs[_0x548c('0x35')](_0x4df279[_0x548c('0x2d')]['sort']);_0x4ba8d1['where']=qs[_0x548c('0x2e')](_[_0x548c('0x36')](_0x4df279[_0x548c('0x2d')],_0x5648b4[_0x548c('0x2e')]),_0x3dd655);if(_0x4df279[_0x548c('0x2d')]['filter']){_0x4ba8d1['where']=_[_0x548c('0x37')](_0x4ba8d1[_0x548c('0x38')],{'$or':_[_0x548c('0x29')](_0x3dd655,function(_0x4778a8){if(_0x4778a8['type']!=='VIRTUAL'){var _0x5a829e={};_0x5a829e[_0x4778a8['name']]={'$like':'%'+_0x4df279[_0x548c('0x2d')][_0x548c('0x39')]+'%'};return _0x5a829e;}})});}_0x4ba8d1=_[_0x548c('0x37')]({},_0x4ba8d1,_0x4df279['options']);var _0x22f017={'where':_0x4ba8d1[_0x548c('0x38')]};return db[_0x548c('0x2a')][_0x548c('0x1a')](_0x22f017)[_0x548c('0x21')](function(_0x2a72ff){_0x240f1c['count']=_0x2a72ff;if(_0x4df279[_0x548c('0x2d')][_0x548c('0x3a')]){_0x4ba8d1['include']=[{'all':!![]}];}return db['SugarcrmConfiguration'][_0x548c('0x3b')](_0x4ba8d1);})[_0x548c('0x21')](function(_0x37f0a5){_0x240f1c[_0x548c('0x3c')]=_0x37f0a5;return _0x240f1c;})[_0x548c('0x21')](respondWithFilteredResult(_0x2628a3,_0x4ba8d1))['catch'](handleError(_0x2628a3,null));};exports[_0x548c('0x3d')]=function(_0x123d0c,_0x2c8558){var _0xc6eb5e={'raw':![],'where':{'id':_0x123d0c['params']['id']}},_0x550977={};_0x550977['model']=_[_0x548c('0x3e')](db[_0x548c('0x2a')][_0x548c('0x3f')]);_0x550977[_0x548c('0x2d')]=_[_0x548c('0x3e')](_0x123d0c[_0x548c('0x2d')]);_0x550977[_0x548c('0x2e')]=_[_0x548c('0x2f')](_0x550977[_0x548c('0x2c')],_0x550977[_0x548c('0x2d')]);_0xc6eb5e[_0x548c('0x30')]=_['intersection'](_0x550977['model'],qs[_0x548c('0x31')](_0x123d0c[_0x548c('0x2d')][_0x548c('0x31')]));_0xc6eb5e[_0x548c('0x30')]=_0xc6eb5e[_0x548c('0x30')][_0x548c('0x32')]?_0xc6eb5e['attributes']:_0x550977[_0x548c('0x2c')];if(_0x123d0c['query'][_0x548c('0x3a')]){_0xc6eb5e[_0x548c('0x40')]=[{'all':!![]}];}_0xc6eb5e=_[_0x548c('0x37')]({},_0xc6eb5e,_0x123d0c['options']);return db[_0x548c('0x2a')][_0x548c('0x41')](_0xc6eb5e)[_0x548c('0x21')](handleEntityNotFound(_0x2c8558,null))[_0x548c('0x21')](respondWithResult(_0x2c8558,null))[_0x548c('0x42')](handleError(_0x2c8558,null));};exports['create']=function(_0x2a7e93,_0x2cd3fa){return db[_0x548c('0x2a')][_0x548c('0x43')](_0x2a7e93[_0x548c('0x44')],{})[_0x548c('0x21')](respondWithResult(_0x2cd3fa,0xc9))['catch'](handleError(_0x2cd3fa,null));};exports[_0x548c('0x45')]=function(_0x5a6a26,_0x1d174a){if(_0x5a6a26[_0x548c('0x44')]['id']){delete _0x5a6a26['body']['id'];}return db['SugarcrmConfiguration'][_0x548c('0x41')]({'where':{'id':_0x5a6a26[_0x548c('0x46')]['id']}})[_0x548c('0x21')](handleEntityNotFound(_0x1d174a,null))['then'](saveUpdates(_0x5a6a26[_0x548c('0x44')],null))[_0x548c('0x21')](respondWithResult(_0x1d174a,null))[_0x548c('0x42')](handleError(_0x1d174a,null));};exports[_0x548c('0x22')]=function(_0x23eb21,_0x259ea3){return db[_0x548c('0x2a')]['find']({'where':{'id':_0x23eb21['params']['id']}})[_0x548c('0x21')](handleEntityNotFound(_0x259ea3,null))[_0x548c('0x21')](removeEntity(_0x259ea3,null))[_0x548c('0x42')](handleError(_0x259ea3,null));};exports[_0x548c('0x47')]=function(_0xef70c8,_0x44ae11,_0x9707cb){var _0x1e53b4={};var _0x55e414={};var _0x3c1402;var _0x29cd4c;return db[_0x548c('0x2a')][_0x548c('0x48')]({'where':{'id':_0xef70c8[_0x548c('0x46')]['id']}})[_0x548c('0x21')](handleEntityNotFound(_0x44ae11,null))[_0x548c('0x21')](function(_0x364977){if(_0x364977){_0x3c1402=_0x364977;_0x55e414['model']=_[_0x548c('0x3e')](db[_0x548c('0x49')][_0x548c('0x3f')]);_0x55e414[_0x548c('0x2d')]=_[_0x548c('0x3e')](_0xef70c8[_0x548c('0x2d')]);_0x55e414[_0x548c('0x2e')]=_[_0x548c('0x2f')](_0x55e414[_0x548c('0x2c')],_0x55e414['query']);_0x1e53b4[_0x548c('0x30')]=_[_0x548c('0x2f')](_0x55e414[_0x548c('0x2c')],qs[_0x548c('0x31')](_0xef70c8[_0x548c('0x2d')][_0x548c('0x31')]));_0x1e53b4['attributes']=_0x1e53b4[_0x548c('0x30')][_0x548c('0x32')]?_0x1e53b4[_0x548c('0x30')]:_0x55e414[_0x548c('0x2c')];_0x1e53b4[_0x548c('0x4a')]=qs[_0x548c('0x35')](_0xef70c8['query'][_0x548c('0x35')]);_0x1e53b4['where']=qs[_0x548c('0x2e')](_['pick'](_0xef70c8[_0x548c('0x2d')],_0x55e414[_0x548c('0x2e')]));if(_0xef70c8[_0x548c('0x2d')][_0x548c('0x39')]){_0x1e53b4[_0x548c('0x38')]=_[_0x548c('0x37')](_0x1e53b4[_0x548c('0x38')],{'$or':_[_0x548c('0x29')](_0x1e53b4['attributes'],function(_0x454db0){var _0x386c68={};_0x386c68[_0x454db0]={'$like':'%'+_0xef70c8['query'][_0x548c('0x39')]+'%'};return _0x386c68;})});}_0x1e53b4=_[_0x548c('0x37')]({},_0x1e53b4,_0xef70c8[_0x548c('0x4b')]);return _0x3c1402[_0x548c('0x47')](_0x1e53b4);}})['then'](function(_0x43c497){if(_0x43c497){_0x29cd4c=_0x43c497['length'];if(!_0xef70c8['query'][_0x548c('0x33')]('nolimit')){_0x1e53b4['limit']=qs[_0x548c('0x1b')](_0xef70c8[_0x548c('0x2d')][_0x548c('0x1b')]);_0x1e53b4[_0x548c('0x18')]=qs['offset'](_0xef70c8['query'][_0x548c('0x18')]);}return _0x3c1402[_0x548c('0x47')](_0x1e53b4);}})[_0x548c('0x21')](function(_0x30c657){if(_0x30c657){return _0x30c657?{'count':_0x29cd4c,'rows':_0x30c657}:null;}})[_0x548c('0x21')](respondWithResult(_0x44ae11,null))[_0x548c('0x42')](handleError(_0x44ae11,null));};exports[_0x548c('0x4c')]=function(_0x1da460,_0x445783,_0x456e21){var _0x2ed780={};var _0x3a8476={};var _0xc19a79;var _0x5ad1f5;return db[_0x548c('0x2a')][_0x548c('0x48')]({'where':{'id':_0x1da460[_0x548c('0x46')]['id']}})[_0x548c('0x21')](handleEntityNotFound(_0x445783,null))[_0x548c('0x21')](function(_0x5d025d){if(_0x5d025d){_0xc19a79=_0x5d025d;_0x3a8476[_0x548c('0x2c')]=_[_0x548c('0x3e')](db[_0x548c('0x49')][_0x548c('0x3f')]);_0x3a8476[_0x548c('0x2d')]=_[_0x548c('0x3e')](_0x1da460['query']);_0x3a8476['filters']=_[_0x548c('0x2f')](_0x3a8476[_0x548c('0x2c')],_0x3a8476['query']);_0x2ed780['attributes']=_[_0x548c('0x2f')](_0x3a8476[_0x548c('0x2c')],qs[_0x548c('0x31')](_0x1da460[_0x548c('0x2d')][_0x548c('0x31')]));_0x2ed780[_0x548c('0x30')]=_0x2ed780[_0x548c('0x30')][_0x548c('0x32')]?_0x2ed780[_0x548c('0x30')]:_0x3a8476[_0x548c('0x2c')];_0x2ed780[_0x548c('0x4a')]=qs[_0x548c('0x35')](_0x1da460[_0x548c('0x2d')][_0x548c('0x35')]);_0x2ed780['where']=qs[_0x548c('0x2e')](_['pick'](_0x1da460[_0x548c('0x2d')],_0x3a8476['filters']));if(_0x1da460['query'][_0x548c('0x39')]){_0x2ed780[_0x548c('0x38')]=_[_0x548c('0x37')](_0x2ed780['where'],{'$or':_[_0x548c('0x29')](_0x2ed780['attributes'],function(_0x4c60c3){var _0x5e8ab8={};_0x5e8ab8[_0x4c60c3]={'$like':'%'+_0x1da460[_0x548c('0x2d')][_0x548c('0x39')]+'%'};return _0x5e8ab8;})});}_0x2ed780=_[_0x548c('0x37')]({},_0x2ed780,_0x1da460[_0x548c('0x4b')]);return _0xc19a79[_0x548c('0x4c')](_0x2ed780);}})[_0x548c('0x21')](function(_0x5cd398){if(_0x5cd398){_0x5ad1f5=_0x5cd398[_0x548c('0x32')];if(!_0x1da460[_0x548c('0x2d')][_0x548c('0x33')](_0x548c('0x34'))){_0x2ed780[_0x548c('0x1b')]=qs[_0x548c('0x1b')](_0x1da460[_0x548c('0x2d')][_0x548c('0x1b')]);_0x2ed780[_0x548c('0x18')]=qs['offset'](_0x1da460['query'][_0x548c('0x18')]);}return _0xc19a79['getSubjects'](_0x2ed780);}})['then'](function(_0x27f6e4){if(_0x27f6e4){return _0x27f6e4?{'count':_0x5ad1f5,'rows':_0x27f6e4}:null;}})[_0x548c('0x21')](respondWithResult(_0x445783,null))[_0x548c('0x42')](handleError(_0x445783,null));};exports[_0x548c('0x4d')]=function(_0x4747fc,_0x1c5c4e,_0x56e3d6){var _0xee5330={};var _0x40d666={};var _0xa9bc62;var _0x43ca81;return db['SugarcrmConfiguration'][_0x548c('0x48')]({'where':{'id':_0x4747fc[_0x548c('0x46')]['id']}})[_0x548c('0x21')](handleEntityNotFound(_0x1c5c4e,null))[_0x548c('0x21')](function(_0x2317f4){if(_0x2317f4){_0xa9bc62=_0x2317f4;_0x40d666[_0x548c('0x2c')]=_[_0x548c('0x3e')](db[_0x548c('0x49')][_0x548c('0x3f')]);_0x40d666[_0x548c('0x2d')]=_[_0x548c('0x3e')](_0x4747fc[_0x548c('0x2d')]);_0x40d666[_0x548c('0x2e')]=_['intersection'](_0x40d666[_0x548c('0x2c')],_0x40d666[_0x548c('0x2d')]);_0xee5330[_0x548c('0x30')]=_[_0x548c('0x2f')](_0x40d666[_0x548c('0x2c')],qs['fields'](_0x4747fc[_0x548c('0x2d')][_0x548c('0x31')]));_0xee5330['attributes']=_0xee5330[_0x548c('0x30')]['length']?_0xee5330[_0x548c('0x30')]:_0x40d666[_0x548c('0x2c')];_0xee5330['order']=qs[_0x548c('0x35')](_0x4747fc[_0x548c('0x2d')][_0x548c('0x35')]);_0xee5330[_0x548c('0x38')]=qs[_0x548c('0x2e')](_['pick'](_0x4747fc[_0x548c('0x2d')],_0x40d666[_0x548c('0x2e')]));if(_0x4747fc['query']['filter']){_0xee5330[_0x548c('0x38')]=_[_0x548c('0x37')](_0xee5330[_0x548c('0x38')],{'$or':_[_0x548c('0x29')](_0xee5330[_0x548c('0x30')],function(_0x3d14f8){var _0x484672={};_0x484672[_0x3d14f8]={'$like':'%'+_0x4747fc[_0x548c('0x2d')][_0x548c('0x39')]+'%'};return _0x484672;})});}_0xee5330=_[_0x548c('0x37')]({},_0xee5330,_0x4747fc[_0x548c('0x4b')]);return _0xa9bc62['getDescriptions'](_0xee5330);}})['then'](function(_0xc6592a){if(_0xc6592a){_0x43ca81=_0xc6592a['length'];if(!_0x4747fc[_0x548c('0x2d')]['hasOwnProperty']('nolimit')){_0xee5330[_0x548c('0x1b')]=qs['limit'](_0x4747fc[_0x548c('0x2d')][_0x548c('0x1b')]);_0xee5330['offset']=qs[_0x548c('0x18')](_0x4747fc['query'][_0x548c('0x18')]);}return _0xa9bc62[_0x548c('0x4d')](_0xee5330);}})['then'](function(_0x3b510e){if(_0x3b510e){return _0x3b510e?{'count':_0x43ca81,'rows':_0x3b510e}:null;}})['then'](respondWithResult(_0x1c5c4e,null))['catch'](handleError(_0x1c5c4e,null));};