045667af843a7c6546341d3753df7913ddc5e273
[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 _0xf624=['findOne','getFields','getSubjects','getDescriptions','eml-format','rimraf','zip-dir','fast-json-patch','mustache','util','fs-extra','lodash','squel','crypto','jsforce','desk.js','papaparse','ioredis','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status','end','json','offset','undefined','limit','count','Content-Range','apply','reject','save','update','then','stack','name','send','index','map','SugarcrmConfiguration','fieldName','key','model','query','keys','attributes','fields','length','hasOwnProperty','nolimit','order','sort','where','filters','pick','merge','type','filter','options','includeAll','include','findAll','rows','catch','params','rawAttributes','intersection','find','create','body','destroy'];(function(_0x442005,_0x5b7454){var _0x28ece2=function(_0x1d21a4){while(--_0x1d21a4){_0x442005['push'](_0x442005['shift']());}};_0x28ece2(++_0x5b7454);}(_0xf624,0x13c));var _0x4f62=function(_0x1607b7,_0x2a438f){_0x1607b7=_0x1607b7-0x0;var _0x73ab3f=_0xf624[_0x1607b7];return _0x73ab3f;};'use strict';var emlformat=require(_0x4f62('0x0'));var rimraf=require(_0x4f62('0x1'));var zipdir=require(_0x4f62('0x2'));var jsonpatch=require(_0x4f62('0x3'));var rp=require('request-promise');var moment=require('moment');var BPromise=require('bluebird');var Mustache=require(_0x4f62('0x4'));var util=require(_0x4f62('0x5'));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(_0x4f62('0x6'));var _=require(_0x4f62('0x7'));var squel=require(_0x4f62('0x8'));var crypto=require(_0x4f62('0x9'));var jsforce=require(_0x4f62('0xa'));var deskjs=require(_0x4f62('0xb'));var toCsv=require('to-csv');var querystring=require('querystring');var Papa=require(_0x4f62('0xc'));var Redis=require(_0x4f62('0xd'));var authService=require('../../components/auth/service');var qs=require(_0x4f62('0xe'));var as=require(_0x4f62('0xf'));var hardwareService=require(_0x4f62('0x10'));var logger=require(_0x4f62('0x11'))(_0x4f62('0x12'));var utils=require(_0x4f62('0x13'));var config=require(_0x4f62('0x14'));var licenseUtil=require(_0x4f62('0x15'));var db=require(_0x4f62('0x16'))['db'];function respondWithStatusCode(_0x3e759b,_0x16b4c7){_0x16b4c7=_0x16b4c7||0xcc;return function(_0x218ce4){if(_0x218ce4){return _0x3e759b[_0x4f62('0x17')](_0x16b4c7);}return _0x3e759b[_0x4f62('0x18')](_0x16b4c7)[_0x4f62('0x19')]();};}function respondWithResult(_0x54ed2d,_0x53d2a2){_0x53d2a2=_0x53d2a2||0xc8;return function(_0xa80e48){if(_0xa80e48){return _0x54ed2d[_0x4f62('0x18')](_0x53d2a2)[_0x4f62('0x1a')](_0xa80e48);}};}function respondWithFilteredResult(_0x5e390f,_0x9c18bc){return function(_0x46392e){if(_0x46392e){var _0x4c1c20=typeof _0x9c18bc[_0x4f62('0x1b')]===_0x4f62('0x1c')&&typeof _0x9c18bc[_0x4f62('0x1d')]===_0x4f62('0x1c');var _0x46a6a9=_0x46392e[_0x4f62('0x1e')];var _0x25d57c=_0x4c1c20?0x0:_0x9c18bc[_0x4f62('0x1b')];var _0x3c1be2=_0x4c1c20?_0x46392e[_0x4f62('0x1e')]:_0x9c18bc[_0x4f62('0x1b')]+_0x9c18bc['limit'];var _0x27b316;if(_0x3c1be2>=_0x46a6a9){_0x3c1be2=_0x46a6a9;_0x27b316=0xc8;}else{_0x27b316=0xce;}_0x5e390f['status'](_0x27b316);return _0x5e390f['set'](_0x4f62('0x1f'),_0x25d57c+'-'+_0x3c1be2+'/'+_0x46a6a9)[_0x4f62('0x1a')](_0x46392e);}return null;};}function patchUpdates(_0x25a97c){return function(_0x40394a){try{jsonpatch[_0x4f62('0x20')](_0x40394a,_0x25a97c,!![]);}catch(_0xa870db){return BPromise[_0x4f62('0x21')](_0xa870db);}return _0x40394a[_0x4f62('0x22')]();};}function saveUpdates(_0x10d05d,_0x3c7fea){return function(_0x2330bb){if(_0x2330bb){return _0x2330bb[_0x4f62('0x23')](_0x10d05d)['then'](function(_0x379a88){return _0x379a88;});}return null;};}function removeEntity(_0x1ba93e,_0x46910a){return function(_0x33a526){if(_0x33a526){return _0x33a526['destroy']()[_0x4f62('0x24')](function(){_0x1ba93e[_0x4f62('0x18')](0xcc)[_0x4f62('0x19')]();});}};}function handleEntityNotFound(_0x2f205a,_0x3460a4){return function(_0x4935a5){if(!_0x4935a5){_0x2f205a['sendStatus'](0x194);}return _0x4935a5;};}function handleError(_0x208405,_0x54632e){_0x54632e=_0x54632e||0x1f4;return function(_0x4f186c){logger['error'](_0x4f186c[_0x4f62('0x25')]);if(_0x4f186c[_0x4f62('0x26')]){delete _0x4f186c[_0x4f62('0x26')];}_0x208405[_0x4f62('0x18')](_0x54632e)[_0x4f62('0x27')](_0x4f186c);};}exports[_0x4f62('0x28')]=function(_0x788594,_0x322139){var _0x3b9252={},_0x1f6865={},_0x2342c8={'count':0x0,'rows':[]};var _0x30b392=_[_0x4f62('0x29')](db[_0x4f62('0x2a')]['rawAttributes'],function(_0x477258){return{'name':_0x477258[_0x4f62('0x2b')],'type':_0x477258['type'][_0x4f62('0x2c')]};});_0x1f6865[_0x4f62('0x2d')]=_['map'](_0x30b392,'name');_0x1f6865[_0x4f62('0x2e')]=_[_0x4f62('0x2f')](_0x788594['query']);_0x1f6865['filters']=_['intersection'](_0x1f6865[_0x4f62('0x2d')],_0x1f6865[_0x4f62('0x2e')]);_0x3b9252[_0x4f62('0x30')]=_['intersection'](_0x1f6865['model'],qs['fields'](_0x788594[_0x4f62('0x2e')][_0x4f62('0x31')]));_0x3b9252[_0x4f62('0x30')]=_0x3b9252[_0x4f62('0x30')][_0x4f62('0x32')]?_0x3b9252[_0x4f62('0x30')]:_0x1f6865['model'];if(!_0x788594[_0x4f62('0x2e')][_0x4f62('0x33')](_0x4f62('0x34'))){_0x3b9252[_0x4f62('0x1d')]=qs[_0x4f62('0x1d')](_0x788594[_0x4f62('0x2e')][_0x4f62('0x1d')]);_0x3b9252[_0x4f62('0x1b')]=qs[_0x4f62('0x1b')](_0x788594[_0x4f62('0x2e')][_0x4f62('0x1b')]);}_0x3b9252[_0x4f62('0x35')]=qs[_0x4f62('0x36')](_0x788594[_0x4f62('0x2e')][_0x4f62('0x36')]);_0x3b9252[_0x4f62('0x37')]=qs[_0x4f62('0x38')](_[_0x4f62('0x39')](_0x788594[_0x4f62('0x2e')],_0x1f6865[_0x4f62('0x38')]),_0x30b392);if(_0x788594[_0x4f62('0x2e')]['filter']){_0x3b9252['where']=_[_0x4f62('0x3a')](_0x3b9252[_0x4f62('0x37')],{'$or':_[_0x4f62('0x29')](_0x30b392,function(_0x1363c2){if(_0x1363c2[_0x4f62('0x3b')]!=='VIRTUAL'){var _0x4429ec={};_0x4429ec[_0x1363c2['name']]={'$like':'%'+_0x788594[_0x4f62('0x2e')][_0x4f62('0x3c')]+'%'};return _0x4429ec;}})});}_0x3b9252=_['merge']({},_0x3b9252,_0x788594[_0x4f62('0x3d')]);var _0xb1d1e4={'where':_0x3b9252[_0x4f62('0x37')]};return db['SugarcrmConfiguration']['count'](_0xb1d1e4)[_0x4f62('0x24')](function(_0xb830d3){_0x2342c8['count']=_0xb830d3;if(_0x788594[_0x4f62('0x2e')][_0x4f62('0x3e')]){_0x3b9252[_0x4f62('0x3f')]=[{'all':!![]}];}return db['SugarcrmConfiguration'][_0x4f62('0x40')](_0x3b9252);})['then'](function(_0xa6ab93){_0x2342c8[_0x4f62('0x41')]=_0xa6ab93;return _0x2342c8;})['then'](respondWithFilteredResult(_0x322139,_0x3b9252))[_0x4f62('0x42')](handleError(_0x322139,null));};exports['show']=function(_0x56c7ab,_0x2f5531){var _0x590db8={'raw':![],'where':{'id':_0x56c7ab[_0x4f62('0x43')]['id']}},_0x4624fc={};_0x4624fc[_0x4f62('0x2d')]=_[_0x4f62('0x2f')](db['SugarcrmConfiguration'][_0x4f62('0x44')]);_0x4624fc[_0x4f62('0x2e')]=_[_0x4f62('0x2f')](_0x56c7ab[_0x4f62('0x2e')]);_0x4624fc[_0x4f62('0x38')]=_['intersection'](_0x4624fc[_0x4f62('0x2d')],_0x4624fc[_0x4f62('0x2e')]);_0x590db8['attributes']=_[_0x4f62('0x45')](_0x4624fc[_0x4f62('0x2d')],qs['fields'](_0x56c7ab['query'][_0x4f62('0x31')]));_0x590db8[_0x4f62('0x30')]=_0x590db8[_0x4f62('0x30')][_0x4f62('0x32')]?_0x590db8[_0x4f62('0x30')]:_0x4624fc[_0x4f62('0x2d')];if(_0x56c7ab['query'][_0x4f62('0x3e')]){_0x590db8['include']=[{'all':!![]}];}_0x590db8=_[_0x4f62('0x3a')]({},_0x590db8,_0x56c7ab['options']);return db[_0x4f62('0x2a')][_0x4f62('0x46')](_0x590db8)[_0x4f62('0x24')](handleEntityNotFound(_0x2f5531,null))[_0x4f62('0x24')](respondWithResult(_0x2f5531,null))[_0x4f62('0x42')](handleError(_0x2f5531,null));};exports[_0x4f62('0x47')]=function(_0x28896c,_0x535096){return db[_0x4f62('0x2a')]['create'](_0x28896c[_0x4f62('0x48')],{})[_0x4f62('0x24')](respondWithResult(_0x535096,0xc9))[_0x4f62('0x42')](handleError(_0x535096,null));};exports[_0x4f62('0x23')]=function(_0x462cd2,_0x7c0a35){if(_0x462cd2[_0x4f62('0x48')]['id']){delete _0x462cd2['body']['id'];}return db[_0x4f62('0x2a')][_0x4f62('0x46')]({'where':{'id':_0x462cd2['params']['id']}})['then'](handleEntityNotFound(_0x7c0a35,null))[_0x4f62('0x24')](saveUpdates(_0x462cd2['body'],null))[_0x4f62('0x24')](respondWithResult(_0x7c0a35,null))[_0x4f62('0x42')](handleError(_0x7c0a35,null));};exports[_0x4f62('0x49')]=function(_0x30a6ec,_0xfaa055){return db[_0x4f62('0x2a')][_0x4f62('0x46')]({'where':{'id':_0x30a6ec[_0x4f62('0x43')]['id']}})[_0x4f62('0x24')](handleEntityNotFound(_0xfaa055,null))['then'](removeEntity(_0xfaa055,null))[_0x4f62('0x42')](handleError(_0xfaa055,null));};exports['getFields']=function(_0x31cbc9,_0x24652e,_0x2a7bb7){var _0x344386={};var _0x1b6097={};var _0x5de6f8;var _0x4f9f78;return db[_0x4f62('0x2a')][_0x4f62('0x4a')]({'where':{'id':_0x31cbc9['params']['id']}})['then'](handleEntityNotFound(_0x24652e,null))[_0x4f62('0x24')](function(_0xe3cadd){if(_0xe3cadd){_0x5de6f8=_0xe3cadd;_0x1b6097['model']=_['keys'](db['SugarcrmField'][_0x4f62('0x44')]);_0x1b6097['query']=_[_0x4f62('0x2f')](_0x31cbc9[_0x4f62('0x2e')]);_0x1b6097[_0x4f62('0x38')]=_[_0x4f62('0x45')](_0x1b6097['model'],_0x1b6097[_0x4f62('0x2e')]);_0x344386[_0x4f62('0x30')]=_[_0x4f62('0x45')](_0x1b6097['model'],qs[_0x4f62('0x31')](_0x31cbc9[_0x4f62('0x2e')]['fields']));_0x344386[_0x4f62('0x30')]=_0x344386['attributes']['length']?_0x344386[_0x4f62('0x30')]:_0x1b6097[_0x4f62('0x2d')];_0x344386['order']=qs[_0x4f62('0x36')](_0x31cbc9[_0x4f62('0x2e')][_0x4f62('0x36')]);_0x344386[_0x4f62('0x37')]=qs['filters'](_[_0x4f62('0x39')](_0x31cbc9[_0x4f62('0x2e')],_0x1b6097['filters']));if(_0x31cbc9['query'][_0x4f62('0x3c')]){_0x344386['where']=_[_0x4f62('0x3a')](_0x344386[_0x4f62('0x37')],{'$or':_[_0x4f62('0x29')](_0x344386[_0x4f62('0x30')],function(_0x5c75fb){var _0x132d56={};_0x132d56[_0x5c75fb]={'$like':'%'+_0x31cbc9['query']['filter']+'%'};return _0x132d56;})});}_0x344386=_[_0x4f62('0x3a')]({},_0x344386,_0x31cbc9[_0x4f62('0x3d')]);return _0x5de6f8[_0x4f62('0x4b')](_0x344386);}})['then'](function(_0xa86dfe){if(_0xa86dfe){_0x4f9f78=_0xa86dfe['length'];if(!_0x31cbc9[_0x4f62('0x2e')][_0x4f62('0x33')](_0x4f62('0x34'))){_0x344386['limit']=qs['limit'](_0x31cbc9['query']['limit']);_0x344386[_0x4f62('0x1b')]=qs['offset'](_0x31cbc9[_0x4f62('0x2e')][_0x4f62('0x1b')]);}return _0x5de6f8['getFields'](_0x344386);}})['then'](function(_0x3ee3e5){if(_0x3ee3e5){return _0x3ee3e5?{'count':_0x4f9f78,'rows':_0x3ee3e5}:null;}})['then'](respondWithResult(_0x24652e,null))[_0x4f62('0x42')](handleError(_0x24652e,null));};exports[_0x4f62('0x4c')]=function(_0x36fe36,_0x29679e,_0x1d025d){var _0xb48e7c={};var _0x441dc0={};var _0x59c7e1;var _0x41ecbb;return db[_0x4f62('0x2a')][_0x4f62('0x4a')]({'where':{'id':_0x36fe36['params']['id']}})['then'](handleEntityNotFound(_0x29679e,null))['then'](function(_0x191fff){if(_0x191fff){_0x59c7e1=_0x191fff;_0x441dc0[_0x4f62('0x2d')]=_[_0x4f62('0x2f')](db['SugarcrmField'][_0x4f62('0x44')]);_0x441dc0['query']=_[_0x4f62('0x2f')](_0x36fe36[_0x4f62('0x2e')]);_0x441dc0['filters']=_[_0x4f62('0x45')](_0x441dc0[_0x4f62('0x2d')],_0x441dc0['query']);_0xb48e7c[_0x4f62('0x30')]=_[_0x4f62('0x45')](_0x441dc0[_0x4f62('0x2d')],qs[_0x4f62('0x31')](_0x36fe36[_0x4f62('0x2e')][_0x4f62('0x31')]));_0xb48e7c[_0x4f62('0x30')]=_0xb48e7c[_0x4f62('0x30')][_0x4f62('0x32')]?_0xb48e7c['attributes']:_0x441dc0['model'];_0xb48e7c[_0x4f62('0x35')]=qs['sort'](_0x36fe36[_0x4f62('0x2e')]['sort']);_0xb48e7c['where']=qs[_0x4f62('0x38')](_[_0x4f62('0x39')](_0x36fe36['query'],_0x441dc0[_0x4f62('0x38')]));if(_0x36fe36[_0x4f62('0x2e')][_0x4f62('0x3c')]){_0xb48e7c[_0x4f62('0x37')]=_[_0x4f62('0x3a')](_0xb48e7c['where'],{'$or':_[_0x4f62('0x29')](_0xb48e7c[_0x4f62('0x30')],function(_0x57a160){var _0x5b8014={};_0x5b8014[_0x57a160]={'$like':'%'+_0x36fe36[_0x4f62('0x2e')][_0x4f62('0x3c')]+'%'};return _0x5b8014;})});}_0xb48e7c=_[_0x4f62('0x3a')]({},_0xb48e7c,_0x36fe36['options']);return _0x59c7e1[_0x4f62('0x4c')](_0xb48e7c);}})['then'](function(_0x1bc589){if(_0x1bc589){_0x41ecbb=_0x1bc589[_0x4f62('0x32')];if(!_0x36fe36[_0x4f62('0x2e')][_0x4f62('0x33')](_0x4f62('0x34'))){_0xb48e7c[_0x4f62('0x1d')]=qs[_0x4f62('0x1d')](_0x36fe36[_0x4f62('0x2e')][_0x4f62('0x1d')]);_0xb48e7c[_0x4f62('0x1b')]=qs[_0x4f62('0x1b')](_0x36fe36[_0x4f62('0x2e')]['offset']);}return _0x59c7e1[_0x4f62('0x4c')](_0xb48e7c);}})[_0x4f62('0x24')](function(_0xf4c805){if(_0xf4c805){return _0xf4c805?{'count':_0x41ecbb,'rows':_0xf4c805}:null;}})[_0x4f62('0x24')](respondWithResult(_0x29679e,null))['catch'](handleError(_0x29679e,null));};exports['getDescriptions']=function(_0xd03fe3,_0x36ab80,_0x413644){var _0x304b08={};var _0xf9c851={};var _0x3dab75;var _0x161d68;return db['SugarcrmConfiguration'][_0x4f62('0x4a')]({'where':{'id':_0xd03fe3[_0x4f62('0x43')]['id']}})['then'](handleEntityNotFound(_0x36ab80,null))[_0x4f62('0x24')](function(_0x3e86f4){if(_0x3e86f4){_0x3dab75=_0x3e86f4;_0xf9c851[_0x4f62('0x2d')]=_[_0x4f62('0x2f')](db['SugarcrmField'][_0x4f62('0x44')]);_0xf9c851[_0x4f62('0x2e')]=_['keys'](_0xd03fe3['query']);_0xf9c851[_0x4f62('0x38')]=_[_0x4f62('0x45')](_0xf9c851[_0x4f62('0x2d')],_0xf9c851['query']);_0x304b08[_0x4f62('0x30')]=_[_0x4f62('0x45')](_0xf9c851['model'],qs['fields'](_0xd03fe3['query'][_0x4f62('0x31')]));_0x304b08[_0x4f62('0x30')]=_0x304b08[_0x4f62('0x30')][_0x4f62('0x32')]?_0x304b08[_0x4f62('0x30')]:_0xf9c851['model'];_0x304b08[_0x4f62('0x35')]=qs[_0x4f62('0x36')](_0xd03fe3[_0x4f62('0x2e')]['sort']);_0x304b08[_0x4f62('0x37')]=qs[_0x4f62('0x38')](_[_0x4f62('0x39')](_0xd03fe3[_0x4f62('0x2e')],_0xf9c851[_0x4f62('0x38')]));if(_0xd03fe3[_0x4f62('0x2e')][_0x4f62('0x3c')]){_0x304b08[_0x4f62('0x37')]=_[_0x4f62('0x3a')](_0x304b08['where'],{'$or':_['map'](_0x304b08[_0x4f62('0x30')],function(_0x47aadd){var _0xa396ba={};_0xa396ba[_0x47aadd]={'$like':'%'+_0xd03fe3['query'][_0x4f62('0x3c')]+'%'};return _0xa396ba;})});}_0x304b08=_[_0x4f62('0x3a')]({},_0x304b08,_0xd03fe3[_0x4f62('0x3d')]);return _0x3dab75[_0x4f62('0x4d')](_0x304b08);}})[_0x4f62('0x24')](function(_0x398511){if(_0x398511){_0x161d68=_0x398511[_0x4f62('0x32')];if(!_0xd03fe3[_0x4f62('0x2e')][_0x4f62('0x33')](_0x4f62('0x34'))){_0x304b08[_0x4f62('0x1d')]=qs[_0x4f62('0x1d')](_0xd03fe3[_0x4f62('0x2e')][_0x4f62('0x1d')]);_0x304b08[_0x4f62('0x1b')]=qs[_0x4f62('0x1b')](_0xd03fe3['query'][_0x4f62('0x1b')]);}return _0x3dab75[_0x4f62('0x4d')](_0x304b08);}})[_0x4f62('0x24')](function(_0x146a7b){if(_0x146a7b){return _0x146a7b?{'count':_0x161d68,'rows':_0x146a7b}:null;}})['then'](respondWithResult(_0x36ab80,null))[_0x4f62('0x42')](handleError(_0x36ab80,null));};