217866aa0c19d8c1f6ed9ebba3597723477c5d63
[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 _0x80f9=['desk.js','papaparse','ioredis','../../components/auth/service','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../components/integrations/configuration','sendStatus','status','json','offset','undefined','limit','count','set','Content-Range','apply','reject','save','update','then','end','stack','name','send','index','map','VtigerAccount','rawAttributes','type','key','model','query','filters','attributes','intersection','fields','hasOwnProperty','order','sort','where','pick','filter','VIRTUAL','merge','options','includeAll','include','catch','params','keys','length','find','create','body','getConfigurations','findOne','VtigerConfiguration','nolimit','addConfiguration','AccountId','Subjects','getSubjects','Descriptions','getDescriptions','channel','VtigerField','username','moduleName','remoteUri','accessKey','%s/webservice.php','GET','getchallenge','success','POST','login','result','token','sessionName','reference','owner','remove','false','label','startsWith','cf_','picklistValues','value','log','vTiger\x20error:\x20','test','error','getFields,\x20%s,\x20%s,\x20%s','getFields','stringify','statusCode','eml-format','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','to-csv','ejs','squel','jsforce'];(function(_0x3b380d,_0x33cbdf){var _0x381379=function(_0x291118){while(--_0x291118){_0x3b380d['push'](_0x3b380d['shift']());}};_0x381379(++_0x33cbdf);}(_0x80f9,0x65));var _0x980f=function(_0x5349df,_0x2c9cfe){_0x5349df=_0x5349df-0x0;var _0x447249=_0x80f9[_0x5349df];return _0x447249;};'use strict';var emlformat=require(_0x980f('0x0'));var rimraf=require('rimraf');var zipdir=require(_0x980f('0x1'));var jsonpatch=require(_0x980f('0x2'));var rp=require(_0x980f('0x3'));var moment=require(_0x980f('0x4'));var BPromise=require(_0x980f('0x5'));var Mustache=require(_0x980f('0x6'));var util=require(_0x980f('0x7'));var path=require(_0x980f('0x8'));var sox=require('sox');var csv=require(_0x980f('0x9'));var ejs=require(_0x980f('0xa'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require('lodash');var squel=require(_0x980f('0xb'));var crypto=require('crypto');var jsforce=require(_0x980f('0xc'));var deskjs=require(_0x980f('0xd'));var toCsv=require('to-csv');var querystring=require('querystring');var Papa=require(_0x980f('0xe'));var Redis=require(_0x980f('0xf'));var authService=require(_0x980f('0x10'));var qs=require('../../components/parsers/qs');var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0x980f('0x11'));var logger=require(_0x980f('0x12'))(_0x980f('0x13'));var utils=require(_0x980f('0x14'));var config=require(_0x980f('0x15'));var licenseUtil=require(_0x980f('0x16'));var db=require('../../mysqldb')['db'];var integrations=require(_0x980f('0x17'));function respondWithStatusCode(_0x3fe209,_0x4929ee){_0x4929ee=_0x4929ee||0xcc;return function(_0x33c277){if(_0x33c277){return _0x3fe209[_0x980f('0x18')](_0x4929ee);}return _0x3fe209[_0x980f('0x19')](_0x4929ee)['end']();};}function respondWithResult(_0x4c613c,_0x4fd14b){_0x4fd14b=_0x4fd14b||0xc8;return function(_0x3abbf9){if(_0x3abbf9){return _0x4c613c['status'](_0x4fd14b)[_0x980f('0x1a')](_0x3abbf9);}};}function respondWithFilteredResult(_0x4234de,_0x5c7b5c){return function(_0x227f16){if(_0x227f16){var _0xa253c1=typeof _0x5c7b5c[_0x980f('0x1b')]===_0x980f('0x1c')&&typeof _0x5c7b5c[_0x980f('0x1d')]==='undefined';var _0x229c79=_0x227f16[_0x980f('0x1e')];var _0x3bbe84=_0xa253c1?0x0:_0x5c7b5c[_0x980f('0x1b')];var _0x18b8b9=_0xa253c1?_0x227f16[_0x980f('0x1e')]:_0x5c7b5c['offset']+_0x5c7b5c['limit'];var _0x1729f8;if(_0x18b8b9>=_0x229c79){_0x18b8b9=_0x229c79;_0x1729f8=0xc8;}else{_0x1729f8=0xce;}_0x4234de['status'](_0x1729f8);return _0x4234de[_0x980f('0x1f')](_0x980f('0x20'),_0x3bbe84+'-'+_0x18b8b9+'/'+_0x229c79)['json'](_0x227f16);}return null;};}function patchUpdates(_0x1d857d){return function(_0x14df2d){try{jsonpatch[_0x980f('0x21')](_0x14df2d,_0x1d857d,!![]);}catch(_0x4cf5b6){return BPromise[_0x980f('0x22')](_0x4cf5b6);}return _0x14df2d[_0x980f('0x23')]();};}function saveUpdates(_0x42c4fe,_0x4d36b0){return function(_0x16b5cb){if(_0x16b5cb){return _0x16b5cb[_0x980f('0x24')](_0x42c4fe)['then'](function(_0x535b0c){return _0x535b0c;});}return null;};}function removeEntity(_0xfd8758,_0x13622e){return function(_0x53493d){if(_0x53493d){return _0x53493d['destroy']()[_0x980f('0x25')](function(){_0xfd8758['status'](0xcc)[_0x980f('0x26')]();});}};}function handleEntityNotFound(_0x169690,_0xa317b8){return function(_0xe6bcf5){if(!_0xe6bcf5){_0x169690[_0x980f('0x18')](0x194);}return _0xe6bcf5;};}function handleError(_0x4914a1,_0x4e0bbd){_0x4e0bbd=_0x4e0bbd||0x1f4;return function(_0x11544c){logger['error'](_0x11544c[_0x980f('0x27')]);if(_0x11544c[_0x980f('0x28')]){delete _0x11544c[_0x980f('0x28')];}_0x4914a1['status'](_0x4e0bbd)[_0x980f('0x29')](_0x11544c);};}exports[_0x980f('0x2a')]=function(_0x2db034,_0x1e32af){var _0x4cf7d2={},_0x47e296={},_0x506a6f={'count':0x0,'rows':[]};var _0xc8293f=_[_0x980f('0x2b')](db[_0x980f('0x2c')][_0x980f('0x2d')],function(_0x483f91){return{'name':_0x483f91['fieldName'],'type':_0x483f91[_0x980f('0x2e')][_0x980f('0x2f')]};});_0x47e296[_0x980f('0x30')]=_[_0x980f('0x2b')](_0xc8293f,_0x980f('0x28'));_0x47e296[_0x980f('0x31')]=_['keys'](_0x2db034[_0x980f('0x31')]);_0x47e296[_0x980f('0x32')]=_['intersection'](_0x47e296[_0x980f('0x30')],_0x47e296[_0x980f('0x31')]);_0x4cf7d2[_0x980f('0x33')]=_[_0x980f('0x34')](_0x47e296['model'],qs['fields'](_0x2db034['query'][_0x980f('0x35')]));_0x4cf7d2[_0x980f('0x33')]=_0x4cf7d2[_0x980f('0x33')]['length']?_0x4cf7d2['attributes']:_0x47e296[_0x980f('0x30')];if(!_0x2db034['query'][_0x980f('0x36')]('nolimit')){_0x4cf7d2[_0x980f('0x1d')]=qs[_0x980f('0x1d')](_0x2db034[_0x980f('0x31')]['limit']);_0x4cf7d2[_0x980f('0x1b')]=qs[_0x980f('0x1b')](_0x2db034[_0x980f('0x31')]['offset']);}_0x4cf7d2[_0x980f('0x37')]=qs[_0x980f('0x38')](_0x2db034['query'][_0x980f('0x38')]);_0x4cf7d2[_0x980f('0x39')]=qs['filters'](_[_0x980f('0x3a')](_0x2db034[_0x980f('0x31')],_0x47e296[_0x980f('0x32')]),_0xc8293f);if(_0x2db034[_0x980f('0x31')][_0x980f('0x3b')]){_0x4cf7d2[_0x980f('0x39')]=_['merge'](_0x4cf7d2[_0x980f('0x39')],{'$or':_[_0x980f('0x2b')](_0xc8293f,function(_0xc37829){if(_0xc37829[_0x980f('0x2e')]!==_0x980f('0x3c')){var _0x3bf305={};_0x3bf305[_0xc37829[_0x980f('0x28')]]={'$like':'%'+_0x2db034['query']['filter']+'%'};return _0x3bf305;}})});}_0x4cf7d2=_[_0x980f('0x3d')]({},_0x4cf7d2,_0x2db034[_0x980f('0x3e')]);var _0x1b5f98={'where':_0x4cf7d2[_0x980f('0x39')]};return db[_0x980f('0x2c')]['count'](_0x1b5f98)[_0x980f('0x25')](function(_0x4fd31b){_0x506a6f[_0x980f('0x1e')]=_0x4fd31b;if(_0x2db034[_0x980f('0x31')][_0x980f('0x3f')]){_0x4cf7d2[_0x980f('0x40')]=[{'all':!![]}];}return db[_0x980f('0x2c')]['findAll'](_0x4cf7d2);})[_0x980f('0x25')](function(_0x18fdf7){_0x506a6f['rows']=_0x18fdf7;return _0x506a6f;})[_0x980f('0x25')](respondWithFilteredResult(_0x1e32af,_0x4cf7d2))[_0x980f('0x41')](handleError(_0x1e32af,null));};exports['show']=function(_0x4853bf,_0x373ca4){var _0x1b4c13={'raw':![],'where':{'id':_0x4853bf[_0x980f('0x42')]['id']}},_0x3bc484={};_0x3bc484[_0x980f('0x30')]=_[_0x980f('0x43')](db[_0x980f('0x2c')][_0x980f('0x2d')]);_0x3bc484[_0x980f('0x31')]=_[_0x980f('0x43')](_0x4853bf[_0x980f('0x31')]);_0x3bc484['filters']=_['intersection'](_0x3bc484[_0x980f('0x30')],_0x3bc484['query']);_0x1b4c13[_0x980f('0x33')]=_['intersection'](_0x3bc484[_0x980f('0x30')],qs[_0x980f('0x35')](_0x4853bf['query'][_0x980f('0x35')]));_0x1b4c13[_0x980f('0x33')]=_0x1b4c13[_0x980f('0x33')][_0x980f('0x44')]?_0x1b4c13[_0x980f('0x33')]:_0x3bc484[_0x980f('0x30')];if(_0x4853bf['query'][_0x980f('0x3f')]){_0x1b4c13['include']=[{'all':!![]}];}_0x1b4c13=_[_0x980f('0x3d')]({},_0x1b4c13,_0x4853bf[_0x980f('0x3e')]);return db[_0x980f('0x2c')][_0x980f('0x45')](_0x1b4c13)[_0x980f('0x25')](handleEntityNotFound(_0x373ca4,null))[_0x980f('0x25')](respondWithResult(_0x373ca4,null))[_0x980f('0x41')](handleError(_0x373ca4,null));};exports['create']=function(_0x5952f3,_0x463a78){return db[_0x980f('0x2c')][_0x980f('0x46')](_0x5952f3[_0x980f('0x47')],{})[_0x980f('0x25')](respondWithResult(_0x463a78,0xc9))[_0x980f('0x41')](handleError(_0x463a78,null));};exports[_0x980f('0x24')]=function(_0x4a21c3,_0x143c9b){if(_0x4a21c3[_0x980f('0x47')]['id']){delete _0x4a21c3[_0x980f('0x47')]['id'];}return db[_0x980f('0x2c')][_0x980f('0x45')]({'where':{'id':_0x4a21c3[_0x980f('0x42')]['id']}})[_0x980f('0x25')](handleEntityNotFound(_0x143c9b,null))[_0x980f('0x25')](saveUpdates(_0x4a21c3[_0x980f('0x47')],null))[_0x980f('0x25')](respondWithResult(_0x143c9b,null))[_0x980f('0x41')](handleError(_0x143c9b,null));};exports['destroy']=function(_0x428053,_0x34b098){return db[_0x980f('0x2c')][_0x980f('0x45')]({'where':{'id':_0x428053[_0x980f('0x42')]['id']}})[_0x980f('0x25')](handleEntityNotFound(_0x34b098,null))['then'](removeEntity(_0x34b098,null))['catch'](handleError(_0x34b098,null));};exports[_0x980f('0x48')]=function(_0x4f33cf,_0x15f012,_0x2a7b24){var _0x5549f4={};var _0x31ca5e={};var _0x3c5395;var _0x1bd65b;return db[_0x980f('0x2c')][_0x980f('0x49')]({'where':{'id':_0x4f33cf['params']['id']}})[_0x980f('0x25')](handleEntityNotFound(_0x15f012,null))[_0x980f('0x25')](function(_0x192c97){if(_0x192c97){_0x3c5395=_0x192c97;_0x31ca5e['model']=_[_0x980f('0x43')](db[_0x980f('0x4a')][_0x980f('0x2d')]);_0x31ca5e[_0x980f('0x31')]=_['keys'](_0x4f33cf[_0x980f('0x31')]);_0x31ca5e[_0x980f('0x32')]=_['intersection'](_0x31ca5e['model'],_0x31ca5e[_0x980f('0x31')]);_0x5549f4[_0x980f('0x33')]=_['intersection'](_0x31ca5e[_0x980f('0x30')],qs['fields'](_0x4f33cf['query']['fields']));_0x5549f4['attributes']=_0x5549f4[_0x980f('0x33')][_0x980f('0x44')]?_0x5549f4['attributes']:_0x31ca5e[_0x980f('0x30')];_0x5549f4['order']=qs[_0x980f('0x38')](_0x4f33cf[_0x980f('0x31')][_0x980f('0x38')]);_0x5549f4[_0x980f('0x39')]=qs[_0x980f('0x32')](_[_0x980f('0x3a')](_0x4f33cf[_0x980f('0x31')],_0x31ca5e[_0x980f('0x32')]));if(_0x4f33cf[_0x980f('0x31')][_0x980f('0x3b')]){_0x5549f4[_0x980f('0x39')]=_[_0x980f('0x3d')](_0x5549f4[_0x980f('0x39')],{'$or':_[_0x980f('0x2b')](_0x5549f4[_0x980f('0x33')],function(_0x56c1d7){var _0x28aeb9={};_0x28aeb9[_0x56c1d7]={'$like':'%'+_0x4f33cf[_0x980f('0x31')][_0x980f('0x3b')]+'%'};return _0x28aeb9;})});}_0x5549f4=_[_0x980f('0x3d')]({},_0x5549f4,_0x4f33cf['options']);return _0x3c5395[_0x980f('0x48')](_0x5549f4);}})[_0x980f('0x25')](function(_0x558625){if(_0x558625){_0x1bd65b=_0x558625[_0x980f('0x44')];if(!_0x4f33cf[_0x980f('0x31')][_0x980f('0x36')](_0x980f('0x4b'))){_0x5549f4[_0x980f('0x1d')]=qs[_0x980f('0x1d')](_0x4f33cf[_0x980f('0x31')][_0x980f('0x1d')]);_0x5549f4[_0x980f('0x1b')]=qs[_0x980f('0x1b')](_0x4f33cf['query'][_0x980f('0x1b')]);}return _0x3c5395[_0x980f('0x48')](_0x5549f4);}})[_0x980f('0x25')](function(_0x58b44c){if(_0x58b44c){return _0x58b44c?{'count':_0x1bd65b,'rows':_0x58b44c}:null;}})[_0x980f('0x25')](respondWithResult(_0x15f012,null))[_0x980f('0x41')](handleError(_0x15f012,null));};exports[_0x980f('0x4c')]=function(_0xc53fe2,_0x1c93df,_0x5a1936){if(_0xc53fe2[_0x980f('0x47')]['id']){delete _0xc53fe2[_0x980f('0x47')]['id'];}return db[_0x980f('0x2c')]['findOne']({'where':{'id':_0xc53fe2[_0x980f('0x42')]['id']}})[_0x980f('0x25')](handleEntityNotFound(_0x1c93df,null))['then'](function(_0x55249f){if(_0x55249f){_0xc53fe2[_0x980f('0x47')][_0x980f('0x4d')]=_0x55249f['id'];_0xc53fe2['body'][_0x980f('0x4e')]=integrations[_0x980f('0x4f')](_0xc53fe2[_0x980f('0x47')]['channel'],_0xc53fe2['body'][_0x980f('0x2e')]);_0xc53fe2[_0x980f('0x47')][_0x980f('0x50')]=integrations[_0x980f('0x51')](_0xc53fe2[_0x980f('0x47')][_0x980f('0x52')],_0xc53fe2['body'][_0x980f('0x2e')]);return db[_0x980f('0x4a')][_0x980f('0x46')](_0xc53fe2[_0x980f('0x47')],{'include':[{'model':db['VtigerField'],'as':_0x980f('0x4e')},{'model':db[_0x980f('0x53')],'as':'Descriptions'}]});}return null;})[_0x980f('0x25')](respondWithResult(_0x1c93df,null))[_0x980f('0x41')](handleError(_0x1c93df,null));};var md5=require('md5');exports['getFields']=function(_0x3ff97b,_0x5d38e6,_0x2390a4){var _0x4082c3=null;var _0x29d7a4=null;var _0xf834a9=null;var _0x53c35f=null;var _0x1cbc6b='';return db[_0x980f('0x2c')][_0x980f('0x49')]({'where':{'id':_0x3ff97b[_0x980f('0x42')]['id']},'attributes':['id',_0x980f('0x28'),_0x980f('0x54'),'description',_0x980f('0x55'),_0x980f('0x56'),'serverUrl',_0x980f('0x57')]})[_0x980f('0x25')](handleEntityNotFound(_0x5d38e6,null))['then'](function(_0x106608){if(_0x106608){_0x4082c3=_0x106608['username'];_0x29d7a4=_0x106608[_0x980f('0x57')];_0xf834a9=_0x106608['moduleName'];_0x1cbc6b=_0x106608[_0x980f('0x56')];var _0x38c0a7=_0x1cbc6b['slice'](-0x1);if(_0x38c0a7==='/'){_0x1cbc6b=_0x1cbc6b['substring'](0x0,_0x1cbc6b['lastIndexOf'](_0x38c0a7));}_0x53c35f=util['format'](_0x980f('0x58'),_0x1cbc6b);var _0x15f4a3={'method':_0x980f('0x59'),'uri':_0x53c35f,'qs':{'operation':_0x980f('0x5a'),'username':_0x106608['username']},'json':!![]};return rp(_0x15f4a3);}})[_0x980f('0x25')](function(_0x4cef41){if(_0x4cef41[_0x980f('0x5b')]){var _0x3e8487={'method':_0x980f('0x5c'),'uri':_0x53c35f,'form':{'operation':_0x980f('0x5d'),'username':_0x4082c3,'accessKey':md5(_0x4cef41[_0x980f('0x5e')][_0x980f('0x5f')]+_0x29d7a4)},'json':!![]};return rp(_0x3e8487);}})['then'](function(_0x378509){if(_0x378509[_0x980f('0x5b')]){var _0x40178c={'method':_0x980f('0x59'),'uri':_0x53c35f,'qs':{'operation':'describe','username':_0x4082c3,'sessionName':_0x378509[_0x980f('0x5e')][_0x980f('0x60')],'elementType':_0xf834a9},'json':!![]};return rp(_0x40178c);}})[_0x980f('0x25')](function(_0x4a9441){if(_0x4a9441[_0x980f('0x5b')]){var _0x11019c=['autogenerated',_0x980f('0x61'),_0x980f('0x62')];_['remove'](_0x4a9441[_0x980f('0x5e')][_0x980f('0x35')],function(_0x56fe5a){return _0x11019c['includes'](_0x56fe5a[_0x980f('0x2e')]['name']);});_[_0x980f('0x63')](_0x4a9441[_0x980f('0x5e')][_0x980f('0x35')],function(_0x4f0f4c){return _0x4f0f4c['editable']===_0x980f('0x64');});return{'count':_0x4a9441['result'][_0x980f('0x35')][_0x980f('0x44')],'rows':_[_0x980f('0x2b')](_0x4a9441[_0x980f('0x5e')][_0x980f('0x35')],function(_0x541547){return{'id':_0x541547['name'],'name':_0x541547[_0x980f('0x65')],'custom':_[_0x980f('0x66')](_0x541547[_0x980f('0x28')],_0x980f('0x67'))?!![]:![],'options':_0x541547[_0x980f('0x2e')][_0x980f('0x28')]==='picklist'&&_0x541547[_0x980f('0x2e')][_0x980f('0x68')]['length']>0x0?_['map'](_0x541547[_0x980f('0x2e')]['picklistValues'],function(_0x2dfea9){return{'name':_0x2dfea9[_0x980f('0x65')],'value':_0x2dfea9[_0x980f('0x69')]};}):[]};})};}})[_0x980f('0x25')](respondWithResult(_0x5d38e6,null))[_0x980f('0x41')](function(_0x44797b){console[_0x980f('0x6a')](_0x980f('0x6b')+_0x44797b);var _0x28fa5f=_0x3ff97b['query'][_0x980f('0x6c')]?0x1f4:_0x44797b['statusCode']||0x1f4;logger[_0x980f('0x6d')](_0x980f('0x6e'),_0x980f('0x6f'),_0x28fa5f,JSON[_0x980f('0x70')](_0x44797b));delete _0x44797b[_0x980f('0x28')];if(_0x28fa5f===0x191){_0x28fa5f=0x190;}_0x5d38e6['status'](_0x28fa5f)[_0x980f('0x29')](_0x3ff97b[_0x980f('0x31')][_0x980f('0x6c')]?{'message':'Wrong\x20credentials','statusCode':_0x44797b[_0x980f('0x71')]}:_0x44797b);});};