Built motion from commit (unavailable).|2.5.21
[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 _0x755f=['clientSecret','substring','/oauth/v2/token','refreshToken','error','lodash','../../config/logger','api','moment','bluebird','request-promise','path','rimraf','../../config/environment','./intZohoAccount.attributes','exports','define','ZohoAccount','changed','zone','serverUrl','clientId','code','host','slice','lastIndexOf','https://accounts.zoho.in','https://accounts.zoho.com.cn','https://accounts.zoho.eu','POST','authorization_code','application/x-www-form-urlencoded','then','parse','refresh_token','info','format','ZohoAccount,\x20Refresh\x20token\x20successfully\x20generated\x20for\x20account\x20\x22%s\x22\x20#%d','name','ZohoAccount,\x20Unable\x20to\x20generate\x20the\x20refresh\x20token\x20for\x20account\x20\x22%s\x22\x20#%d','inspect'];(function(_0x4c27ba,_0x308565){var _0x2ebda9=function(_0x24e3f1){while(--_0x24e3f1){_0x4c27ba['push'](_0x4c27ba['shift']());}};_0x2ebda9(++_0x308565);}(_0x755f,0xa9));var _0xf755=function(_0x1cf561,_0x32def2){_0x1cf561=_0x1cf561-0x0;var _0x148491=_0x755f[_0x1cf561];return _0x148491;};'use strict';var _=require(_0xf755('0x0'));var util=require('util');var logger=require(_0xf755('0x1'))(_0xf755('0x2'));var moment=require(_0xf755('0x3'));var BPromise=require(_0xf755('0x4'));var rp=require(_0xf755('0x5'));var fs=require('fs');var path=require(_0xf755('0x6'));var rimraf=require(_0xf755('0x7'));var config=require(_0xf755('0x8'));var attributes=require(_0xf755('0x9'));var integrations=require('../../components/integrations/configuration');module[_0xf755('0xa')]=function(_0x3dfd59,_0x3d6c53){return _0x3dfd59[_0xf755('0xb')](_0xf755('0xc'),attributes,{'tableName':'int_zoho_accounts','paranoid':![],'indexes':[],'timestamps':!![],'hooks':{'beforeCreate':function(_0x401136,_0x1a3dce,_0x3f9506){if(_0x401136[_0xf755('0xd')](_0xf755('0xe'))||_0x401136['changed'](_0xf755('0xf'))||_0x401136[_0xf755('0xd')](_0xf755('0x10'))||_0x401136[_0xf755('0xd')]('clientSecret')||_0x401136[_0xf755('0xd')](_0xf755('0x11'))||_0x401136[_0xf755('0xd')](_0xf755('0x12'))){var _0x234049,_0x441b2f;_0x234049=_0x401136[_0xf755('0xf')];var _0x2c2bed=_0x234049[_0xf755('0x13')](-0x1);if(_0x2c2bed==='/'||_0x2c2bed==='\x5c'||_0x2c2bed===':'){_0x234049=_0x234049['substring'](0x0,_0x234049[_0xf755('0x14')](_0x2c2bed));}switch(_0x401136[_0xf755('0xe')]){case'US':_0x441b2f='https://accounts.zoho.com';break;case'AU':_0x441b2f='https://accounts.zoho.com.au';break;case'IN':_0x441b2f=_0xf755('0x15');break;case'CN':_0x441b2f=_0xf755('0x16');break;default:_0x441b2f=_0xf755('0x17');break;}var _0x374b52={'method':_0xf755('0x18'),'uri':_0x441b2f+'/oauth/v2/token','form':{'grant_type':_0xf755('0x19'),'client_id':_0x401136[_0xf755('0x10')],'client_secret':_0x401136['clientSecret'],'redirect_uri':_0x401136[_0xf755('0xf')],'code':_0x401136[_0xf755('0x11')]},'headers':{'Content-Type':_0xf755('0x1a')}};return rp(_0x374b52)[_0xf755('0x1b')](function(_0x25d679){var _0x1a96d4=JSON[_0xf755('0x1c')](_0x25d679);if(_0x1a96d4['refresh_token']){_0x401136['refreshToken']=_0x1a96d4[_0xf755('0x1d')];logger[_0xf755('0x1e')](util[_0xf755('0x1f')](_0xf755('0x20'),_0x401136[_0xf755('0x21')],_0x401136['id']));}else{logger['error'](util[_0xf755('0x1f')](_0xf755('0x22'),_0x401136[_0xf755('0x21')],_0x401136['id']),util[_0xf755('0x23')](_0x1a96d4,{'showHidden':![],'depth':null}));}return _0x3f9506(null,_0x1a3dce);})['catch'](function(_0x595309){logger['error'](util[_0xf755('0x1f')](_0xf755('0x22'),_0x401136[_0xf755('0x21')],_0x401136['id']),util[_0xf755('0x23')](parsedResult,{'showHidden':![],'depth':null}));return _0x3f9506(_0x595309);});}else{return _0x3f9506(null,_0x1a3dce);}},'beforeUpdate':function(_0x58ae2e,_0x323149,_0xb079b6){if(_0x58ae2e[_0xf755('0xd')](_0xf755('0xe'))||_0x58ae2e[_0xf755('0xd')](_0xf755('0xf'))||_0x58ae2e[_0xf755('0xd')](_0xf755('0x10'))||_0x58ae2e['changed'](_0xf755('0x24'))||_0x58ae2e[_0xf755('0xd')]('code')||_0x58ae2e['changed'](_0xf755('0x12'))){var _0x5dbb99,_0x1d8583;_0x5dbb99=_0x58ae2e[_0xf755('0xf')];var _0x4fc6a6=_0x5dbb99[_0xf755('0x13')](-0x1);if(_0x4fc6a6==='/'||_0x4fc6a6==='\x5c'||_0x4fc6a6===':'){_0x5dbb99=_0x5dbb99[_0xf755('0x25')](0x0,_0x5dbb99['lastIndexOf'](_0x4fc6a6));}switch(_0x58ae2e['zone']){case'US':_0x1d8583='https://accounts.zoho.com';break;case'AU':_0x1d8583='https://accounts.zoho.com.au';break;case'IN':_0x1d8583=_0xf755('0x15');break;case'CN':_0x1d8583='https://accounts.zoho.com.cn';break;default:_0x1d8583='https://accounts.zoho.eu';break;}var _0x3da484={'method':_0xf755('0x18'),'uri':_0x1d8583+_0xf755('0x26'),'form':{'grant_type':'authorization_code','client_id':_0x58ae2e[_0xf755('0x10')],'client_secret':_0x58ae2e[_0xf755('0x24')],'redirect_uri':_0x58ae2e[_0xf755('0xf')],'code':_0x58ae2e[_0xf755('0x11')]},'headers':{'Content-Type':_0xf755('0x1a')}};return rp(_0x3da484)[_0xf755('0x1b')](function(_0x12a81c){var _0x2fd13f=JSON[_0xf755('0x1c')](_0x12a81c);if(_0x2fd13f[_0xf755('0x1d')]){_0x58ae2e[_0xf755('0x27')]=_0x2fd13f[_0xf755('0x1d')];logger[_0xf755('0x1e')](util['format'](_0xf755('0x20'),_0x58ae2e[_0xf755('0x21')],_0x58ae2e['id']));}else{logger[_0xf755('0x28')](util[_0xf755('0x1f')]('ZohoAccount,\x20Unable\x20to\x20generate\x20the\x20refresh\x20token\x20for\x20account\x20\x22%s\x22\x20#%d',_0x58ae2e[_0xf755('0x21')],_0x58ae2e['id']),util['inspect'](_0x2fd13f,{'showHidden':![],'depth':null}));}return _0xb079b6(null,_0x323149);})['catch'](function(_0x185495){logger[_0xf755('0x28')](util[_0xf755('0x1f')](_0xf755('0x22'),_0x58ae2e[_0xf755('0x21')],_0x58ae2e['id']),util[_0xf755('0x23')](parsedResult,{'showHidden':![],'depth':null}));return _0xb079b6(_0x185495);});}else{return _0xb079b6(null,_0x323149);}}}});};