b43c409c215a0db9d70af5524a24e8904fa88c9e
[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 _0x88f4=['sendStatus','status','end','json','undefined','limit','count','offset','set','Content-Range','apply','update','then','destroy','error','stack','name','send','index','map','fieldName','type','key','model','query','keys','filters','intersection','attributes','fields','length','hasOwnProperty','nolimit','order','sort','where','pick','filter','merge','VIRTUAL','options','VtigerConfiguration','includeAll','findAll','rows','catch','params','rawAttributes','include','find','create','body','getFields','VtigerField','getSubjects','findOne','getDescriptions','eml-format','rimraf','zip-dir','request-promise','moment','mustache','path','to-csv','ejs','fs-extra','lodash','squel','crypto','jsforce','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/logger','../../config/utils','../../config/license/util','../../mysqldb'];(function(_0x513aa5,_0x1fba79){var _0x1a1a7c=function(_0x240e3f){while(--_0x240e3f){_0x513aa5['push'](_0x513aa5['shift']());}};_0x1a1a7c(++_0x1fba79);}(_0x88f4,0x17d));var _0x488f=function(_0x55bb53,_0x5590af){_0x55bb53=_0x55bb53-0x0;var _0x396e95=_0x88f4[_0x55bb53];return _0x396e95;};'use strict';var emlformat=require(_0x488f('0x0'));var rimraf=require(_0x488f('0x1'));var zipdir=require(_0x488f('0x2'));var jsonpatch=require('fast-json-patch');var rp=require(_0x488f('0x3'));var moment=require(_0x488f('0x4'));var BPromise=require('bluebird');var Mustache=require(_0x488f('0x5'));var util=require('util');var path=require(_0x488f('0x6'));var sox=require('sox');var csv=require(_0x488f('0x7'));var ejs=require(_0x488f('0x8'));var fs=require('fs');var fs_extra=require(_0x488f('0x9'));var _=require(_0x488f('0xa'));var squel=require(_0x488f('0xb'));var crypto=require(_0x488f('0xc'));var jsforce=require(_0x488f('0xd'));var deskjs=require(_0x488f('0xe'));var toCsv=require(_0x488f('0x7'));var querystring=require(_0x488f('0xf'));var Papa=require(_0x488f('0x10'));var Redis=require(_0x488f('0x11'));var authService=require(_0x488f('0x12'));var qs=require('../../components/parsers/qs');var as=require(_0x488f('0x13'));var hardwareService=require('../../config/license/hardware');var logger=require(_0x488f('0x14'))('api');var utils=require(_0x488f('0x15'));var config=require('../../config/environment');var licenseUtil=require(_0x488f('0x16'));var db=require(_0x488f('0x17'))['db'];function respondWithStatusCode(_0x5cdbac,_0x57302c){_0x57302c=_0x57302c||0xcc;return function(_0x2dd8ca){if(_0x2dd8ca){return _0x5cdbac[_0x488f('0x18')](_0x57302c);}return _0x5cdbac[_0x488f('0x19')](_0x57302c)[_0x488f('0x1a')]();};}function respondWithResult(_0x4d45c8,_0x3b0251){_0x3b0251=_0x3b0251||0xc8;return function(_0x26c5ef){if(_0x26c5ef){return _0x4d45c8[_0x488f('0x19')](_0x3b0251)[_0x488f('0x1b')](_0x26c5ef);}};}function respondWithFilteredResult(_0xdb9a78,_0x53927f){return function(_0x4bf947){if(_0x4bf947){var _0x4e1484=typeof _0x53927f['offset']===_0x488f('0x1c')&&typeof _0x53927f[_0x488f('0x1d')]===_0x488f('0x1c');var _0xc96718=_0x4bf947[_0x488f('0x1e')];var _0x4904b8=_0x4e1484?0x0:_0x53927f[_0x488f('0x1f')];var _0x514060=_0x4e1484?_0x4bf947[_0x488f('0x1e')]:_0x53927f[_0x488f('0x1f')]+_0x53927f[_0x488f('0x1d')];var _0x5d167c;if(_0x514060>=_0xc96718){_0x514060=_0xc96718;_0x5d167c=0xc8;}else{_0x5d167c=0xce;}_0xdb9a78[_0x488f('0x19')](_0x5d167c);return _0xdb9a78[_0x488f('0x20')](_0x488f('0x21'),_0x4904b8+'-'+_0x514060+'/'+_0xc96718)[_0x488f('0x1b')](_0x4bf947);}return null;};}function patchUpdates(_0x4ab567){return function(_0x3e55ac){try{jsonpatch[_0x488f('0x22')](_0x3e55ac,_0x4ab567,!![]);}catch(_0x4da27a){return BPromise['reject'](_0x4da27a);}return _0x3e55ac['save']();};}function saveUpdates(_0x365072,_0x409840){return function(_0x5bf81b){if(_0x5bf81b){return _0x5bf81b[_0x488f('0x23')](_0x365072)[_0x488f('0x24')](function(_0x18f1b6){return _0x18f1b6;});}return null;};}function removeEntity(_0x14ff89,_0x21409b){return function(_0x4410f2){if(_0x4410f2){return _0x4410f2[_0x488f('0x25')]()[_0x488f('0x24')](function(){_0x14ff89[_0x488f('0x19')](0xcc)[_0x488f('0x1a')]();});}};}function handleEntityNotFound(_0x39a1a9,_0x30a1a2){return function(_0x10156e){if(!_0x10156e){_0x39a1a9['sendStatus'](0x194);}return _0x10156e;};}function handleError(_0x47c8d9,_0x248a2d){_0x248a2d=_0x248a2d||0x1f4;return function(_0x53cb1d){logger[_0x488f('0x26')](_0x53cb1d[_0x488f('0x27')]);if(_0x53cb1d[_0x488f('0x28')]){delete _0x53cb1d[_0x488f('0x28')];}_0x47c8d9[_0x488f('0x19')](_0x248a2d)[_0x488f('0x29')](_0x53cb1d);};}exports[_0x488f('0x2a')]=function(_0x10dfc2,_0x590f48){var _0x2266d3={},_0x577cc2={},_0x4b1075={'count':0x0,'rows':[]};var _0x45ce61=_[_0x488f('0x2b')](db['VtigerConfiguration']['rawAttributes'],function(_0x339ca3){return{'name':_0x339ca3[_0x488f('0x2c')],'type':_0x339ca3[_0x488f('0x2d')][_0x488f('0x2e')]};});_0x577cc2[_0x488f('0x2f')]=_[_0x488f('0x2b')](_0x45ce61,'name');_0x577cc2[_0x488f('0x30')]=_[_0x488f('0x31')](_0x10dfc2[_0x488f('0x30')]);_0x577cc2[_0x488f('0x32')]=_[_0x488f('0x33')](_0x577cc2[_0x488f('0x2f')],_0x577cc2[_0x488f('0x30')]);_0x2266d3[_0x488f('0x34')]=_['intersection'](_0x577cc2[_0x488f('0x2f')],qs[_0x488f('0x35')](_0x10dfc2[_0x488f('0x30')][_0x488f('0x35')]));_0x2266d3[_0x488f('0x34')]=_0x2266d3['attributes'][_0x488f('0x36')]?_0x2266d3['attributes']:_0x577cc2['model'];if(!_0x10dfc2[_0x488f('0x30')][_0x488f('0x37')](_0x488f('0x38'))){_0x2266d3[_0x488f('0x1d')]=qs[_0x488f('0x1d')](_0x10dfc2[_0x488f('0x30')][_0x488f('0x1d')]);_0x2266d3[_0x488f('0x1f')]=qs[_0x488f('0x1f')](_0x10dfc2[_0x488f('0x30')][_0x488f('0x1f')]);}_0x2266d3[_0x488f('0x39')]=qs[_0x488f('0x3a')](_0x10dfc2[_0x488f('0x30')][_0x488f('0x3a')]);_0x2266d3[_0x488f('0x3b')]=qs['filters'](_[_0x488f('0x3c')](_0x10dfc2[_0x488f('0x30')],_0x577cc2[_0x488f('0x32')]),_0x45ce61);if(_0x10dfc2['query'][_0x488f('0x3d')]){_0x2266d3['where']=_[_0x488f('0x3e')](_0x2266d3[_0x488f('0x3b')],{'$or':_['map'](_0x45ce61,function(_0x527064){if(_0x527064[_0x488f('0x2d')]!==_0x488f('0x3f')){var _0x1a3b0e={};_0x1a3b0e[_0x527064['name']]={'$like':'%'+_0x10dfc2[_0x488f('0x30')][_0x488f('0x3d')]+'%'};return _0x1a3b0e;}})});}_0x2266d3=_[_0x488f('0x3e')]({},_0x2266d3,_0x10dfc2[_0x488f('0x40')]);var _0x35e0b5={'where':_0x2266d3[_0x488f('0x3b')]};return db[_0x488f('0x41')][_0x488f('0x1e')](_0x35e0b5)[_0x488f('0x24')](function(_0x4d4f3d){_0x4b1075[_0x488f('0x1e')]=_0x4d4f3d;if(_0x10dfc2[_0x488f('0x30')][_0x488f('0x42')]){_0x2266d3['include']=[{'all':!![]}];}return db[_0x488f('0x41')][_0x488f('0x43')](_0x2266d3);})[_0x488f('0x24')](function(_0x317974){_0x4b1075[_0x488f('0x44')]=_0x317974;return _0x4b1075;})[_0x488f('0x24')](respondWithFilteredResult(_0x590f48,_0x2266d3))[_0x488f('0x45')](handleError(_0x590f48,null));};exports['show']=function(_0x43abc4,_0x4a2e46){var _0x5d32e1={'raw':![],'where':{'id':_0x43abc4[_0x488f('0x46')]['id']}},_0x4fffe3={};_0x4fffe3[_0x488f('0x2f')]=_['keys'](db[_0x488f('0x41')][_0x488f('0x47')]);_0x4fffe3[_0x488f('0x30')]=_[_0x488f('0x31')](_0x43abc4[_0x488f('0x30')]);_0x4fffe3['filters']=_[_0x488f('0x33')](_0x4fffe3[_0x488f('0x2f')],_0x4fffe3['query']);_0x5d32e1['attributes']=_[_0x488f('0x33')](_0x4fffe3[_0x488f('0x2f')],qs['fields'](_0x43abc4[_0x488f('0x30')][_0x488f('0x35')]));_0x5d32e1[_0x488f('0x34')]=_0x5d32e1[_0x488f('0x34')][_0x488f('0x36')]?_0x5d32e1[_0x488f('0x34')]:_0x4fffe3[_0x488f('0x2f')];if(_0x43abc4[_0x488f('0x30')][_0x488f('0x42')]){_0x5d32e1[_0x488f('0x48')]=[{'all':!![]}];}_0x5d32e1=_[_0x488f('0x3e')]({},_0x5d32e1,_0x43abc4['options']);return db[_0x488f('0x41')][_0x488f('0x49')](_0x5d32e1)['then'](handleEntityNotFound(_0x4a2e46,null))[_0x488f('0x24')](respondWithResult(_0x4a2e46,null))[_0x488f('0x45')](handleError(_0x4a2e46,null));};exports[_0x488f('0x4a')]=function(_0x1aef1a,_0x4c9f6e){return db['VtigerConfiguration'][_0x488f('0x4a')](_0x1aef1a['body'],{})[_0x488f('0x24')](respondWithResult(_0x4c9f6e,0xc9))['catch'](handleError(_0x4c9f6e,null));};exports[_0x488f('0x23')]=function(_0x3c40b0,_0x33dd5b){if(_0x3c40b0['body']['id']){delete _0x3c40b0[_0x488f('0x4b')]['id'];}return db[_0x488f('0x41')][_0x488f('0x49')]({'where':{'id':_0x3c40b0['params']['id']}})[_0x488f('0x24')](handleEntityNotFound(_0x33dd5b,null))[_0x488f('0x24')](saveUpdates(_0x3c40b0['body'],null))['then'](respondWithResult(_0x33dd5b,null))[_0x488f('0x45')](handleError(_0x33dd5b,null));};exports[_0x488f('0x25')]=function(_0x6f52d0,_0x1515e9){return db[_0x488f('0x41')]['find']({'where':{'id':_0x6f52d0[_0x488f('0x46')]['id']}})['then'](handleEntityNotFound(_0x1515e9,null))[_0x488f('0x24')](removeEntity(_0x1515e9,null))[_0x488f('0x45')](handleError(_0x1515e9,null));};exports[_0x488f('0x4c')]=function(_0x4fba60,_0x4bd8eb,_0xff4837){var _0x5af1f3={};var _0x487d0d={};var _0x164f6a;var _0x18e13d;return db[_0x488f('0x41')]['findOne']({'where':{'id':_0x4fba60[_0x488f('0x46')]['id']}})[_0x488f('0x24')](handleEntityNotFound(_0x4bd8eb,null))[_0x488f('0x24')](function(_0x31504c){if(_0x31504c){_0x164f6a=_0x31504c;_0x487d0d[_0x488f('0x2f')]=_['keys'](db[_0x488f('0x4d')]['rawAttributes']);_0x487d0d[_0x488f('0x30')]=_[_0x488f('0x31')](_0x4fba60['query']);_0x487d0d['filters']=_['intersection'](_0x487d0d['model'],_0x487d0d[_0x488f('0x30')]);_0x5af1f3[_0x488f('0x34')]=_[_0x488f('0x33')](_0x487d0d[_0x488f('0x2f')],qs[_0x488f('0x35')](_0x4fba60['query'][_0x488f('0x35')]));_0x5af1f3[_0x488f('0x34')]=_0x5af1f3['attributes'][_0x488f('0x36')]?_0x5af1f3[_0x488f('0x34')]:_0x487d0d[_0x488f('0x2f')];_0x5af1f3[_0x488f('0x39')]=qs['sort'](_0x4fba60[_0x488f('0x30')][_0x488f('0x3a')]);_0x5af1f3['where']=qs['filters'](_[_0x488f('0x3c')](_0x4fba60[_0x488f('0x30')],_0x487d0d[_0x488f('0x32')]));if(_0x4fba60[_0x488f('0x30')][_0x488f('0x3d')]){_0x5af1f3['where']=_[_0x488f('0x3e')](_0x5af1f3[_0x488f('0x3b')],{'$or':_['map'](_0x5af1f3[_0x488f('0x34')],function(_0x2e9a5f){var _0xdb1d15={};_0xdb1d15[_0x2e9a5f]={'$like':'%'+_0x4fba60[_0x488f('0x30')][_0x488f('0x3d')]+'%'};return _0xdb1d15;})});}_0x5af1f3=_['merge']({},_0x5af1f3,_0x4fba60['options']);return _0x164f6a[_0x488f('0x4c')](_0x5af1f3);}})[_0x488f('0x24')](function(_0xcaf622){if(_0xcaf622){_0x18e13d=_0xcaf622[_0x488f('0x36')];if(!_0x4fba60['query']['hasOwnProperty'](_0x488f('0x38'))){_0x5af1f3[_0x488f('0x1d')]=qs[_0x488f('0x1d')](_0x4fba60[_0x488f('0x30')][_0x488f('0x1d')]);_0x5af1f3[_0x488f('0x1f')]=qs['offset'](_0x4fba60[_0x488f('0x30')][_0x488f('0x1f')]);}return _0x164f6a[_0x488f('0x4c')](_0x5af1f3);}})[_0x488f('0x24')](function(_0x3df063){if(_0x3df063){return _0x3df063?{'count':_0x18e13d,'rows':_0x3df063}:null;}})[_0x488f('0x24')](respondWithResult(_0x4bd8eb,null))[_0x488f('0x45')](handleError(_0x4bd8eb,null));};exports[_0x488f('0x4e')]=function(_0x14972d,_0x20f82d,_0x530bfb){var _0x146028={};var _0x40c04a={};var _0x1db665;var _0x33f761;return db['VtigerConfiguration'][_0x488f('0x4f')]({'where':{'id':_0x14972d[_0x488f('0x46')]['id']}})[_0x488f('0x24')](handleEntityNotFound(_0x20f82d,null))[_0x488f('0x24')](function(_0x1abde2){if(_0x1abde2){_0x1db665=_0x1abde2;_0x40c04a[_0x488f('0x2f')]=_[_0x488f('0x31')](db[_0x488f('0x4d')][_0x488f('0x47')]);_0x40c04a[_0x488f('0x30')]=_['keys'](_0x14972d[_0x488f('0x30')]);_0x40c04a[_0x488f('0x32')]=_[_0x488f('0x33')](_0x40c04a['model'],_0x40c04a['query']);_0x146028[_0x488f('0x34')]=_['intersection'](_0x40c04a[_0x488f('0x2f')],qs[_0x488f('0x35')](_0x14972d[_0x488f('0x30')][_0x488f('0x35')]));_0x146028['attributes']=_0x146028[_0x488f('0x34')][_0x488f('0x36')]?_0x146028[_0x488f('0x34')]:_0x40c04a[_0x488f('0x2f')];_0x146028[_0x488f('0x39')]=qs[_0x488f('0x3a')](_0x14972d[_0x488f('0x30')][_0x488f('0x3a')]);_0x146028[_0x488f('0x3b')]=qs[_0x488f('0x32')](_[_0x488f('0x3c')](_0x14972d[_0x488f('0x30')],_0x40c04a[_0x488f('0x32')]));if(_0x14972d[_0x488f('0x30')]['filter']){_0x146028[_0x488f('0x3b')]=_[_0x488f('0x3e')](_0x146028['where'],{'$or':_[_0x488f('0x2b')](_0x146028[_0x488f('0x34')],function(_0x175221){var _0x2405da={};_0x2405da[_0x175221]={'$like':'%'+_0x14972d[_0x488f('0x30')][_0x488f('0x3d')]+'%'};return _0x2405da;})});}_0x146028=_[_0x488f('0x3e')]({},_0x146028,_0x14972d[_0x488f('0x40')]);return _0x1db665['getSubjects'](_0x146028);}})[_0x488f('0x24')](function(_0x55b57c){if(_0x55b57c){_0x33f761=_0x55b57c[_0x488f('0x36')];if(!_0x14972d[_0x488f('0x30')][_0x488f('0x37')](_0x488f('0x38'))){_0x146028['limit']=qs[_0x488f('0x1d')](_0x14972d['query']['limit']);_0x146028[_0x488f('0x1f')]=qs[_0x488f('0x1f')](_0x14972d[_0x488f('0x30')][_0x488f('0x1f')]);}return _0x1db665[_0x488f('0x4e')](_0x146028);}})[_0x488f('0x24')](function(_0x23959b){if(_0x23959b){return _0x23959b?{'count':_0x33f761,'rows':_0x23959b}:null;}})[_0x488f('0x24')](respondWithResult(_0x20f82d,null))[_0x488f('0x45')](handleError(_0x20f82d,null));};exports[_0x488f('0x50')]=function(_0xe9d289,_0x44425b,_0x4a76eb){var _0x1e1bb3={};var _0x5c7916={};var _0x42dff8;var _0x4d5821;return db['VtigerConfiguration']['findOne']({'where':{'id':_0xe9d289['params']['id']}})[_0x488f('0x24')](handleEntityNotFound(_0x44425b,null))[_0x488f('0x24')](function(_0x13eaa4){if(_0x13eaa4){_0x42dff8=_0x13eaa4;_0x5c7916['model']=_['keys'](db[_0x488f('0x4d')][_0x488f('0x47')]);_0x5c7916['query']=_[_0x488f('0x31')](_0xe9d289['query']);_0x5c7916[_0x488f('0x32')]=_[_0x488f('0x33')](_0x5c7916[_0x488f('0x2f')],_0x5c7916[_0x488f('0x30')]);_0x1e1bb3[_0x488f('0x34')]=_['intersection'](_0x5c7916['model'],qs['fields'](_0xe9d289[_0x488f('0x30')][_0x488f('0x35')]));_0x1e1bb3[_0x488f('0x34')]=_0x1e1bb3[_0x488f('0x34')]['length']?_0x1e1bb3[_0x488f('0x34')]:_0x5c7916[_0x488f('0x2f')];_0x1e1bb3[_0x488f('0x39')]=qs[_0x488f('0x3a')](_0xe9d289[_0x488f('0x30')][_0x488f('0x3a')]);_0x1e1bb3[_0x488f('0x3b')]=qs[_0x488f('0x32')](_[_0x488f('0x3c')](_0xe9d289[_0x488f('0x30')],_0x5c7916['filters']));if(_0xe9d289[_0x488f('0x30')][_0x488f('0x3d')]){_0x1e1bb3[_0x488f('0x3b')]=_[_0x488f('0x3e')](_0x1e1bb3['where'],{'$or':_[_0x488f('0x2b')](_0x1e1bb3[_0x488f('0x34')],function(_0x29bdd8){var _0x3873a5={};_0x3873a5[_0x29bdd8]={'$like':'%'+_0xe9d289[_0x488f('0x30')][_0x488f('0x3d')]+'%'};return _0x3873a5;})});}_0x1e1bb3=_['merge']({},_0x1e1bb3,_0xe9d289['options']);return _0x42dff8[_0x488f('0x50')](_0x1e1bb3);}})[_0x488f('0x24')](function(_0x4d70e7){if(_0x4d70e7){_0x4d5821=_0x4d70e7[_0x488f('0x36')];if(!_0xe9d289[_0x488f('0x30')][_0x488f('0x37')](_0x488f('0x38'))){_0x1e1bb3['limit']=qs[_0x488f('0x1d')](_0xe9d289['query'][_0x488f('0x1d')]);_0x1e1bb3['offset']=qs['offset'](_0xe9d289['query'][_0x488f('0x1f')]);}return _0x42dff8['getDescriptions'](_0x1e1bb3);}})[_0x488f('0x24')](function(_0x5db579){if(_0x5db579){return _0x5db579?{'count':_0x4d5821,'rows':_0x5db579}:null;}})['then'](respondWithResult(_0x44425b,null))['catch'](handleError(_0x44425b,null));};