Built motion from commit ce03bc3d.|2.5.39
[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 _0x8bb8=['includeAll','options','find','create','body','getConfigurations','findOne','addConfiguration','AccountId','Subjects','Descriptions','getDescriptions','ZohoConfiguration','zone','https://accounts.zoho.com','https://accounts.zoho.com.au','https://accounts.zoho.in','https://accounts.zoho.eu','/oauth/v2/token','refreshToken','clientId','refresh_token','access_token','Unable\x20to\x20retrieve\x20the\x20access\x20token','getFields','clientSecret','serverUrl','code','host','com','slice','substring','lastIndexOf','Refresh\x20Token\x20not\x20available\x20for\x20the\x20account','format','https://www.zohoapis.%s/crm/v2/settings/fields','Zoho-oauthtoken\x20','response','message','Owner','Subject','Description','WhoId','Call_Result','Call_Start_Time','remove','includes','system_mandatory','push','display_label','data_type','pick_list_values','test','statusCode','getFields,\x20%s,\x20%s,\x20%s','Wrong\x20credentials','eml-format','rimraf','zip-dir','fast-json-patch','bluebird','path','sox','ejs','fs-extra','squel','crypto','desk.js','querystring','papaparse','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/license/util','../../mysqldb','../../components/integrations/configuration','sendStatus','status','end','json','offset','undefined','count','set','Content-Range','apply','reject','save','update','then','destroy','error','stack','name','send','map','rawAttributes','type','key','query','keys','intersection','model','fields','attributes','length','hasOwnProperty','nolimit','limit','order','sort','where','filters','pick','filter','merge','VIRTUAL','ZohoAccount','include','rows','catch','params'];(function(_0x112435,_0x28583c){var _0x9c492b=function(_0x289f3f){while(--_0x289f3f){_0x112435['push'](_0x112435['shift']());}};_0x9c492b(++_0x28583c);}(_0x8bb8,0xb5));var _0x88bb=function(_0xb3f627,_0x13268e){_0xb3f627=_0xb3f627-0x0;var _0x1be684=_0x8bb8[_0xb3f627];return _0x1be684;};'use strict';var emlformat=require(_0x88bb('0x0'));var rimraf=require(_0x88bb('0x1'));var zipdir=require(_0x88bb('0x2'));var jsonpatch=require(_0x88bb('0x3'));var rp=require('request-promise');var moment=require('moment');var BPromise=require(_0x88bb('0x4'));var Mustache=require('mustache');var util=require('util');var path=require(_0x88bb('0x5'));var sox=require(_0x88bb('0x6'));var csv=require('to-csv');var ejs=require(_0x88bb('0x7'));var fs=require('fs');var fs_extra=require(_0x88bb('0x8'));var _=require('lodash');var squel=require(_0x88bb('0x9'));var crypto=require(_0x88bb('0xa'));var jsforce=require('jsforce');var deskjs=require(_0x88bb('0xb'));var toCsv=require('to-csv');var querystring=require(_0x88bb('0xc'));var Papa=require(_0x88bb('0xd'));var Redis=require('ioredis');var authService=require(_0x88bb('0xe'));var qs=require(_0x88bb('0xf'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0x88bb('0x10'));var logger=require(_0x88bb('0x11'))(_0x88bb('0x12'));var utils=require(_0x88bb('0x13'));var config=require('../../config/environment');var licenseUtil=require(_0x88bb('0x14'));var db=require(_0x88bb('0x15'))['db'];var integrations=require(_0x88bb('0x16'));function respondWithStatusCode(_0x5657a0,_0x5f230e){_0x5f230e=_0x5f230e||0xcc;return function(_0x5a1cd4){if(_0x5a1cd4){return _0x5657a0[_0x88bb('0x17')](_0x5f230e);}return _0x5657a0[_0x88bb('0x18')](_0x5f230e)[_0x88bb('0x19')]();};}function respondWithResult(_0x3e3858,_0x3de592){_0x3de592=_0x3de592||0xc8;return function(_0x189fd0){if(_0x189fd0){return _0x3e3858[_0x88bb('0x18')](_0x3de592)[_0x88bb('0x1a')](_0x189fd0);}};}function respondWithFilteredResult(_0x3edc09,_0x50319c){return function(_0x897f7e){if(_0x897f7e){var _0x2b1459=typeof _0x50319c[_0x88bb('0x1b')]===_0x88bb('0x1c')&&typeof _0x50319c['limit']==='undefined';var _0x1d4dc=_0x897f7e[_0x88bb('0x1d')];var _0x100b0d=_0x2b1459?0x0:_0x50319c[_0x88bb('0x1b')];var _0x576922=_0x2b1459?_0x897f7e[_0x88bb('0x1d')]:_0x50319c[_0x88bb('0x1b')]+_0x50319c['limit'];var _0xa88ff3;if(_0x576922>=_0x1d4dc){_0x576922=_0x1d4dc;_0xa88ff3=0xc8;}else{_0xa88ff3=0xce;}_0x3edc09['status'](_0xa88ff3);return _0x3edc09[_0x88bb('0x1e')](_0x88bb('0x1f'),_0x100b0d+'-'+_0x576922+'/'+_0x1d4dc)[_0x88bb('0x1a')](_0x897f7e);}return null;};}function patchUpdates(_0x36d807){return function(_0x2a998e){try{jsonpatch[_0x88bb('0x20')](_0x2a998e,_0x36d807,!![]);}catch(_0x159ccd){return BPromise[_0x88bb('0x21')](_0x159ccd);}return _0x2a998e[_0x88bb('0x22')]();};}function saveUpdates(_0x58d76d,_0x3be49c){return function(_0x33fcd5){if(_0x33fcd5){return _0x33fcd5[_0x88bb('0x23')](_0x58d76d)[_0x88bb('0x24')](function(_0x29b2f0){return _0x29b2f0;});}return null;};}function removeEntity(_0x5b3ec4,_0x44b416){return function(_0x1ddbe7){if(_0x1ddbe7){return _0x1ddbe7[_0x88bb('0x25')]()[_0x88bb('0x24')](function(){_0x5b3ec4[_0x88bb('0x18')](0xcc)[_0x88bb('0x19')]();});}};}function handleEntityNotFound(_0x27a1cc,_0x2a0f8e){return function(_0x4209ef){if(!_0x4209ef){_0x27a1cc[_0x88bb('0x17')](0x194);}return _0x4209ef;};}function handleError(_0x358d24,_0x22f16a){_0x22f16a=_0x22f16a||0x1f4;return function(_0x6e0945){logger[_0x88bb('0x26')](_0x6e0945[_0x88bb('0x27')]);if(_0x6e0945[_0x88bb('0x28')]){delete _0x6e0945[_0x88bb('0x28')];}_0x358d24[_0x88bb('0x18')](_0x22f16a)[_0x88bb('0x29')](_0x6e0945);};}exports['index']=function(_0x296300,_0x108369){var _0x5dd528={},_0x1ae50e={},_0xff2df2={'count':0x0,'rows':[]};var _0x473281=_[_0x88bb('0x2a')](db['ZohoAccount'][_0x88bb('0x2b')],function(_0x45dcfd){return{'name':_0x45dcfd['fieldName'],'type':_0x45dcfd[_0x88bb('0x2c')][_0x88bb('0x2d')]};});_0x1ae50e['model']=_[_0x88bb('0x2a')](_0x473281,_0x88bb('0x28'));_0x1ae50e[_0x88bb('0x2e')]=_[_0x88bb('0x2f')](_0x296300['query']);_0x1ae50e['filters']=_[_0x88bb('0x30')](_0x1ae50e[_0x88bb('0x31')],_0x1ae50e[_0x88bb('0x2e')]);_0x5dd528['attributes']=_['intersection'](_0x1ae50e[_0x88bb('0x31')],qs[_0x88bb('0x32')](_0x296300[_0x88bb('0x2e')][_0x88bb('0x32')]));_0x5dd528[_0x88bb('0x33')]=_0x5dd528['attributes'][_0x88bb('0x34')]?_0x5dd528[_0x88bb('0x33')]:_0x1ae50e['model'];if(!_0x296300[_0x88bb('0x2e')][_0x88bb('0x35')](_0x88bb('0x36'))){_0x5dd528[_0x88bb('0x37')]=qs['limit'](_0x296300['query'][_0x88bb('0x37')]);_0x5dd528['offset']=qs[_0x88bb('0x1b')](_0x296300[_0x88bb('0x2e')]['offset']);}_0x5dd528[_0x88bb('0x38')]=qs['sort'](_0x296300[_0x88bb('0x2e')][_0x88bb('0x39')]);_0x5dd528[_0x88bb('0x3a')]=qs[_0x88bb('0x3b')](_[_0x88bb('0x3c')](_0x296300[_0x88bb('0x2e')],_0x1ae50e[_0x88bb('0x3b')]),_0x473281);if(_0x296300[_0x88bb('0x2e')][_0x88bb('0x3d')]){_0x5dd528[_0x88bb('0x3a')]=_[_0x88bb('0x3e')](_0x5dd528['where'],{'$or':_['map'](_0x473281,function(_0x53589d){if(_0x53589d[_0x88bb('0x2c')]!==_0x88bb('0x3f')){var _0x5b2dc3={};_0x5b2dc3[_0x53589d[_0x88bb('0x28')]]={'$like':'%'+_0x296300[_0x88bb('0x2e')][_0x88bb('0x3d')]+'%'};return _0x5b2dc3;}})});}_0x5dd528=_[_0x88bb('0x3e')]({},_0x5dd528,_0x296300['options']);var _0x206adb={'where':_0x5dd528[_0x88bb('0x3a')]};return db[_0x88bb('0x40')][_0x88bb('0x1d')](_0x206adb)[_0x88bb('0x24')](function(_0x4bd81e){_0xff2df2['count']=_0x4bd81e;if(_0x296300['query']['includeAll']){_0x5dd528[_0x88bb('0x41')]=[{'all':!![]}];}return db[_0x88bb('0x40')]['findAll'](_0x5dd528);})[_0x88bb('0x24')](function(_0x57fb27){_0xff2df2[_0x88bb('0x42')]=_0x57fb27;return _0xff2df2;})[_0x88bb('0x24')](respondWithFilteredResult(_0x108369,_0x5dd528))[_0x88bb('0x43')](handleError(_0x108369,null));};exports['show']=function(_0x4541b7,_0x342740){var _0x3312ed={'raw':![],'where':{'id':_0x4541b7[_0x88bb('0x44')]['id']}},_0x59ad2d={};_0x59ad2d[_0x88bb('0x31')]=_[_0x88bb('0x2f')](db[_0x88bb('0x40')][_0x88bb('0x2b')]);_0x59ad2d[_0x88bb('0x2e')]=_[_0x88bb('0x2f')](_0x4541b7[_0x88bb('0x2e')]);_0x59ad2d['filters']=_[_0x88bb('0x30')](_0x59ad2d['model'],_0x59ad2d[_0x88bb('0x2e')]);_0x3312ed['attributes']=_[_0x88bb('0x30')](_0x59ad2d[_0x88bb('0x31')],qs[_0x88bb('0x32')](_0x4541b7[_0x88bb('0x2e')][_0x88bb('0x32')]));_0x3312ed[_0x88bb('0x33')]=_0x3312ed[_0x88bb('0x33')][_0x88bb('0x34')]?_0x3312ed['attributes']:_0x59ad2d[_0x88bb('0x31')];if(_0x4541b7[_0x88bb('0x2e')][_0x88bb('0x45')]){_0x3312ed[_0x88bb('0x41')]=[{'all':!![]}];}_0x3312ed=_['merge']({},_0x3312ed,_0x4541b7[_0x88bb('0x46')]);return db[_0x88bb('0x40')][_0x88bb('0x47')](_0x3312ed)['then'](handleEntityNotFound(_0x342740,null))['then'](respondWithResult(_0x342740,null))['catch'](handleError(_0x342740,null));};exports['create']=function(_0x20510d,_0x5b3d98){return db[_0x88bb('0x40')][_0x88bb('0x48')](_0x20510d[_0x88bb('0x49')],{})[_0x88bb('0x24')](respondWithResult(_0x5b3d98,0xc9))['catch'](handleError(_0x5b3d98,null));};exports[_0x88bb('0x23')]=function(_0x252480,_0x609726){if(_0x252480['body']['id']){delete _0x252480[_0x88bb('0x49')]['id'];}return db[_0x88bb('0x40')]['find']({'where':{'id':_0x252480[_0x88bb('0x44')]['id']}})['then'](handleEntityNotFound(_0x609726,null))[_0x88bb('0x24')](saveUpdates(_0x252480[_0x88bb('0x49')],null))['then'](respondWithResult(_0x609726,null))[_0x88bb('0x43')](handleError(_0x609726,null));};exports[_0x88bb('0x25')]=function(_0x1051a1,_0xb78e2d){return db[_0x88bb('0x40')][_0x88bb('0x47')]({'where':{'id':_0x1051a1[_0x88bb('0x44')]['id']}})[_0x88bb('0x24')](handleEntityNotFound(_0xb78e2d,null))[_0x88bb('0x24')](removeEntity(_0xb78e2d,null))[_0x88bb('0x43')](handleError(_0xb78e2d,null));};exports[_0x88bb('0x4a')]=function(_0x15c9bb,_0x1809f2,_0x3ced01){var _0x50c73b={};var _0x51e95c={};var _0x274761;var _0xd98f86;return db[_0x88bb('0x40')][_0x88bb('0x4b')]({'where':{'id':_0x15c9bb[_0x88bb('0x44')]['id']}})[_0x88bb('0x24')](handleEntityNotFound(_0x1809f2,null))[_0x88bb('0x24')](function(_0x1039cc){if(_0x1039cc){_0x274761=_0x1039cc;_0x51e95c[_0x88bb('0x31')]=_[_0x88bb('0x2f')](db['ZohoConfiguration'][_0x88bb('0x2b')]);_0x51e95c[_0x88bb('0x2e')]=_[_0x88bb('0x2f')](_0x15c9bb[_0x88bb('0x2e')]);_0x51e95c['filters']=_[_0x88bb('0x30')](_0x51e95c[_0x88bb('0x31')],_0x51e95c['query']);_0x50c73b[_0x88bb('0x33')]=_[_0x88bb('0x30')](_0x51e95c[_0x88bb('0x31')],qs[_0x88bb('0x32')](_0x15c9bb[_0x88bb('0x2e')]['fields']));_0x50c73b[_0x88bb('0x33')]=_0x50c73b[_0x88bb('0x33')][_0x88bb('0x34')]?_0x50c73b[_0x88bb('0x33')]:_0x51e95c[_0x88bb('0x31')];_0x50c73b['order']=qs[_0x88bb('0x39')](_0x15c9bb[_0x88bb('0x2e')][_0x88bb('0x39')]);_0x50c73b[_0x88bb('0x3a')]=qs['filters'](_[_0x88bb('0x3c')](_0x15c9bb[_0x88bb('0x2e')],_0x51e95c[_0x88bb('0x3b')]));if(_0x15c9bb[_0x88bb('0x2e')][_0x88bb('0x3d')]){_0x50c73b[_0x88bb('0x3a')]=_[_0x88bb('0x3e')](_0x50c73b[_0x88bb('0x3a')],{'$or':_[_0x88bb('0x2a')](_0x50c73b[_0x88bb('0x33')],function(_0x1988b1){var _0x19e4ef={};_0x19e4ef[_0x1988b1]={'$like':'%'+_0x15c9bb[_0x88bb('0x2e')]['filter']+'%'};return _0x19e4ef;})});}_0x50c73b=_['merge']({},_0x50c73b,_0x15c9bb[_0x88bb('0x46')]);return _0x274761[_0x88bb('0x4a')](_0x50c73b);}})[_0x88bb('0x24')](function(_0x20e34d){if(_0x20e34d){_0xd98f86=_0x20e34d['length'];if(!_0x15c9bb[_0x88bb('0x2e')][_0x88bb('0x35')](_0x88bb('0x36'))){_0x50c73b[_0x88bb('0x37')]=qs[_0x88bb('0x37')](_0x15c9bb[_0x88bb('0x2e')][_0x88bb('0x37')]);_0x50c73b[_0x88bb('0x1b')]=qs['offset'](_0x15c9bb[_0x88bb('0x2e')][_0x88bb('0x1b')]);}return _0x274761[_0x88bb('0x4a')](_0x50c73b);}})[_0x88bb('0x24')](function(_0x514327){if(_0x514327){return _0x514327?{'count':_0xd98f86,'rows':_0x514327}:null;}})[_0x88bb('0x24')](respondWithResult(_0x1809f2,null))['catch'](handleError(_0x1809f2,null));};exports[_0x88bb('0x4c')]=function(_0x13e83a,_0x58af7e,_0x2967b2){if(_0x13e83a[_0x88bb('0x49')]['id']){delete _0x13e83a['body']['id'];}return db[_0x88bb('0x40')][_0x88bb('0x4b')]({'where':{'id':_0x13e83a[_0x88bb('0x44')]['id']}})['then'](handleEntityNotFound(_0x58af7e,null))[_0x88bb('0x24')](function(_0x3f6d8a){if(_0x3f6d8a){_0x13e83a[_0x88bb('0x49')][_0x88bb('0x4d')]=_0x3f6d8a['id'];_0x13e83a[_0x88bb('0x49')][_0x88bb('0x4e')]=integrations['getSubjects'](_0x13e83a[_0x88bb('0x49')]['channel'],_0x13e83a['body'][_0x88bb('0x2c')]);_0x13e83a['body'][_0x88bb('0x4f')]=integrations[_0x88bb('0x50')](_0x13e83a[_0x88bb('0x49')]['channel'],_0x13e83a['body'][_0x88bb('0x2c')]);return db[_0x88bb('0x51')]['create'](_0x13e83a[_0x88bb('0x49')],{'include':[{'model':db['ZohoField'],'as':_0x88bb('0x4e')},{'model':db['ZohoField'],'as':_0x88bb('0x4f')}]});}return null;})[_0x88bb('0x24')](respondWithResult(_0x58af7e,null))[_0x88bb('0x43')](handleError(_0x58af7e,null));};function getAccessToken(_0x47ef1b){return new Promise(function(_0x1b5774,_0x16a884){var _0x9c2c69;switch(_0x47ef1b[_0x88bb('0x52')]){case'US':_0x9c2c69=_0x88bb('0x53');break;case'AU':_0x9c2c69=_0x88bb('0x54');break;case'EU':_0x9c2c69='https://accounts.zoho.eu';break;case'IN':_0x9c2c69=_0x88bb('0x55');break;case'CN':_0x9c2c69='https://accounts.zoho.com.cn';break;default:_0x9c2c69=_0x88bb('0x56');break;}var _0x27d902={'method':'POST','uri':_0x9c2c69+_0x88bb('0x57'),'qs':{'refresh_token':_0x47ef1b[_0x88bb('0x58')],'client_id':_0x47ef1b[_0x88bb('0x59')],'client_secret':_0x47ef1b['clientSecret'],'grant_type':_0x88bb('0x5a')},'json':!![]};return rp(_0x27d902)['then'](function(_0x19e583){if(_0x19e583[_0x88bb('0x5b')]){_0x1b5774(_0x19e583['access_token']);}else{_0x16a884(_0x88bb('0x5c'));}})[_0x88bb('0x43')](function(_0x439ec3){_0x16a884(_0x439ec3);});});}exports[_0x88bb('0x5d')]=function(_0x1480c8,_0x2c00c6,_0x448b94){var _0x323e0c,_0x14f410,_0x1cc40c,_0x4ab944,_0x47a36c;return db['ZohoAccount'][_0x88bb('0x4b')]({'where':{'id':_0x1480c8[_0x88bb('0x44')]['id']},'attributes':['id','host','zone',_0x88bb('0x59'),_0x88bb('0x5e'),_0x88bb('0x5f'),_0x88bb('0x60'),'refreshToken']})[_0x88bb('0x24')](handleEntityNotFound(_0x2c00c6,null))[_0x88bb('0x24')](function(_0x47d3bf){_0x47a36c=_0x47d3bf;_0x323e0c=_0x47a36c[_0x88bb('0x61')];_0x14f410=_0x323e0c==='zoho.eu'?'eu':_0x88bb('0x62');_0x1cc40c=_0x47a36c['serverUrl'];var _0x26b193=_0x1cc40c[_0x88bb('0x63')](-0x1);if(_0x26b193==='/'||_0x26b193==='\x5c'||_0x26b193===':'){_0x1cc40c=_0x1cc40c[_0x88bb('0x64')](0x0,_0x1cc40c[_0x88bb('0x65')](_0x26b193));}if(!_0x47a36c[_0x88bb('0x58')]){throw new Error(_0x88bb('0x66'));}return getAccessToken(_0x47a36c);})[_0x88bb('0x24')](function(_0x1d10a2){if(_0x1d10a2){return rp({'method':'GET','uri':util[_0x88bb('0x67')](_0x88bb('0x68'),_0x14f410),'qs':{'module':'Calls'},'headers':{'Authorization':_0x88bb('0x69')+_0x1d10a2},'json':!![]});}})[_0x88bb('0x24')](function(_0x3321e9){if(_0x3321e9){if(_0x3321e9[_0x88bb('0x6a')]&&_0x3321e9['response']['error']){throw new Error(_0x3321e9[_0x88bb('0x6a')]['error'][_0x88bb('0x6b')]);}var _0x589659=[];var _0x346741=[];var _0x300135=[_0x88bb('0x6c'),_0x88bb('0x6d'),_0x88bb('0x6e'),_0x88bb('0x6f'),'Call_Duration',_0x88bb('0x70'),_0x88bb('0x71'),'Call_Type'];_0x346741=_[_0x88bb('0x72')](_0x3321e9[_0x88bb('0x32')],function(_0x19d445){return _0x300135[_0x88bb('0x73')](_0x19d445['api_name']);});for(var _0x18b367=0x0;_0x18b367<_0x346741[_0x88bb('0x34')];_0x18b367++){if(_0x346741[_0x18b367][_0x88bb('0x74')]===![]){_0x589659[_0x88bb('0x75')]({'id':_0x346741[_0x18b367]['id'],'name':_0x346741[_0x18b367][_0x88bb('0x76')],'custom':_0x346741[_0x18b367]['custom_field'],'options':_0x346741[_0x18b367][_0x88bb('0x77')]==='picklist'&&_0x346741[_0x18b367][_0x88bb('0x78')][_0x88bb('0x34')]>-0x1?_[_0x88bb('0x2a')](_0x346741[_0x18b367][_0x88bb('0x78')],function(_0x17976e){return{'name':_0x17976e,'value':_0x17976e};}):[]});}}return{'count':_0x589659[_0x88bb('0x34')],'rows':_0x589659};}else{return{'count':0x0,'rows':[]};}})[_0x88bb('0x24')](respondWithResult(_0x2c00c6,null))[_0x88bb('0x43')](function(_0x1c423d){var _0x203801=_0x1480c8[_0x88bb('0x2e')][_0x88bb('0x79')]?0x1f4:_0x1c423d[_0x88bb('0x7a')]||0x1f4;logger[_0x88bb('0x26')](_0x88bb('0x7b'),'getFields',_0x203801,JSON['stringify'](_0x1c423d));delete _0x1c423d[_0x88bb('0x28')];if(_0x203801===0x191){_0x203801=0x190;}_0x2c00c6[_0x88bb('0x18')](_0x203801)[_0x88bb('0x29')](_0x1480c8[_0x88bb('0x2e')][_0x88bb('0x79')]?{'message':_0x88bb('0x7c'),'statusCode':_0x1c423d[_0x88bb('0x7a')]}:_0x1c423d);});};