Built motion from commit e6806ed6.|2.6.0
[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 _0x09cb=['api','moment','bluebird','request-promise','path','../../components/integrations/configuration','int_zoho_accounts','changed','zone','serverUrl','clientId','code','substring','lastIndexOf','https://accounts.zoho.com.au','https://accounts.zoho.in','https://accounts.zoho.eu','POST','/oauth/v2/token','authorization_code','application/x-www-form-urlencoded','then','parse','refreshToken','refresh_token','format','ZohoAccount,\x20Refresh\x20token\x20successfully\x20generated\x20for\x20account\x20\x22%s\x22\x20#%d','error','name','inspect','ZohoAccount,\x20Unable\x20to\x20generate\x20the\x20refresh\x20token\x20for\x20account\x20\x22%s\x22\x20#%d','clientSecret','host','slice','info','catch','lodash'];(function(_0x399ad9,_0x496ab5){var _0x23e9b4=function(_0x99d67d){while(--_0x99d67d){_0x399ad9['push'](_0x399ad9['shift']());}};_0x23e9b4(++_0x496ab5);}(_0x09cb,0xdd));var _0xb09c=function(_0x568b83,_0x41e4c0){_0x568b83=_0x568b83-0x0;var _0x116189=_0x09cb[_0x568b83];return _0x116189;};'use strict';var _=require(_0xb09c('0x0'));var util=require('util');var logger=require('../../config/logger')(_0xb09c('0x1'));var moment=require(_0xb09c('0x2'));var BPromise=require(_0xb09c('0x3'));var rp=require(_0xb09c('0x4'));var fs=require('fs');var path=require(_0xb09c('0x5'));var rimraf=require('rimraf');var config=require('../../config/environment');var attributes=require('./intZohoAccount.attributes');var integrations=require(_0xb09c('0x6'));module['exports']=function(_0x3342e1,_0x249b28){return _0x3342e1['define']('ZohoAccount',attributes,{'tableName':_0xb09c('0x7'),'paranoid':![],'indexes':[],'timestamps':!![],'hooks':{'beforeCreate':function(_0x490a92,_0x4c2e64,_0x453d73){if(_0x490a92[_0xb09c('0x8')](_0xb09c('0x9'))||_0x490a92[_0xb09c('0x8')](_0xb09c('0xa'))||_0x490a92[_0xb09c('0x8')](_0xb09c('0xb'))||_0x490a92[_0xb09c('0x8')]('clientSecret')||_0x490a92[_0xb09c('0x8')](_0xb09c('0xc'))||_0x490a92['changed']('host')){var _0x2905b8,_0x1e82a1;_0x2905b8=_0x490a92[_0xb09c('0xa')];var _0x2c11b4=_0x2905b8['slice'](-0x1);if(_0x2c11b4==='/'||_0x2c11b4==='\x5c'||_0x2c11b4===':'){_0x2905b8=_0x2905b8[_0xb09c('0xd')](0x0,_0x2905b8[_0xb09c('0xe')](_0x2c11b4));}switch(_0x490a92[_0xb09c('0x9')]){case'US':_0x1e82a1='https://accounts.zoho.com';break;case'AU':_0x1e82a1=_0xb09c('0xf');break;case'IN':_0x1e82a1=_0xb09c('0x10');break;case'CN':_0x1e82a1='https://accounts.zoho.com.cn';break;default:_0x1e82a1=_0xb09c('0x11');break;}var _0x25c067={'method':_0xb09c('0x12'),'uri':_0x1e82a1+_0xb09c('0x13'),'form':{'grant_type':_0xb09c('0x14'),'client_id':_0x490a92[_0xb09c('0xb')],'client_secret':_0x490a92['clientSecret'],'redirect_uri':_0x490a92[_0xb09c('0xa')],'code':_0x490a92['code']},'headers':{'Content-Type':_0xb09c('0x15')}};return rp(_0x25c067)[_0xb09c('0x16')](function(_0x294b19){var _0x12a900=JSON[_0xb09c('0x17')](_0x294b19);if(_0x12a900['refresh_token']){_0x490a92[_0xb09c('0x18')]=_0x12a900[_0xb09c('0x19')];logger['info'](util[_0xb09c('0x1a')](_0xb09c('0x1b'),_0x490a92['name'],_0x490a92['id']));}else{logger[_0xb09c('0x1c')](util[_0xb09c('0x1a')]('ZohoAccount,\x20Unable\x20to\x20generate\x20the\x20refresh\x20token\x20for\x20account\x20\x22%s\x22\x20#%d',_0x490a92[_0xb09c('0x1d')],_0x490a92['id']),util[_0xb09c('0x1e')](_0x12a900,{'showHidden':![],'depth':null}));}return _0x453d73(null,_0x4c2e64);})['catch'](function(_0x1ca864){logger[_0xb09c('0x1c')](util[_0xb09c('0x1a')](_0xb09c('0x1f'),_0x490a92['name'],_0x490a92['id']),util[_0xb09c('0x1e')](parsedResult,{'showHidden':![],'depth':null}));return _0x453d73(_0x1ca864);});}else{return _0x453d73(null,_0x4c2e64);}},'beforeUpdate':function(_0x1330c0,_0x4458ad,_0x4fb785){if(_0x1330c0['changed'](_0xb09c('0x9'))||_0x1330c0[_0xb09c('0x8')]('serverUrl')||_0x1330c0['changed'](_0xb09c('0xb'))||_0x1330c0[_0xb09c('0x8')](_0xb09c('0x20'))||_0x1330c0[_0xb09c('0x8')](_0xb09c('0xc'))||_0x1330c0['changed'](_0xb09c('0x21'))){var _0x1a70ca,_0x4a434c;_0x1a70ca=_0x1330c0[_0xb09c('0xa')];var _0x1ea575=_0x1a70ca[_0xb09c('0x22')](-0x1);if(_0x1ea575==='/'||_0x1ea575==='\x5c'||_0x1ea575===':'){_0x1a70ca=_0x1a70ca['substring'](0x0,_0x1a70ca[_0xb09c('0xe')](_0x1ea575));}switch(_0x1330c0[_0xb09c('0x9')]){case'US':_0x4a434c='https://accounts.zoho.com';break;case'AU':_0x4a434c=_0xb09c('0xf');break;case'IN':_0x4a434c=_0xb09c('0x10');break;case'CN':_0x4a434c='https://accounts.zoho.com.cn';break;default:_0x4a434c=_0xb09c('0x11');break;}var _0x443841={'method':_0xb09c('0x12'),'uri':_0x4a434c+_0xb09c('0x13'),'form':{'grant_type':_0xb09c('0x14'),'client_id':_0x1330c0[_0xb09c('0xb')],'client_secret':_0x1330c0['clientSecret'],'redirect_uri':_0x1330c0[_0xb09c('0xa')],'code':_0x1330c0[_0xb09c('0xc')]},'headers':{'Content-Type':_0xb09c('0x15')}};return rp(_0x443841)[_0xb09c('0x16')](function(_0x56f286){var _0x3dbabe=JSON['parse'](_0x56f286);if(_0x3dbabe[_0xb09c('0x19')]){_0x1330c0['refreshToken']=_0x3dbabe[_0xb09c('0x19')];logger[_0xb09c('0x23')](util['format']('ZohoAccount,\x20Refresh\x20token\x20successfully\x20generated\x20for\x20account\x20\x22%s\x22\x20#%d',_0x1330c0[_0xb09c('0x1d')],_0x1330c0['id']));}else{logger[_0xb09c('0x1c')](util[_0xb09c('0x1a')]('ZohoAccount,\x20Unable\x20to\x20generate\x20the\x20refresh\x20token\x20for\x20account\x20\x22%s\x22\x20#%d',_0x1330c0[_0xb09c('0x1d')],_0x1330c0['id']),util[_0xb09c('0x1e')](_0x3dbabe,{'showHidden':![],'depth':null}));}return _0x4fb785(null,_0x4458ad);})[_0xb09c('0x24')](function(_0x3c8795){logger[_0xb09c('0x1c')](util[_0xb09c('0x1a')](_0xb09c('0x1f'),_0x1330c0[_0xb09c('0x1d')],_0x1330c0['id']),util[_0xb09c('0x1e')](parsedResult,{'showHidden':![],'depth':null}));return _0x4fb785(_0x3c8795);});}else{return _0x4fb785(null,_0x4458ad);}}}});};