Built motion from commit 37e1ed56.|2.6.29
[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 _0x8f35=['editable','false','startsWith','cf_','picklistValues','label','value','test','stringify','send','Wrong\x20credentials','statusCode','rimraf','zip-dir','fast-json-patch','request-promise','bluebird','path','sox','to-csv','lodash','squel','crypto','jsforce','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status','json','undefined','count','offset','limit','set','Content-Range','apply','update','then','destroy','end','error','stack','name','map','VtigerAccount','rawAttributes','fieldName','type','key','model','query','filters','intersection','attributes','fields','length','hasOwnProperty','order','sort','where','filter','VIRTUAL','merge','options','includeAll','include','rows','catch','show','params','keys','find','create','body','getConfigurations','VtigerConfiguration','pick','addConfiguration','findOne','AccountId','getSubjects','channel','Descriptions','VtigerField','Subjects','md5','getFields','username','description','remoteUri','serverUrl','accessKey','slice','substring','format','%s/webservice.php','GET','POST','login','result','token','describe','sessionName','autogenerated','reference','includes','remove'];(function(_0xccf869,_0x2556a5){var _0x21a9db=function(_0xcc4835){while(--_0xcc4835){_0xccf869['push'](_0xccf869['shift']());}};_0x21a9db(++_0x2556a5);}(_0x8f35,0xfa));var _0x58f3=function(_0x4d613c,_0x55f220){_0x4d613c=_0x4d613c-0x0;var _0x301fe3=_0x8f35[_0x4d613c];return _0x301fe3;};'use strict';var emlformat=require('eml-format');var rimraf=require(_0x58f3('0x0'));var zipdir=require(_0x58f3('0x1'));var jsonpatch=require(_0x58f3('0x2'));var rp=require(_0x58f3('0x3'));var moment=require('moment');var BPromise=require(_0x58f3('0x4'));var Mustache=require('mustache');var util=require('util');var path=require(_0x58f3('0x5'));var sox=require(_0x58f3('0x6'));var csv=require(_0x58f3('0x7'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0x58f3('0x8'));var squel=require(_0x58f3('0x9'));var crypto=require(_0x58f3('0xa'));var jsforce=require(_0x58f3('0xb'));var deskjs=require(_0x58f3('0xc'));var toCsv=require(_0x58f3('0x7'));var querystring=require(_0x58f3('0xd'));var Papa=require(_0x58f3('0xe'));var Redis=require(_0x58f3('0xf'));var authService=require(_0x58f3('0x10'));var qs=require(_0x58f3('0x11'));var as=require(_0x58f3('0x12'));var hardwareService=require(_0x58f3('0x13'));var logger=require(_0x58f3('0x14'))(_0x58f3('0x15'));var utils=require(_0x58f3('0x16'));var config=require(_0x58f3('0x17'));var licenseUtil=require(_0x58f3('0x18'));var db=require(_0x58f3('0x19'))['db'];var integrations=require('../../components/integrations/configuration');function respondWithStatusCode(_0x52a615,_0x325026){_0x325026=_0x325026||0xcc;return function(_0x2ef594){if(_0x2ef594){return _0x52a615[_0x58f3('0x1a')](_0x325026);}return _0x52a615[_0x58f3('0x1b')](_0x325026)['end']();};}function respondWithResult(_0x3eadde,_0x346f98){_0x346f98=_0x346f98||0xc8;return function(_0x1508e5){if(_0x1508e5){return _0x3eadde[_0x58f3('0x1b')](_0x346f98)[_0x58f3('0x1c')](_0x1508e5);}};}function respondWithFilteredResult(_0x345140,_0x186354){return function(_0x14c8de){if(_0x14c8de){var _0x1d1f91=typeof _0x186354['offset']===_0x58f3('0x1d')&&typeof _0x186354['limit']==='undefined';var _0x2bfa62=_0x14c8de[_0x58f3('0x1e')];var _0x37dc88=_0x1d1f91?0x0:_0x186354[_0x58f3('0x1f')];var _0x13d2da=_0x1d1f91?_0x14c8de[_0x58f3('0x1e')]:_0x186354[_0x58f3('0x1f')]+_0x186354[_0x58f3('0x20')];var _0x88183d;if(_0x13d2da>=_0x2bfa62){_0x13d2da=_0x2bfa62;_0x88183d=0xc8;}else{_0x88183d=0xce;}_0x345140['status'](_0x88183d);return _0x345140[_0x58f3('0x21')](_0x58f3('0x22'),_0x37dc88+'-'+_0x13d2da+'/'+_0x2bfa62)['json'](_0x14c8de);}return null;};}function patchUpdates(_0x476d13){return function(_0x1a2068){try{jsonpatch[_0x58f3('0x23')](_0x1a2068,_0x476d13,!![]);}catch(_0x5e1c8b){return BPromise['reject'](_0x5e1c8b);}return _0x1a2068['save']();};}function saveUpdates(_0x2e7f01,_0x160534){return function(_0x36cf2b){if(_0x36cf2b){return _0x36cf2b[_0x58f3('0x24')](_0x2e7f01)[_0x58f3('0x25')](function(_0x47c7a8){return _0x47c7a8;});}return null;};}function removeEntity(_0x594a5e,_0xfb7238){return function(_0x497d53){if(_0x497d53){return _0x497d53[_0x58f3('0x26')]()[_0x58f3('0x25')](function(){_0x594a5e[_0x58f3('0x1b')](0xcc)[_0x58f3('0x27')]();});}};}function handleEntityNotFound(_0x37e959,_0x2ef255){return function(_0x50153d){if(!_0x50153d){_0x37e959[_0x58f3('0x1a')](0x194);}return _0x50153d;};}function handleError(_0x91423e,_0x36e6b7){_0x36e6b7=_0x36e6b7||0x1f4;return function(_0x4fde09){logger[_0x58f3('0x28')](_0x4fde09[_0x58f3('0x29')]);if(_0x4fde09[_0x58f3('0x2a')]){delete _0x4fde09['name'];}_0x91423e['status'](_0x36e6b7)['send'](_0x4fde09);};}exports['index']=function(_0x58be4b,_0x305d25){var _0x56ce4d={},_0x5a3e65={},_0x149fd9={'count':0x0,'rows':[]};var _0x5e5852=_[_0x58f3('0x2b')](db[_0x58f3('0x2c')][_0x58f3('0x2d')],function(_0x365044){return{'name':_0x365044[_0x58f3('0x2e')],'type':_0x365044[_0x58f3('0x2f')][_0x58f3('0x30')]};});_0x5a3e65[_0x58f3('0x31')]=_['map'](_0x5e5852,_0x58f3('0x2a'));_0x5a3e65[_0x58f3('0x32')]=_['keys'](_0x58be4b[_0x58f3('0x32')]);_0x5a3e65[_0x58f3('0x33')]=_[_0x58f3('0x34')](_0x5a3e65[_0x58f3('0x31')],_0x5a3e65[_0x58f3('0x32')]);_0x56ce4d[_0x58f3('0x35')]=_[_0x58f3('0x34')](_0x5a3e65[_0x58f3('0x31')],qs[_0x58f3('0x36')](_0x58be4b['query'][_0x58f3('0x36')]));_0x56ce4d[_0x58f3('0x35')]=_0x56ce4d[_0x58f3('0x35')][_0x58f3('0x37')]?_0x56ce4d[_0x58f3('0x35')]:_0x5a3e65['model'];if(!_0x58be4b[_0x58f3('0x32')][_0x58f3('0x38')]('nolimit')){_0x56ce4d[_0x58f3('0x20')]=qs[_0x58f3('0x20')](_0x58be4b[_0x58f3('0x32')][_0x58f3('0x20')]);_0x56ce4d['offset']=qs[_0x58f3('0x1f')](_0x58be4b[_0x58f3('0x32')]['offset']);}_0x56ce4d[_0x58f3('0x39')]=qs['sort'](_0x58be4b[_0x58f3('0x32')][_0x58f3('0x3a')]);_0x56ce4d[_0x58f3('0x3b')]=qs[_0x58f3('0x33')](_['pick'](_0x58be4b[_0x58f3('0x32')],_0x5a3e65[_0x58f3('0x33')]),_0x5e5852);if(_0x58be4b['query'][_0x58f3('0x3c')]){_0x56ce4d[_0x58f3('0x3b')]=_['merge'](_0x56ce4d[_0x58f3('0x3b')],{'$or':_[_0x58f3('0x2b')](_0x5e5852,function(_0x3ae2ee){if(_0x3ae2ee['type']!==_0x58f3('0x3d')){var _0x2cdd6f={};_0x2cdd6f[_0x3ae2ee['name']]={'$like':'%'+_0x58be4b[_0x58f3('0x32')][_0x58f3('0x3c')]+'%'};return _0x2cdd6f;}})});}_0x56ce4d=_[_0x58f3('0x3e')]({},_0x56ce4d,_0x58be4b[_0x58f3('0x3f')]);var _0x726e4d={'where':_0x56ce4d[_0x58f3('0x3b')]};return db['VtigerAccount']['count'](_0x726e4d)[_0x58f3('0x25')](function(_0x32bec6){_0x149fd9['count']=_0x32bec6;if(_0x58be4b[_0x58f3('0x32')][_0x58f3('0x40')]){_0x56ce4d[_0x58f3('0x41')]=[{'all':!![]}];}return db[_0x58f3('0x2c')]['findAll'](_0x56ce4d);})[_0x58f3('0x25')](function(_0x40f1b7){_0x149fd9[_0x58f3('0x42')]=_0x40f1b7;return _0x149fd9;})[_0x58f3('0x25')](respondWithFilteredResult(_0x305d25,_0x56ce4d))[_0x58f3('0x43')](handleError(_0x305d25,null));};exports[_0x58f3('0x44')]=function(_0x56dbfa,_0x484a46){var _0x5695d7={'raw':![],'where':{'id':_0x56dbfa[_0x58f3('0x45')]['id']}},_0x472a15={};_0x472a15[_0x58f3('0x31')]=_[_0x58f3('0x46')](db[_0x58f3('0x2c')]['rawAttributes']);_0x472a15[_0x58f3('0x32')]=_['keys'](_0x56dbfa[_0x58f3('0x32')]);_0x472a15[_0x58f3('0x33')]=_[_0x58f3('0x34')](_0x472a15[_0x58f3('0x31')],_0x472a15[_0x58f3('0x32')]);_0x5695d7['attributes']=_[_0x58f3('0x34')](_0x472a15[_0x58f3('0x31')],qs[_0x58f3('0x36')](_0x56dbfa[_0x58f3('0x32')][_0x58f3('0x36')]));_0x5695d7['attributes']=_0x5695d7['attributes']['length']?_0x5695d7[_0x58f3('0x35')]:_0x472a15[_0x58f3('0x31')];if(_0x56dbfa[_0x58f3('0x32')][_0x58f3('0x40')]){_0x5695d7['include']=[{'all':!![]}];}_0x5695d7=_[_0x58f3('0x3e')]({},_0x5695d7,_0x56dbfa['options']);return db[_0x58f3('0x2c')][_0x58f3('0x47')](_0x5695d7)['then'](handleEntityNotFound(_0x484a46,null))[_0x58f3('0x25')](respondWithResult(_0x484a46,null))[_0x58f3('0x43')](handleError(_0x484a46,null));};exports[_0x58f3('0x48')]=function(_0xa7a38c,_0x1b6273){return db['VtigerAccount']['create'](_0xa7a38c[_0x58f3('0x49')],{})['then'](respondWithResult(_0x1b6273,0xc9))['catch'](handleError(_0x1b6273,null));};exports[_0x58f3('0x24')]=function(_0x5070f9,_0x285586){if(_0x5070f9[_0x58f3('0x49')]['id']){delete _0x5070f9[_0x58f3('0x49')]['id'];}return db[_0x58f3('0x2c')][_0x58f3('0x47')]({'where':{'id':_0x5070f9[_0x58f3('0x45')]['id']}})['then'](handleEntityNotFound(_0x285586,null))[_0x58f3('0x25')](saveUpdates(_0x5070f9[_0x58f3('0x49')],null))[_0x58f3('0x25')](respondWithResult(_0x285586,null))[_0x58f3('0x43')](handleError(_0x285586,null));};exports[_0x58f3('0x26')]=function(_0xc941d0,_0xb61fcf){return db['VtigerAccount'][_0x58f3('0x47')]({'where':{'id':_0xc941d0['params']['id']}})[_0x58f3('0x25')](handleEntityNotFound(_0xb61fcf,null))[_0x58f3('0x25')](removeEntity(_0xb61fcf,null))[_0x58f3('0x43')](handleError(_0xb61fcf,null));};exports[_0x58f3('0x4a')]=function(_0x47962c,_0x39d4f0,_0x5bc09c){var _0x4c4389={};var _0x2039f9={};var _0x57f29f;var _0x5af677;return db[_0x58f3('0x2c')]['findOne']({'where':{'id':_0x47962c[_0x58f3('0x45')]['id']}})[_0x58f3('0x25')](handleEntityNotFound(_0x39d4f0,null))[_0x58f3('0x25')](function(_0x2c9aab){if(_0x2c9aab){_0x57f29f=_0x2c9aab;_0x2039f9[_0x58f3('0x31')]=_[_0x58f3('0x46')](db[_0x58f3('0x4b')][_0x58f3('0x2d')]);_0x2039f9[_0x58f3('0x32')]=_['keys'](_0x47962c[_0x58f3('0x32')]);_0x2039f9['filters']=_[_0x58f3('0x34')](_0x2039f9[_0x58f3('0x31')],_0x2039f9[_0x58f3('0x32')]);_0x4c4389[_0x58f3('0x35')]=_['intersection'](_0x2039f9[_0x58f3('0x31')],qs[_0x58f3('0x36')](_0x47962c[_0x58f3('0x32')][_0x58f3('0x36')]));_0x4c4389[_0x58f3('0x35')]=_0x4c4389[_0x58f3('0x35')][_0x58f3('0x37')]?_0x4c4389[_0x58f3('0x35')]:_0x2039f9['model'];_0x4c4389[_0x58f3('0x39')]=qs[_0x58f3('0x3a')](_0x47962c['query'][_0x58f3('0x3a')]);_0x4c4389[_0x58f3('0x3b')]=qs[_0x58f3('0x33')](_[_0x58f3('0x4c')](_0x47962c['query'],_0x2039f9[_0x58f3('0x33')]));if(_0x47962c[_0x58f3('0x32')]['filter']){_0x4c4389[_0x58f3('0x3b')]=_[_0x58f3('0x3e')](_0x4c4389[_0x58f3('0x3b')],{'$or':_[_0x58f3('0x2b')](_0x4c4389[_0x58f3('0x35')],function(_0x110684){var _0x459178={};_0x459178[_0x110684]={'$like':'%'+_0x47962c[_0x58f3('0x32')]['filter']+'%'};return _0x459178;})});}_0x4c4389=_[_0x58f3('0x3e')]({},_0x4c4389,_0x47962c[_0x58f3('0x3f')]);return _0x57f29f[_0x58f3('0x4a')](_0x4c4389);}})[_0x58f3('0x25')](function(_0x26370e){if(_0x26370e){_0x5af677=_0x26370e[_0x58f3('0x37')];if(!_0x47962c[_0x58f3('0x32')]['hasOwnProperty']('nolimit')){_0x4c4389[_0x58f3('0x20')]=qs[_0x58f3('0x20')](_0x47962c[_0x58f3('0x32')]['limit']);_0x4c4389[_0x58f3('0x1f')]=qs[_0x58f3('0x1f')](_0x47962c['query'][_0x58f3('0x1f')]);}return _0x57f29f[_0x58f3('0x4a')](_0x4c4389);}})[_0x58f3('0x25')](function(_0x2207af){if(_0x2207af){return _0x2207af?{'count':_0x5af677,'rows':_0x2207af}:null;}})[_0x58f3('0x25')](respondWithResult(_0x39d4f0,null))[_0x58f3('0x43')](handleError(_0x39d4f0,null));};exports[_0x58f3('0x4d')]=function(_0x5a0fdd,_0x37de17,_0x5e2231){if(_0x5a0fdd['body']['id']){delete _0x5a0fdd[_0x58f3('0x49')]['id'];}return db['VtigerAccount'][_0x58f3('0x4e')]({'where':{'id':_0x5a0fdd[_0x58f3('0x45')]['id']}})[_0x58f3('0x25')](handleEntityNotFound(_0x37de17,null))[_0x58f3('0x25')](function(_0x591440){if(_0x591440){_0x5a0fdd[_0x58f3('0x49')][_0x58f3('0x4f')]=_0x591440['id'];_0x5a0fdd[_0x58f3('0x49')]['Subjects']=integrations[_0x58f3('0x50')](_0x5a0fdd['body'][_0x58f3('0x51')],_0x5a0fdd[_0x58f3('0x49')]['type']);_0x5a0fdd[_0x58f3('0x49')][_0x58f3('0x52')]=integrations['getDescriptions'](_0x5a0fdd[_0x58f3('0x49')][_0x58f3('0x51')],_0x5a0fdd[_0x58f3('0x49')][_0x58f3('0x2f')]);return db[_0x58f3('0x4b')][_0x58f3('0x48')](_0x5a0fdd[_0x58f3('0x49')],{'include':[{'model':db[_0x58f3('0x53')],'as':_0x58f3('0x54')},{'model':db[_0x58f3('0x53')],'as':'Descriptions'}]});}return null;})[_0x58f3('0x25')](respondWithResult(_0x37de17,null))[_0x58f3('0x43')](handleError(_0x37de17,null));};var md5=require(_0x58f3('0x55'));exports[_0x58f3('0x56')]=function(_0x16c23c,_0x35b62a,_0x48a031){var _0x4e0053=null;var _0x2f5184=null;var _0x5de5fe=null;var _0x3584cb=null;var _0x2c749e='';return db[_0x58f3('0x2c')][_0x58f3('0x4e')]({'where':{'id':_0x16c23c['params']['id']},'attributes':['id',_0x58f3('0x2a'),_0x58f3('0x57'),_0x58f3('0x58'),'moduleName',_0x58f3('0x59'),_0x58f3('0x5a'),_0x58f3('0x5b')]})['then'](handleEntityNotFound(_0x35b62a,null))[_0x58f3('0x25')](function(_0x43a673){if(_0x43a673){_0x4e0053=_0x43a673[_0x58f3('0x57')];_0x2f5184=_0x43a673[_0x58f3('0x5b')];_0x5de5fe=_0x43a673['moduleName'];_0x2c749e=_0x43a673[_0x58f3('0x59')];var _0x1e83c3=_0x2c749e[_0x58f3('0x5c')](-0x1);if(_0x1e83c3==='/'){_0x2c749e=_0x2c749e[_0x58f3('0x5d')](0x0,_0x2c749e['lastIndexOf'](_0x1e83c3));}_0x3584cb=util[_0x58f3('0x5e')](_0x58f3('0x5f'),_0x2c749e);var _0x3aae2d={'method':_0x58f3('0x60'),'uri':_0x3584cb,'qs':{'operation':'getchallenge','username':_0x43a673['username']},'json':!![]};return rp(_0x3aae2d);}})[_0x58f3('0x25')](function(_0x3b4c71){if(_0x3b4c71['success']){var _0x1de441={'method':_0x58f3('0x61'),'uri':_0x3584cb,'form':{'operation':_0x58f3('0x62'),'username':_0x4e0053,'accessKey':md5(_0x3b4c71[_0x58f3('0x63')][_0x58f3('0x64')]+_0x2f5184)},'json':!![]};return rp(_0x1de441);}})[_0x58f3('0x25')](function(_0x359648){if(_0x359648['success']){var _0xbcae23={'method':'GET','uri':_0x3584cb,'qs':{'operation':_0x58f3('0x65'),'username':_0x4e0053,'sessionName':_0x359648[_0x58f3('0x63')][_0x58f3('0x66')],'elementType':_0x5de5fe},'json':!![]};return rp(_0xbcae23);}})[_0x58f3('0x25')](function(_0x1f4e5e){if(_0x1f4e5e['success']){var _0x12c13c=[_0x58f3('0x67'),_0x58f3('0x68'),'owner'];_['remove'](_0x1f4e5e['result'][_0x58f3('0x36')],function(_0x11906d){return _0x12c13c[_0x58f3('0x69')](_0x11906d['type']['name']);});_[_0x58f3('0x6a')](_0x1f4e5e[_0x58f3('0x63')][_0x58f3('0x36')],function(_0x2a959e){return _0x2a959e[_0x58f3('0x6b')]===_0x58f3('0x6c');});return{'count':_0x1f4e5e[_0x58f3('0x63')]['fields'][_0x58f3('0x37')],'rows':_[_0x58f3('0x2b')](_0x1f4e5e[_0x58f3('0x63')][_0x58f3('0x36')],function(_0x4e86a2){return{'id':_0x4e86a2[_0x58f3('0x2a')],'name':_0x4e86a2['label'],'custom':_[_0x58f3('0x6d')](_0x4e86a2[_0x58f3('0x2a')],_0x58f3('0x6e'))?!![]:![],'options':_0x4e86a2[_0x58f3('0x2f')]['name']==='picklist'&&_0x4e86a2[_0x58f3('0x2f')][_0x58f3('0x6f')][_0x58f3('0x37')]>0x0?_[_0x58f3('0x2b')](_0x4e86a2['type'][_0x58f3('0x6f')],function(_0x469f2f){return{'name':_0x469f2f[_0x58f3('0x70')],'value':_0x469f2f[_0x58f3('0x71')]};}):[]};})};}})['then'](respondWithResult(_0x35b62a,null))[_0x58f3('0x43')](function(_0x472888){console['log']('vTiger\x20error:\x20'+_0x472888);var _0x351bfc=_0x16c23c['query'][_0x58f3('0x72')]?0x1f4:_0x472888['statusCode']||0x1f4;logger[_0x58f3('0x28')]('getFields,\x20%s,\x20%s,\x20%s',_0x58f3('0x56'),_0x351bfc,JSON[_0x58f3('0x73')](_0x472888));delete _0x472888[_0x58f3('0x2a')];if(_0x351bfc===0x191){_0x351bfc=0x190;}_0x35b62a[_0x58f3('0x1b')](_0x351bfc)[_0x58f3('0x74')](_0x16c23c[_0x58f3('0x32')][_0x58f3('0x72')]?{'message':_0x58f3('0x75'),'statusCode':_0x472888[_0x58f3('0x76')]}:_0x472888);});};