971d8ca01021b246b97a8e25d6b8369520f6fe37
[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 _0x840f=['moment','mustache','util','path','to-csv','ejs','fs-extra','lodash','jsforce','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/logger','api','../../config/utils','../../config/environment','../../mysqldb','../../components/integrations/configuration','sendStatus','status','end','json','offset','undefined','limit','count','set','apply','update','then','destroy','error','name','send','index','map','VtigerAccount','rawAttributes','fieldName','type','key','model','query','filters','intersection','attributes','fields','length','hasOwnProperty','order','sort','where','pick','filter','merge','includeAll','include','findAll','catch','show','keys','options','find','create','body','params','getConfigurations','findOne','VtigerConfiguration','nolimit','addConfiguration','AccountId','Subjects','getSubjects','channel','Descriptions','getDescriptions','VtigerField','md5','getFields','moduleName','serverUrl','accessKey','username','remoteUri','slice','substring','format','%s/webservice.php','GET','getchallenge','POST','login','result','token','success','describe','sessionName','autogenerated','reference','owner','remove','editable','false','label','startsWith','cf_','picklistValues','value','vTiger\x20error:\x20','test','statusCode','getFields,\x20%s,\x20%s,\x20%s','stringify','eml-format','rimraf','zip-dir','fast-json-patch','request-promise'];(function(_0x180bc5,_0x17fd8d){var _0x310566=function(_0x5cf9fd){while(--_0x5cf9fd){_0x180bc5['push'](_0x180bc5['shift']());}};_0x310566(++_0x17fd8d);}(_0x840f,0x76));var _0xf840=function(_0x1fc356,_0x5de529){_0x1fc356=_0x1fc356-0x0;var _0x4b5c33=_0x840f[_0x1fc356];return _0x4b5c33;};'use strict';var emlformat=require(_0xf840('0x0'));var rimraf=require(_0xf840('0x1'));var zipdir=require(_0xf840('0x2'));var jsonpatch=require(_0xf840('0x3'));var rp=require(_0xf840('0x4'));var moment=require(_0xf840('0x5'));var BPromise=require('bluebird');var Mustache=require(_0xf840('0x6'));var util=require(_0xf840('0x7'));var path=require(_0xf840('0x8'));var sox=require('sox');var csv=require(_0xf840('0x9'));var ejs=require(_0xf840('0xa'));var fs=require('fs');var fs_extra=require(_0xf840('0xb'));var _=require(_0xf840('0xc'));var squel=require('squel');var crypto=require('crypto');var jsforce=require(_0xf840('0xd'));var deskjs=require(_0xf840('0xe'));var toCsv=require(_0xf840('0x9'));var querystring=require(_0xf840('0xf'));var Papa=require(_0xf840('0x10'));var Redis=require(_0xf840('0x11'));var authService=require(_0xf840('0x12'));var qs=require(_0xf840('0x13'));var as=require(_0xf840('0x14'));var hardwareService=require('../../config/license/hardware');var logger=require(_0xf840('0x15'))(_0xf840('0x16'));var utils=require(_0xf840('0x17'));var config=require(_0xf840('0x18'));var licenseUtil=require('../../config/license/util');var db=require(_0xf840('0x19'))['db'];var integrations=require(_0xf840('0x1a'));function respondWithStatusCode(_0x431a08,_0xfedb85){_0xfedb85=_0xfedb85||0xcc;return function(_0x2911e9){if(_0x2911e9){return _0x431a08[_0xf840('0x1b')](_0xfedb85);}return _0x431a08[_0xf840('0x1c')](_0xfedb85)[_0xf840('0x1d')]();};}function respondWithResult(_0x5a0077,_0x1fed8d){_0x1fed8d=_0x1fed8d||0xc8;return function(_0x95ee2a){if(_0x95ee2a){return _0x5a0077['status'](_0x1fed8d)[_0xf840('0x1e')](_0x95ee2a);}};}function respondWithFilteredResult(_0x43cf90,_0x5a7b33){return function(_0x41af72){if(_0x41af72){var _0x3197d7=typeof _0x5a7b33[_0xf840('0x1f')]===_0xf840('0x20')&&typeof _0x5a7b33[_0xf840('0x21')]===_0xf840('0x20');var _0x46495b=_0x41af72[_0xf840('0x22')];var _0x18fa2b=_0x3197d7?0x0:_0x5a7b33[_0xf840('0x1f')];var _0x32b721=_0x3197d7?_0x41af72[_0xf840('0x22')]:_0x5a7b33[_0xf840('0x1f')]+_0x5a7b33['limit'];var _0x2ea020;if(_0x32b721>=_0x46495b){_0x32b721=_0x46495b;_0x2ea020=0xc8;}else{_0x2ea020=0xce;}_0x43cf90[_0xf840('0x1c')](_0x2ea020);return _0x43cf90[_0xf840('0x23')]('Content-Range',_0x18fa2b+'-'+_0x32b721+'/'+_0x46495b)[_0xf840('0x1e')](_0x41af72);}return null;};}function patchUpdates(_0x295701){return function(_0x5ac5ed){try{jsonpatch[_0xf840('0x24')](_0x5ac5ed,_0x295701,!![]);}catch(_0x134119){return BPromise['reject'](_0x134119);}return _0x5ac5ed['save']();};}function saveUpdates(_0x5e6bee,_0x5b31d0){return function(_0xdd3ecc){if(_0xdd3ecc){return _0xdd3ecc[_0xf840('0x25')](_0x5e6bee)[_0xf840('0x26')](function(_0x275031){return _0x275031;});}return null;};}function removeEntity(_0x4a53a2,_0x5c474d){return function(_0x50742a){if(_0x50742a){return _0x50742a[_0xf840('0x27')]()[_0xf840('0x26')](function(){_0x4a53a2[_0xf840('0x1c')](0xcc)[_0xf840('0x1d')]();});}};}function handleEntityNotFound(_0x1a5625,_0x3b9da6){return function(_0x528ae4){if(!_0x528ae4){_0x1a5625['sendStatus'](0x194);}return _0x528ae4;};}function handleError(_0x225604,_0x930f7f){_0x930f7f=_0x930f7f||0x1f4;return function(_0x323f74){logger[_0xf840('0x28')](_0x323f74['stack']);if(_0x323f74[_0xf840('0x29')]){delete _0x323f74['name'];}_0x225604['status'](_0x930f7f)[_0xf840('0x2a')](_0x323f74);};}exports[_0xf840('0x2b')]=function(_0x1edbd6,_0x5774a5){var _0x211781={},_0x59ea51={},_0x4ca38c={'count':0x0,'rows':[]};var _0x2fd0ab=_[_0xf840('0x2c')](db[_0xf840('0x2d')][_0xf840('0x2e')],function(_0x5e5192){return{'name':_0x5e5192[_0xf840('0x2f')],'type':_0x5e5192[_0xf840('0x30')][_0xf840('0x31')]};});_0x59ea51[_0xf840('0x32')]=_[_0xf840('0x2c')](_0x2fd0ab,'name');_0x59ea51[_0xf840('0x33')]=_['keys'](_0x1edbd6['query']);_0x59ea51[_0xf840('0x34')]=_[_0xf840('0x35')](_0x59ea51[_0xf840('0x32')],_0x59ea51[_0xf840('0x33')]);_0x211781[_0xf840('0x36')]=_[_0xf840('0x35')](_0x59ea51['model'],qs[_0xf840('0x37')](_0x1edbd6['query'][_0xf840('0x37')]));_0x211781['attributes']=_0x211781[_0xf840('0x36')][_0xf840('0x38')]?_0x211781[_0xf840('0x36')]:_0x59ea51['model'];if(!_0x1edbd6['query'][_0xf840('0x39')]('nolimit')){_0x211781[_0xf840('0x21')]=qs[_0xf840('0x21')](_0x1edbd6[_0xf840('0x33')][_0xf840('0x21')]);_0x211781[_0xf840('0x1f')]=qs[_0xf840('0x1f')](_0x1edbd6['query'][_0xf840('0x1f')]);}_0x211781[_0xf840('0x3a')]=qs[_0xf840('0x3b')](_0x1edbd6['query'][_0xf840('0x3b')]);_0x211781[_0xf840('0x3c')]=qs[_0xf840('0x34')](_[_0xf840('0x3d')](_0x1edbd6[_0xf840('0x33')],_0x59ea51['filters']),_0x2fd0ab);if(_0x1edbd6[_0xf840('0x33')][_0xf840('0x3e')]){_0x211781[_0xf840('0x3c')]=_[_0xf840('0x3f')](_0x211781[_0xf840('0x3c')],{'$or':_[_0xf840('0x2c')](_0x2fd0ab,function(_0x153be9){if(_0x153be9[_0xf840('0x30')]!=='VIRTUAL'){var _0x2625b7={};_0x2625b7[_0x153be9['name']]={'$like':'%'+_0x1edbd6['query'][_0xf840('0x3e')]+'%'};return _0x2625b7;}})});}_0x211781=_[_0xf840('0x3f')]({},_0x211781,_0x1edbd6['options']);var _0x4f52a3={'where':_0x211781[_0xf840('0x3c')]};return db['VtigerAccount'][_0xf840('0x22')](_0x4f52a3)[_0xf840('0x26')](function(_0x50dd92){_0x4ca38c[_0xf840('0x22')]=_0x50dd92;if(_0x1edbd6[_0xf840('0x33')][_0xf840('0x40')]){_0x211781[_0xf840('0x41')]=[{'all':!![]}];}return db[_0xf840('0x2d')][_0xf840('0x42')](_0x211781);})[_0xf840('0x26')](function(_0x34bea0){_0x4ca38c['rows']=_0x34bea0;return _0x4ca38c;})[_0xf840('0x26')](respondWithFilteredResult(_0x5774a5,_0x211781))[_0xf840('0x43')](handleError(_0x5774a5,null));};exports[_0xf840('0x44')]=function(_0x47c3ff,_0x21e363){var _0x42f152={'raw':![],'where':{'id':_0x47c3ff['params']['id']}},_0x4b2f78={};_0x4b2f78[_0xf840('0x32')]=_[_0xf840('0x45')](db['VtigerAccount'][_0xf840('0x2e')]);_0x4b2f78[_0xf840('0x33')]=_[_0xf840('0x45')](_0x47c3ff[_0xf840('0x33')]);_0x4b2f78[_0xf840('0x34')]=_[_0xf840('0x35')](_0x4b2f78['model'],_0x4b2f78['query']);_0x42f152['attributes']=_[_0xf840('0x35')](_0x4b2f78[_0xf840('0x32')],qs[_0xf840('0x37')](_0x47c3ff[_0xf840('0x33')]['fields']));_0x42f152['attributes']=_0x42f152[_0xf840('0x36')][_0xf840('0x38')]?_0x42f152[_0xf840('0x36')]:_0x4b2f78[_0xf840('0x32')];if(_0x47c3ff[_0xf840('0x33')][_0xf840('0x40')]){_0x42f152[_0xf840('0x41')]=[{'all':!![]}];}_0x42f152=_[_0xf840('0x3f')]({},_0x42f152,_0x47c3ff[_0xf840('0x46')]);return db[_0xf840('0x2d')][_0xf840('0x47')](_0x42f152)['then'](handleEntityNotFound(_0x21e363,null))[_0xf840('0x26')](respondWithResult(_0x21e363,null))['catch'](handleError(_0x21e363,null));};exports[_0xf840('0x48')]=function(_0x2d5a68,_0x25d1ee){return db['VtigerAccount'][_0xf840('0x48')](_0x2d5a68[_0xf840('0x49')],{})['then'](respondWithResult(_0x25d1ee,0xc9))[_0xf840('0x43')](handleError(_0x25d1ee,null));};exports['update']=function(_0x4cefc1,_0x56f26c){if(_0x4cefc1[_0xf840('0x49')]['id']){delete _0x4cefc1[_0xf840('0x49')]['id'];}return db[_0xf840('0x2d')]['find']({'where':{'id':_0x4cefc1[_0xf840('0x4a')]['id']}})[_0xf840('0x26')](handleEntityNotFound(_0x56f26c,null))[_0xf840('0x26')](saveUpdates(_0x4cefc1[_0xf840('0x49')],null))[_0xf840('0x26')](respondWithResult(_0x56f26c,null))[_0xf840('0x43')](handleError(_0x56f26c,null));};exports['destroy']=function(_0x1aa637,_0x1402b3){return db[_0xf840('0x2d')][_0xf840('0x47')]({'where':{'id':_0x1aa637['params']['id']}})[_0xf840('0x26')](handleEntityNotFound(_0x1402b3,null))['then'](removeEntity(_0x1402b3,null))['catch'](handleError(_0x1402b3,null));};exports[_0xf840('0x4b')]=function(_0x4ded9d,_0x367962,_0x54c7f0){var _0x8b9cc6={};var _0x5e2bdb={};var _0x341d9;var _0x279cc7;return db[_0xf840('0x2d')][_0xf840('0x4c')]({'where':{'id':_0x4ded9d[_0xf840('0x4a')]['id']}})[_0xf840('0x26')](handleEntityNotFound(_0x367962,null))['then'](function(_0x442d42){if(_0x442d42){_0x341d9=_0x442d42;_0x5e2bdb[_0xf840('0x32')]=_[_0xf840('0x45')](db[_0xf840('0x4d')]['rawAttributes']);_0x5e2bdb[_0xf840('0x33')]=_[_0xf840('0x45')](_0x4ded9d[_0xf840('0x33')]);_0x5e2bdb['filters']=_[_0xf840('0x35')](_0x5e2bdb[_0xf840('0x32')],_0x5e2bdb[_0xf840('0x33')]);_0x8b9cc6[_0xf840('0x36')]=_[_0xf840('0x35')](_0x5e2bdb['model'],qs[_0xf840('0x37')](_0x4ded9d[_0xf840('0x33')][_0xf840('0x37')]));_0x8b9cc6[_0xf840('0x36')]=_0x8b9cc6[_0xf840('0x36')][_0xf840('0x38')]?_0x8b9cc6['attributes']:_0x5e2bdb[_0xf840('0x32')];_0x8b9cc6[_0xf840('0x3a')]=qs[_0xf840('0x3b')](_0x4ded9d[_0xf840('0x33')][_0xf840('0x3b')]);_0x8b9cc6[_0xf840('0x3c')]=qs[_0xf840('0x34')](_['pick'](_0x4ded9d[_0xf840('0x33')],_0x5e2bdb[_0xf840('0x34')]));if(_0x4ded9d[_0xf840('0x33')]['filter']){_0x8b9cc6[_0xf840('0x3c')]=_[_0xf840('0x3f')](_0x8b9cc6[_0xf840('0x3c')],{'$or':_[_0xf840('0x2c')](_0x8b9cc6[_0xf840('0x36')],function(_0x107b79){var _0x5bcfac={};_0x5bcfac[_0x107b79]={'$like':'%'+_0x4ded9d[_0xf840('0x33')][_0xf840('0x3e')]+'%'};return _0x5bcfac;})});}_0x8b9cc6=_['merge']({},_0x8b9cc6,_0x4ded9d['options']);return _0x341d9[_0xf840('0x4b')](_0x8b9cc6);}})[_0xf840('0x26')](function(_0x24e69d){if(_0x24e69d){_0x279cc7=_0x24e69d[_0xf840('0x38')];if(!_0x4ded9d[_0xf840('0x33')][_0xf840('0x39')](_0xf840('0x4e'))){_0x8b9cc6[_0xf840('0x21')]=qs[_0xf840('0x21')](_0x4ded9d[_0xf840('0x33')]['limit']);_0x8b9cc6[_0xf840('0x1f')]=qs[_0xf840('0x1f')](_0x4ded9d[_0xf840('0x33')][_0xf840('0x1f')]);}return _0x341d9['getConfigurations'](_0x8b9cc6);}})['then'](function(_0x28dead){if(_0x28dead){return _0x28dead?{'count':_0x279cc7,'rows':_0x28dead}:null;}})[_0xf840('0x26')](respondWithResult(_0x367962,null))['catch'](handleError(_0x367962,null));};exports[_0xf840('0x4f')]=function(_0x46d224,_0x5b6e1a,_0x39f213){if(_0x46d224[_0xf840('0x49')]['id']){delete _0x46d224['body']['id'];}return db[_0xf840('0x2d')]['findOne']({'where':{'id':_0x46d224[_0xf840('0x4a')]['id']}})[_0xf840('0x26')](handleEntityNotFound(_0x5b6e1a,null))['then'](function(_0x9c184a){if(_0x9c184a){_0x46d224[_0xf840('0x49')][_0xf840('0x50')]=_0x9c184a['id'];_0x46d224[_0xf840('0x49')][_0xf840('0x51')]=integrations[_0xf840('0x52')](_0x46d224['body'][_0xf840('0x53')],_0x46d224[_0xf840('0x49')]['type']);_0x46d224[_0xf840('0x49')][_0xf840('0x54')]=integrations[_0xf840('0x55')](_0x46d224[_0xf840('0x49')][_0xf840('0x53')],_0x46d224[_0xf840('0x49')][_0xf840('0x30')]);return db[_0xf840('0x4d')][_0xf840('0x48')](_0x46d224[_0xf840('0x49')],{'include':[{'model':db[_0xf840('0x56')],'as':_0xf840('0x51')},{'model':db['VtigerField'],'as':_0xf840('0x54')}]});}return null;})['then'](respondWithResult(_0x5b6e1a,null))[_0xf840('0x43')](handleError(_0x5b6e1a,null));};var md5=require(_0xf840('0x57'));exports[_0xf840('0x58')]=function(_0x36e834,_0x3adc8c,_0x27fd90){var _0x2f3f09=null;var _0x5202c8=null;var _0x30e68d=null;var _0x7ba9d5=null;var _0x43fc35='';return db[_0xf840('0x2d')][_0xf840('0x4c')]({'where':{'id':_0x36e834[_0xf840('0x4a')]['id']},'attributes':['id',_0xf840('0x29'),'username','description',_0xf840('0x59'),'remoteUri',_0xf840('0x5a'),_0xf840('0x5b')]})[_0xf840('0x26')](handleEntityNotFound(_0x3adc8c,null))[_0xf840('0x26')](function(_0x2ddac4){if(_0x2ddac4){_0x2f3f09=_0x2ddac4[_0xf840('0x5c')];_0x5202c8=_0x2ddac4[_0xf840('0x5b')];_0x30e68d=_0x2ddac4[_0xf840('0x59')];_0x43fc35=_0x2ddac4[_0xf840('0x5d')];var _0x511e2a=_0x43fc35[_0xf840('0x5e')](-0x1);if(_0x511e2a==='/'){_0x43fc35=_0x43fc35[_0xf840('0x5f')](0x0,_0x43fc35['lastIndexOf'](_0x511e2a));}_0x7ba9d5=util[_0xf840('0x60')](_0xf840('0x61'),_0x43fc35);var _0x53fe71={'method':_0xf840('0x62'),'uri':_0x7ba9d5,'qs':{'operation':_0xf840('0x63'),'username':_0x2ddac4[_0xf840('0x5c')]},'json':!![]};return rp(_0x53fe71);}})['then'](function(_0x59f164){if(_0x59f164['success']){var _0x6dd285={'method':_0xf840('0x64'),'uri':_0x7ba9d5,'form':{'operation':_0xf840('0x65'),'username':_0x2f3f09,'accessKey':md5(_0x59f164[_0xf840('0x66')][_0xf840('0x67')]+_0x5202c8)},'json':!![]};return rp(_0x6dd285);}})[_0xf840('0x26')](function(_0x2a4e4e){if(_0x2a4e4e[_0xf840('0x68')]){var _0x1dfd56={'method':_0xf840('0x62'),'uri':_0x7ba9d5,'qs':{'operation':_0xf840('0x69'),'username':_0x2f3f09,'sessionName':_0x2a4e4e[_0xf840('0x66')][_0xf840('0x6a')],'elementType':_0x30e68d},'json':!![]};return rp(_0x1dfd56);}})[_0xf840('0x26')](function(_0x2333c7){if(_0x2333c7['success']){var _0x171f30=[_0xf840('0x6b'),_0xf840('0x6c'),_0xf840('0x6d')];_[_0xf840('0x6e')](_0x2333c7[_0xf840('0x66')]['fields'],function(_0x2ea818){return _0x171f30['includes'](_0x2ea818[_0xf840('0x30')][_0xf840('0x29')]);});_[_0xf840('0x6e')](_0x2333c7[_0xf840('0x66')][_0xf840('0x37')],function(_0x4dcf94){return _0x4dcf94[_0xf840('0x6f')]===_0xf840('0x70');});return{'count':_0x2333c7[_0xf840('0x66')]['fields'][_0xf840('0x38')],'rows':_[_0xf840('0x2c')](_0x2333c7['result'][_0xf840('0x37')],function(_0x55b1c7){return{'id':_0x55b1c7['name'],'name':_0x55b1c7[_0xf840('0x71')],'custom':_[_0xf840('0x72')](_0x55b1c7[_0xf840('0x29')],_0xf840('0x73'))?!![]:![],'options':_0x55b1c7[_0xf840('0x30')][_0xf840('0x29')]==='picklist'&&_0x55b1c7[_0xf840('0x30')][_0xf840('0x74')][_0xf840('0x38')]>0x0?_[_0xf840('0x2c')](_0x55b1c7[_0xf840('0x30')][_0xf840('0x74')],function(_0x52111c){return{'name':_0x52111c[_0xf840('0x71')],'value':_0x52111c[_0xf840('0x75')]};}):[]};})};}})[_0xf840('0x26')](respondWithResult(_0x3adc8c,null))[_0xf840('0x43')](function(_0x10db99){console['log'](_0xf840('0x76')+_0x10db99);var _0x45be00=_0x36e834[_0xf840('0x33')][_0xf840('0x77')]?0x1f4:_0x10db99[_0xf840('0x78')]||0x1f4;logger[_0xf840('0x28')](_0xf840('0x79'),'getFields',_0x45be00,JSON[_0xf840('0x7a')](_0x10db99));delete _0x10db99[_0xf840('0x29')];if(_0x45be00===0x191){_0x45be00=0x190;}_0x3adc8c['status'](_0x45be00)[_0xf840('0x2a')](_0x36e834[_0xf840('0x33')][_0xf840('0x77')]?{'message':'Wrong\x20credentials','statusCode':_0x10db99[_0xf840('0x78')]}:_0x10db99);});};