Built motion from commit (unavailable).|2.3.4
[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 _0x26f9=['value','statusCode','getFields,\x20%s,\x20%s,\x20%s','Wrong\x20credentials','html-pdf','eml-format','zip-dir','moment','bluebird','util','path','sox','to-csv','ejs','lodash','squel','crypto','jsforce','desk.js','querystring','../../components/auth/service','../../config/license/hardware','../../components/parsers/qs','../../config/logger','api','../../config/utils','../../config/environment','../../components/integrations/configuration','jayson/promise','client','http','request','then','VtigerAccount,\x20%s,\x20%s','request\x20sent','VtigerAccount,\x20%s,\x20%s,\x20%s','stringify','code','error','message','info','result','catch','sendStatus','status','json','count','offset','set','reject','update','destroy','end','stack','name','send','index','VtigerAccount','rawAttributes','keys','query','filters','intersection','model','fields','attributes','length','hasOwnProperty','nolimit','limit','order','sort','where','pick','filter','merge','map','type','VIRTUAL','field','options','include','findAll','rows','show','params','includeAll','find','create','body','getConfigurations','VtigerConfiguration','addConfiguration','findOne','Subjects','Descriptions','channel','VtigerField','md5','getFields','moduleName','remoteUri','serverUrl','username','accessKey','format','%s/webservice.php','getchallenge','POST','token','success','GET','sessionName','autogenerated','owner','includes','remove','cf_','picklist','picklistValues','label'];(function(_0x3da707,_0x30c36d){var _0x342ee7=function(_0x175135){while(--_0x175135){_0x3da707['push'](_0x3da707['shift']());}};_0x342ee7(++_0x30c36d);}(_0x26f9,0x16f));var _0x926f=function(_0x1f063b,_0x321a3c){_0x1f063b=_0x1f063b-0x0;var _0x4d864b=_0x26f9[_0x1f063b];return _0x4d864b;};'use strict';var pdf=require(_0x926f('0x0'));var emlformat=require(_0x926f('0x1'));var rimraf=require('rimraf');var zipdir=require(_0x926f('0x2'));var jsonpatch=require('fast-json-patch');var rp=require('request-promise');var moment=require(_0x926f('0x3'));var BPromise=require(_0x926f('0x4'));var Mustache=require('mustache');var util=require(_0x926f('0x5'));var path=require(_0x926f('0x6'));var sox=require(_0x926f('0x7'));var csv=require(_0x926f('0x8'));var ejs=require(_0x926f('0x9'));var fs=require('fs');var _=require(_0x926f('0xa'));var squel=require(_0x926f('0xb'));var crypto=require(_0x926f('0xc'));var jsforce=require(_0x926f('0xd'));var deskjs=require(_0x926f('0xe'));var toCsv=require('to-csv');var querystring=require(_0x926f('0xf'));var Papa=require('papaparse');var authService=require(_0x926f('0x10'));var hardwareService=require(_0x926f('0x11'));var Redis=require('ioredis');var qs=require(_0x926f('0x12'));var logger=require(_0x926f('0x13'))(_0x926f('0x14'));var utils=require(_0x926f('0x15'));var config=require(_0x926f('0x16'));var db=require('../../mysqldb')['db'];var integrations=require(_0x926f('0x17'));var jayson=require(_0x926f('0x18'));var client=jayson[_0x926f('0x19')][_0x926f('0x1a')]({'port':0x232a});function respondWithRpcPromise(_0x44f5da,_0x450b6e,_0x378bb1,_0x539e5d){return new BPromise(function(_0x48ee07,_0x5e6fb1){var _0x11197b=_0x539e5d||client;return _0x11197b[_0x926f('0x1b')](_0x44f5da,_0x378bb1)[_0x926f('0x1c')](function(_0x280759){logger['info'](_0x926f('0x1d'),_0x450b6e,_0x926f('0x1e'));logger['debug'](_0x926f('0x1f'),_0x450b6e,'request\x20sent',JSON[_0x926f('0x20')](_0x280759));if(_0x280759['error']){if(_0x280759['error'][_0x926f('0x21')]===0x1f4){logger[_0x926f('0x22')]('VtigerAccount,\x20%s,\x20%s',_0x450b6e,_0x280759[_0x926f('0x22')][_0x926f('0x23')]);return _0x5e6fb1(_0x280759[_0x926f('0x22')]['message']);}logger[_0x926f('0x22')](_0x926f('0x1d'),_0x450b6e,_0x280759['error'][_0x926f('0x23')]);return _0x48ee07(_0x280759[_0x926f('0x22')]['message']);}else{logger[_0x926f('0x24')](_0x926f('0x1d'),_0x450b6e,_0x926f('0x1e'));_0x48ee07(_0x280759[_0x926f('0x25')][_0x926f('0x23')]);}})[_0x926f('0x26')](function(_0x412260){logger[_0x926f('0x22')](_0x926f('0x1d'),_0x450b6e,_0x412260);_0x5e6fb1(_0x412260);});});}function respondWithStatusCode(_0x61738a,_0x2be66b){_0x2be66b=_0x2be66b||0xcc;return function(_0x4a93a4){if(_0x4a93a4){return _0x61738a[_0x926f('0x27')](_0x2be66b);}return _0x61738a[_0x926f('0x28')](_0x2be66b)['end']();};}function respondWithResult(_0x640fe0,_0x84bccd){_0x84bccd=_0x84bccd||0xc8;return function(_0x36a17e){if(_0x36a17e){return _0x640fe0[_0x926f('0x28')](_0x84bccd)[_0x926f('0x29')](_0x36a17e);}};}function respondWithFilteredResult(_0x3887fc,_0x4b8964){return function(_0x110e59){if(_0x110e59){var _0x5065dd=_0x110e59[_0x926f('0x2a')],_0x17e071=_0x4b8964['offset'],_0x37230f=_0x4b8964[_0x926f('0x2b')]+_0x4b8964['limit'],_0x2a8fff;if(_0x37230f>=_0x5065dd){_0x37230f=_0x5065dd;_0x2a8fff=0xc8;}else{_0x2a8fff=0xce;}_0x3887fc[_0x926f('0x28')](_0x2a8fff);return _0x3887fc[_0x926f('0x2c')]('Content-Range',_0x17e071+'-'+_0x37230f+'/'+_0x5065dd)['json'](_0x110e59);}return null;};}function patchUpdates(_0x19bd54){return function(_0x1f2e2e){try{jsonpatch['apply'](_0x1f2e2e,_0x19bd54,!![]);}catch(_0x33c1fb){return BPromise[_0x926f('0x2d')](_0x33c1fb);}return _0x1f2e2e['save']();};}function saveUpdates(_0x5cae09,_0x39966d){return function(_0x1d61d6){if(_0x1d61d6){return _0x1d61d6[_0x926f('0x2e')](_0x5cae09)['then'](function(_0x681ceb){return _0x681ceb;});}return null;};}function removeEntity(_0x12de3e,_0x32c45f){return function(_0x553d93){if(_0x553d93){return _0x553d93[_0x926f('0x2f')]()[_0x926f('0x1c')](function(){_0x12de3e[_0x926f('0x28')](0xcc)[_0x926f('0x30')]();});}};}function handleEntityNotFound(_0x1061da,_0x426a40){return function(_0x589409){if(!_0x589409){_0x1061da[_0x926f('0x27')](0x194);}return _0x589409;};}function handleError(_0xf17230,_0x2ac053){_0x2ac053=_0x2ac053||0x1f4;return function(_0xb8117f){logger['error'](_0xb8117f[_0x926f('0x31')]);if(_0xb8117f[_0x926f('0x32')]){delete _0xb8117f[_0x926f('0x32')];}_0xf17230[_0x926f('0x28')](_0x2ac053)[_0x926f('0x33')](_0xb8117f);};}exports[_0x926f('0x34')]=function(_0x1d7e5c,_0xaad45d){var _0x2184c1={},_0x41bfac={},_0xd4d998={'count':0x0,'rows':[]};var _0x482322=db[_0x926f('0x35')][_0x926f('0x36')];_0x41bfac['model']=_[_0x926f('0x37')](_0x482322);_0x41bfac[_0x926f('0x38')]=_[_0x926f('0x37')](_0x1d7e5c[_0x926f('0x38')]);_0x41bfac[_0x926f('0x39')]=_[_0x926f('0x3a')](_0x41bfac[_0x926f('0x3b')],_0x41bfac[_0x926f('0x38')]);_0x2184c1['attributes']=_[_0x926f('0x3a')](_0x41bfac['model'],qs[_0x926f('0x3c')](_0x1d7e5c[_0x926f('0x38')][_0x926f('0x3c')]));_0x2184c1[_0x926f('0x3d')]=_0x2184c1[_0x926f('0x3d')][_0x926f('0x3e')]?_0x2184c1[_0x926f('0x3d')]:_0x41bfac[_0x926f('0x3b')];if(!_0x1d7e5c[_0x926f('0x38')][_0x926f('0x3f')](_0x926f('0x40'))){_0x2184c1[_0x926f('0x41')]=qs[_0x926f('0x41')](_0x1d7e5c[_0x926f('0x38')][_0x926f('0x41')]);_0x2184c1['offset']=qs[_0x926f('0x2b')](_0x1d7e5c[_0x926f('0x38')][_0x926f('0x2b')]);}_0x2184c1[_0x926f('0x42')]=qs[_0x926f('0x43')](_0x1d7e5c[_0x926f('0x38')][_0x926f('0x43')]);_0x2184c1[_0x926f('0x44')]=qs['filters'](_[_0x926f('0x45')](_0x1d7e5c[_0x926f('0x38')],_0x41bfac[_0x926f('0x39')]));if(_0x1d7e5c[_0x926f('0x38')][_0x926f('0x46')]){_0x2184c1['where']=_[_0x926f('0x47')](_0x2184c1[_0x926f('0x44')],{'$or':_[_0x926f('0x48')](_0x482322,function(_0x39cb80){if(_0x39cb80[_0x926f('0x49')]['key']!==_0x926f('0x4a')){var _0x10701e={};_0x10701e[_0x39cb80[_0x926f('0x4b')]]={'$like':'%'+_0x1d7e5c[_0x926f('0x38')][_0x926f('0x46')]+'%'};return _0x10701e;}})});}_0x2184c1=_['merge']({},_0x2184c1,_0x1d7e5c[_0x926f('0x4c')]);var _0x533ca7={'where':_0x2184c1[_0x926f('0x44')]};return db[_0x926f('0x35')][_0x926f('0x2a')](_0x533ca7)[_0x926f('0x1c')](function(_0x35e23a){_0xd4d998[_0x926f('0x2a')]=_0x35e23a;if(_0x1d7e5c[_0x926f('0x38')]['includeAll']){_0x2184c1[_0x926f('0x4d')]=[{'all':!![]}];}return db[_0x926f('0x35')][_0x926f('0x4e')](_0x2184c1);})[_0x926f('0x1c')](function(_0x3a39e8){_0xd4d998[_0x926f('0x4f')]=_0x3a39e8;return _0xd4d998;})[_0x926f('0x1c')](respondWithFilteredResult(_0xaad45d,_0x2184c1))[_0x926f('0x26')](handleError(_0xaad45d,null));};exports[_0x926f('0x50')]=function(_0x165cf3,_0x5b6f99){var _0x351ac6={'raw':![],'where':{'id':_0x165cf3[_0x926f('0x51')]['id']}},_0x5119aa={};_0x5119aa[_0x926f('0x3b')]=_[_0x926f('0x37')](db[_0x926f('0x35')][_0x926f('0x36')]);_0x5119aa['query']=_[_0x926f('0x37')](_0x165cf3[_0x926f('0x38')]);_0x5119aa[_0x926f('0x39')]=_[_0x926f('0x3a')](_0x5119aa[_0x926f('0x3b')],_0x5119aa['query']);_0x351ac6[_0x926f('0x3d')]=_[_0x926f('0x3a')](_0x5119aa[_0x926f('0x3b')],qs[_0x926f('0x3c')](_0x165cf3[_0x926f('0x38')][_0x926f('0x3c')]));_0x351ac6['attributes']=_0x351ac6[_0x926f('0x3d')]['length']?_0x351ac6['attributes']:_0x5119aa[_0x926f('0x3b')];if(_0x165cf3[_0x926f('0x38')][_0x926f('0x52')]){_0x351ac6[_0x926f('0x4d')]=[{'all':!![]}];}_0x351ac6=_[_0x926f('0x47')]({},_0x351ac6,_0x165cf3['options']);return db[_0x926f('0x35')][_0x926f('0x53')](_0x351ac6)[_0x926f('0x1c')](handleEntityNotFound(_0x5b6f99,null))['then'](respondWithResult(_0x5b6f99,null))[_0x926f('0x26')](handleError(_0x5b6f99,null));};exports[_0x926f('0x54')]=function(_0x46604e,_0x27b82a){return db['VtigerAccount'][_0x926f('0x54')](_0x46604e['body'],{})[_0x926f('0x1c')](respondWithResult(_0x27b82a,0xc9))['catch'](handleError(_0x27b82a,null));};exports[_0x926f('0x2e')]=function(_0x3728ea,_0x535d01){if(_0x3728ea[_0x926f('0x55')]['id']){delete _0x3728ea[_0x926f('0x55')]['id'];}return db[_0x926f('0x35')][_0x926f('0x53')]({'where':{'id':_0x3728ea[_0x926f('0x51')]['id']}})['then'](handleEntityNotFound(_0x535d01,null))[_0x926f('0x1c')](saveUpdates(_0x3728ea[_0x926f('0x55')],null))[_0x926f('0x1c')](respondWithResult(_0x535d01,null))[_0x926f('0x26')](handleError(_0x535d01,null));};exports[_0x926f('0x2f')]=function(_0xbc13b1,_0x5463c3){return db[_0x926f('0x35')][_0x926f('0x53')]({'where':{'id':_0xbc13b1[_0x926f('0x51')]['id']}})[_0x926f('0x1c')](handleEntityNotFound(_0x5463c3,null))[_0x926f('0x1c')](removeEntity(_0x5463c3,null))[_0x926f('0x26')](handleError(_0x5463c3,null));};exports[_0x926f('0x56')]=function(_0x3e0c4a,_0x4691fa,_0x3e6f36){var _0x36ac06={};var _0x25f95b={};var _0xc74fb2;var _0x126442;return db[_0x926f('0x35')]['findOne']({'where':{'id':_0x3e0c4a[_0x926f('0x51')]['id']}})['then'](handleEntityNotFound(_0x4691fa,null))[_0x926f('0x1c')](function(_0x39c37c){if(_0x39c37c){_0xc74fb2=_0x39c37c;_0x25f95b[_0x926f('0x3b')]=_[_0x926f('0x37')](db[_0x926f('0x57')][_0x926f('0x36')]);_0x25f95b[_0x926f('0x38')]=_[_0x926f('0x37')](_0x3e0c4a[_0x926f('0x38')]);_0x25f95b[_0x926f('0x39')]=_[_0x926f('0x3a')](_0x25f95b[_0x926f('0x3b')],_0x25f95b[_0x926f('0x38')]);_0x36ac06[_0x926f('0x3d')]=_[_0x926f('0x3a')](_0x25f95b[_0x926f('0x3b')],qs[_0x926f('0x3c')](_0x3e0c4a[_0x926f('0x38')][_0x926f('0x3c')]));_0x36ac06[_0x926f('0x3d')]=_0x36ac06[_0x926f('0x3d')][_0x926f('0x3e')]?_0x36ac06[_0x926f('0x3d')]:_0x25f95b[_0x926f('0x3b')];_0x36ac06[_0x926f('0x42')]=qs['sort'](_0x3e0c4a['query']['sort']);_0x36ac06[_0x926f('0x44')]=qs[_0x926f('0x39')](_[_0x926f('0x45')](_0x3e0c4a[_0x926f('0x38')],_0x25f95b[_0x926f('0x39')]));if(_0x3e0c4a[_0x926f('0x38')]['filter']){_0x36ac06[_0x926f('0x44')]=_[_0x926f('0x47')](_0x36ac06[_0x926f('0x44')],{'$or':_[_0x926f('0x48')](_0x36ac06[_0x926f('0x3d')],function(_0x182868){var _0x2c558e={};_0x2c558e[_0x182868]={'$like':'%'+_0x3e0c4a[_0x926f('0x38')]['filter']+'%'};return _0x2c558e;})});}_0x36ac06=_[_0x926f('0x47')]({},_0x36ac06,_0x3e0c4a[_0x926f('0x4c')]);return _0xc74fb2[_0x926f('0x56')](_0x36ac06);}})[_0x926f('0x1c')](function(_0x361756){if(_0x361756){_0x126442=_0x361756['length'];if(!_0x3e0c4a[_0x926f('0x38')][_0x926f('0x3f')](_0x926f('0x40'))){_0x36ac06['limit']=qs[_0x926f('0x41')](_0x3e0c4a[_0x926f('0x38')][_0x926f('0x41')]);_0x36ac06[_0x926f('0x2b')]=qs[_0x926f('0x2b')](_0x3e0c4a[_0x926f('0x38')]['offset']);}return _0xc74fb2[_0x926f('0x56')](_0x36ac06);}})[_0x926f('0x1c')](function(_0x44c503){if(_0x44c503){return _0x44c503?{'count':_0x126442,'rows':_0x44c503}:null;}})[_0x926f('0x1c')](respondWithResult(_0x4691fa,null))[_0x926f('0x26')](handleError(_0x4691fa,null));};exports[_0x926f('0x58')]=function(_0x36621d,_0xc791e6,_0x3bf842){if(_0x36621d[_0x926f('0x55')]['id']){delete _0x36621d[_0x926f('0x55')]['id'];}return db[_0x926f('0x35')][_0x926f('0x59')]({'where':{'id':_0x36621d[_0x926f('0x51')]['id']}})['then'](handleEntityNotFound(_0xc791e6,null))[_0x926f('0x1c')](function(_0x2a462a){if(_0x2a462a){_0x36621d[_0x926f('0x55')]['AccountId']=_0x2a462a['id'];_0x36621d[_0x926f('0x55')][_0x926f('0x5a')]=integrations['getSubjects'](_0x36621d[_0x926f('0x55')]['channel'],_0x36621d[_0x926f('0x55')][_0x926f('0x49')]);_0x36621d[_0x926f('0x55')][_0x926f('0x5b')]=integrations['getDescriptions'](_0x36621d['body'][_0x926f('0x5c')],_0x36621d[_0x926f('0x55')][_0x926f('0x49')]);return db['VtigerConfiguration'][_0x926f('0x54')](_0x36621d[_0x926f('0x55')],{'include':[{'model':db['VtigerField'],'as':'Subjects'},{'model':db[_0x926f('0x5d')],'as':_0x926f('0x5b')}]});}return null;})[_0x926f('0x1c')](respondWithResult(_0xc791e6,null))[_0x926f('0x26')](handleError(_0xc791e6,null));};var md5=require(_0x926f('0x5e'));exports[_0x926f('0x5f')]=function(_0xc31092,_0x36935d,_0x37860a){var _0x7d9025=null;var _0x1de15f=null;var _0x3c4387=null;var _0x56cbb1=null;var _0x1588b2='';return db[_0x926f('0x35')]['findOne']({'where':{'id':_0xc31092[_0x926f('0x51')]['id']},'attributes':['id','name','username','description',_0x926f('0x60'),_0x926f('0x61'),_0x926f('0x62'),'accessKey']})['then'](handleEntityNotFound(_0x36935d,null))[_0x926f('0x1c')](function(_0x3d59dc){if(_0x3d59dc){_0x7d9025=_0x3d59dc[_0x926f('0x63')];_0x1de15f=_0x3d59dc[_0x926f('0x64')];_0x3c4387=_0x3d59dc[_0x926f('0x60')];_0x1588b2=_0x3d59dc[_0x926f('0x61')];var _0x3c45e1=_0x1588b2['slice'](-0x1);if(_0x3c45e1==='/'){_0x1588b2=_0x1588b2['substring'](0x0,_0x1588b2['lastIndexOf'](_0x3c45e1));}_0x56cbb1=util[_0x926f('0x65')](_0x926f('0x66'),_0x1588b2);var _0x3e1664={'method':'GET','uri':_0x56cbb1,'qs':{'operation':_0x926f('0x67'),'username':_0x3d59dc['username']},'json':!![]};return rp(_0x3e1664);}})[_0x926f('0x1c')](function(_0x371bee){if(_0x371bee['success']){var _0x5a05ae={'method':_0x926f('0x68'),'uri':_0x56cbb1,'form':{'operation':'login','username':_0x7d9025,'accessKey':md5(_0x371bee[_0x926f('0x25')][_0x926f('0x69')]+_0x1de15f)},'json':!![]};return rp(_0x5a05ae);}})[_0x926f('0x1c')](function(_0x401bb3){if(_0x401bb3[_0x926f('0x6a')]){var _0x779a60={'method':_0x926f('0x6b'),'uri':_0x56cbb1,'qs':{'operation':'describe','username':_0x7d9025,'sessionName':_0x401bb3['result'][_0x926f('0x6c')],'elementType':_0x3c4387},'json':!![]};return rp(_0x779a60);}})[_0x926f('0x1c')](function(_0x15845d){if(_0x15845d[_0x926f('0x6a')]){var _0x496045=[_0x926f('0x6d'),'reference',_0x926f('0x6e')];_['remove'](_0x15845d[_0x926f('0x25')][_0x926f('0x3c')],function(_0x1ee903){return _0x496045[_0x926f('0x6f')](_0x1ee903[_0x926f('0x49')]['name']);});_[_0x926f('0x70')](_0x15845d[_0x926f('0x25')][_0x926f('0x3c')],function(_0x17df98){return _0x17df98['editable']==='false';});return{'count':_0x15845d[_0x926f('0x25')][_0x926f('0x3c')][_0x926f('0x3e')],'rows':_[_0x926f('0x48')](_0x15845d[_0x926f('0x25')][_0x926f('0x3c')],function(_0x5eb506){return{'id':_0x5eb506[_0x926f('0x32')],'name':_0x5eb506['label'],'custom':_['startsWith'](_0x5eb506['name'],_0x926f('0x71'))?!![]:![],'options':_0x5eb506[_0x926f('0x49')][_0x926f('0x32')]===_0x926f('0x72')&&_0x5eb506['type'][_0x926f('0x73')][_0x926f('0x3e')]>0x0?_[_0x926f('0x48')](_0x5eb506[_0x926f('0x49')][_0x926f('0x73')],function(_0x5bc24d){return{'name':_0x5bc24d[_0x926f('0x74')],'value':_0x5bc24d[_0x926f('0x75')]};}):[]};})};}})[_0x926f('0x1c')](respondWithResult(_0x36935d,null))['catch'](function(_0x5596f6){console['log']('vTiger\x20error:\x20'+_0x5596f6);var _0x5ee2f5=_0xc31092[_0x926f('0x38')]['test']?0x1f4:_0x5596f6[_0x926f('0x76')]||0x1f4;logger[_0x926f('0x22')](_0x926f('0x77'),_0x926f('0x5f'),_0x5ee2f5,JSON[_0x926f('0x20')](_0x5596f6));delete _0x5596f6[_0x926f('0x32')];if(_0x5ee2f5===0x191){_0x5ee2f5=0x190;}_0x36935d[_0x926f('0x28')](_0x5ee2f5)['send'](_0xc31092[_0x926f('0x38')]['test']?{'message':_0x926f('0x78'),'statusCode':_0x5596f6[_0x926f('0x76')]}:_0x5596f6);});};