86c51ebfd5be7e01e259d6ce0c86174caafcf5b8
[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 _0x243f=['bluebird','util','ejs','lodash','squel','crypto','jsforce','desk.js','to-csv','querystring','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','api','../../config/utils','../../config/environment','../../config/license/util','sendStatus','end','status','json','limit','undefined','count','offset','set','Content-Range','apply','update','then','destroy','error','stack','name','send','index','map','SugarcrmConfiguration','rawAttributes','fieldName','type','key','model','query','keys','intersection','attributes','fields','length','hasOwnProperty','nolimit','sort','where','filters','filter','merge','VIRTUAL','options','includeAll','include','rows','show','params','find','catch','create','body','getFields','findOne','order','pick','getSubjects','getDescriptions','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment'];(function(_0x251751,_0x113a01){var _0x53ee2d=function(_0x361b44){while(--_0x361b44){_0x251751['push'](_0x251751['shift']());}};_0x53ee2d(++_0x113a01);}(_0x243f,0x185));var _0xf243=function(_0x377f50,_0x5589ba){_0x377f50=_0x377f50-0x0;var _0x14a9d2=_0x243f[_0x377f50];return _0x14a9d2;};'use strict';var emlformat=require(_0xf243('0x0'));var rimraf=require(_0xf243('0x1'));var zipdir=require(_0xf243('0x2'));var jsonpatch=require(_0xf243('0x3'));var rp=require(_0xf243('0x4'));var moment=require(_0xf243('0x5'));var BPromise=require(_0xf243('0x6'));var Mustache=require('mustache');var util=require(_0xf243('0x7'));var path=require('path');var sox=require('sox');var csv=require('to-csv');var ejs=require(_0xf243('0x8'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0xf243('0x9'));var squel=require(_0xf243('0xa'));var crypto=require(_0xf243('0xb'));var jsforce=require(_0xf243('0xc'));var deskjs=require(_0xf243('0xd'));var toCsv=require(_0xf243('0xe'));var querystring=require(_0xf243('0xf'));var Papa=require('papaparse');var Redis=require('ioredis');var authService=require('../../components/auth/service');var qs=require(_0xf243('0x10'));var as=require(_0xf243('0x11'));var hardwareService=require(_0xf243('0x12'));var logger=require('../../config/logger')(_0xf243('0x13'));var utils=require(_0xf243('0x14'));var config=require(_0xf243('0x15'));var licenseUtil=require(_0xf243('0x16'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x1a9c71,_0x297edf){_0x297edf=_0x297edf||0xcc;return function(_0x5a47da){if(_0x5a47da){return _0x1a9c71[_0xf243('0x17')](_0x297edf);}return _0x1a9c71['status'](_0x297edf)[_0xf243('0x18')]();};}function respondWithResult(_0x40f27d,_0x74b6f6){_0x74b6f6=_0x74b6f6||0xc8;return function(_0x2cf789){if(_0x2cf789){return _0x40f27d[_0xf243('0x19')](_0x74b6f6)[_0xf243('0x1a')](_0x2cf789);}};}function respondWithFilteredResult(_0x2bb828,_0x5bdd88){return function(_0x15c386){if(_0x15c386){var _0x4ee0b8=typeof _0x5bdd88['offset']==='undefined'&&typeof _0x5bdd88[_0xf243('0x1b')]===_0xf243('0x1c');var _0x2a7950=_0x15c386[_0xf243('0x1d')];var _0x3a89b6=_0x4ee0b8?0x0:_0x5bdd88[_0xf243('0x1e')];var _0x119c4a=_0x4ee0b8?_0x15c386[_0xf243('0x1d')]:_0x5bdd88[_0xf243('0x1e')]+_0x5bdd88[_0xf243('0x1b')];var _0x3fcc31;if(_0x119c4a>=_0x2a7950){_0x119c4a=_0x2a7950;_0x3fcc31=0xc8;}else{_0x3fcc31=0xce;}_0x2bb828[_0xf243('0x19')](_0x3fcc31);return _0x2bb828[_0xf243('0x1f')](_0xf243('0x20'),_0x3a89b6+'-'+_0x119c4a+'/'+_0x2a7950)[_0xf243('0x1a')](_0x15c386);}return null;};}function patchUpdates(_0x8b7a1b){return function(_0xce4b40){try{jsonpatch[_0xf243('0x21')](_0xce4b40,_0x8b7a1b,!![]);}catch(_0x75c422){return BPromise['reject'](_0x75c422);}return _0xce4b40['save']();};}function saveUpdates(_0x2ded2c,_0x518c09){return function(_0x30d540){if(_0x30d540){return _0x30d540[_0xf243('0x22')](_0x2ded2c)[_0xf243('0x23')](function(_0x397918){return _0x397918;});}return null;};}function removeEntity(_0x4da784,_0x5329d8){return function(_0xfb598){if(_0xfb598){return _0xfb598[_0xf243('0x24')]()[_0xf243('0x23')](function(){_0x4da784[_0xf243('0x19')](0xcc)[_0xf243('0x18')]();});}};}function handleEntityNotFound(_0x2a6a81,_0x3fa57c){return function(_0x38f84a){if(!_0x38f84a){_0x2a6a81[_0xf243('0x17')](0x194);}return _0x38f84a;};}function handleError(_0x4fe035,_0x58b5a0){_0x58b5a0=_0x58b5a0||0x1f4;return function(_0x525350){logger[_0xf243('0x25')](_0x525350[_0xf243('0x26')]);if(_0x525350['name']){delete _0x525350[_0xf243('0x27')];}_0x4fe035[_0xf243('0x19')](_0x58b5a0)[_0xf243('0x28')](_0x525350);};}exports[_0xf243('0x29')]=function(_0x1bbfc9,_0x9df574){var _0x12f809={},_0xe88db0={},_0x449f5f={'count':0x0,'rows':[]};var _0x319a2c=_[_0xf243('0x2a')](db[_0xf243('0x2b')][_0xf243('0x2c')],function(_0x49238e){return{'name':_0x49238e[_0xf243('0x2d')],'type':_0x49238e[_0xf243('0x2e')][_0xf243('0x2f')]};});_0xe88db0[_0xf243('0x30')]=_[_0xf243('0x2a')](_0x319a2c,_0xf243('0x27'));_0xe88db0[_0xf243('0x31')]=_[_0xf243('0x32')](_0x1bbfc9['query']);_0xe88db0['filters']=_[_0xf243('0x33')](_0xe88db0[_0xf243('0x30')],_0xe88db0[_0xf243('0x31')]);_0x12f809[_0xf243('0x34')]=_[_0xf243('0x33')](_0xe88db0[_0xf243('0x30')],qs['fields'](_0x1bbfc9[_0xf243('0x31')][_0xf243('0x35')]));_0x12f809[_0xf243('0x34')]=_0x12f809[_0xf243('0x34')][_0xf243('0x36')]?_0x12f809[_0xf243('0x34')]:_0xe88db0[_0xf243('0x30')];if(!_0x1bbfc9[_0xf243('0x31')][_0xf243('0x37')](_0xf243('0x38'))){_0x12f809[_0xf243('0x1b')]=qs['limit'](_0x1bbfc9[_0xf243('0x31')][_0xf243('0x1b')]);_0x12f809[_0xf243('0x1e')]=qs[_0xf243('0x1e')](_0x1bbfc9[_0xf243('0x31')]['offset']);}_0x12f809['order']=qs['sort'](_0x1bbfc9['query'][_0xf243('0x39')]);_0x12f809[_0xf243('0x3a')]=qs[_0xf243('0x3b')](_['pick'](_0x1bbfc9[_0xf243('0x31')],_0xe88db0[_0xf243('0x3b')]),_0x319a2c);if(_0x1bbfc9[_0xf243('0x31')][_0xf243('0x3c')]){_0x12f809[_0xf243('0x3a')]=_[_0xf243('0x3d')](_0x12f809[_0xf243('0x3a')],{'$or':_[_0xf243('0x2a')](_0x319a2c,function(_0x595d77){if(_0x595d77[_0xf243('0x2e')]!==_0xf243('0x3e')){var _0x16e0fe={};_0x16e0fe[_0x595d77[_0xf243('0x27')]]={'$like':'%'+_0x1bbfc9[_0xf243('0x31')][_0xf243('0x3c')]+'%'};return _0x16e0fe;}})});}_0x12f809=_['merge']({},_0x12f809,_0x1bbfc9[_0xf243('0x3f')]);var _0x104783={'where':_0x12f809[_0xf243('0x3a')]};return db[_0xf243('0x2b')][_0xf243('0x1d')](_0x104783)['then'](function(_0x196d05){_0x449f5f['count']=_0x196d05;if(_0x1bbfc9[_0xf243('0x31')][_0xf243('0x40')]){_0x12f809[_0xf243('0x41')]=[{'all':!![]}];}return db['SugarcrmConfiguration']['findAll'](_0x12f809);})[_0xf243('0x23')](function(_0x7cfc38){_0x449f5f[_0xf243('0x42')]=_0x7cfc38;return _0x449f5f;})[_0xf243('0x23')](respondWithFilteredResult(_0x9df574,_0x12f809))['catch'](handleError(_0x9df574,null));};exports[_0xf243('0x43')]=function(_0x376265,_0x23d241){var _0x4e0f6a={'raw':![],'where':{'id':_0x376265[_0xf243('0x44')]['id']}},_0x5cce1a={};_0x5cce1a['model']=_[_0xf243('0x32')](db[_0xf243('0x2b')][_0xf243('0x2c')]);_0x5cce1a[_0xf243('0x31')]=_[_0xf243('0x32')](_0x376265[_0xf243('0x31')]);_0x5cce1a[_0xf243('0x3b')]=_['intersection'](_0x5cce1a[_0xf243('0x30')],_0x5cce1a[_0xf243('0x31')]);_0x4e0f6a['attributes']=_[_0xf243('0x33')](_0x5cce1a['model'],qs[_0xf243('0x35')](_0x376265[_0xf243('0x31')][_0xf243('0x35')]));_0x4e0f6a['attributes']=_0x4e0f6a[_0xf243('0x34')]['length']?_0x4e0f6a[_0xf243('0x34')]:_0x5cce1a[_0xf243('0x30')];if(_0x376265['query'][_0xf243('0x40')]){_0x4e0f6a[_0xf243('0x41')]=[{'all':!![]}];}_0x4e0f6a=_[_0xf243('0x3d')]({},_0x4e0f6a,_0x376265['options']);return db[_0xf243('0x2b')][_0xf243('0x45')](_0x4e0f6a)[_0xf243('0x23')](handleEntityNotFound(_0x23d241,null))[_0xf243('0x23')](respondWithResult(_0x23d241,null))[_0xf243('0x46')](handleError(_0x23d241,null));};exports[_0xf243('0x47')]=function(_0xde7c83,_0x137c39){return db[_0xf243('0x2b')][_0xf243('0x47')](_0xde7c83[_0xf243('0x48')],{})[_0xf243('0x23')](respondWithResult(_0x137c39,0xc9))[_0xf243('0x46')](handleError(_0x137c39,null));};exports[_0xf243('0x22')]=function(_0x6bde11,_0x4346e5){if(_0x6bde11[_0xf243('0x48')]['id']){delete _0x6bde11[_0xf243('0x48')]['id'];}return db[_0xf243('0x2b')][_0xf243('0x45')]({'where':{'id':_0x6bde11['params']['id']}})[_0xf243('0x23')](handleEntityNotFound(_0x4346e5,null))[_0xf243('0x23')](saveUpdates(_0x6bde11[_0xf243('0x48')],null))[_0xf243('0x23')](respondWithResult(_0x4346e5,null))[_0xf243('0x46')](handleError(_0x4346e5,null));};exports['destroy']=function(_0x5927e6,_0x56c916){return db['SugarcrmConfiguration'][_0xf243('0x45')]({'where':{'id':_0x5927e6[_0xf243('0x44')]['id']}})['then'](handleEntityNotFound(_0x56c916,null))['then'](removeEntity(_0x56c916,null))[_0xf243('0x46')](handleError(_0x56c916,null));};exports[_0xf243('0x49')]=function(_0x28806e,_0x32ef28,_0x151932){var _0x4e6480={};var _0x41775f={};var _0x2953ca;var _0x275501;return db[_0xf243('0x2b')][_0xf243('0x4a')]({'where':{'id':_0x28806e[_0xf243('0x44')]['id']}})[_0xf243('0x23')](handleEntityNotFound(_0x32ef28,null))[_0xf243('0x23')](function(_0xe4ec30){if(_0xe4ec30){_0x2953ca=_0xe4ec30;_0x41775f['model']=_[_0xf243('0x32')](db['SugarcrmField'][_0xf243('0x2c')]);_0x41775f[_0xf243('0x31')]=_['keys'](_0x28806e[_0xf243('0x31')]);_0x41775f[_0xf243('0x3b')]=_[_0xf243('0x33')](_0x41775f[_0xf243('0x30')],_0x41775f[_0xf243('0x31')]);_0x4e6480[_0xf243('0x34')]=_[_0xf243('0x33')](_0x41775f[_0xf243('0x30')],qs[_0xf243('0x35')](_0x28806e['query'][_0xf243('0x35')]));_0x4e6480[_0xf243('0x34')]=_0x4e6480[_0xf243('0x34')][_0xf243('0x36')]?_0x4e6480[_0xf243('0x34')]:_0x41775f[_0xf243('0x30')];_0x4e6480[_0xf243('0x4b')]=qs['sort'](_0x28806e[_0xf243('0x31')][_0xf243('0x39')]);_0x4e6480[_0xf243('0x3a')]=qs[_0xf243('0x3b')](_[_0xf243('0x4c')](_0x28806e[_0xf243('0x31')],_0x41775f['filters']));if(_0x28806e[_0xf243('0x31')]['filter']){_0x4e6480[_0xf243('0x3a')]=_[_0xf243('0x3d')](_0x4e6480[_0xf243('0x3a')],{'$or':_[_0xf243('0x2a')](_0x4e6480['attributes'],function(_0x29d055){var _0x5c7578={};_0x5c7578[_0x29d055]={'$like':'%'+_0x28806e[_0xf243('0x31')][_0xf243('0x3c')]+'%'};return _0x5c7578;})});}_0x4e6480=_[_0xf243('0x3d')]({},_0x4e6480,_0x28806e[_0xf243('0x3f')]);return _0x2953ca[_0xf243('0x49')](_0x4e6480);}})['then'](function(_0x44d8e0){if(_0x44d8e0){_0x275501=_0x44d8e0['length'];if(!_0x28806e[_0xf243('0x31')][_0xf243('0x37')](_0xf243('0x38'))){_0x4e6480[_0xf243('0x1b')]=qs['limit'](_0x28806e[_0xf243('0x31')]['limit']);_0x4e6480[_0xf243('0x1e')]=qs[_0xf243('0x1e')](_0x28806e[_0xf243('0x31')][_0xf243('0x1e')]);}return _0x2953ca['getFields'](_0x4e6480);}})[_0xf243('0x23')](function(_0x566f9c){if(_0x566f9c){return _0x566f9c?{'count':_0x275501,'rows':_0x566f9c}:null;}})[_0xf243('0x23')](respondWithResult(_0x32ef28,null))[_0xf243('0x46')](handleError(_0x32ef28,null));};exports[_0xf243('0x4d')]=function(_0x1b22e2,_0x1814ef,_0x40f121){var _0x20472c={};var _0x3dafc9={};var _0x39c770;var _0x439444;return db['SugarcrmConfiguration']['findOne']({'where':{'id':_0x1b22e2[_0xf243('0x44')]['id']}})[_0xf243('0x23')](handleEntityNotFound(_0x1814ef,null))[_0xf243('0x23')](function(_0x26d9ec){if(_0x26d9ec){_0x39c770=_0x26d9ec;_0x3dafc9[_0xf243('0x30')]=_[_0xf243('0x32')](db['SugarcrmField'][_0xf243('0x2c')]);_0x3dafc9[_0xf243('0x31')]=_[_0xf243('0x32')](_0x1b22e2[_0xf243('0x31')]);_0x3dafc9[_0xf243('0x3b')]=_['intersection'](_0x3dafc9['model'],_0x3dafc9[_0xf243('0x31')]);_0x20472c[_0xf243('0x34')]=_['intersection'](_0x3dafc9[_0xf243('0x30')],qs[_0xf243('0x35')](_0x1b22e2[_0xf243('0x31')]['fields']));_0x20472c[_0xf243('0x34')]=_0x20472c['attributes'][_0xf243('0x36')]?_0x20472c['attributes']:_0x3dafc9[_0xf243('0x30')];_0x20472c[_0xf243('0x4b')]=qs[_0xf243('0x39')](_0x1b22e2[_0xf243('0x31')][_0xf243('0x39')]);_0x20472c['where']=qs[_0xf243('0x3b')](_[_0xf243('0x4c')](_0x1b22e2['query'],_0x3dafc9[_0xf243('0x3b')]));if(_0x1b22e2[_0xf243('0x31')][_0xf243('0x3c')]){_0x20472c[_0xf243('0x3a')]=_[_0xf243('0x3d')](_0x20472c[_0xf243('0x3a')],{'$or':_['map'](_0x20472c[_0xf243('0x34')],function(_0x4a65d2){var _0x310b9f={};_0x310b9f[_0x4a65d2]={'$like':'%'+_0x1b22e2[_0xf243('0x31')]['filter']+'%'};return _0x310b9f;})});}_0x20472c=_['merge']({},_0x20472c,_0x1b22e2[_0xf243('0x3f')]);return _0x39c770['getSubjects'](_0x20472c);}})[_0xf243('0x23')](function(_0x401a0e){if(_0x401a0e){_0x439444=_0x401a0e[_0xf243('0x36')];if(!_0x1b22e2['query'][_0xf243('0x37')](_0xf243('0x38'))){_0x20472c[_0xf243('0x1b')]=qs[_0xf243('0x1b')](_0x1b22e2[_0xf243('0x31')][_0xf243('0x1b')]);_0x20472c[_0xf243('0x1e')]=qs[_0xf243('0x1e')](_0x1b22e2[_0xf243('0x31')][_0xf243('0x1e')]);}return _0x39c770[_0xf243('0x4d')](_0x20472c);}})[_0xf243('0x23')](function(_0x3a568b){if(_0x3a568b){return _0x3a568b?{'count':_0x439444,'rows':_0x3a568b}:null;}})['then'](respondWithResult(_0x1814ef,null))[_0xf243('0x46')](handleError(_0x1814ef,null));};exports[_0xf243('0x4e')]=function(_0x34011b,_0x4ab7c5,_0x4c3a49){var _0x334ddb={};var _0x1b11b7={};var _0x52b4b4;var _0x9749c;return db['SugarcrmConfiguration'][_0xf243('0x4a')]({'where':{'id':_0x34011b['params']['id']}})['then'](handleEntityNotFound(_0x4ab7c5,null))[_0xf243('0x23')](function(_0x499387){if(_0x499387){_0x52b4b4=_0x499387;_0x1b11b7['model']=_[_0xf243('0x32')](db['SugarcrmField'][_0xf243('0x2c')]);_0x1b11b7[_0xf243('0x31')]=_[_0xf243('0x32')](_0x34011b['query']);_0x1b11b7[_0xf243('0x3b')]=_[_0xf243('0x33')](_0x1b11b7[_0xf243('0x30')],_0x1b11b7['query']);_0x334ddb[_0xf243('0x34')]=_['intersection'](_0x1b11b7[_0xf243('0x30')],qs['fields'](_0x34011b['query'][_0xf243('0x35')]));_0x334ddb['attributes']=_0x334ddb[_0xf243('0x34')][_0xf243('0x36')]?_0x334ddb[_0xf243('0x34')]:_0x1b11b7[_0xf243('0x30')];_0x334ddb[_0xf243('0x4b')]=qs['sort'](_0x34011b[_0xf243('0x31')][_0xf243('0x39')]);_0x334ddb[_0xf243('0x3a')]=qs[_0xf243('0x3b')](_[_0xf243('0x4c')](_0x34011b[_0xf243('0x31')],_0x1b11b7[_0xf243('0x3b')]));if(_0x34011b['query']['filter']){_0x334ddb['where']=_[_0xf243('0x3d')](_0x334ddb[_0xf243('0x3a')],{'$or':_['map'](_0x334ddb[_0xf243('0x34')],function(_0x5816d6){var _0x4ca76c={};_0x4ca76c[_0x5816d6]={'$like':'%'+_0x34011b[_0xf243('0x31')][_0xf243('0x3c')]+'%'};return _0x4ca76c;})});}_0x334ddb=_[_0xf243('0x3d')]({},_0x334ddb,_0x34011b[_0xf243('0x3f')]);return _0x52b4b4[_0xf243('0x4e')](_0x334ddb);}})[_0xf243('0x23')](function(_0x55f8d9){if(_0x55f8d9){_0x9749c=_0x55f8d9['length'];if(!_0x34011b[_0xf243('0x31')][_0xf243('0x37')](_0xf243('0x38'))){_0x334ddb[_0xf243('0x1b')]=qs[_0xf243('0x1b')](_0x34011b[_0xf243('0x31')][_0xf243('0x1b')]);_0x334ddb['offset']=qs[_0xf243('0x1e')](_0x34011b[_0xf243('0x31')]['offset']);}return _0x52b4b4[_0xf243('0x4e')](_0x334ddb);}})[_0xf243('0x23')](function(_0x4869f3){if(_0x4869f3){return _0x4869f3?{'count':_0x9749c,'rows':_0x4869f3}:null;}})[_0xf243('0x23')](respondWithResult(_0x4ab7c5,null))[_0xf243('0x46')](handleError(_0x4ab7c5,null));};