Built motion from commit 9d0783e6.|2.6.13
[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 _0x836f=['findAll','rows','catch','show','params','options','find','create','body','update','getConfigurations','VtigerConfiguration','map','addConfiguration','findOne','AccountId','Subjects','getSubjects','channel','Descriptions','getDescriptions','VtigerField','md5','getFields','username','moduleName','remoteUri','serverUrl','accessKey','substring','format','%s/webservice.php','GET','getchallenge','success','POST','login','token','describe','result','autogenerated','reference','owner','remove','includes','editable','false','cf_','picklistValues','label','value','log','test','statusCode','getFields,\x20%s,\x20%s,\x20%s','stringify','Wrong\x20credentials','rimraf','zip-dir','fast-json-patch','bluebird','mustache','path','sox','to-csv','ejs','lodash','squel','jsforce','desk.js','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','../../components/integrations/configuration','sendStatus','status','end','json','offset','undefined','limit','count','Content-Range','apply','reject','save','then','destroy','error','stack','name','send','index','VtigerAccount','rawAttributes','fieldName','type','key','query','keys','filters','intersection','model','attributes','fields','length','hasOwnProperty','nolimit','order','sort','pick','filter','where','merge','includeAll'];(function(_0x2b47a0,_0x21c4aa){var _0x54e5f5=function(_0x4fc1f9){while(--_0x4fc1f9){_0x2b47a0['push'](_0x2b47a0['shift']());}};_0x54e5f5(++_0x21c4aa);}(_0x836f,0x12f));var _0xf836=function(_0x37f9c5,_0x277779){_0x37f9c5=_0x37f9c5-0x0;var _0x3e423b=_0x836f[_0x37f9c5];return _0x3e423b;};'use strict';var emlformat=require('eml-format');var rimraf=require(_0xf836('0x0'));var zipdir=require(_0xf836('0x1'));var jsonpatch=require(_0xf836('0x2'));var rp=require('request-promise');var moment=require('moment');var BPromise=require(_0xf836('0x3'));var Mustache=require(_0xf836('0x4'));var util=require('util');var path=require(_0xf836('0x5'));var sox=require(_0xf836('0x6'));var csv=require(_0xf836('0x7'));var ejs=require(_0xf836('0x8'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0xf836('0x9'));var squel=require(_0xf836('0xa'));var crypto=require('crypto');var jsforce=require(_0xf836('0xb'));var deskjs=require(_0xf836('0xc'));var toCsv=require(_0xf836('0x7'));var querystring=require('querystring');var Papa=require(_0xf836('0xd'));var Redis=require(_0xf836('0xe'));var authService=require(_0xf836('0xf'));var qs=require(_0xf836('0x10'));var as=require(_0xf836('0x11'));var hardwareService=require(_0xf836('0x12'));var logger=require(_0xf836('0x13'))('api');var utils=require(_0xf836('0x14'));var config=require(_0xf836('0x15'));var licenseUtil=require(_0xf836('0x16'));var db=require(_0xf836('0x17'))['db'];var integrations=require(_0xf836('0x18'));function respondWithStatusCode(_0x498737,_0x1b7301){_0x1b7301=_0x1b7301||0xcc;return function(_0x1a7a5f){if(_0x1a7a5f){return _0x498737[_0xf836('0x19')](_0x1b7301);}return _0x498737[_0xf836('0x1a')](_0x1b7301)[_0xf836('0x1b')]();};}function respondWithResult(_0xe5ecce,_0x5c0b01){_0x5c0b01=_0x5c0b01||0xc8;return function(_0x5623df){if(_0x5623df){return _0xe5ecce[_0xf836('0x1a')](_0x5c0b01)[_0xf836('0x1c')](_0x5623df);}};}function respondWithFilteredResult(_0x17981a,_0x17db4a){return function(_0x1ad14d){if(_0x1ad14d){var _0x36164c=typeof _0x17db4a[_0xf836('0x1d')]===_0xf836('0x1e')&&typeof _0x17db4a[_0xf836('0x1f')]===_0xf836('0x1e');var _0x556760=_0x1ad14d[_0xf836('0x20')];var _0x21454f=_0x36164c?0x0:_0x17db4a['offset'];var _0x19446c=_0x36164c?_0x1ad14d[_0xf836('0x20')]:_0x17db4a[_0xf836('0x1d')]+_0x17db4a['limit'];var _0x1c11d8;if(_0x19446c>=_0x556760){_0x19446c=_0x556760;_0x1c11d8=0xc8;}else{_0x1c11d8=0xce;}_0x17981a[_0xf836('0x1a')](_0x1c11d8);return _0x17981a['set'](_0xf836('0x21'),_0x21454f+'-'+_0x19446c+'/'+_0x556760)[_0xf836('0x1c')](_0x1ad14d);}return null;};}function patchUpdates(_0x2cbe67){return function(_0x1ae75b){try{jsonpatch[_0xf836('0x22')](_0x1ae75b,_0x2cbe67,!![]);}catch(_0x1b0772){return BPromise[_0xf836('0x23')](_0x1b0772);}return _0x1ae75b[_0xf836('0x24')]();};}function saveUpdates(_0x280584,_0x2907e0){return function(_0x25fd9c){if(_0x25fd9c){return _0x25fd9c['update'](_0x280584)[_0xf836('0x25')](function(_0x4f73d8){return _0x4f73d8;});}return null;};}function removeEntity(_0x2d9137,_0x5a361f){return function(_0x2ba695){if(_0x2ba695){return _0x2ba695[_0xf836('0x26')]()[_0xf836('0x25')](function(){_0x2d9137[_0xf836('0x1a')](0xcc)[_0xf836('0x1b')]();});}};}function handleEntityNotFound(_0x5445a5,_0x568f27){return function(_0x191056){if(!_0x191056){_0x5445a5[_0xf836('0x19')](0x194);}return _0x191056;};}function handleError(_0x372286,_0x57ba4d){_0x57ba4d=_0x57ba4d||0x1f4;return function(_0x5290fe){logger[_0xf836('0x27')](_0x5290fe[_0xf836('0x28')]);if(_0x5290fe[_0xf836('0x29')]){delete _0x5290fe[_0xf836('0x29')];}_0x372286[_0xf836('0x1a')](_0x57ba4d)[_0xf836('0x2a')](_0x5290fe);};}exports[_0xf836('0x2b')]=function(_0x458cd5,_0x3744ea){var _0x3a6627={},_0x213619={},_0xcdde3d={'count':0x0,'rows':[]};var _0x10c32f=_['map'](db[_0xf836('0x2c')][_0xf836('0x2d')],function(_0x5aa451){return{'name':_0x5aa451[_0xf836('0x2e')],'type':_0x5aa451[_0xf836('0x2f')][_0xf836('0x30')]};});_0x213619['model']=_['map'](_0x10c32f,_0xf836('0x29'));_0x213619[_0xf836('0x31')]=_[_0xf836('0x32')](_0x458cd5[_0xf836('0x31')]);_0x213619[_0xf836('0x33')]=_[_0xf836('0x34')](_0x213619[_0xf836('0x35')],_0x213619['query']);_0x3a6627[_0xf836('0x36')]=_['intersection'](_0x213619[_0xf836('0x35')],qs[_0xf836('0x37')](_0x458cd5[_0xf836('0x31')][_0xf836('0x37')]));_0x3a6627['attributes']=_0x3a6627['attributes'][_0xf836('0x38')]?_0x3a6627['attributes']:_0x213619[_0xf836('0x35')];if(!_0x458cd5[_0xf836('0x31')][_0xf836('0x39')](_0xf836('0x3a'))){_0x3a6627[_0xf836('0x1f')]=qs[_0xf836('0x1f')](_0x458cd5[_0xf836('0x31')][_0xf836('0x1f')]);_0x3a6627[_0xf836('0x1d')]=qs[_0xf836('0x1d')](_0x458cd5[_0xf836('0x31')][_0xf836('0x1d')]);}_0x3a6627[_0xf836('0x3b')]=qs[_0xf836('0x3c')](_0x458cd5[_0xf836('0x31')][_0xf836('0x3c')]);_0x3a6627['where']=qs[_0xf836('0x33')](_[_0xf836('0x3d')](_0x458cd5[_0xf836('0x31')],_0x213619['filters']),_0x10c32f);if(_0x458cd5[_0xf836('0x31')][_0xf836('0x3e')]){_0x3a6627[_0xf836('0x3f')]=_[_0xf836('0x40')](_0x3a6627['where'],{'$or':_['map'](_0x10c32f,function(_0x3cd5a3){if(_0x3cd5a3[_0xf836('0x2f')]!=='VIRTUAL'){var _0x132fdc={};_0x132fdc[_0x3cd5a3[_0xf836('0x29')]]={'$like':'%'+_0x458cd5['query'][_0xf836('0x3e')]+'%'};return _0x132fdc;}})});}_0x3a6627=_[_0xf836('0x40')]({},_0x3a6627,_0x458cd5['options']);var _0x519ba9={'where':_0x3a6627[_0xf836('0x3f')]};return db[_0xf836('0x2c')][_0xf836('0x20')](_0x519ba9)[_0xf836('0x25')](function(_0x32b31f){_0xcdde3d['count']=_0x32b31f;if(_0x458cd5[_0xf836('0x31')][_0xf836('0x41')]){_0x3a6627['include']=[{'all':!![]}];}return db[_0xf836('0x2c')][_0xf836('0x42')](_0x3a6627);})[_0xf836('0x25')](function(_0x587a30){_0xcdde3d[_0xf836('0x43')]=_0x587a30;return _0xcdde3d;})[_0xf836('0x25')](respondWithFilteredResult(_0x3744ea,_0x3a6627))[_0xf836('0x44')](handleError(_0x3744ea,null));};exports[_0xf836('0x45')]=function(_0x1476c1,_0x1ce159){var _0xc03a61={'raw':![],'where':{'id':_0x1476c1[_0xf836('0x46')]['id']}},_0x2c27a6={};_0x2c27a6['model']=_[_0xf836('0x32')](db[_0xf836('0x2c')][_0xf836('0x2d')]);_0x2c27a6[_0xf836('0x31')]=_[_0xf836('0x32')](_0x1476c1[_0xf836('0x31')]);_0x2c27a6[_0xf836('0x33')]=_[_0xf836('0x34')](_0x2c27a6[_0xf836('0x35')],_0x2c27a6[_0xf836('0x31')]);_0xc03a61['attributes']=_['intersection'](_0x2c27a6[_0xf836('0x35')],qs['fields'](_0x1476c1['query'][_0xf836('0x37')]));_0xc03a61[_0xf836('0x36')]=_0xc03a61[_0xf836('0x36')][_0xf836('0x38')]?_0xc03a61[_0xf836('0x36')]:_0x2c27a6[_0xf836('0x35')];if(_0x1476c1[_0xf836('0x31')][_0xf836('0x41')]){_0xc03a61['include']=[{'all':!![]}];}_0xc03a61=_[_0xf836('0x40')]({},_0xc03a61,_0x1476c1[_0xf836('0x47')]);return db[_0xf836('0x2c')][_0xf836('0x48')](_0xc03a61)[_0xf836('0x25')](handleEntityNotFound(_0x1ce159,null))[_0xf836('0x25')](respondWithResult(_0x1ce159,null))['catch'](handleError(_0x1ce159,null));};exports[_0xf836('0x49')]=function(_0x366ef3,_0x107520){return db['VtigerAccount']['create'](_0x366ef3[_0xf836('0x4a')],{})[_0xf836('0x25')](respondWithResult(_0x107520,0xc9))[_0xf836('0x44')](handleError(_0x107520,null));};exports[_0xf836('0x4b')]=function(_0x274fc1,_0x29bb61){if(_0x274fc1[_0xf836('0x4a')]['id']){delete _0x274fc1[_0xf836('0x4a')]['id'];}return db['VtigerAccount'][_0xf836('0x48')]({'where':{'id':_0x274fc1[_0xf836('0x46')]['id']}})['then'](handleEntityNotFound(_0x29bb61,null))[_0xf836('0x25')](saveUpdates(_0x274fc1[_0xf836('0x4a')],null))[_0xf836('0x25')](respondWithResult(_0x29bb61,null))[_0xf836('0x44')](handleError(_0x29bb61,null));};exports[_0xf836('0x26')]=function(_0x4c1235,_0x3ca7dc){return db[_0xf836('0x2c')]['find']({'where':{'id':_0x4c1235[_0xf836('0x46')]['id']}})[_0xf836('0x25')](handleEntityNotFound(_0x3ca7dc,null))[_0xf836('0x25')](removeEntity(_0x3ca7dc,null))[_0xf836('0x44')](handleError(_0x3ca7dc,null));};exports[_0xf836('0x4c')]=function(_0x5d557d,_0x17a68b,_0x5a32a0){var _0x5c9a47={};var _0x2159e9={};var _0x4d5add;var _0x3ad78e;return db['VtigerAccount']['findOne']({'where':{'id':_0x5d557d[_0xf836('0x46')]['id']}})[_0xf836('0x25')](handleEntityNotFound(_0x17a68b,null))[_0xf836('0x25')](function(_0x5459e6){if(_0x5459e6){_0x4d5add=_0x5459e6;_0x2159e9[_0xf836('0x35')]=_[_0xf836('0x32')](db[_0xf836('0x4d')][_0xf836('0x2d')]);_0x2159e9['query']=_[_0xf836('0x32')](_0x5d557d['query']);_0x2159e9[_0xf836('0x33')]=_[_0xf836('0x34')](_0x2159e9['model'],_0x2159e9['query']);_0x5c9a47[_0xf836('0x36')]=_[_0xf836('0x34')](_0x2159e9['model'],qs[_0xf836('0x37')](_0x5d557d[_0xf836('0x31')][_0xf836('0x37')]));_0x5c9a47[_0xf836('0x36')]=_0x5c9a47[_0xf836('0x36')][_0xf836('0x38')]?_0x5c9a47[_0xf836('0x36')]:_0x2159e9[_0xf836('0x35')];_0x5c9a47['order']=qs[_0xf836('0x3c')](_0x5d557d[_0xf836('0x31')]['sort']);_0x5c9a47['where']=qs['filters'](_['pick'](_0x5d557d[_0xf836('0x31')],_0x2159e9['filters']));if(_0x5d557d['query']['filter']){_0x5c9a47[_0xf836('0x3f')]=_[_0xf836('0x40')](_0x5c9a47[_0xf836('0x3f')],{'$or':_[_0xf836('0x4e')](_0x5c9a47['attributes'],function(_0x3a0e8a){var _0x171e39={};_0x171e39[_0x3a0e8a]={'$like':'%'+_0x5d557d[_0xf836('0x31')]['filter']+'%'};return _0x171e39;})});}_0x5c9a47=_[_0xf836('0x40')]({},_0x5c9a47,_0x5d557d[_0xf836('0x47')]);return _0x4d5add['getConfigurations'](_0x5c9a47);}})['then'](function(_0x48df96){if(_0x48df96){_0x3ad78e=_0x48df96[_0xf836('0x38')];if(!_0x5d557d[_0xf836('0x31')][_0xf836('0x39')]('nolimit')){_0x5c9a47[_0xf836('0x1f')]=qs[_0xf836('0x1f')](_0x5d557d[_0xf836('0x31')][_0xf836('0x1f')]);_0x5c9a47[_0xf836('0x1d')]=qs[_0xf836('0x1d')](_0x5d557d[_0xf836('0x31')]['offset']);}return _0x4d5add[_0xf836('0x4c')](_0x5c9a47);}})['then'](function(_0x44c496){if(_0x44c496){return _0x44c496?{'count':_0x3ad78e,'rows':_0x44c496}:null;}})[_0xf836('0x25')](respondWithResult(_0x17a68b,null))[_0xf836('0x44')](handleError(_0x17a68b,null));};exports[_0xf836('0x4f')]=function(_0x2b71b0,_0x10c9fd,_0x5ab0cf){if(_0x2b71b0['body']['id']){delete _0x2b71b0['body']['id'];}return db['VtigerAccount'][_0xf836('0x50')]({'where':{'id':_0x2b71b0[_0xf836('0x46')]['id']}})[_0xf836('0x25')](handleEntityNotFound(_0x10c9fd,null))[_0xf836('0x25')](function(_0x45df80){if(_0x45df80){_0x2b71b0[_0xf836('0x4a')][_0xf836('0x51')]=_0x45df80['id'];_0x2b71b0[_0xf836('0x4a')][_0xf836('0x52')]=integrations[_0xf836('0x53')](_0x2b71b0[_0xf836('0x4a')][_0xf836('0x54')],_0x2b71b0[_0xf836('0x4a')][_0xf836('0x2f')]);_0x2b71b0['body'][_0xf836('0x55')]=integrations[_0xf836('0x56')](_0x2b71b0[_0xf836('0x4a')][_0xf836('0x54')],_0x2b71b0[_0xf836('0x4a')][_0xf836('0x2f')]);return db['VtigerConfiguration'][_0xf836('0x49')](_0x2b71b0[_0xf836('0x4a')],{'include':[{'model':db[_0xf836('0x57')],'as':_0xf836('0x52')},{'model':db[_0xf836('0x57')],'as':_0xf836('0x55')}]});}return null;})[_0xf836('0x25')](respondWithResult(_0x10c9fd,null))[_0xf836('0x44')](handleError(_0x10c9fd,null));};var md5=require(_0xf836('0x58'));exports[_0xf836('0x59')]=function(_0xe5e52d,_0x476151,_0x4b0741){var _0x3a1d69=null;var _0x2374d3=null;var _0x20af84=null;var _0x367930=null;var _0x4017d4='';return db['VtigerAccount'][_0xf836('0x50')]({'where':{'id':_0xe5e52d[_0xf836('0x46')]['id']},'attributes':['id',_0xf836('0x29'),_0xf836('0x5a'),'description',_0xf836('0x5b'),_0xf836('0x5c'),_0xf836('0x5d'),_0xf836('0x5e')]})[_0xf836('0x25')](handleEntityNotFound(_0x476151,null))[_0xf836('0x25')](function(_0x46bb2e){if(_0x46bb2e){_0x3a1d69=_0x46bb2e[_0xf836('0x5a')];_0x2374d3=_0x46bb2e['accessKey'];_0x20af84=_0x46bb2e[_0xf836('0x5b')];_0x4017d4=_0x46bb2e['remoteUri'];var _0x488e9f=_0x4017d4['slice'](-0x1);if(_0x488e9f==='/'){_0x4017d4=_0x4017d4[_0xf836('0x5f')](0x0,_0x4017d4['lastIndexOf'](_0x488e9f));}_0x367930=util[_0xf836('0x60')](_0xf836('0x61'),_0x4017d4);var _0x1f83f9={'method':_0xf836('0x62'),'uri':_0x367930,'qs':{'operation':_0xf836('0x63'),'username':_0x46bb2e[_0xf836('0x5a')]},'json':!![]};return rp(_0x1f83f9);}})[_0xf836('0x25')](function(_0x4708f5){if(_0x4708f5[_0xf836('0x64')]){var _0x532a4d={'method':_0xf836('0x65'),'uri':_0x367930,'form':{'operation':_0xf836('0x66'),'username':_0x3a1d69,'accessKey':md5(_0x4708f5['result'][_0xf836('0x67')]+_0x2374d3)},'json':!![]};return rp(_0x532a4d);}})[_0xf836('0x25')](function(_0x2c963a){if(_0x2c963a[_0xf836('0x64')]){var _0x299773={'method':'GET','uri':_0x367930,'qs':{'operation':_0xf836('0x68'),'username':_0x3a1d69,'sessionName':_0x2c963a[_0xf836('0x69')]['sessionName'],'elementType':_0x20af84},'json':!![]};return rp(_0x299773);}})[_0xf836('0x25')](function(_0x243e0d){if(_0x243e0d[_0xf836('0x64')]){var _0x4f861a=[_0xf836('0x6a'),_0xf836('0x6b'),_0xf836('0x6c')];_[_0xf836('0x6d')](_0x243e0d[_0xf836('0x69')][_0xf836('0x37')],function(_0x348e21){return _0x4f861a[_0xf836('0x6e')](_0x348e21[_0xf836('0x2f')][_0xf836('0x29')]);});_[_0xf836('0x6d')](_0x243e0d[_0xf836('0x69')]['fields'],function(_0x2b559c){return _0x2b559c[_0xf836('0x6f')]===_0xf836('0x70');});return{'count':_0x243e0d[_0xf836('0x69')][_0xf836('0x37')][_0xf836('0x38')],'rows':_['map'](_0x243e0d['result'][_0xf836('0x37')],function(_0x4a4753){return{'id':_0x4a4753[_0xf836('0x29')],'name':_0x4a4753['label'],'custom':_['startsWith'](_0x4a4753[_0xf836('0x29')],_0xf836('0x71'))?!![]:![],'options':_0x4a4753[_0xf836('0x2f')]['name']==='picklist'&&_0x4a4753[_0xf836('0x2f')][_0xf836('0x72')][_0xf836('0x38')]>0x0?_[_0xf836('0x4e')](_0x4a4753['type']['picklistValues'],function(_0x1f4f9c){return{'name':_0x1f4f9c[_0xf836('0x73')],'value':_0x1f4f9c[_0xf836('0x74')]};}):[]};})};}})[_0xf836('0x25')](respondWithResult(_0x476151,null))[_0xf836('0x44')](function(_0x64f55){console[_0xf836('0x75')]('vTiger\x20error:\x20'+_0x64f55);var _0x2f12ce=_0xe5e52d['query'][_0xf836('0x76')]?0x1f4:_0x64f55[_0xf836('0x77')]||0x1f4;logger[_0xf836('0x27')](_0xf836('0x78'),'getFields',_0x2f12ce,JSON[_0xf836('0x79')](_0x64f55));delete _0x64f55['name'];if(_0x2f12ce===0x191){_0x2f12ce=0x190;}_0x476151[_0xf836('0x1a')](_0x2f12ce)[_0xf836('0x2a')](_0xe5e52d[_0xf836('0x31')][_0xf836('0x76')]?{'message':_0xf836('0x7a'),'statusCode':_0x64f55[_0xf836('0x77')]}:_0x64f55);});};