Built motion from commit 9b156c94.|2.6.22
[motion2.git] / server / api / intVtigerAccount / intVtigerAccount.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 _0x87b3=['filters','attributes','intersection','fields','length','hasOwnProperty','order','sort','where','filter','merge','VIRTUAL','options','includeAll','include','findAll','rows','catch','show','keys','rawAttributes','create','body','params','destroy','getConfigurations','findOne','AccountId','Subjects','getSubjects','channel','Descriptions','getDescriptions','md5','getFields','username','description','moduleName','remoteUri','serverUrl','accessKey','slice','substring','lastIndexOf','format','%s/webservice.php','GET','getchallenge','success','POST','login','result','describe','sessionName','autogenerated','reference','includes','remove','editable','false','cf_','picklist','picklistValues','label','value','log','vTiger\x20error:\x20','test','getFields,\x20%s,\x20%s,\x20%s','Wrong\x20credentials','statusCode','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','sox','ejs','fs-extra','lodash','squel','crypto','desk.js','querystring','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/license/util','../../mysqldb','../../components/integrations/configuration','sendStatus','status','json','limit','count','offset','set','Content-Range','apply','save','update','then','end','error','stack','name','send','map','VtigerAccount','fieldName','type','key','model','query'];(function(_0x19babd,_0x1d4ef7){var _0x314eaf=function(_0x24ea35){while(--_0x24ea35){_0x19babd['push'](_0x19babd['shift']());}};_0x314eaf(++_0x1d4ef7);}(_0x87b3,0xbf));var _0x387b=function(_0x56ab39,_0x1f9d11){_0x56ab39=_0x56ab39-0x0;var _0x32f0d0=_0x87b3[_0x56ab39];return _0x32f0d0;};'use strict';var emlformat=require(_0x387b('0x0'));var rimraf=require(_0x387b('0x1'));var zipdir=require(_0x387b('0x2'));var jsonpatch=require(_0x387b('0x3'));var rp=require(_0x387b('0x4'));var moment=require(_0x387b('0x5'));var BPromise=require(_0x387b('0x6'));var Mustache=require(_0x387b('0x7'));var util=require('util');var path=require('path');var sox=require(_0x387b('0x8'));var csv=require('to-csv');var ejs=require(_0x387b('0x9'));var fs=require('fs');var fs_extra=require(_0x387b('0xa'));var _=require(_0x387b('0xb'));var squel=require(_0x387b('0xc'));var crypto=require(_0x387b('0xd'));var jsforce=require('jsforce');var deskjs=require(_0x387b('0xe'));var toCsv=require('to-csv');var querystring=require(_0x387b('0xf'));var Papa=require('papaparse');var Redis=require('ioredis');var authService=require(_0x387b('0x10'));var qs=require('../../components/parsers/qs');var as=require(_0x387b('0x11'));var hardwareService=require(_0x387b('0x12'));var logger=require(_0x387b('0x13'))(_0x387b('0x14'));var utils=require(_0x387b('0x15'));var config=require('../../config/environment');var licenseUtil=require(_0x387b('0x16'));var db=require(_0x387b('0x17'))['db'];var integrations=require(_0x387b('0x18'));function respondWithStatusCode(_0x4ea002,_0x139938){_0x139938=_0x139938||0xcc;return function(_0x30fc62){if(_0x30fc62){return _0x4ea002[_0x387b('0x19')](_0x139938);}return _0x4ea002['status'](_0x139938)['end']();};}function respondWithResult(_0x4b0aa4,_0x2d708f){_0x2d708f=_0x2d708f||0xc8;return function(_0xb6cdb8){if(_0xb6cdb8){return _0x4b0aa4[_0x387b('0x1a')](_0x2d708f)[_0x387b('0x1b')](_0xb6cdb8);}};}function respondWithFilteredResult(_0x1fb98a,_0x3b35d7){return function(_0x58a188){if(_0x58a188){var _0x3b65ad=typeof _0x3b35d7['offset']==='undefined'&&typeof _0x3b35d7[_0x387b('0x1c')]==='undefined';var _0x5e66b9=_0x58a188[_0x387b('0x1d')];var _0x258d8b=_0x3b65ad?0x0:_0x3b35d7[_0x387b('0x1e')];var _0x3ec213=_0x3b65ad?_0x58a188[_0x387b('0x1d')]:_0x3b35d7['offset']+_0x3b35d7[_0x387b('0x1c')];var _0x1c62c0;if(_0x3ec213>=_0x5e66b9){_0x3ec213=_0x5e66b9;_0x1c62c0=0xc8;}else{_0x1c62c0=0xce;}_0x1fb98a['status'](_0x1c62c0);return _0x1fb98a[_0x387b('0x1f')](_0x387b('0x20'),_0x258d8b+'-'+_0x3ec213+'/'+_0x5e66b9)['json'](_0x58a188);}return null;};}function patchUpdates(_0x904558){return function(_0x5335d9){try{jsonpatch[_0x387b('0x21')](_0x5335d9,_0x904558,!![]);}catch(_0x160350){return BPromise['reject'](_0x160350);}return _0x5335d9[_0x387b('0x22')]();};}function saveUpdates(_0x2e674e,_0x5f0768){return function(_0x50b701){if(_0x50b701){return _0x50b701[_0x387b('0x23')](_0x2e674e)[_0x387b('0x24')](function(_0x31ff38){return _0x31ff38;});}return null;};}function removeEntity(_0x1c996a,_0x29b9ab){return function(_0x3208f5){if(_0x3208f5){return _0x3208f5['destroy']()[_0x387b('0x24')](function(){_0x1c996a[_0x387b('0x1a')](0xcc)[_0x387b('0x25')]();});}};}function handleEntityNotFound(_0x22fcf5,_0xbdfb8c){return function(_0x124f94){if(!_0x124f94){_0x22fcf5['sendStatus'](0x194);}return _0x124f94;};}function handleError(_0x266001,_0x1864c8){_0x1864c8=_0x1864c8||0x1f4;return function(_0x22def4){logger[_0x387b('0x26')](_0x22def4[_0x387b('0x27')]);if(_0x22def4['name']){delete _0x22def4[_0x387b('0x28')];}_0x266001[_0x387b('0x1a')](_0x1864c8)[_0x387b('0x29')](_0x22def4);};}exports['index']=function(_0x58f451,_0x15fdfc){var _0x201f8a={},_0x1e443f={},_0x36f2e9={'count':0x0,'rows':[]};var _0x1ffb7b=_[_0x387b('0x2a')](db[_0x387b('0x2b')]['rawAttributes'],function(_0x438465){return{'name':_0x438465[_0x387b('0x2c')],'type':_0x438465[_0x387b('0x2d')][_0x387b('0x2e')]};});_0x1e443f[_0x387b('0x2f')]=_[_0x387b('0x2a')](_0x1ffb7b,_0x387b('0x28'));_0x1e443f[_0x387b('0x30')]=_['keys'](_0x58f451[_0x387b('0x30')]);_0x1e443f[_0x387b('0x31')]=_['intersection'](_0x1e443f[_0x387b('0x2f')],_0x1e443f[_0x387b('0x30')]);_0x201f8a[_0x387b('0x32')]=_[_0x387b('0x33')](_0x1e443f[_0x387b('0x2f')],qs['fields'](_0x58f451[_0x387b('0x30')][_0x387b('0x34')]));_0x201f8a[_0x387b('0x32')]=_0x201f8a[_0x387b('0x32')][_0x387b('0x35')]?_0x201f8a[_0x387b('0x32')]:_0x1e443f[_0x387b('0x2f')];if(!_0x58f451[_0x387b('0x30')][_0x387b('0x36')]('nolimit')){_0x201f8a[_0x387b('0x1c')]=qs['limit'](_0x58f451[_0x387b('0x30')][_0x387b('0x1c')]);_0x201f8a['offset']=qs[_0x387b('0x1e')](_0x58f451[_0x387b('0x30')][_0x387b('0x1e')]);}_0x201f8a[_0x387b('0x37')]=qs[_0x387b('0x38')](_0x58f451['query'][_0x387b('0x38')]);_0x201f8a[_0x387b('0x39')]=qs[_0x387b('0x31')](_['pick'](_0x58f451['query'],_0x1e443f[_0x387b('0x31')]),_0x1ffb7b);if(_0x58f451[_0x387b('0x30')][_0x387b('0x3a')]){_0x201f8a[_0x387b('0x39')]=_[_0x387b('0x3b')](_0x201f8a[_0x387b('0x39')],{'$or':_[_0x387b('0x2a')](_0x1ffb7b,function(_0x10ba35){if(_0x10ba35[_0x387b('0x2d')]!==_0x387b('0x3c')){var _0x11f562={};_0x11f562[_0x10ba35[_0x387b('0x28')]]={'$like':'%'+_0x58f451['query'][_0x387b('0x3a')]+'%'};return _0x11f562;}})});}_0x201f8a=_[_0x387b('0x3b')]({},_0x201f8a,_0x58f451[_0x387b('0x3d')]);var _0x2e571f={'where':_0x201f8a['where']};return db[_0x387b('0x2b')]['count'](_0x2e571f)[_0x387b('0x24')](function(_0x438ea5){_0x36f2e9[_0x387b('0x1d')]=_0x438ea5;if(_0x58f451[_0x387b('0x30')][_0x387b('0x3e')]){_0x201f8a[_0x387b('0x3f')]=[{'all':!![]}];}return db[_0x387b('0x2b')][_0x387b('0x40')](_0x201f8a);})[_0x387b('0x24')](function(_0x136d32){_0x36f2e9[_0x387b('0x41')]=_0x136d32;return _0x36f2e9;})[_0x387b('0x24')](respondWithFilteredResult(_0x15fdfc,_0x201f8a))[_0x387b('0x42')](handleError(_0x15fdfc,null));};exports[_0x387b('0x43')]=function(_0x2cda60,_0x1020c0){var _0x2d8c87={'raw':![],'where':{'id':_0x2cda60['params']['id']}},_0x2b63af={};_0x2b63af[_0x387b('0x2f')]=_[_0x387b('0x44')](db['VtigerAccount'][_0x387b('0x45')]);_0x2b63af[_0x387b('0x30')]=_['keys'](_0x2cda60['query']);_0x2b63af[_0x387b('0x31')]=_[_0x387b('0x33')](_0x2b63af[_0x387b('0x2f')],_0x2b63af['query']);_0x2d8c87[_0x387b('0x32')]=_['intersection'](_0x2b63af[_0x387b('0x2f')],qs[_0x387b('0x34')](_0x2cda60[_0x387b('0x30')][_0x387b('0x34')]));_0x2d8c87[_0x387b('0x32')]=_0x2d8c87[_0x387b('0x32')][_0x387b('0x35')]?_0x2d8c87['attributes']:_0x2b63af['model'];if(_0x2cda60[_0x387b('0x30')][_0x387b('0x3e')]){_0x2d8c87['include']=[{'all':!![]}];}_0x2d8c87=_[_0x387b('0x3b')]({},_0x2d8c87,_0x2cda60[_0x387b('0x3d')]);return db[_0x387b('0x2b')]['find'](_0x2d8c87)[_0x387b('0x24')](handleEntityNotFound(_0x1020c0,null))[_0x387b('0x24')](respondWithResult(_0x1020c0,null))['catch'](handleError(_0x1020c0,null));};exports[_0x387b('0x46')]=function(_0x348e36,_0x4859d2){return db[_0x387b('0x2b')][_0x387b('0x46')](_0x348e36['body'],{})[_0x387b('0x24')](respondWithResult(_0x4859d2,0xc9))[_0x387b('0x42')](handleError(_0x4859d2,null));};exports['update']=function(_0x5f14c7,_0x525b08){if(_0x5f14c7[_0x387b('0x47')]['id']){delete _0x5f14c7[_0x387b('0x47')]['id'];}return db[_0x387b('0x2b')]['find']({'where':{'id':_0x5f14c7[_0x387b('0x48')]['id']}})['then'](handleEntityNotFound(_0x525b08,null))[_0x387b('0x24')](saveUpdates(_0x5f14c7[_0x387b('0x47')],null))[_0x387b('0x24')](respondWithResult(_0x525b08,null))[_0x387b('0x42')](handleError(_0x525b08,null));};exports[_0x387b('0x49')]=function(_0x541e3d,_0x91434){return db[_0x387b('0x2b')]['find']({'where':{'id':_0x541e3d['params']['id']}})[_0x387b('0x24')](handleEntityNotFound(_0x91434,null))['then'](removeEntity(_0x91434,null))['catch'](handleError(_0x91434,null));};exports[_0x387b('0x4a')]=function(_0x394068,_0x48ce06,_0x4daf0f){var _0x52a498={};var _0x108f75={};var _0x5c924b;var _0x1bf87c;return db[_0x387b('0x2b')][_0x387b('0x4b')]({'where':{'id':_0x394068[_0x387b('0x48')]['id']}})[_0x387b('0x24')](handleEntityNotFound(_0x48ce06,null))[_0x387b('0x24')](function(_0x88cb9f){if(_0x88cb9f){_0x5c924b=_0x88cb9f;_0x108f75[_0x387b('0x2f')]=_[_0x387b('0x44')](db['VtigerConfiguration'][_0x387b('0x45')]);_0x108f75['query']=_['keys'](_0x394068['query']);_0x108f75['filters']=_[_0x387b('0x33')](_0x108f75[_0x387b('0x2f')],_0x108f75['query']);_0x52a498[_0x387b('0x32')]=_[_0x387b('0x33')](_0x108f75[_0x387b('0x2f')],qs[_0x387b('0x34')](_0x394068[_0x387b('0x30')]['fields']));_0x52a498[_0x387b('0x32')]=_0x52a498[_0x387b('0x32')]['length']?_0x52a498[_0x387b('0x32')]:_0x108f75[_0x387b('0x2f')];_0x52a498[_0x387b('0x37')]=qs['sort'](_0x394068[_0x387b('0x30')]['sort']);_0x52a498[_0x387b('0x39')]=qs['filters'](_['pick'](_0x394068[_0x387b('0x30')],_0x108f75[_0x387b('0x31')]));if(_0x394068[_0x387b('0x30')][_0x387b('0x3a')]){_0x52a498['where']=_['merge'](_0x52a498[_0x387b('0x39')],{'$or':_[_0x387b('0x2a')](_0x52a498[_0x387b('0x32')],function(_0x27bf1b){var _0x1e14dd={};_0x1e14dd[_0x27bf1b]={'$like':'%'+_0x394068['query'][_0x387b('0x3a')]+'%'};return _0x1e14dd;})});}_0x52a498=_[_0x387b('0x3b')]({},_0x52a498,_0x394068[_0x387b('0x3d')]);return _0x5c924b[_0x387b('0x4a')](_0x52a498);}})[_0x387b('0x24')](function(_0x50b383){if(_0x50b383){_0x1bf87c=_0x50b383['length'];if(!_0x394068[_0x387b('0x30')][_0x387b('0x36')]('nolimit')){_0x52a498[_0x387b('0x1c')]=qs[_0x387b('0x1c')](_0x394068[_0x387b('0x30')][_0x387b('0x1c')]);_0x52a498['offset']=qs[_0x387b('0x1e')](_0x394068[_0x387b('0x30')]['offset']);}return _0x5c924b['getConfigurations'](_0x52a498);}})[_0x387b('0x24')](function(_0x22b519){if(_0x22b519){return _0x22b519?{'count':_0x1bf87c,'rows':_0x22b519}:null;}})[_0x387b('0x24')](respondWithResult(_0x48ce06,null))[_0x387b('0x42')](handleError(_0x48ce06,null));};exports['addConfiguration']=function(_0x3539c0,_0x46d7c8,_0x4c2676){if(_0x3539c0[_0x387b('0x47')]['id']){delete _0x3539c0[_0x387b('0x47')]['id'];}return db[_0x387b('0x2b')][_0x387b('0x4b')]({'where':{'id':_0x3539c0[_0x387b('0x48')]['id']}})[_0x387b('0x24')](handleEntityNotFound(_0x46d7c8,null))[_0x387b('0x24')](function(_0x2088e9){if(_0x2088e9){_0x3539c0[_0x387b('0x47')][_0x387b('0x4c')]=_0x2088e9['id'];_0x3539c0[_0x387b('0x47')][_0x387b('0x4d')]=integrations[_0x387b('0x4e')](_0x3539c0[_0x387b('0x47')][_0x387b('0x4f')],_0x3539c0[_0x387b('0x47')]['type']);_0x3539c0['body'][_0x387b('0x50')]=integrations[_0x387b('0x51')](_0x3539c0[_0x387b('0x47')][_0x387b('0x4f')],_0x3539c0[_0x387b('0x47')][_0x387b('0x2d')]);return db['VtigerConfiguration'][_0x387b('0x46')](_0x3539c0[_0x387b('0x47')],{'include':[{'model':db['VtigerField'],'as':_0x387b('0x4d')},{'model':db['VtigerField'],'as':_0x387b('0x50')}]});}return null;})[_0x387b('0x24')](respondWithResult(_0x46d7c8,null))[_0x387b('0x42')](handleError(_0x46d7c8,null));};var md5=require(_0x387b('0x52'));exports[_0x387b('0x53')]=function(_0x3d0066,_0x1e9d0a,_0x60eb70){var _0x21ffca=null;var _0x1b5fea=null;var _0xe384bc=null;var _0x3a0116=null;var _0x4ca5c2='';return db[_0x387b('0x2b')][_0x387b('0x4b')]({'where':{'id':_0x3d0066[_0x387b('0x48')]['id']},'attributes':['id',_0x387b('0x28'),_0x387b('0x54'),_0x387b('0x55'),_0x387b('0x56'),_0x387b('0x57'),_0x387b('0x58'),_0x387b('0x59')]})[_0x387b('0x24')](handleEntityNotFound(_0x1e9d0a,null))[_0x387b('0x24')](function(_0x48aab8){if(_0x48aab8){_0x21ffca=_0x48aab8[_0x387b('0x54')];_0x1b5fea=_0x48aab8[_0x387b('0x59')];_0xe384bc=_0x48aab8[_0x387b('0x56')];_0x4ca5c2=_0x48aab8[_0x387b('0x57')];var _0x20ea13=_0x4ca5c2[_0x387b('0x5a')](-0x1);if(_0x20ea13==='/'){_0x4ca5c2=_0x4ca5c2[_0x387b('0x5b')](0x0,_0x4ca5c2[_0x387b('0x5c')](_0x20ea13));}_0x3a0116=util[_0x387b('0x5d')](_0x387b('0x5e'),_0x4ca5c2);var _0x42562d={'method':_0x387b('0x5f'),'uri':_0x3a0116,'qs':{'operation':_0x387b('0x60'),'username':_0x48aab8[_0x387b('0x54')]},'json':!![]};return rp(_0x42562d);}})[_0x387b('0x24')](function(_0x2486bf){if(_0x2486bf[_0x387b('0x61')]){var _0x125ca2={'method':_0x387b('0x62'),'uri':_0x3a0116,'form':{'operation':_0x387b('0x63'),'username':_0x21ffca,'accessKey':md5(_0x2486bf[_0x387b('0x64')]['token']+_0x1b5fea)},'json':!![]};return rp(_0x125ca2);}})['then'](function(_0x32f41c){if(_0x32f41c[_0x387b('0x61')]){var _0x4aa0e9={'method':_0x387b('0x5f'),'uri':_0x3a0116,'qs':{'operation':_0x387b('0x65'),'username':_0x21ffca,'sessionName':_0x32f41c['result'][_0x387b('0x66')],'elementType':_0xe384bc},'json':!![]};return rp(_0x4aa0e9);}})[_0x387b('0x24')](function(_0x56511c){if(_0x56511c[_0x387b('0x61')]){var _0x3afefc=[_0x387b('0x67'),_0x387b('0x68'),'owner'];_['remove'](_0x56511c[_0x387b('0x64')][_0x387b('0x34')],function(_0x23aaa5){return _0x3afefc[_0x387b('0x69')](_0x23aaa5[_0x387b('0x2d')][_0x387b('0x28')]);});_[_0x387b('0x6a')](_0x56511c[_0x387b('0x64')][_0x387b('0x34')],function(_0x1f55fa){return _0x1f55fa[_0x387b('0x6b')]===_0x387b('0x6c');});return{'count':_0x56511c[_0x387b('0x64')][_0x387b('0x34')][_0x387b('0x35')],'rows':_[_0x387b('0x2a')](_0x56511c[_0x387b('0x64')][_0x387b('0x34')],function(_0x59cffb){return{'id':_0x59cffb['name'],'name':_0x59cffb['label'],'custom':_['startsWith'](_0x59cffb['name'],_0x387b('0x6d'))?!![]:![],'options':_0x59cffb['type']['name']===_0x387b('0x6e')&&_0x59cffb[_0x387b('0x2d')][_0x387b('0x6f')][_0x387b('0x35')]>0x0?_[_0x387b('0x2a')](_0x59cffb['type']['picklistValues'],function(_0x4e87fb){return{'name':_0x4e87fb[_0x387b('0x70')],'value':_0x4e87fb[_0x387b('0x71')]};}):[]};})};}})[_0x387b('0x24')](respondWithResult(_0x1e9d0a,null))[_0x387b('0x42')](function(_0x4ca365){console[_0x387b('0x72')](_0x387b('0x73')+_0x4ca365);var _0x20d680=_0x3d0066[_0x387b('0x30')][_0x387b('0x74')]?0x1f4:_0x4ca365['statusCode']||0x1f4;logger['error'](_0x387b('0x75'),_0x387b('0x53'),_0x20d680,JSON['stringify'](_0x4ca365));delete _0x4ca365[_0x387b('0x28')];if(_0x20d680===0x191){_0x20d680=0x190;}_0x1e9d0a[_0x387b('0x1a')](_0x20d680)['send'](_0x3d0066[_0x387b('0x30')]['test']?{'message':_0x387b('0x76'),'statusCode':_0x4ca365[_0x387b('0x77')]}:_0x4ca365);});};