7980a72317bc2cbf38ed16f485a0518f887d9d28
[motion2.git] / server / api / intVtigerConfiguration / intVtigerConfiguration.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 _0x780d=['rows','show','params','intersection','fields','includeAll','options','find','catch','create','body','getFields','VtigerField','nolimit','getSubjects','findOne','getDescriptions','eml-format','rimraf','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','sox','to-csv','ejs','lodash','squel','desk.js','querystring','ioredis','../../components/auth/service','../../config/license/hardware','../../config/logger','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status','offset','undefined','limit','count','set','Content-Range','json','apply','reject','save','update','then','destroy','end','error','stack','name','send','index','VtigerConfiguration','rawAttributes','fieldName','type','key','model','map','query','keys','filters','attributes','length','hasOwnProperty','order','sort','where','pick','filter','merge','include','findAll'];(function(_0x3bc82d,_0x59187f){var _0x2cfb3d=function(_0x515ac3){while(--_0x515ac3){_0x3bc82d['push'](_0x3bc82d['shift']());}};_0x2cfb3d(++_0x59187f);}(_0x780d,0x10a));var _0xd780=function(_0x455aa2,_0x5934f5){_0x455aa2=_0x455aa2-0x0;var _0x5b8d8d=_0x780d[_0x455aa2];return _0x5b8d8d;};'use strict';var emlformat=require(_0xd780('0x0'));var rimraf=require(_0xd780('0x1'));var zipdir=require('zip-dir');var jsonpatch=require(_0xd780('0x2'));var rp=require(_0xd780('0x3'));var moment=require(_0xd780('0x4'));var BPromise=require(_0xd780('0x5'));var Mustache=require(_0xd780('0x6'));var util=require(_0xd780('0x7'));var path=require(_0xd780('0x8'));var sox=require(_0xd780('0x9'));var csv=require(_0xd780('0xa'));var ejs=require(_0xd780('0xb'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0xd780('0xc'));var squel=require(_0xd780('0xd'));var crypto=require('crypto');var jsforce=require('jsforce');var deskjs=require(_0xd780('0xe'));var toCsv=require(_0xd780('0xa'));var querystring=require(_0xd780('0xf'));var Papa=require('papaparse');var Redis=require(_0xd780('0x10'));var authService=require(_0xd780('0x11'));var qs=require('../../components/parsers/qs');var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0xd780('0x12'));var logger=require(_0xd780('0x13'))('api');var utils=require(_0xd780('0x14'));var config=require(_0xd780('0x15'));var licenseUtil=require(_0xd780('0x16'));var db=require(_0xd780('0x17'))['db'];function respondWithStatusCode(_0xde9558,_0x3b981a){_0x3b981a=_0x3b981a||0xcc;return function(_0x1a15b6){if(_0x1a15b6){return _0xde9558[_0xd780('0x18')](_0x3b981a);}return _0xde9558[_0xd780('0x19')](_0x3b981a)['end']();};}function respondWithResult(_0x5c427e,_0x4ee591){_0x4ee591=_0x4ee591||0xc8;return function(_0x2bfeab){if(_0x2bfeab){return _0x5c427e[_0xd780('0x19')](_0x4ee591)['json'](_0x2bfeab);}};}function respondWithFilteredResult(_0x36ccc3,_0x3e5d64){return function(_0x3e1155){if(_0x3e1155){var _0xed93cd=typeof _0x3e5d64[_0xd780('0x1a')]===_0xd780('0x1b')&&typeof _0x3e5d64[_0xd780('0x1c')]===_0xd780('0x1b');var _0x474dfd=_0x3e1155[_0xd780('0x1d')];var _0x557902=_0xed93cd?0x0:_0x3e5d64[_0xd780('0x1a')];var _0x493c44=_0xed93cd?_0x3e1155[_0xd780('0x1d')]:_0x3e5d64[_0xd780('0x1a')]+_0x3e5d64[_0xd780('0x1c')];var _0x50400a;if(_0x493c44>=_0x474dfd){_0x493c44=_0x474dfd;_0x50400a=0xc8;}else{_0x50400a=0xce;}_0x36ccc3[_0xd780('0x19')](_0x50400a);return _0x36ccc3[_0xd780('0x1e')](_0xd780('0x1f'),_0x557902+'-'+_0x493c44+'/'+_0x474dfd)[_0xd780('0x20')](_0x3e1155);}return null;};}function patchUpdates(_0x21c440){return function(_0x41eaf2){try{jsonpatch[_0xd780('0x21')](_0x41eaf2,_0x21c440,!![]);}catch(_0x29303b){return BPromise[_0xd780('0x22')](_0x29303b);}return _0x41eaf2[_0xd780('0x23')]();};}function saveUpdates(_0x323594,_0x3ba116){return function(_0xc46975){if(_0xc46975){return _0xc46975[_0xd780('0x24')](_0x323594)[_0xd780('0x25')](function(_0x46fc4d){return _0x46fc4d;});}return null;};}function removeEntity(_0x5e717f,_0x30d083){return function(_0x1295a5){if(_0x1295a5){return _0x1295a5[_0xd780('0x26')]()[_0xd780('0x25')](function(){_0x5e717f[_0xd780('0x19')](0xcc)[_0xd780('0x27')]();});}};}function handleEntityNotFound(_0x44f5b9,_0x526796){return function(_0x5d4c1e){if(!_0x5d4c1e){_0x44f5b9[_0xd780('0x18')](0x194);}return _0x5d4c1e;};}function handleError(_0xd4f866,_0x23caa4){_0x23caa4=_0x23caa4||0x1f4;return function(_0x5db004){logger[_0xd780('0x28')](_0x5db004[_0xd780('0x29')]);if(_0x5db004['name']){delete _0x5db004[_0xd780('0x2a')];}_0xd4f866[_0xd780('0x19')](_0x23caa4)[_0xd780('0x2b')](_0x5db004);};}exports[_0xd780('0x2c')]=function(_0x45225e,_0xccd122){var _0xb03d8d={},_0x4fd64a={},_0x26a3b6={'count':0x0,'rows':[]};var _0x19f042=_['map'](db[_0xd780('0x2d')][_0xd780('0x2e')],function(_0x26d6c3){return{'name':_0x26d6c3[_0xd780('0x2f')],'type':_0x26d6c3[_0xd780('0x30')][_0xd780('0x31')]};});_0x4fd64a[_0xd780('0x32')]=_[_0xd780('0x33')](_0x19f042,_0xd780('0x2a'));_0x4fd64a[_0xd780('0x34')]=_[_0xd780('0x35')](_0x45225e[_0xd780('0x34')]);_0x4fd64a[_0xd780('0x36')]=_['intersection'](_0x4fd64a[_0xd780('0x32')],_0x4fd64a[_0xd780('0x34')]);_0xb03d8d[_0xd780('0x37')]=_['intersection'](_0x4fd64a[_0xd780('0x32')],qs['fields'](_0x45225e[_0xd780('0x34')]['fields']));_0xb03d8d[_0xd780('0x37')]=_0xb03d8d[_0xd780('0x37')][_0xd780('0x38')]?_0xb03d8d[_0xd780('0x37')]:_0x4fd64a['model'];if(!_0x45225e[_0xd780('0x34')][_0xd780('0x39')]('nolimit')){_0xb03d8d[_0xd780('0x1c')]=qs[_0xd780('0x1c')](_0x45225e[_0xd780('0x34')][_0xd780('0x1c')]);_0xb03d8d[_0xd780('0x1a')]=qs['offset'](_0x45225e[_0xd780('0x34')][_0xd780('0x1a')]);}_0xb03d8d[_0xd780('0x3a')]=qs[_0xd780('0x3b')](_0x45225e[_0xd780('0x34')]['sort']);_0xb03d8d[_0xd780('0x3c')]=qs[_0xd780('0x36')](_[_0xd780('0x3d')](_0x45225e[_0xd780('0x34')],_0x4fd64a[_0xd780('0x36')]),_0x19f042);if(_0x45225e[_0xd780('0x34')][_0xd780('0x3e')]){_0xb03d8d[_0xd780('0x3c')]=_[_0xd780('0x3f')](_0xb03d8d[_0xd780('0x3c')],{'$or':_[_0xd780('0x33')](_0x19f042,function(_0x424777){if(_0x424777[_0xd780('0x30')]!=='VIRTUAL'){var _0x5d4e4a={};_0x5d4e4a[_0x424777[_0xd780('0x2a')]]={'$like':'%'+_0x45225e['query'][_0xd780('0x3e')]+'%'};return _0x5d4e4a;}})});}_0xb03d8d=_[_0xd780('0x3f')]({},_0xb03d8d,_0x45225e['options']);var _0x21f716={'where':_0xb03d8d[_0xd780('0x3c')]};return db[_0xd780('0x2d')]['count'](_0x21f716)[_0xd780('0x25')](function(_0xaefe81){_0x26a3b6[_0xd780('0x1d')]=_0xaefe81;if(_0x45225e[_0xd780('0x34')]['includeAll']){_0xb03d8d[_0xd780('0x40')]=[{'all':!![]}];}return db[_0xd780('0x2d')][_0xd780('0x41')](_0xb03d8d);})['then'](function(_0x322c79){_0x26a3b6[_0xd780('0x42')]=_0x322c79;return _0x26a3b6;})[_0xd780('0x25')](respondWithFilteredResult(_0xccd122,_0xb03d8d))['catch'](handleError(_0xccd122,null));};exports[_0xd780('0x43')]=function(_0x2ed574,_0x14e6eb){var _0x21906f={'raw':![],'where':{'id':_0x2ed574[_0xd780('0x44')]['id']}},_0x1953dc={};_0x1953dc['model']=_['keys'](db[_0xd780('0x2d')]['rawAttributes']);_0x1953dc[_0xd780('0x34')]=_[_0xd780('0x35')](_0x2ed574[_0xd780('0x34')]);_0x1953dc[_0xd780('0x36')]=_['intersection'](_0x1953dc[_0xd780('0x32')],_0x1953dc[_0xd780('0x34')]);_0x21906f[_0xd780('0x37')]=_[_0xd780('0x45')](_0x1953dc[_0xd780('0x32')],qs[_0xd780('0x46')](_0x2ed574[_0xd780('0x34')][_0xd780('0x46')]));_0x21906f[_0xd780('0x37')]=_0x21906f[_0xd780('0x37')][_0xd780('0x38')]?_0x21906f[_0xd780('0x37')]:_0x1953dc['model'];if(_0x2ed574['query'][_0xd780('0x47')]){_0x21906f['include']=[{'all':!![]}];}_0x21906f=_[_0xd780('0x3f')]({},_0x21906f,_0x2ed574[_0xd780('0x48')]);return db[_0xd780('0x2d')][_0xd780('0x49')](_0x21906f)['then'](handleEntityNotFound(_0x14e6eb,null))[_0xd780('0x25')](respondWithResult(_0x14e6eb,null))[_0xd780('0x4a')](handleError(_0x14e6eb,null));};exports[_0xd780('0x4b')]=function(_0x53381c,_0x112b23){return db[_0xd780('0x2d')][_0xd780('0x4b')](_0x53381c[_0xd780('0x4c')],{})['then'](respondWithResult(_0x112b23,0xc9))[_0xd780('0x4a')](handleError(_0x112b23,null));};exports[_0xd780('0x24')]=function(_0x23799e,_0x121cb2){if(_0x23799e[_0xd780('0x4c')]['id']){delete _0x23799e[_0xd780('0x4c')]['id'];}return db['VtigerConfiguration']['find']({'where':{'id':_0x23799e[_0xd780('0x44')]['id']}})[_0xd780('0x25')](handleEntityNotFound(_0x121cb2,null))[_0xd780('0x25')](saveUpdates(_0x23799e[_0xd780('0x4c')],null))['then'](respondWithResult(_0x121cb2,null))['catch'](handleError(_0x121cb2,null));};exports['destroy']=function(_0xc9a622,_0x3c7d01){return db['VtigerConfiguration']['find']({'where':{'id':_0xc9a622[_0xd780('0x44')]['id']}})['then'](handleEntityNotFound(_0x3c7d01,null))[_0xd780('0x25')](removeEntity(_0x3c7d01,null))['catch'](handleError(_0x3c7d01,null));};exports[_0xd780('0x4d')]=function(_0x44ffa6,_0x144223,_0x2bc27f){var _0x29f03a={};var _0x5dffb7={};var _0x2b2b08;var _0x1ccf2d;return db[_0xd780('0x2d')]['findOne']({'where':{'id':_0x44ffa6[_0xd780('0x44')]['id']}})['then'](handleEntityNotFound(_0x144223,null))[_0xd780('0x25')](function(_0x3ccacc){if(_0x3ccacc){_0x2b2b08=_0x3ccacc;_0x5dffb7[_0xd780('0x32')]=_['keys'](db[_0xd780('0x4e')][_0xd780('0x2e')]);_0x5dffb7['query']=_[_0xd780('0x35')](_0x44ffa6['query']);_0x5dffb7['filters']=_[_0xd780('0x45')](_0x5dffb7['model'],_0x5dffb7[_0xd780('0x34')]);_0x29f03a[_0xd780('0x37')]=_[_0xd780('0x45')](_0x5dffb7[_0xd780('0x32')],qs['fields'](_0x44ffa6['query'][_0xd780('0x46')]));_0x29f03a['attributes']=_0x29f03a['attributes'][_0xd780('0x38')]?_0x29f03a[_0xd780('0x37')]:_0x5dffb7['model'];_0x29f03a['order']=qs['sort'](_0x44ffa6[_0xd780('0x34')][_0xd780('0x3b')]);_0x29f03a[_0xd780('0x3c')]=qs[_0xd780('0x36')](_['pick'](_0x44ffa6[_0xd780('0x34')],_0x5dffb7[_0xd780('0x36')]));if(_0x44ffa6[_0xd780('0x34')][_0xd780('0x3e')]){_0x29f03a['where']=_[_0xd780('0x3f')](_0x29f03a[_0xd780('0x3c')],{'$or':_[_0xd780('0x33')](_0x29f03a[_0xd780('0x37')],function(_0x2360b4){var _0x549abb={};_0x549abb[_0x2360b4]={'$like':'%'+_0x44ffa6[_0xd780('0x34')][_0xd780('0x3e')]+'%'};return _0x549abb;})});}_0x29f03a=_['merge']({},_0x29f03a,_0x44ffa6[_0xd780('0x48')]);return _0x2b2b08[_0xd780('0x4d')](_0x29f03a);}})[_0xd780('0x25')](function(_0x445b14){if(_0x445b14){_0x1ccf2d=_0x445b14[_0xd780('0x38')];if(!_0x44ffa6[_0xd780('0x34')][_0xd780('0x39')](_0xd780('0x4f'))){_0x29f03a[_0xd780('0x1c')]=qs[_0xd780('0x1c')](_0x44ffa6[_0xd780('0x34')][_0xd780('0x1c')]);_0x29f03a[_0xd780('0x1a')]=qs[_0xd780('0x1a')](_0x44ffa6['query'][_0xd780('0x1a')]);}return _0x2b2b08['getFields'](_0x29f03a);}})[_0xd780('0x25')](function(_0x426c4b){if(_0x426c4b){return _0x426c4b?{'count':_0x1ccf2d,'rows':_0x426c4b}:null;}})[_0xd780('0x25')](respondWithResult(_0x144223,null))[_0xd780('0x4a')](handleError(_0x144223,null));};exports[_0xd780('0x50')]=function(_0x5385b7,_0x1c9924,_0x37ea32){var _0x89124b={};var _0xd29be1={};var _0x1ec344;var _0x421b15;return db['VtigerConfiguration'][_0xd780('0x51')]({'where':{'id':_0x5385b7[_0xd780('0x44')]['id']}})[_0xd780('0x25')](handleEntityNotFound(_0x1c9924,null))[_0xd780('0x25')](function(_0x539297){if(_0x539297){_0x1ec344=_0x539297;_0xd29be1[_0xd780('0x32')]=_[_0xd780('0x35')](db['VtigerField']['rawAttributes']);_0xd29be1[_0xd780('0x34')]=_[_0xd780('0x35')](_0x5385b7['query']);_0xd29be1['filters']=_[_0xd780('0x45')](_0xd29be1['model'],_0xd29be1[_0xd780('0x34')]);_0x89124b[_0xd780('0x37')]=_[_0xd780('0x45')](_0xd29be1['model'],qs[_0xd780('0x46')](_0x5385b7['query'][_0xd780('0x46')]));_0x89124b[_0xd780('0x37')]=_0x89124b[_0xd780('0x37')]['length']?_0x89124b[_0xd780('0x37')]:_0xd29be1[_0xd780('0x32')];_0x89124b[_0xd780('0x3a')]=qs['sort'](_0x5385b7['query'][_0xd780('0x3b')]);_0x89124b[_0xd780('0x3c')]=qs[_0xd780('0x36')](_[_0xd780('0x3d')](_0x5385b7[_0xd780('0x34')],_0xd29be1['filters']));if(_0x5385b7['query'][_0xd780('0x3e')]){_0x89124b['where']=_[_0xd780('0x3f')](_0x89124b[_0xd780('0x3c')],{'$or':_[_0xd780('0x33')](_0x89124b[_0xd780('0x37')],function(_0x67bca0){var _0xcfd896={};_0xcfd896[_0x67bca0]={'$like':'%'+_0x5385b7[_0xd780('0x34')][_0xd780('0x3e')]+'%'};return _0xcfd896;})});}_0x89124b=_[_0xd780('0x3f')]({},_0x89124b,_0x5385b7[_0xd780('0x48')]);return _0x1ec344[_0xd780('0x50')](_0x89124b);}})[_0xd780('0x25')](function(_0x3ef216){if(_0x3ef216){_0x421b15=_0x3ef216['length'];if(!_0x5385b7[_0xd780('0x34')][_0xd780('0x39')](_0xd780('0x4f'))){_0x89124b[_0xd780('0x1c')]=qs['limit'](_0x5385b7[_0xd780('0x34')][_0xd780('0x1c')]);_0x89124b['offset']=qs[_0xd780('0x1a')](_0x5385b7[_0xd780('0x34')]['offset']);}return _0x1ec344[_0xd780('0x50')](_0x89124b);}})[_0xd780('0x25')](function(_0x21f0bf){if(_0x21f0bf){return _0x21f0bf?{'count':_0x421b15,'rows':_0x21f0bf}:null;}})[_0xd780('0x25')](respondWithResult(_0x1c9924,null))['catch'](handleError(_0x1c9924,null));};exports[_0xd780('0x52')]=function(_0x2b8d99,_0x5bb907,_0x44b2bc){var _0x1db65d={};var _0x4149c1={};var _0xef1520;var _0x456bdc;return db[_0xd780('0x2d')][_0xd780('0x51')]({'where':{'id':_0x2b8d99['params']['id']}})[_0xd780('0x25')](handleEntityNotFound(_0x5bb907,null))[_0xd780('0x25')](function(_0x5b5106){if(_0x5b5106){_0xef1520=_0x5b5106;_0x4149c1[_0xd780('0x32')]=_[_0xd780('0x35')](db['VtigerField']['rawAttributes']);_0x4149c1[_0xd780('0x34')]=_[_0xd780('0x35')](_0x2b8d99[_0xd780('0x34')]);_0x4149c1[_0xd780('0x36')]=_[_0xd780('0x45')](_0x4149c1[_0xd780('0x32')],_0x4149c1[_0xd780('0x34')]);_0x1db65d[_0xd780('0x37')]=_[_0xd780('0x45')](_0x4149c1[_0xd780('0x32')],qs[_0xd780('0x46')](_0x2b8d99[_0xd780('0x34')][_0xd780('0x46')]));_0x1db65d['attributes']=_0x1db65d['attributes'][_0xd780('0x38')]?_0x1db65d[_0xd780('0x37')]:_0x4149c1[_0xd780('0x32')];_0x1db65d[_0xd780('0x3a')]=qs[_0xd780('0x3b')](_0x2b8d99[_0xd780('0x34')][_0xd780('0x3b')]);_0x1db65d[_0xd780('0x3c')]=qs[_0xd780('0x36')](_[_0xd780('0x3d')](_0x2b8d99[_0xd780('0x34')],_0x4149c1['filters']));if(_0x2b8d99[_0xd780('0x34')][_0xd780('0x3e')]){_0x1db65d[_0xd780('0x3c')]=_[_0xd780('0x3f')](_0x1db65d[_0xd780('0x3c')],{'$or':_['map'](_0x1db65d[_0xd780('0x37')],function(_0x155866){var _0x48b601={};_0x48b601[_0x155866]={'$like':'%'+_0x2b8d99['query'][_0xd780('0x3e')]+'%'};return _0x48b601;})});}_0x1db65d=_['merge']({},_0x1db65d,_0x2b8d99['options']);return _0xef1520['getDescriptions'](_0x1db65d);}})[_0xd780('0x25')](function(_0x144740){if(_0x144740){_0x456bdc=_0x144740[_0xd780('0x38')];if(!_0x2b8d99[_0xd780('0x34')][_0xd780('0x39')](_0xd780('0x4f'))){_0x1db65d['limit']=qs[_0xd780('0x1c')](_0x2b8d99[_0xd780('0x34')][_0xd780('0x1c')]);_0x1db65d[_0xd780('0x1a')]=qs['offset'](_0x2b8d99[_0xd780('0x34')]['offset']);}return _0xef1520[_0xd780('0x52')](_0x1db65d);}})[_0xd780('0x25')](function(_0x3877cc){if(_0x3877cc){return _0x3877cc?{'count':_0x456bdc,'rows':_0x3877cc}:null;}})[_0xd780('0x25')](respondWithResult(_0x5bb907,null))[_0xd780('0x4a')](handleError(_0x5bb907,null));};