a628e970193ab8c0a3695642902c0a2bf9d579ab
[motion2.git] / server / api / intZohoAccount / intZohoAccount.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 _0x24b4=['host','zoho.eu','com','serverUrl','slice','substring','lastIndexOf','Refresh\x20Token\x20not\x20available\x20for\x20the\x20account','GET','https://www.zohoapis.%s/crm/v2/settings/fields','Calls','Zoho-oauthtoken\x20','response','message','Subject','Description','Call_Duration','Call_Result','Call_Start_Time','remove','includes','api_name','system_mandatory','push','display_label','data_type','picklist','pick_list_values','test','getFields,\x20%s,\x20%s,\x20%s','stringify','Wrong\x20credentials','statusCode','eml-format','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','lodash','desk.js','to-csv','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','../../components/integrations/configuration','sendStatus','status','end','json','undefined','count','offset','limit','set','Content-Range','apply','reject','save','update','then','destroy','error','stack','name','send','index','map','rawAttributes','fieldName','key','keys','query','filters','intersection','model','attributes','fields','length','hasOwnProperty','order','sort','where','pick','filter','merge','type','options','ZohoAccount','includeAll','include','catch','show','find','create','body','params','getConfigurations','findOne','ZohoConfiguration','nolimit','addConfiguration','AccountId','getSubjects','getDescriptions','ZohoField','zone','https://accounts.zoho.com','https://accounts.zoho.com.au','https://accounts.zoho.eu','https://accounts.zoho.in','POST','/oauth/v2/token','refreshToken','clientId','clientSecret','refresh_token','access_token','Unable\x20to\x20retrieve\x20the\x20access\x20token','getFields'];(function(_0x143314,_0x297ce1){var _0x4f48c1=function(_0xe96dd){while(--_0xe96dd){_0x143314['push'](_0x143314['shift']());}};_0x4f48c1(++_0x297ce1);}(_0x24b4,0x1a7));var _0x424b=function(_0x7a044f,_0x5a302b){_0x7a044f=_0x7a044f-0x0;var _0x55474a=_0x24b4[_0x7a044f];return _0x55474a;};'use strict';var emlformat=require(_0x424b('0x0'));var rimraf=require('rimraf');var zipdir=require(_0x424b('0x1'));var jsonpatch=require(_0x424b('0x2'));var rp=require(_0x424b('0x3'));var moment=require(_0x424b('0x4'));var BPromise=require(_0x424b('0x5'));var Mustache=require(_0x424b('0x6'));var util=require(_0x424b('0x7'));var path=require(_0x424b('0x8'));var sox=require('sox');var csv=require('to-csv');var ejs=require('ejs');var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0x424b('0x9'));var squel=require('squel');var crypto=require('crypto');var jsforce=require('jsforce');var deskjs=require(_0x424b('0xa'));var toCsv=require(_0x424b('0xb'));var querystring=require(_0x424b('0xc'));var Papa=require(_0x424b('0xd'));var Redis=require(_0x424b('0xe'));var authService=require(_0x424b('0xf'));var qs=require('../../components/parsers/qs');var as=require(_0x424b('0x10'));var hardwareService=require(_0x424b('0x11'));var logger=require('../../config/logger')('api');var utils=require(_0x424b('0x12'));var config=require(_0x424b('0x13'));var licenseUtil=require(_0x424b('0x14'));var db=require(_0x424b('0x15'))['db'];var integrations=require(_0x424b('0x16'));function respondWithStatusCode(_0x36f284,_0x3c48e0){_0x3c48e0=_0x3c48e0||0xcc;return function(_0x21952d){if(_0x21952d){return _0x36f284[_0x424b('0x17')](_0x3c48e0);}return _0x36f284[_0x424b('0x18')](_0x3c48e0)[_0x424b('0x19')]();};}function respondWithResult(_0x11a5b5,_0x34f6f9){_0x34f6f9=_0x34f6f9||0xc8;return function(_0x1a1101){if(_0x1a1101){return _0x11a5b5[_0x424b('0x18')](_0x34f6f9)[_0x424b('0x1a')](_0x1a1101);}};}function respondWithFilteredResult(_0x270173,_0x2f1934){return function(_0xf01334){if(_0xf01334){var _0x51806f=typeof _0x2f1934['offset']===_0x424b('0x1b')&&typeof _0x2f1934['limit']==='undefined';var _0xcbfe6d=_0xf01334[_0x424b('0x1c')];var _0x141325=_0x51806f?0x0:_0x2f1934[_0x424b('0x1d')];var _0x26d80a=_0x51806f?_0xf01334['count']:_0x2f1934[_0x424b('0x1d')]+_0x2f1934[_0x424b('0x1e')];var _0x5a5288;if(_0x26d80a>=_0xcbfe6d){_0x26d80a=_0xcbfe6d;_0x5a5288=0xc8;}else{_0x5a5288=0xce;}_0x270173[_0x424b('0x18')](_0x5a5288);return _0x270173[_0x424b('0x1f')](_0x424b('0x20'),_0x141325+'-'+_0x26d80a+'/'+_0xcbfe6d)['json'](_0xf01334);}return null;};}function patchUpdates(_0x277146){return function(_0xe52836){try{jsonpatch[_0x424b('0x21')](_0xe52836,_0x277146,!![]);}catch(_0x34b425){return BPromise[_0x424b('0x22')](_0x34b425);}return _0xe52836[_0x424b('0x23')]();};}function saveUpdates(_0x43c279,_0x39e528){return function(_0x2a7d5c){if(_0x2a7d5c){return _0x2a7d5c[_0x424b('0x24')](_0x43c279)[_0x424b('0x25')](function(_0x16458e){return _0x16458e;});}return null;};}function removeEntity(_0x4a168a,_0x4ce19a){return function(_0x261c30){if(_0x261c30){return _0x261c30[_0x424b('0x26')]()[_0x424b('0x25')](function(){_0x4a168a['status'](0xcc)['end']();});}};}function handleEntityNotFound(_0x5e78f6,_0xfa8972){return function(_0x4d0ed5){if(!_0x4d0ed5){_0x5e78f6[_0x424b('0x17')](0x194);}return _0x4d0ed5;};}function handleError(_0x17b674,_0x519f1a){_0x519f1a=_0x519f1a||0x1f4;return function(_0x2aaf80){logger[_0x424b('0x27')](_0x2aaf80[_0x424b('0x28')]);if(_0x2aaf80['name']){delete _0x2aaf80[_0x424b('0x29')];}_0x17b674[_0x424b('0x18')](_0x519f1a)[_0x424b('0x2a')](_0x2aaf80);};}exports[_0x424b('0x2b')]=function(_0x585e8f,_0xee42f6){var _0x280b4e={},_0x49500b={},_0x35a7f2={'count':0x0,'rows':[]};var _0x1e83b6=_[_0x424b('0x2c')](db['ZohoAccount'][_0x424b('0x2d')],function(_0x467e00){return{'name':_0x467e00[_0x424b('0x2e')],'type':_0x467e00['type'][_0x424b('0x2f')]};});_0x49500b['model']=_[_0x424b('0x2c')](_0x1e83b6,_0x424b('0x29'));_0x49500b['query']=_[_0x424b('0x30')](_0x585e8f[_0x424b('0x31')]);_0x49500b[_0x424b('0x32')]=_[_0x424b('0x33')](_0x49500b[_0x424b('0x34')],_0x49500b[_0x424b('0x31')]);_0x280b4e[_0x424b('0x35')]=_[_0x424b('0x33')](_0x49500b[_0x424b('0x34')],qs[_0x424b('0x36')](_0x585e8f[_0x424b('0x31')][_0x424b('0x36')]));_0x280b4e[_0x424b('0x35')]=_0x280b4e[_0x424b('0x35')][_0x424b('0x37')]?_0x280b4e[_0x424b('0x35')]:_0x49500b[_0x424b('0x34')];if(!_0x585e8f[_0x424b('0x31')][_0x424b('0x38')]('nolimit')){_0x280b4e[_0x424b('0x1e')]=qs['limit'](_0x585e8f[_0x424b('0x31')][_0x424b('0x1e')]);_0x280b4e[_0x424b('0x1d')]=qs[_0x424b('0x1d')](_0x585e8f[_0x424b('0x31')][_0x424b('0x1d')]);}_0x280b4e[_0x424b('0x39')]=qs[_0x424b('0x3a')](_0x585e8f[_0x424b('0x31')][_0x424b('0x3a')]);_0x280b4e[_0x424b('0x3b')]=qs['filters'](_[_0x424b('0x3c')](_0x585e8f['query'],_0x49500b[_0x424b('0x32')]),_0x1e83b6);if(_0x585e8f['query'][_0x424b('0x3d')]){_0x280b4e[_0x424b('0x3b')]=_[_0x424b('0x3e')](_0x280b4e[_0x424b('0x3b')],{'$or':_['map'](_0x1e83b6,function(_0x5c2d6a){if(_0x5c2d6a[_0x424b('0x3f')]!=='VIRTUAL'){var _0x3e6f9c={};_0x3e6f9c[_0x5c2d6a['name']]={'$like':'%'+_0x585e8f['query'][_0x424b('0x3d')]+'%'};return _0x3e6f9c;}})});}_0x280b4e=_[_0x424b('0x3e')]({},_0x280b4e,_0x585e8f[_0x424b('0x40')]);var _0x2c3c77={'where':_0x280b4e[_0x424b('0x3b')]};return db[_0x424b('0x41')][_0x424b('0x1c')](_0x2c3c77)['then'](function(_0x3ea5d3){_0x35a7f2['count']=_0x3ea5d3;if(_0x585e8f[_0x424b('0x31')][_0x424b('0x42')]){_0x280b4e[_0x424b('0x43')]=[{'all':!![]}];}return db[_0x424b('0x41')]['findAll'](_0x280b4e);})[_0x424b('0x25')](function(_0x25c3ce){_0x35a7f2['rows']=_0x25c3ce;return _0x35a7f2;})[_0x424b('0x25')](respondWithFilteredResult(_0xee42f6,_0x280b4e))[_0x424b('0x44')](handleError(_0xee42f6,null));};exports[_0x424b('0x45')]=function(_0x53a8b7,_0x34816f){var _0xfd4637={'raw':![],'where':{'id':_0x53a8b7['params']['id']}},_0x49b200={};_0x49b200['model']=_[_0x424b('0x30')](db[_0x424b('0x41')]['rawAttributes']);_0x49b200[_0x424b('0x31')]=_[_0x424b('0x30')](_0x53a8b7[_0x424b('0x31')]);_0x49b200['filters']=_[_0x424b('0x33')](_0x49b200[_0x424b('0x34')],_0x49b200[_0x424b('0x31')]);_0xfd4637['attributes']=_[_0x424b('0x33')](_0x49b200['model'],qs[_0x424b('0x36')](_0x53a8b7[_0x424b('0x31')][_0x424b('0x36')]));_0xfd4637[_0x424b('0x35')]=_0xfd4637[_0x424b('0x35')]['length']?_0xfd4637[_0x424b('0x35')]:_0x49b200['model'];if(_0x53a8b7[_0x424b('0x31')]['includeAll']){_0xfd4637[_0x424b('0x43')]=[{'all':!![]}];}_0xfd4637=_[_0x424b('0x3e')]({},_0xfd4637,_0x53a8b7[_0x424b('0x40')]);return db[_0x424b('0x41')][_0x424b('0x46')](_0xfd4637)[_0x424b('0x25')](handleEntityNotFound(_0x34816f,null))[_0x424b('0x25')](respondWithResult(_0x34816f,null))[_0x424b('0x44')](handleError(_0x34816f,null));};exports[_0x424b('0x47')]=function(_0x542c73,_0x5ee090){return db[_0x424b('0x41')]['create'](_0x542c73[_0x424b('0x48')],{})[_0x424b('0x25')](respondWithResult(_0x5ee090,0xc9))['catch'](handleError(_0x5ee090,null));};exports['update']=function(_0x4d20f0,_0x341898){if(_0x4d20f0[_0x424b('0x48')]['id']){delete _0x4d20f0['body']['id'];}return db[_0x424b('0x41')][_0x424b('0x46')]({'where':{'id':_0x4d20f0[_0x424b('0x49')]['id']}})['then'](handleEntityNotFound(_0x341898,null))[_0x424b('0x25')](saveUpdates(_0x4d20f0[_0x424b('0x48')],null))['then'](respondWithResult(_0x341898,null))['catch'](handleError(_0x341898,null));};exports[_0x424b('0x26')]=function(_0x3b8d1a,_0x1dbe1d){return db[_0x424b('0x41')]['find']({'where':{'id':_0x3b8d1a[_0x424b('0x49')]['id']}})[_0x424b('0x25')](handleEntityNotFound(_0x1dbe1d,null))[_0x424b('0x25')](removeEntity(_0x1dbe1d,null))[_0x424b('0x44')](handleError(_0x1dbe1d,null));};exports[_0x424b('0x4a')]=function(_0x2b39e0,_0x4e5eae,_0x23eeaf){var _0x3c6ade={};var _0xd03aef={};var _0x80759b;var _0x5a0ff9;return db[_0x424b('0x41')][_0x424b('0x4b')]({'where':{'id':_0x2b39e0[_0x424b('0x49')]['id']}})['then'](handleEntityNotFound(_0x4e5eae,null))[_0x424b('0x25')](function(_0x29b2f0){if(_0x29b2f0){_0x80759b=_0x29b2f0;_0xd03aef[_0x424b('0x34')]=_[_0x424b('0x30')](db[_0x424b('0x4c')]['rawAttributes']);_0xd03aef[_0x424b('0x31')]=_[_0x424b('0x30')](_0x2b39e0['query']);_0xd03aef['filters']=_['intersection'](_0xd03aef['model'],_0xd03aef[_0x424b('0x31')]);_0x3c6ade['attributes']=_['intersection'](_0xd03aef[_0x424b('0x34')],qs[_0x424b('0x36')](_0x2b39e0[_0x424b('0x31')][_0x424b('0x36')]));_0x3c6ade[_0x424b('0x35')]=_0x3c6ade[_0x424b('0x35')][_0x424b('0x37')]?_0x3c6ade[_0x424b('0x35')]:_0xd03aef[_0x424b('0x34')];_0x3c6ade[_0x424b('0x39')]=qs[_0x424b('0x3a')](_0x2b39e0[_0x424b('0x31')][_0x424b('0x3a')]);_0x3c6ade[_0x424b('0x3b')]=qs[_0x424b('0x32')](_[_0x424b('0x3c')](_0x2b39e0[_0x424b('0x31')],_0xd03aef[_0x424b('0x32')]));if(_0x2b39e0[_0x424b('0x31')][_0x424b('0x3d')]){_0x3c6ade[_0x424b('0x3b')]=_[_0x424b('0x3e')](_0x3c6ade['where'],{'$or':_[_0x424b('0x2c')](_0x3c6ade[_0x424b('0x35')],function(_0x4fb106){var _0x273c80={};_0x273c80[_0x4fb106]={'$like':'%'+_0x2b39e0['query'][_0x424b('0x3d')]+'%'};return _0x273c80;})});}_0x3c6ade=_['merge']({},_0x3c6ade,_0x2b39e0[_0x424b('0x40')]);return _0x80759b[_0x424b('0x4a')](_0x3c6ade);}})[_0x424b('0x25')](function(_0x28dd0c){if(_0x28dd0c){_0x5a0ff9=_0x28dd0c['length'];if(!_0x2b39e0[_0x424b('0x31')]['hasOwnProperty'](_0x424b('0x4d'))){_0x3c6ade[_0x424b('0x1e')]=qs['limit'](_0x2b39e0[_0x424b('0x31')][_0x424b('0x1e')]);_0x3c6ade['offset']=qs['offset'](_0x2b39e0[_0x424b('0x31')][_0x424b('0x1d')]);}return _0x80759b[_0x424b('0x4a')](_0x3c6ade);}})[_0x424b('0x25')](function(_0x3a8391){if(_0x3a8391){return _0x3a8391?{'count':_0x5a0ff9,'rows':_0x3a8391}:null;}})['then'](respondWithResult(_0x4e5eae,null))[_0x424b('0x44')](handleError(_0x4e5eae,null));};exports[_0x424b('0x4e')]=function(_0x211c81,_0x3d0a21,_0x241c28){if(_0x211c81[_0x424b('0x48')]['id']){delete _0x211c81['body']['id'];}return db['ZohoAccount'][_0x424b('0x4b')]({'where':{'id':_0x211c81[_0x424b('0x49')]['id']}})[_0x424b('0x25')](handleEntityNotFound(_0x3d0a21,null))[_0x424b('0x25')](function(_0x49cbc5){if(_0x49cbc5){_0x211c81['body'][_0x424b('0x4f')]=_0x49cbc5['id'];_0x211c81[_0x424b('0x48')]['Subjects']=integrations[_0x424b('0x50')](_0x211c81[_0x424b('0x48')]['channel'],_0x211c81[_0x424b('0x48')][_0x424b('0x3f')]);_0x211c81[_0x424b('0x48')]['Descriptions']=integrations[_0x424b('0x51')](_0x211c81[_0x424b('0x48')]['channel'],_0x211c81[_0x424b('0x48')][_0x424b('0x3f')]);return db[_0x424b('0x4c')][_0x424b('0x47')](_0x211c81[_0x424b('0x48')],{'include':[{'model':db['ZohoField'],'as':'Subjects'},{'model':db[_0x424b('0x52')],'as':'Descriptions'}]});}return null;})[_0x424b('0x25')](respondWithResult(_0x3d0a21,null))[_0x424b('0x44')](handleError(_0x3d0a21,null));};function getAccessToken(_0x43c5c5){return new Promise(function(_0x2e9a99,_0x251cb7){var _0x116924;switch(_0x43c5c5[_0x424b('0x53')]){case'US':_0x116924=_0x424b('0x54');break;case'AU':_0x116924=_0x424b('0x55');break;case'EU':_0x116924=_0x424b('0x56');break;case'IN':_0x116924=_0x424b('0x57');break;case'CN':_0x116924='https://accounts.zoho.com.cn';break;default:_0x116924=_0x424b('0x56');break;}var _0x2b8efd={'method':_0x424b('0x58'),'uri':_0x116924+_0x424b('0x59'),'qs':{'refresh_token':_0x43c5c5[_0x424b('0x5a')],'client_id':_0x43c5c5[_0x424b('0x5b')],'client_secret':_0x43c5c5[_0x424b('0x5c')],'grant_type':_0x424b('0x5d')},'json':!![]};return rp(_0x2b8efd)['then'](function(_0x254a27){if(_0x254a27[_0x424b('0x5e')]){_0x2e9a99(_0x254a27[_0x424b('0x5e')]);}else{_0x251cb7(_0x424b('0x5f'));}})['catch'](function(_0x52e1ec){_0x251cb7(_0x52e1ec);});});}exports[_0x424b('0x60')]=function(_0x495b52,_0x25117f,_0x42e3fa){var _0xd43b9d,_0x44a013,_0x2c64f9,_0xb4084b,_0x5f8b4a;return db['ZohoAccount']['findOne']({'where':{'id':_0x495b52[_0x424b('0x49')]['id']},'attributes':['id',_0x424b('0x61'),_0x424b('0x53'),_0x424b('0x5b'),'clientSecret','serverUrl','code','refreshToken']})['then'](handleEntityNotFound(_0x25117f,null))[_0x424b('0x25')](function(_0x377dad){_0x5f8b4a=_0x377dad;_0xd43b9d=_0x5f8b4a[_0x424b('0x61')];_0x44a013=_0xd43b9d===_0x424b('0x62')?'eu':_0x424b('0x63');_0x2c64f9=_0x5f8b4a[_0x424b('0x64')];var _0x46bd97=_0x2c64f9[_0x424b('0x65')](-0x1);if(_0x46bd97==='/'||_0x46bd97==='\x5c'||_0x46bd97===':'){_0x2c64f9=_0x2c64f9[_0x424b('0x66')](0x0,_0x2c64f9[_0x424b('0x67')](_0x46bd97));}if(!_0x5f8b4a[_0x424b('0x5a')]){throw new Error(_0x424b('0x68'));}return getAccessToken(_0x5f8b4a);})['then'](function(_0x460920){if(_0x460920){return rp({'method':_0x424b('0x69'),'uri':util['format'](_0x424b('0x6a'),_0x44a013),'qs':{'module':_0x424b('0x6b')},'headers':{'Authorization':_0x424b('0x6c')+_0x460920},'json':!![]});}})[_0x424b('0x25')](function(_0x48a7d5){if(_0x48a7d5){if(_0x48a7d5[_0x424b('0x6d')]&&_0x48a7d5[_0x424b('0x6d')][_0x424b('0x27')]){throw new Error(_0x48a7d5[_0x424b('0x6d')]['error'][_0x424b('0x6e')]);}var _0x365a3f=[];var _0x304713=[];var _0x762ba8=['Owner',_0x424b('0x6f'),_0x424b('0x70'),'WhoId',_0x424b('0x71'),_0x424b('0x72'),_0x424b('0x73'),'Call_Type'];_0x304713=_[_0x424b('0x74')](_0x48a7d5[_0x424b('0x36')],function(_0x2decc8){return _0x762ba8[_0x424b('0x75')](_0x2decc8[_0x424b('0x76')]);});for(var _0x3aa2f4=0x0;_0x3aa2f4<_0x304713[_0x424b('0x37')];_0x3aa2f4++){if(_0x304713[_0x3aa2f4][_0x424b('0x77')]===![]){_0x365a3f[_0x424b('0x78')]({'id':_0x304713[_0x3aa2f4]['id'],'name':_0x304713[_0x3aa2f4][_0x424b('0x79')],'custom':_0x304713[_0x3aa2f4]['custom_field'],'options':_0x304713[_0x3aa2f4][_0x424b('0x7a')]===_0x424b('0x7b')&&_0x304713[_0x3aa2f4][_0x424b('0x7c')][_0x424b('0x37')]>-0x1?_[_0x424b('0x2c')](_0x304713[_0x3aa2f4][_0x424b('0x7c')],function(_0x75692c){return{'name':_0x75692c,'value':_0x75692c};}):[]});}}return{'count':_0x365a3f[_0x424b('0x37')],'rows':_0x365a3f};}else{return{'count':0x0,'rows':[]};}})['then'](respondWithResult(_0x25117f,null))[_0x424b('0x44')](function(_0x12eb93){var _0x5e40d2=_0x495b52['query'][_0x424b('0x7d')]?0x1f4:_0x12eb93['statusCode']||0x1f4;logger['error'](_0x424b('0x7e'),_0x424b('0x60'),_0x5e40d2,JSON[_0x424b('0x7f')](_0x12eb93));delete _0x12eb93[_0x424b('0x29')];if(_0x5e40d2===0x191){_0x5e40d2=0x190;}_0x25117f[_0x424b('0x18')](_0x5e40d2)[_0x424b('0x2a')](_0x495b52[_0x424b('0x31')][_0x424b('0x7d')]?{'message':_0x424b('0x80'),'statusCode':_0x12eb93[_0x424b('0x81')]}:_0x12eb93);});};