828ddf77a6540d26100cfd8d189539f8c05fe924
[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 _0xc206=['pick_list_values','test','getFields,\x20%s,\x20%s,\x20%s','getFields','stringify','statusCode','eml-format','fast-json-patch','request-promise','moment','bluebird','mustache','util','fs-extra','lodash','squel','jsforce','desk.js','to-csv','querystring','papaparse','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../components/integrations/configuration','status','end','json','offset','limit','undefined','count','set','update','destroy','then','error','name','send','map','ZohoAccount','type','key','model','query','keys','filters','intersection','attributes','fields','length','hasOwnProperty','order','sort','where','pick','filter','merge','VIRTUAL','options','include','findAll','rows','catch','show','params','rawAttributes','includeAll','find','create','body','getConfigurations','findOne','addConfiguration','AccountId','Subjects','Descriptions','getDescriptions','channel','ZohoField','zone','https://accounts.zoho.com','https://accounts.zoho.eu','https://accounts.zoho.in','https://accounts.zoho.com.cn','POST','/oauth/v2/token','clientId','clientSecret','refresh_token','access_token','serverUrl','code','refreshToken','host','zoho.eu','slice','lastIndexOf','Refresh\x20Token\x20not\x20available\x20for\x20the\x20account','https://www.zohoapis.%s/crm/v2/settings/fields','Calls','Zoho-oauthtoken\x20','response','Owner','Subject','Description','Call_Duration','Call_Result','Call_Start_Time','Call_Type','api_name','system_mandatory','push','display_label','custom_field','picklist'];(function(_0x20a584,_0xe384a5){var _0x4b41a8=function(_0x2127c6){while(--_0x2127c6){_0x20a584['push'](_0x20a584['shift']());}};_0x4b41a8(++_0xe384a5);}(_0xc206,0x174));var _0x6c20=function(_0x3b1b23,_0x21bccd){_0x3b1b23=_0x3b1b23-0x0;var _0x1efc85=_0xc206[_0x3b1b23];return _0x1efc85;};'use strict';var emlformat=require(_0x6c20('0x0'));var rimraf=require('rimraf');var zipdir=require('zip-dir');var jsonpatch=require(_0x6c20('0x1'));var rp=require(_0x6c20('0x2'));var moment=require(_0x6c20('0x3'));var BPromise=require(_0x6c20('0x4'));var Mustache=require(_0x6c20('0x5'));var util=require(_0x6c20('0x6'));var path=require('path');var sox=require('sox');var csv=require('to-csv');var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0x6c20('0x7'));var _=require(_0x6c20('0x8'));var squel=require(_0x6c20('0x9'));var crypto=require('crypto');var jsforce=require(_0x6c20('0xa'));var deskjs=require(_0x6c20('0xb'));var toCsv=require(_0x6c20('0xc'));var querystring=require(_0x6c20('0xd'));var Papa=require(_0x6c20('0xe'));var Redis=require('ioredis');var authService=require(_0x6c20('0xf'));var qs=require(_0x6c20('0x10'));var as=require(_0x6c20('0x11'));var hardwareService=require(_0x6c20('0x12'));var logger=require(_0x6c20('0x13'))(_0x6c20('0x14'));var utils=require(_0x6c20('0x15'));var config=require(_0x6c20('0x16'));var licenseUtil=require(_0x6c20('0x17'));var db=require('../../mysqldb')['db'];var integrations=require(_0x6c20('0x18'));function respondWithStatusCode(_0x49aa0e,_0x40f61f){_0x40f61f=_0x40f61f||0xcc;return function(_0x5a0b82){if(_0x5a0b82){return _0x49aa0e['sendStatus'](_0x40f61f);}return _0x49aa0e[_0x6c20('0x19')](_0x40f61f)[_0x6c20('0x1a')]();};}function respondWithResult(_0x11c089,_0x25d8be){_0x25d8be=_0x25d8be||0xc8;return function(_0x2a9378){if(_0x2a9378){return _0x11c089[_0x6c20('0x19')](_0x25d8be)[_0x6c20('0x1b')](_0x2a9378);}};}function respondWithFilteredResult(_0x52ab90,_0x3c9f51){return function(_0x99126d){if(_0x99126d){var _0x2a9a48=typeof _0x3c9f51[_0x6c20('0x1c')]==='undefined'&&typeof _0x3c9f51[_0x6c20('0x1d')]===_0x6c20('0x1e');var _0x31ed4c=_0x99126d[_0x6c20('0x1f')];var _0xd59953=_0x2a9a48?0x0:_0x3c9f51[_0x6c20('0x1c')];var _0x377b88=_0x2a9a48?_0x99126d[_0x6c20('0x1f')]:_0x3c9f51['offset']+_0x3c9f51[_0x6c20('0x1d')];var _0xd0f11d;if(_0x377b88>=_0x31ed4c){_0x377b88=_0x31ed4c;_0xd0f11d=0xc8;}else{_0xd0f11d=0xce;}_0x52ab90[_0x6c20('0x19')](_0xd0f11d);return _0x52ab90[_0x6c20('0x20')]('Content-Range',_0xd59953+'-'+_0x377b88+'/'+_0x31ed4c)[_0x6c20('0x1b')](_0x99126d);}return null;};}function patchUpdates(_0x3deec2){return function(_0x115d5d){try{jsonpatch['apply'](_0x115d5d,_0x3deec2,!![]);}catch(_0x5ea27a){return BPromise['reject'](_0x5ea27a);}return _0x115d5d['save']();};}function saveUpdates(_0x3aa292,_0x23cba4){return function(_0x2e5b28){if(_0x2e5b28){return _0x2e5b28[_0x6c20('0x21')](_0x3aa292)['then'](function(_0x718119){return _0x718119;});}return null;};}function removeEntity(_0x3b92d6,_0x5a3d38){return function(_0x5531c8){if(_0x5531c8){return _0x5531c8[_0x6c20('0x22')]()[_0x6c20('0x23')](function(){_0x3b92d6[_0x6c20('0x19')](0xcc)[_0x6c20('0x1a')]();});}};}function handleEntityNotFound(_0x5b44b6,_0x462407){return function(_0x2760c8){if(!_0x2760c8){_0x5b44b6['sendStatus'](0x194);}return _0x2760c8;};}function handleError(_0x13f889,_0x513fa4){_0x513fa4=_0x513fa4||0x1f4;return function(_0x29fb12){logger[_0x6c20('0x24')](_0x29fb12['stack']);if(_0x29fb12[_0x6c20('0x25')]){delete _0x29fb12[_0x6c20('0x25')];}_0x13f889[_0x6c20('0x19')](_0x513fa4)[_0x6c20('0x26')](_0x29fb12);};}exports['index']=function(_0x104cbb,_0x5be8ad){var _0x1d3b59={},_0x26c0b6={},_0x362217={'count':0x0,'rows':[]};var _0x111d58=_[_0x6c20('0x27')](db[_0x6c20('0x28')]['rawAttributes'],function(_0x2e4ce8){return{'name':_0x2e4ce8['fieldName'],'type':_0x2e4ce8[_0x6c20('0x29')][_0x6c20('0x2a')]};});_0x26c0b6[_0x6c20('0x2b')]=_[_0x6c20('0x27')](_0x111d58,_0x6c20('0x25'));_0x26c0b6[_0x6c20('0x2c')]=_[_0x6c20('0x2d')](_0x104cbb[_0x6c20('0x2c')]);_0x26c0b6[_0x6c20('0x2e')]=_[_0x6c20('0x2f')](_0x26c0b6[_0x6c20('0x2b')],_0x26c0b6['query']);_0x1d3b59[_0x6c20('0x30')]=_['intersection'](_0x26c0b6['model'],qs[_0x6c20('0x31')](_0x104cbb[_0x6c20('0x2c')][_0x6c20('0x31')]));_0x1d3b59[_0x6c20('0x30')]=_0x1d3b59[_0x6c20('0x30')][_0x6c20('0x32')]?_0x1d3b59['attributes']:_0x26c0b6[_0x6c20('0x2b')];if(!_0x104cbb[_0x6c20('0x2c')][_0x6c20('0x33')]('nolimit')){_0x1d3b59[_0x6c20('0x1d')]=qs[_0x6c20('0x1d')](_0x104cbb['query'][_0x6c20('0x1d')]);_0x1d3b59[_0x6c20('0x1c')]=qs[_0x6c20('0x1c')](_0x104cbb[_0x6c20('0x2c')][_0x6c20('0x1c')]);}_0x1d3b59[_0x6c20('0x34')]=qs[_0x6c20('0x35')](_0x104cbb[_0x6c20('0x2c')][_0x6c20('0x35')]);_0x1d3b59[_0x6c20('0x36')]=qs['filters'](_[_0x6c20('0x37')](_0x104cbb[_0x6c20('0x2c')],_0x26c0b6[_0x6c20('0x2e')]),_0x111d58);if(_0x104cbb[_0x6c20('0x2c')][_0x6c20('0x38')]){_0x1d3b59['where']=_[_0x6c20('0x39')](_0x1d3b59[_0x6c20('0x36')],{'$or':_[_0x6c20('0x27')](_0x111d58,function(_0x44eb89){if(_0x44eb89[_0x6c20('0x29')]!==_0x6c20('0x3a')){var _0x27f80f={};_0x27f80f[_0x44eb89[_0x6c20('0x25')]]={'$like':'%'+_0x104cbb[_0x6c20('0x2c')]['filter']+'%'};return _0x27f80f;}})});}_0x1d3b59=_[_0x6c20('0x39')]({},_0x1d3b59,_0x104cbb[_0x6c20('0x3b')]);var _0x3577f6={'where':_0x1d3b59[_0x6c20('0x36')]};return db[_0x6c20('0x28')][_0x6c20('0x1f')](_0x3577f6)[_0x6c20('0x23')](function(_0x11ba59){_0x362217['count']=_0x11ba59;if(_0x104cbb[_0x6c20('0x2c')]['includeAll']){_0x1d3b59[_0x6c20('0x3c')]=[{'all':!![]}];}return db[_0x6c20('0x28')][_0x6c20('0x3d')](_0x1d3b59);})[_0x6c20('0x23')](function(_0x1b7940){_0x362217[_0x6c20('0x3e')]=_0x1b7940;return _0x362217;})['then'](respondWithFilteredResult(_0x5be8ad,_0x1d3b59))[_0x6c20('0x3f')](handleError(_0x5be8ad,null));};exports[_0x6c20('0x40')]=function(_0x20ce96,_0x272155){var _0x5d3e43={'raw':![],'where':{'id':_0x20ce96[_0x6c20('0x41')]['id']}},_0x1c3586={};_0x1c3586[_0x6c20('0x2b')]=_[_0x6c20('0x2d')](db[_0x6c20('0x28')][_0x6c20('0x42')]);_0x1c3586[_0x6c20('0x2c')]=_[_0x6c20('0x2d')](_0x20ce96[_0x6c20('0x2c')]);_0x1c3586[_0x6c20('0x2e')]=_[_0x6c20('0x2f')](_0x1c3586['model'],_0x1c3586[_0x6c20('0x2c')]);_0x5d3e43['attributes']=_['intersection'](_0x1c3586['model'],qs[_0x6c20('0x31')](_0x20ce96[_0x6c20('0x2c')][_0x6c20('0x31')]));_0x5d3e43[_0x6c20('0x30')]=_0x5d3e43[_0x6c20('0x30')][_0x6c20('0x32')]?_0x5d3e43[_0x6c20('0x30')]:_0x1c3586['model'];if(_0x20ce96[_0x6c20('0x2c')][_0x6c20('0x43')]){_0x5d3e43['include']=[{'all':!![]}];}_0x5d3e43=_[_0x6c20('0x39')]({},_0x5d3e43,_0x20ce96[_0x6c20('0x3b')]);return db['ZohoAccount'][_0x6c20('0x44')](_0x5d3e43)[_0x6c20('0x23')](handleEntityNotFound(_0x272155,null))[_0x6c20('0x23')](respondWithResult(_0x272155,null))[_0x6c20('0x3f')](handleError(_0x272155,null));};exports['create']=function(_0x160401,_0x205b3f){return db['ZohoAccount'][_0x6c20('0x45')](_0x160401[_0x6c20('0x46')],{})[_0x6c20('0x23')](respondWithResult(_0x205b3f,0xc9))['catch'](handleError(_0x205b3f,null));};exports['update']=function(_0x367cf4,_0x2e6299){if(_0x367cf4[_0x6c20('0x46')]['id']){delete _0x367cf4['body']['id'];}return db['ZohoAccount'][_0x6c20('0x44')]({'where':{'id':_0x367cf4[_0x6c20('0x41')]['id']}})['then'](handleEntityNotFound(_0x2e6299,null))['then'](saveUpdates(_0x367cf4['body'],null))[_0x6c20('0x23')](respondWithResult(_0x2e6299,null))[_0x6c20('0x3f')](handleError(_0x2e6299,null));};exports['destroy']=function(_0x3b655e,_0x4cc599){return db['ZohoAccount']['find']({'where':{'id':_0x3b655e[_0x6c20('0x41')]['id']}})[_0x6c20('0x23')](handleEntityNotFound(_0x4cc599,null))[_0x6c20('0x23')](removeEntity(_0x4cc599,null))[_0x6c20('0x3f')](handleError(_0x4cc599,null));};exports[_0x6c20('0x47')]=function(_0x1202d3,_0x193888,_0x2ac8e1){var _0x2ea08f={};var _0x39803e={};var _0x24d509;var _0x13a7d8;return db['ZohoAccount'][_0x6c20('0x48')]({'where':{'id':_0x1202d3[_0x6c20('0x41')]['id']}})[_0x6c20('0x23')](handleEntityNotFound(_0x193888,null))[_0x6c20('0x23')](function(_0x1557e1){if(_0x1557e1){_0x24d509=_0x1557e1;_0x39803e[_0x6c20('0x2b')]=_[_0x6c20('0x2d')](db['ZohoConfiguration'][_0x6c20('0x42')]);_0x39803e[_0x6c20('0x2c')]=_[_0x6c20('0x2d')](_0x1202d3['query']);_0x39803e[_0x6c20('0x2e')]=_[_0x6c20('0x2f')](_0x39803e[_0x6c20('0x2b')],_0x39803e[_0x6c20('0x2c')]);_0x2ea08f[_0x6c20('0x30')]=_[_0x6c20('0x2f')](_0x39803e[_0x6c20('0x2b')],qs[_0x6c20('0x31')](_0x1202d3[_0x6c20('0x2c')][_0x6c20('0x31')]));_0x2ea08f[_0x6c20('0x30')]=_0x2ea08f['attributes'][_0x6c20('0x32')]?_0x2ea08f['attributes']:_0x39803e[_0x6c20('0x2b')];_0x2ea08f[_0x6c20('0x34')]=qs[_0x6c20('0x35')](_0x1202d3[_0x6c20('0x2c')][_0x6c20('0x35')]);_0x2ea08f[_0x6c20('0x36')]=qs[_0x6c20('0x2e')](_['pick'](_0x1202d3[_0x6c20('0x2c')],_0x39803e[_0x6c20('0x2e')]));if(_0x1202d3[_0x6c20('0x2c')][_0x6c20('0x38')]){_0x2ea08f[_0x6c20('0x36')]=_[_0x6c20('0x39')](_0x2ea08f['where'],{'$or':_['map'](_0x2ea08f[_0x6c20('0x30')],function(_0x20b9c1){var _0x15a047={};_0x15a047[_0x20b9c1]={'$like':'%'+_0x1202d3[_0x6c20('0x2c')]['filter']+'%'};return _0x15a047;})});}_0x2ea08f=_[_0x6c20('0x39')]({},_0x2ea08f,_0x1202d3['options']);return _0x24d509['getConfigurations'](_0x2ea08f);}})[_0x6c20('0x23')](function(_0x40ea96){if(_0x40ea96){_0x13a7d8=_0x40ea96[_0x6c20('0x32')];if(!_0x1202d3['query'][_0x6c20('0x33')]('nolimit')){_0x2ea08f['limit']=qs[_0x6c20('0x1d')](_0x1202d3[_0x6c20('0x2c')][_0x6c20('0x1d')]);_0x2ea08f[_0x6c20('0x1c')]=qs[_0x6c20('0x1c')](_0x1202d3[_0x6c20('0x2c')]['offset']);}return _0x24d509[_0x6c20('0x47')](_0x2ea08f);}})[_0x6c20('0x23')](function(_0x4f62fd){if(_0x4f62fd){return _0x4f62fd?{'count':_0x13a7d8,'rows':_0x4f62fd}:null;}})[_0x6c20('0x23')](respondWithResult(_0x193888,null))[_0x6c20('0x3f')](handleError(_0x193888,null));};exports[_0x6c20('0x49')]=function(_0x301e89,_0x4f5f48,_0x2a5256){if(_0x301e89[_0x6c20('0x46')]['id']){delete _0x301e89['body']['id'];}return db['ZohoAccount']['findOne']({'where':{'id':_0x301e89[_0x6c20('0x41')]['id']}})[_0x6c20('0x23')](handleEntityNotFound(_0x4f5f48,null))[_0x6c20('0x23')](function(_0x457bf4){if(_0x457bf4){_0x301e89[_0x6c20('0x46')][_0x6c20('0x4a')]=_0x457bf4['id'];_0x301e89[_0x6c20('0x46')][_0x6c20('0x4b')]=integrations['getSubjects'](_0x301e89['body']['channel'],_0x301e89[_0x6c20('0x46')][_0x6c20('0x29')]);_0x301e89['body'][_0x6c20('0x4c')]=integrations[_0x6c20('0x4d')](_0x301e89[_0x6c20('0x46')][_0x6c20('0x4e')],_0x301e89[_0x6c20('0x46')]['type']);return db['ZohoConfiguration'][_0x6c20('0x45')](_0x301e89['body'],{'include':[{'model':db[_0x6c20('0x4f')],'as':'Subjects'},{'model':db[_0x6c20('0x4f')],'as':_0x6c20('0x4c')}]});}return null;})[_0x6c20('0x23')](respondWithResult(_0x4f5f48,null))[_0x6c20('0x3f')](handleError(_0x4f5f48,null));};function getAccessToken(_0x51a810){return new Promise(function(_0x47cbb8,_0x55f692){var _0x4c9783;switch(_0x51a810[_0x6c20('0x50')]){case'US':_0x4c9783=_0x6c20('0x51');break;case'AU':_0x4c9783='https://accounts.zoho.com.au';break;case'EU':_0x4c9783=_0x6c20('0x52');break;case'IN':_0x4c9783=_0x6c20('0x53');break;case'CN':_0x4c9783=_0x6c20('0x54');break;default:_0x4c9783=_0x6c20('0x52');break;}var _0x43a23d={'method':_0x6c20('0x55'),'uri':_0x4c9783+_0x6c20('0x56'),'qs':{'refresh_token':_0x51a810['refreshToken'],'client_id':_0x51a810[_0x6c20('0x57')],'client_secret':_0x51a810[_0x6c20('0x58')],'grant_type':_0x6c20('0x59')},'json':!![]};return rp(_0x43a23d)[_0x6c20('0x23')](function(_0x51d73e){if(_0x51d73e[_0x6c20('0x5a')]){_0x47cbb8(_0x51d73e[_0x6c20('0x5a')]);}else{_0x55f692('Unable\x20to\x20retrieve\x20the\x20access\x20token');}})[_0x6c20('0x3f')](function(_0x2f53df){_0x55f692(_0x2f53df);});});}exports['getFields']=function(_0x3efebf,_0x2d6233,_0x3a7f23){var _0x307025,_0x4055a1,_0x281066,_0x327c39,_0x350ccc;return db[_0x6c20('0x28')][_0x6c20('0x48')]({'where':{'id':_0x3efebf['params']['id']},'attributes':['id','host',_0x6c20('0x50'),'clientId',_0x6c20('0x58'),_0x6c20('0x5b'),_0x6c20('0x5c'),_0x6c20('0x5d')]})[_0x6c20('0x23')](handleEntityNotFound(_0x2d6233,null))['then'](function(_0x8d9b3e){_0x350ccc=_0x8d9b3e;_0x307025=_0x350ccc[_0x6c20('0x5e')];_0x4055a1=_0x307025===_0x6c20('0x5f')?'eu':'com';_0x281066=_0x350ccc[_0x6c20('0x5b')];var _0x1ce4dc=_0x281066[_0x6c20('0x60')](-0x1);if(_0x1ce4dc==='/'||_0x1ce4dc==='\x5c'||_0x1ce4dc===':'){_0x281066=_0x281066['substring'](0x0,_0x281066[_0x6c20('0x61')](_0x1ce4dc));}if(!_0x350ccc[_0x6c20('0x5d')]){throw new Error(_0x6c20('0x62'));}return getAccessToken(_0x350ccc);})[_0x6c20('0x23')](function(_0x38d632){if(_0x38d632){return rp({'method':'GET','uri':util['format'](_0x6c20('0x63'),_0x4055a1),'qs':{'module':_0x6c20('0x64')},'headers':{'Authorization':_0x6c20('0x65')+_0x38d632},'json':!![]});}})[_0x6c20('0x23')](function(_0xeaf326){if(_0xeaf326){if(_0xeaf326[_0x6c20('0x66')]&&_0xeaf326['response']['error']){throw new Error(_0xeaf326[_0x6c20('0x66')][_0x6c20('0x24')]['message']);}var _0x1efa24=[];var _0x24f104=[];var _0x3ca004=[_0x6c20('0x67'),_0x6c20('0x68'),_0x6c20('0x69'),'WhoId',_0x6c20('0x6a'),_0x6c20('0x6b'),_0x6c20('0x6c'),_0x6c20('0x6d')];_0x24f104=_['remove'](_0xeaf326['fields'],function(_0x47baf8){return _0x3ca004['includes'](_0x47baf8[_0x6c20('0x6e')]);});for(var _0x39b38f=0x0;_0x39b38f<_0x24f104[_0x6c20('0x32')];_0x39b38f++){if(_0x24f104[_0x39b38f][_0x6c20('0x6f')]===![]){_0x1efa24[_0x6c20('0x70')]({'id':_0x24f104[_0x39b38f]['id'],'name':_0x24f104[_0x39b38f][_0x6c20('0x71')],'custom':_0x24f104[_0x39b38f][_0x6c20('0x72')],'options':_0x24f104[_0x39b38f]['data_type']===_0x6c20('0x73')&&_0x24f104[_0x39b38f][_0x6c20('0x74')]['length']>-0x1?_[_0x6c20('0x27')](_0x24f104[_0x39b38f][_0x6c20('0x74')],function(_0xaa4558){return{'name':_0xaa4558,'value':_0xaa4558};}):[]});}}return{'count':_0x1efa24[_0x6c20('0x32')],'rows':_0x1efa24};}else{return{'count':0x0,'rows':[]};}})[_0x6c20('0x23')](respondWithResult(_0x2d6233,null))[_0x6c20('0x3f')](function(_0x243526){var _0x3d59c1=_0x3efebf[_0x6c20('0x2c')][_0x6c20('0x75')]?0x1f4:_0x243526['statusCode']||0x1f4;logger[_0x6c20('0x24')](_0x6c20('0x76'),_0x6c20('0x77'),_0x3d59c1,JSON[_0x6c20('0x78')](_0x243526));delete _0x243526[_0x6c20('0x25')];if(_0x3d59c1===0x191){_0x3d59c1=0x190;}_0x2d6233[_0x6c20('0x19')](_0x3d59c1)['send'](_0x3efebf[_0x6c20('0x2c')][_0x6c20('0x75')]?{'message':'Wrong\x20credentials','statusCode':_0x243526[_0x6c20('0x79')]}:_0x243526);});};