Built motion from commit ffdca78c.|2.6.24
[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 _0x515f=['intersection','attributes','fields','hasOwnProperty','nolimit','offset','order','sort','pick','filter','where','merge','VIRTUAL','options','SugarcrmConfiguration','include','findAll','rows','catch','show','length','find','create','body','params','getFields','findOne','SugarcrmField','getSubjects','getDescriptions','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','mustache','to-csv','fs-extra','lodash','squel','crypto','desk.js','papaparse','ioredis','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','api','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status','end','json','limit','undefined','count','set','Content-Range','apply','reject','update','then','destroy','stack','rawAttributes','fieldName','type','key','model','map','name','query','keys','filters'];(function(_0x45d3f0,_0x4be53f){var _0x314b79=function(_0x563d32){while(--_0x563d32){_0x45d3f0['push'](_0x45d3f0['shift']());}};_0x314b79(++_0x4be53f);}(_0x515f,0x6b));var _0xf515=function(_0x4bab5f,_0x43960a){_0x4bab5f=_0x4bab5f-0x0;var _0x5c29c9=_0x515f[_0x4bab5f];return _0x5c29c9;};'use strict';var emlformat=require(_0xf515('0x0'));var rimraf=require(_0xf515('0x1'));var zipdir=require(_0xf515('0x2'));var jsonpatch=require(_0xf515('0x3'));var rp=require(_0xf515('0x4'));var moment=require(_0xf515('0x5'));var BPromise=require('bluebird');var Mustache=require(_0xf515('0x6'));var util=require('util');var path=require('path');var sox=require('sox');var csv=require(_0xf515('0x7'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0xf515('0x8'));var _=require(_0xf515('0x9'));var squel=require(_0xf515('0xa'));var crypto=require(_0xf515('0xb'));var jsforce=require('jsforce');var deskjs=require(_0xf515('0xc'));var toCsv=require('to-csv');var querystring=require('querystring');var Papa=require(_0xf515('0xd'));var Redis=require(_0xf515('0xe'));var authService=require('../../components/auth/service');var qs=require(_0xf515('0xf'));var as=require(_0xf515('0x10'));var hardwareService=require(_0xf515('0x11'));var logger=require('../../config/logger')(_0xf515('0x12'));var utils=require('../../config/utils');var config=require(_0xf515('0x13'));var licenseUtil=require(_0xf515('0x14'));var db=require(_0xf515('0x15'))['db'];function respondWithStatusCode(_0x13be79,_0x6689f7){_0x6689f7=_0x6689f7||0xcc;return function(_0x5e8916){if(_0x5e8916){return _0x13be79[_0xf515('0x16')](_0x6689f7);}return _0x13be79[_0xf515('0x17')](_0x6689f7)[_0xf515('0x18')]();};}function respondWithResult(_0xaef0d5,_0x4696d9){_0x4696d9=_0x4696d9||0xc8;return function(_0x1ffc65){if(_0x1ffc65){return _0xaef0d5['status'](_0x4696d9)[_0xf515('0x19')](_0x1ffc65);}};}function respondWithFilteredResult(_0x14f317,_0x2bb205){return function(_0x1e8570){if(_0x1e8570){var _0xa0054a=typeof _0x2bb205['offset']==='undefined'&&typeof _0x2bb205[_0xf515('0x1a')]===_0xf515('0x1b');var _0xfcd3f2=_0x1e8570[_0xf515('0x1c')];var _0x428264=_0xa0054a?0x0:_0x2bb205['offset'];var _0x29aa3a=_0xa0054a?_0x1e8570[_0xf515('0x1c')]:_0x2bb205['offset']+_0x2bb205['limit'];var _0x54ad65;if(_0x29aa3a>=_0xfcd3f2){_0x29aa3a=_0xfcd3f2;_0x54ad65=0xc8;}else{_0x54ad65=0xce;}_0x14f317[_0xf515('0x17')](_0x54ad65);return _0x14f317[_0xf515('0x1d')](_0xf515('0x1e'),_0x428264+'-'+_0x29aa3a+'/'+_0xfcd3f2)[_0xf515('0x19')](_0x1e8570);}return null;};}function patchUpdates(_0x3d6ef5){return function(_0x4f0841){try{jsonpatch[_0xf515('0x1f')](_0x4f0841,_0x3d6ef5,!![]);}catch(_0x263ea0){return BPromise[_0xf515('0x20')](_0x263ea0);}return _0x4f0841['save']();};}function saveUpdates(_0x243e9d,_0x4f0d4b){return function(_0x53844c){if(_0x53844c){return _0x53844c[_0xf515('0x21')](_0x243e9d)[_0xf515('0x22')](function(_0x317403){return _0x317403;});}return null;};}function removeEntity(_0x38f82d,_0x1052ac){return function(_0x297cbd){if(_0x297cbd){return _0x297cbd[_0xf515('0x23')]()[_0xf515('0x22')](function(){_0x38f82d[_0xf515('0x17')](0xcc)['end']();});}};}function handleEntityNotFound(_0x389800,_0x2b4cd5){return function(_0x492f0e){if(!_0x492f0e){_0x389800[_0xf515('0x16')](0x194);}return _0x492f0e;};}function handleError(_0x28236d,_0x5df0c1){_0x5df0c1=_0x5df0c1||0x1f4;return function(_0x59716b){logger['error'](_0x59716b[_0xf515('0x24')]);if(_0x59716b['name']){delete _0x59716b['name'];}_0x28236d['status'](_0x5df0c1)['send'](_0x59716b);};}exports['index']=function(_0x530b73,_0x28fcae){var _0x35585b={},_0x595bc9={},_0x4294f6={'count':0x0,'rows':[]};var _0x4693ba=_['map'](db['SugarcrmConfiguration'][_0xf515('0x25')],function(_0x1464c8){return{'name':_0x1464c8[_0xf515('0x26')],'type':_0x1464c8[_0xf515('0x27')][_0xf515('0x28')]};});_0x595bc9[_0xf515('0x29')]=_[_0xf515('0x2a')](_0x4693ba,_0xf515('0x2b'));_0x595bc9[_0xf515('0x2c')]=_[_0xf515('0x2d')](_0x530b73['query']);_0x595bc9[_0xf515('0x2e')]=_[_0xf515('0x2f')](_0x595bc9[_0xf515('0x29')],_0x595bc9[_0xf515('0x2c')]);_0x35585b[_0xf515('0x30')]=_['intersection'](_0x595bc9[_0xf515('0x29')],qs[_0xf515('0x31')](_0x530b73['query']['fields']));_0x35585b[_0xf515('0x30')]=_0x35585b[_0xf515('0x30')]['length']?_0x35585b[_0xf515('0x30')]:_0x595bc9['model'];if(!_0x530b73[_0xf515('0x2c')][_0xf515('0x32')](_0xf515('0x33'))){_0x35585b[_0xf515('0x1a')]=qs[_0xf515('0x1a')](_0x530b73[_0xf515('0x2c')][_0xf515('0x1a')]);_0x35585b[_0xf515('0x34')]=qs['offset'](_0x530b73['query'][_0xf515('0x34')]);}_0x35585b[_0xf515('0x35')]=qs[_0xf515('0x36')](_0x530b73['query'][_0xf515('0x36')]);_0x35585b['where']=qs[_0xf515('0x2e')](_[_0xf515('0x37')](_0x530b73[_0xf515('0x2c')],_0x595bc9['filters']),_0x4693ba);if(_0x530b73[_0xf515('0x2c')][_0xf515('0x38')]){_0x35585b[_0xf515('0x39')]=_[_0xf515('0x3a')](_0x35585b[_0xf515('0x39')],{'$or':_[_0xf515('0x2a')](_0x4693ba,function(_0x385414){if(_0x385414[_0xf515('0x27')]!==_0xf515('0x3b')){var _0xa3c016={};_0xa3c016[_0x385414[_0xf515('0x2b')]]={'$like':'%'+_0x530b73['query']['filter']+'%'};return _0xa3c016;}})});}_0x35585b=_[_0xf515('0x3a')]({},_0x35585b,_0x530b73[_0xf515('0x3c')]);var _0x3754a2={'where':_0x35585b[_0xf515('0x39')]};return db[_0xf515('0x3d')][_0xf515('0x1c')](_0x3754a2)[_0xf515('0x22')](function(_0x26d9c9){_0x4294f6[_0xf515('0x1c')]=_0x26d9c9;if(_0x530b73[_0xf515('0x2c')]['includeAll']){_0x35585b[_0xf515('0x3e')]=[{'all':!![]}];}return db[_0xf515('0x3d')][_0xf515('0x3f')](_0x35585b);})[_0xf515('0x22')](function(_0x46450f){_0x4294f6[_0xf515('0x40')]=_0x46450f;return _0x4294f6;})[_0xf515('0x22')](respondWithFilteredResult(_0x28fcae,_0x35585b))[_0xf515('0x41')](handleError(_0x28fcae,null));};exports[_0xf515('0x42')]=function(_0x43050c,_0x50c454){var _0x973add={'raw':![],'where':{'id':_0x43050c['params']['id']}},_0x44cbd1={};_0x44cbd1[_0xf515('0x29')]=_[_0xf515('0x2d')](db[_0xf515('0x3d')][_0xf515('0x25')]);_0x44cbd1[_0xf515('0x2c')]=_['keys'](_0x43050c[_0xf515('0x2c')]);_0x44cbd1[_0xf515('0x2e')]=_[_0xf515('0x2f')](_0x44cbd1['model'],_0x44cbd1[_0xf515('0x2c')]);_0x973add[_0xf515('0x30')]=_['intersection'](_0x44cbd1['model'],qs['fields'](_0x43050c[_0xf515('0x2c')][_0xf515('0x31')]));_0x973add[_0xf515('0x30')]=_0x973add[_0xf515('0x30')][_0xf515('0x43')]?_0x973add[_0xf515('0x30')]:_0x44cbd1[_0xf515('0x29')];if(_0x43050c['query']['includeAll']){_0x973add[_0xf515('0x3e')]=[{'all':!![]}];}_0x973add=_[_0xf515('0x3a')]({},_0x973add,_0x43050c[_0xf515('0x3c')]);return db[_0xf515('0x3d')][_0xf515('0x44')](_0x973add)[_0xf515('0x22')](handleEntityNotFound(_0x50c454,null))[_0xf515('0x22')](respondWithResult(_0x50c454,null))[_0xf515('0x41')](handleError(_0x50c454,null));};exports[_0xf515('0x45')]=function(_0x1ebf32,_0x5811bf){return db[_0xf515('0x3d')][_0xf515('0x45')](_0x1ebf32[_0xf515('0x46')],{})[_0xf515('0x22')](respondWithResult(_0x5811bf,0xc9))['catch'](handleError(_0x5811bf,null));};exports[_0xf515('0x21')]=function(_0x309203,_0x31c8de){if(_0x309203[_0xf515('0x46')]['id']){delete _0x309203[_0xf515('0x46')]['id'];}return db['SugarcrmConfiguration'][_0xf515('0x44')]({'where':{'id':_0x309203[_0xf515('0x47')]['id']}})[_0xf515('0x22')](handleEntityNotFound(_0x31c8de,null))['then'](saveUpdates(_0x309203['body'],null))[_0xf515('0x22')](respondWithResult(_0x31c8de,null))[_0xf515('0x41')](handleError(_0x31c8de,null));};exports[_0xf515('0x23')]=function(_0x5bb04e,_0x5db469){return db[_0xf515('0x3d')]['find']({'where':{'id':_0x5bb04e[_0xf515('0x47')]['id']}})['then'](handleEntityNotFound(_0x5db469,null))[_0xf515('0x22')](removeEntity(_0x5db469,null))[_0xf515('0x41')](handleError(_0x5db469,null));};exports[_0xf515('0x48')]=function(_0x3764a5,_0x15cce5,_0x2b1498){var _0x151dc2={};var _0x424750={};var _0x1b8c3c;var _0x4d4496;return db['SugarcrmConfiguration'][_0xf515('0x49')]({'where':{'id':_0x3764a5[_0xf515('0x47')]['id']}})['then'](handleEntityNotFound(_0x15cce5,null))[_0xf515('0x22')](function(_0x1d040e){if(_0x1d040e){_0x1b8c3c=_0x1d040e;_0x424750[_0xf515('0x29')]=_[_0xf515('0x2d')](db[_0xf515('0x4a')][_0xf515('0x25')]);_0x424750[_0xf515('0x2c')]=_[_0xf515('0x2d')](_0x3764a5['query']);_0x424750[_0xf515('0x2e')]=_[_0xf515('0x2f')](_0x424750[_0xf515('0x29')],_0x424750[_0xf515('0x2c')]);_0x151dc2[_0xf515('0x30')]=_[_0xf515('0x2f')](_0x424750[_0xf515('0x29')],qs['fields'](_0x3764a5[_0xf515('0x2c')][_0xf515('0x31')]));_0x151dc2[_0xf515('0x30')]=_0x151dc2['attributes'][_0xf515('0x43')]?_0x151dc2[_0xf515('0x30')]:_0x424750[_0xf515('0x29')];_0x151dc2[_0xf515('0x35')]=qs[_0xf515('0x36')](_0x3764a5[_0xf515('0x2c')]['sort']);_0x151dc2[_0xf515('0x39')]=qs['filters'](_['pick'](_0x3764a5[_0xf515('0x2c')],_0x424750[_0xf515('0x2e')]));if(_0x3764a5[_0xf515('0x2c')][_0xf515('0x38')]){_0x151dc2[_0xf515('0x39')]=_[_0xf515('0x3a')](_0x151dc2[_0xf515('0x39')],{'$or':_[_0xf515('0x2a')](_0x151dc2[_0xf515('0x30')],function(_0x61ae2f){var _0x213a1e={};_0x213a1e[_0x61ae2f]={'$like':'%'+_0x3764a5[_0xf515('0x2c')]['filter']+'%'};return _0x213a1e;})});}_0x151dc2=_['merge']({},_0x151dc2,_0x3764a5[_0xf515('0x3c')]);return _0x1b8c3c[_0xf515('0x48')](_0x151dc2);}})['then'](function(_0x571988){if(_0x571988){_0x4d4496=_0x571988['length'];if(!_0x3764a5[_0xf515('0x2c')][_0xf515('0x32')](_0xf515('0x33'))){_0x151dc2[_0xf515('0x1a')]=qs[_0xf515('0x1a')](_0x3764a5[_0xf515('0x2c')][_0xf515('0x1a')]);_0x151dc2['offset']=qs[_0xf515('0x34')](_0x3764a5[_0xf515('0x2c')]['offset']);}return _0x1b8c3c['getFields'](_0x151dc2);}})[_0xf515('0x22')](function(_0x491976){if(_0x491976){return _0x491976?{'count':_0x4d4496,'rows':_0x491976}:null;}})[_0xf515('0x22')](respondWithResult(_0x15cce5,null))[_0xf515('0x41')](handleError(_0x15cce5,null));};exports['getSubjects']=function(_0x202069,_0xd5ab1b,_0x2da167){var _0x4f3e7f={};var _0x2b1851={};var _0x445263;var _0x53c096;return db[_0xf515('0x3d')][_0xf515('0x49')]({'where':{'id':_0x202069['params']['id']}})[_0xf515('0x22')](handleEntityNotFound(_0xd5ab1b,null))[_0xf515('0x22')](function(_0x229bfa){if(_0x229bfa){_0x445263=_0x229bfa;_0x2b1851['model']=_[_0xf515('0x2d')](db[_0xf515('0x4a')]['rawAttributes']);_0x2b1851[_0xf515('0x2c')]=_[_0xf515('0x2d')](_0x202069[_0xf515('0x2c')]);_0x2b1851[_0xf515('0x2e')]=_['intersection'](_0x2b1851[_0xf515('0x29')],_0x2b1851['query']);_0x4f3e7f['attributes']=_[_0xf515('0x2f')](_0x2b1851['model'],qs[_0xf515('0x31')](_0x202069[_0xf515('0x2c')][_0xf515('0x31')]));_0x4f3e7f[_0xf515('0x30')]=_0x4f3e7f[_0xf515('0x30')][_0xf515('0x43')]?_0x4f3e7f[_0xf515('0x30')]:_0x2b1851[_0xf515('0x29')];_0x4f3e7f[_0xf515('0x35')]=qs[_0xf515('0x36')](_0x202069[_0xf515('0x2c')][_0xf515('0x36')]);_0x4f3e7f['where']=qs[_0xf515('0x2e')](_['pick'](_0x202069['query'],_0x2b1851[_0xf515('0x2e')]));if(_0x202069[_0xf515('0x2c')][_0xf515('0x38')]){_0x4f3e7f['where']=_[_0xf515('0x3a')](_0x4f3e7f[_0xf515('0x39')],{'$or':_['map'](_0x4f3e7f[_0xf515('0x30')],function(_0x3777bb){var _0x26d079={};_0x26d079[_0x3777bb]={'$like':'%'+_0x202069[_0xf515('0x2c')][_0xf515('0x38')]+'%'};return _0x26d079;})});}_0x4f3e7f=_[_0xf515('0x3a')]({},_0x4f3e7f,_0x202069[_0xf515('0x3c')]);return _0x445263[_0xf515('0x4b')](_0x4f3e7f);}})[_0xf515('0x22')](function(_0x265e13){if(_0x265e13){_0x53c096=_0x265e13[_0xf515('0x43')];if(!_0x202069['query'][_0xf515('0x32')](_0xf515('0x33'))){_0x4f3e7f[_0xf515('0x1a')]=qs['limit'](_0x202069[_0xf515('0x2c')][_0xf515('0x1a')]);_0x4f3e7f[_0xf515('0x34')]=qs[_0xf515('0x34')](_0x202069['query'][_0xf515('0x34')]);}return _0x445263[_0xf515('0x4b')](_0x4f3e7f);}})[_0xf515('0x22')](function(_0x3d5393){if(_0x3d5393){return _0x3d5393?{'count':_0x53c096,'rows':_0x3d5393}:null;}})[_0xf515('0x22')](respondWithResult(_0xd5ab1b,null))[_0xf515('0x41')](handleError(_0xd5ab1b,null));};exports['getDescriptions']=function(_0x310d32,_0x3468a5,_0x26d4b6){var _0x2d3fae={};var _0x252210={};var _0x4c93e2;var _0x5787c2;return db[_0xf515('0x3d')][_0xf515('0x49')]({'where':{'id':_0x310d32['params']['id']}})['then'](handleEntityNotFound(_0x3468a5,null))['then'](function(_0x5c8bc0){if(_0x5c8bc0){_0x4c93e2=_0x5c8bc0;_0x252210[_0xf515('0x29')]=_[_0xf515('0x2d')](db[_0xf515('0x4a')][_0xf515('0x25')]);_0x252210[_0xf515('0x2c')]=_['keys'](_0x310d32[_0xf515('0x2c')]);_0x252210[_0xf515('0x2e')]=_[_0xf515('0x2f')](_0x252210[_0xf515('0x29')],_0x252210['query']);_0x2d3fae[_0xf515('0x30')]=_[_0xf515('0x2f')](_0x252210[_0xf515('0x29')],qs[_0xf515('0x31')](_0x310d32[_0xf515('0x2c')][_0xf515('0x31')]));_0x2d3fae[_0xf515('0x30')]=_0x2d3fae[_0xf515('0x30')]['length']?_0x2d3fae[_0xf515('0x30')]:_0x252210[_0xf515('0x29')];_0x2d3fae[_0xf515('0x35')]=qs[_0xf515('0x36')](_0x310d32[_0xf515('0x2c')][_0xf515('0x36')]);_0x2d3fae['where']=qs['filters'](_['pick'](_0x310d32['query'],_0x252210[_0xf515('0x2e')]));if(_0x310d32[_0xf515('0x2c')][_0xf515('0x38')]){_0x2d3fae[_0xf515('0x39')]=_['merge'](_0x2d3fae[_0xf515('0x39')],{'$or':_[_0xf515('0x2a')](_0x2d3fae[_0xf515('0x30')],function(_0x230bb4){var _0x5ce285={};_0x5ce285[_0x230bb4]={'$like':'%'+_0x310d32[_0xf515('0x2c')]['filter']+'%'};return _0x5ce285;})});}_0x2d3fae=_[_0xf515('0x3a')]({},_0x2d3fae,_0x310d32[_0xf515('0x3c')]);return _0x4c93e2[_0xf515('0x4c')](_0x2d3fae);}})[_0xf515('0x22')](function(_0x4765b3){if(_0x4765b3){_0x5787c2=_0x4765b3['length'];if(!_0x310d32[_0xf515('0x2c')][_0xf515('0x32')](_0xf515('0x33'))){_0x2d3fae[_0xf515('0x1a')]=qs[_0xf515('0x1a')](_0x310d32[_0xf515('0x2c')][_0xf515('0x1a')]);_0x2d3fae[_0xf515('0x34')]=qs[_0xf515('0x34')](_0x310d32[_0xf515('0x2c')][_0xf515('0x34')]);}return _0x4c93e2[_0xf515('0x4c')](_0x2d3fae);}})['then'](function(_0x33eff8){if(_0x33eff8){return _0x33eff8?{'count':_0x5787c2,'rows':_0x33eff8}:null;}})['then'](respondWithResult(_0x3468a5,null))['catch'](handleError(_0x3468a5,null));};