Built motion from commit b7e79837.|2.6.29
[motion2.git] / server / api / intZohoAccount / intZohoAccount.model.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 _0x0f7b=['serverUrl','clientId','clientSecret','code','host','slice','substring','lastIndexOf','https://accounts.zoho.com','https://accounts.zoho.com.au','https://accounts.zoho.in','https://accounts.zoho.eu','POST','authorization_code','application/x-www-form-urlencoded','parse','refreshToken','info','format','ZohoAccount,\x20Refresh\x20token\x20successfully\x20generated\x20for\x20account\x20\x22%s\x22\x20#%d','error','ZohoAccount,\x20Unable\x20to\x20generate\x20the\x20refresh\x20token\x20for\x20account\x20\x22%s\x22\x20#%d','name','catch','zone','https://accounts.zoho.com.cn','then','refresh_token','inspect','lodash','util','../../config/logger','moment','path','rimraf','../../config/environment','./intZohoAccount.attributes','../../components/integrations/configuration','exports','int_zoho_accounts','changed'];(function(_0x52121d,_0x1d1704){var _0x448627=function(_0x495bf0){while(--_0x495bf0){_0x52121d['push'](_0x52121d['shift']());}};_0x448627(++_0x1d1704);}(_0x0f7b,0x113));var _0xb0f7=function(_0x5cb1af,_0x4bdc8c){_0x5cb1af=_0x5cb1af-0x0;var _0x8d7940=_0x0f7b[_0x5cb1af];return _0x8d7940;};'use strict';var _=require(_0xb0f7('0x0'));var util=require(_0xb0f7('0x1'));var logger=require(_0xb0f7('0x2'))('api');var moment=require(_0xb0f7('0x3'));var BPromise=require('bluebird');var rp=require('request-promise');var fs=require('fs');var path=require(_0xb0f7('0x4'));var rimraf=require(_0xb0f7('0x5'));var config=require(_0xb0f7('0x6'));var attributes=require(_0xb0f7('0x7'));var integrations=require(_0xb0f7('0x8'));module[_0xb0f7('0x9')]=function(_0x2a2606,_0x13ea21){return _0x2a2606['define']('ZohoAccount',attributes,{'tableName':_0xb0f7('0xa'),'paranoid':![],'indexes':[],'timestamps':!![],'hooks':{'beforeCreate':function(_0x3a4246,_0x482f25,_0x515c34){if(_0x3a4246[_0xb0f7('0xb')]('zone')||_0x3a4246[_0xb0f7('0xb')](_0xb0f7('0xc'))||_0x3a4246[_0xb0f7('0xb')](_0xb0f7('0xd'))||_0x3a4246[_0xb0f7('0xb')](_0xb0f7('0xe'))||_0x3a4246[_0xb0f7('0xb')](_0xb0f7('0xf'))||_0x3a4246[_0xb0f7('0xb')](_0xb0f7('0x10'))){var _0x3060ea,_0x223578;_0x3060ea=_0x3a4246[_0xb0f7('0xc')];var _0x233f59=_0x3060ea[_0xb0f7('0x11')](-0x1);if(_0x233f59==='/'||_0x233f59==='\x5c'||_0x233f59===':'){_0x3060ea=_0x3060ea[_0xb0f7('0x12')](0x0,_0x3060ea[_0xb0f7('0x13')](_0x233f59));}switch(_0x3a4246['zone']){case'US':_0x223578=_0xb0f7('0x14');break;case'AU':_0x223578=_0xb0f7('0x15');break;case'IN':_0x223578=_0xb0f7('0x16');break;case'CN':_0x223578='https://accounts.zoho.com.cn';break;default:_0x223578=_0xb0f7('0x17');break;}var _0x4ac3c0={'method':_0xb0f7('0x18'),'uri':_0x223578+'/oauth/v2/token','form':{'grant_type':_0xb0f7('0x19'),'client_id':_0x3a4246[_0xb0f7('0xd')],'client_secret':_0x3a4246[_0xb0f7('0xe')],'redirect_uri':_0x3a4246['serverUrl'],'code':_0x3a4246[_0xb0f7('0xf')]},'headers':{'Content-Type':_0xb0f7('0x1a')}};return rp(_0x4ac3c0)['then'](function(_0x59da15){var _0x269500=JSON[_0xb0f7('0x1b')](_0x59da15);if(_0x269500['refresh_token']){_0x3a4246[_0xb0f7('0x1c')]=_0x269500['refresh_token'];logger[_0xb0f7('0x1d')](util[_0xb0f7('0x1e')](_0xb0f7('0x1f'),_0x3a4246['name'],_0x3a4246['id']));}else{logger[_0xb0f7('0x20')](util[_0xb0f7('0x1e')](_0xb0f7('0x21'),_0x3a4246[_0xb0f7('0x22')],_0x3a4246['id']),util['inspect'](_0x269500,{'showHidden':![],'depth':null}));}return _0x515c34(null,_0x482f25);})[_0xb0f7('0x23')](function(_0xc6235b){logger['error'](util[_0xb0f7('0x1e')](_0xb0f7('0x21'),_0x3a4246[_0xb0f7('0x22')],_0x3a4246['id']),util['inspect'](parsedResult,{'showHidden':![],'depth':null}));return _0x515c34(_0xc6235b);});}else{return _0x515c34(null,_0x482f25);}},'beforeUpdate':function(_0x4233b4,_0x755164,_0x5690e0){if(_0x4233b4[_0xb0f7('0xb')]('zone')||_0x4233b4[_0xb0f7('0xb')]('serverUrl')||_0x4233b4[_0xb0f7('0xb')](_0xb0f7('0xd'))||_0x4233b4['changed'](_0xb0f7('0xe'))||_0x4233b4['changed'](_0xb0f7('0xf'))||_0x4233b4[_0xb0f7('0xb')]('host')){var _0x10a9f0,_0x14270f;_0x10a9f0=_0x4233b4['serverUrl'];var _0x5a65e6=_0x10a9f0[_0xb0f7('0x11')](-0x1);if(_0x5a65e6==='/'||_0x5a65e6==='\x5c'||_0x5a65e6===':'){_0x10a9f0=_0x10a9f0['substring'](0x0,_0x10a9f0[_0xb0f7('0x13')](_0x5a65e6));}switch(_0x4233b4[_0xb0f7('0x24')]){case'US':_0x14270f=_0xb0f7('0x14');break;case'AU':_0x14270f=_0xb0f7('0x15');break;case'IN':_0x14270f=_0xb0f7('0x16');break;case'CN':_0x14270f=_0xb0f7('0x25');break;default:_0x14270f='https://accounts.zoho.eu';break;}var _0x5da5a3={'method':_0xb0f7('0x18'),'uri':_0x14270f+'/oauth/v2/token','form':{'grant_type':_0xb0f7('0x19'),'client_id':_0x4233b4[_0xb0f7('0xd')],'client_secret':_0x4233b4[_0xb0f7('0xe')],'redirect_uri':_0x4233b4['serverUrl'],'code':_0x4233b4[_0xb0f7('0xf')]},'headers':{'Content-Type':_0xb0f7('0x1a')}};return rp(_0x5da5a3)[_0xb0f7('0x26')](function(_0x454289){var _0x3a7ac9=JSON['parse'](_0x454289);if(_0x3a7ac9['refresh_token']){_0x4233b4['refreshToken']=_0x3a7ac9[_0xb0f7('0x27')];logger[_0xb0f7('0x1d')](util['format']('ZohoAccount,\x20Refresh\x20token\x20successfully\x20generated\x20for\x20account\x20\x22%s\x22\x20#%d',_0x4233b4[_0xb0f7('0x22')],_0x4233b4['id']));}else{logger[_0xb0f7('0x20')](util['format'](_0xb0f7('0x21'),_0x4233b4['name'],_0x4233b4['id']),util[_0xb0f7('0x28')](_0x3a7ac9,{'showHidden':![],'depth':null}));}return _0x5690e0(null,_0x755164);})[_0xb0f7('0x23')](function(_0x4c2e48){logger[_0xb0f7('0x20')](util['format']('ZohoAccount,\x20Unable\x20to\x20generate\x20the\x20refresh\x20token\x20for\x20account\x20\x22%s\x22\x20#%d',_0x4233b4['name'],_0x4233b4['id']),util[_0xb0f7('0x28')](parsedResult,{'showHidden':![],'depth':null}));return _0x5690e0(_0x4c2e48);});}else{return _0x5690e0(null,_0x755164);}}}});};