2af22a2e0af0c7d9680adf8d0ff412cbe7c54817
[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 _0xab3c=['https://accounts.zoho.com.au','POST','clientSecret','application/x-www-form-urlencoded','then','parse','refresh_token','refreshToken','info','format','ZohoAccount,\x20Refresh\x20token\x20successfully\x20generated\x20for\x20account\x20\x22%s\x22\x20#%d','name','error','ZohoAccount,\x20Unable\x20to\x20generate\x20the\x20refresh\x20token\x20for\x20account\x20\x22%s\x22\x20#%d','inspect','substring','https://accounts.zoho.in','https://accounts.zoho.com.cn','https://accounts.zoho.eu','/oauth/v2/token','authorization_code','catch','util','../../config/logger','api','moment','bluebird','request-promise','path','rimraf','../../components/integrations/configuration','exports','int_zoho_accounts','changed','zone','serverUrl','clientId','code','host','slice','lastIndexOf','https://accounts.zoho.com'];(function(_0x3dd15e,_0x1f0015){var _0x231fd0=function(_0x4f680a){while(--_0x4f680a){_0x3dd15e['push'](_0x3dd15e['shift']());}};_0x231fd0(++_0x1f0015);}(_0xab3c,0x94));var _0xcab3=function(_0x3dd15e,_0x1f0015){_0x3dd15e=_0x3dd15e-0x0;var _0x231fd0=_0xab3c[_0x3dd15e];return _0x231fd0;};'use strict';var _=require('lodash');var util=require(_0xcab3('0x0'));var logger=require(_0xcab3('0x1'))(_0xcab3('0x2'));var moment=require(_0xcab3('0x3'));var BPromise=require(_0xcab3('0x4'));var rp=require(_0xcab3('0x5'));var fs=require('fs');var path=require(_0xcab3('0x6'));var rimraf=require(_0xcab3('0x7'));var config=require('../../config/environment');var attributes=require('./intZohoAccount.attributes');var integrations=require(_0xcab3('0x8'));module[_0xcab3('0x9')]=function(_0x461765,_0x5358b1){return _0x461765['define']('ZohoAccount',attributes,{'tableName':_0xcab3('0xa'),'paranoid':![],'indexes':[],'timestamps':!![],'hooks':{'beforeCreate':function(_0x556426,_0x3d62e5,_0x38ade4){if(_0x556426[_0xcab3('0xb')](_0xcab3('0xc'))||_0x556426[_0xcab3('0xb')](_0xcab3('0xd'))||_0x556426[_0xcab3('0xb')](_0xcab3('0xe'))||_0x556426[_0xcab3('0xb')]('clientSecret')||_0x556426[_0xcab3('0xb')](_0xcab3('0xf'))||_0x556426[_0xcab3('0xb')](_0xcab3('0x10'))){var _0x2bd963,_0x16dc97;_0x2bd963=_0x556426[_0xcab3('0xd')];var _0x39df48=_0x2bd963[_0xcab3('0x11')](-0x1);if(_0x39df48==='/'||_0x39df48==='\x5c'||_0x39df48===':'){_0x2bd963=_0x2bd963['substring'](0x0,_0x2bd963[_0xcab3('0x12')](_0x39df48));}switch(_0x556426['zone']){case'US':_0x16dc97=_0xcab3('0x13');break;case'AU':_0x16dc97=_0xcab3('0x14');break;case'IN':_0x16dc97='https://accounts.zoho.in';break;case'CN':_0x16dc97='https://accounts.zoho.com.cn';break;default:_0x16dc97='https://accounts.zoho.eu';break;}var _0x16efac={'method':_0xcab3('0x15'),'uri':_0x16dc97+'/oauth/v2/token','form':{'grant_type':'authorization_code','client_id':_0x556426['clientId'],'client_secret':_0x556426[_0xcab3('0x16')],'redirect_uri':_0x556426[_0xcab3('0xd')],'code':_0x556426[_0xcab3('0xf')]},'headers':{'Content-Type':_0xcab3('0x17')}};return rp(_0x16efac)[_0xcab3('0x18')](function(_0x229b10){var _0x1468d9=JSON[_0xcab3('0x19')](_0x229b10);if(_0x1468d9[_0xcab3('0x1a')]){_0x556426[_0xcab3('0x1b')]=_0x1468d9['refresh_token'];logger[_0xcab3('0x1c')](util[_0xcab3('0x1d')](_0xcab3('0x1e'),_0x556426[_0xcab3('0x1f')],_0x556426['id']));}else{logger[_0xcab3('0x20')](util['format'](_0xcab3('0x21'),_0x556426[_0xcab3('0x1f')],_0x556426['id']),util[_0xcab3('0x22')](_0x1468d9,{'showHidden':![],'depth':null}));}return _0x38ade4(null,_0x3d62e5);})['catch'](function(_0x38aea9){logger[_0xcab3('0x20')](util[_0xcab3('0x1d')]('ZohoAccount,\x20Unable\x20to\x20generate\x20the\x20refresh\x20token\x20for\x20account\x20\x22%s\x22\x20#%d',_0x556426[_0xcab3('0x1f')],_0x556426['id']),util[_0xcab3('0x22')](parsedResult,{'showHidden':![],'depth':null}));return _0x38ade4(_0x38aea9);});}else{return _0x38ade4(null,_0x3d62e5);}},'beforeUpdate':function(_0x40341d,_0x85ebaf,_0x5e824e){if(_0x40341d['changed'](_0xcab3('0xc'))||_0x40341d[_0xcab3('0xb')](_0xcab3('0xd'))||_0x40341d[_0xcab3('0xb')](_0xcab3('0xe'))||_0x40341d[_0xcab3('0xb')](_0xcab3('0x16'))||_0x40341d[_0xcab3('0xb')](_0xcab3('0xf'))||_0x40341d['changed'](_0xcab3('0x10'))){var _0x92b70b,_0x7ced90;_0x92b70b=_0x40341d[_0xcab3('0xd')];var _0x42c8db=_0x92b70b[_0xcab3('0x11')](-0x1);if(_0x42c8db==='/'||_0x42c8db==='\x5c'||_0x42c8db===':'){_0x92b70b=_0x92b70b[_0xcab3('0x23')](0x0,_0x92b70b['lastIndexOf'](_0x42c8db));}switch(_0x40341d[_0xcab3('0xc')]){case'US':_0x7ced90=_0xcab3('0x13');break;case'AU':_0x7ced90='https://accounts.zoho.com.au';break;case'IN':_0x7ced90=_0xcab3('0x24');break;case'CN':_0x7ced90=_0xcab3('0x25');break;default:_0x7ced90=_0xcab3('0x26');break;}var _0x2ca996={'method':_0xcab3('0x15'),'uri':_0x7ced90+_0xcab3('0x27'),'form':{'grant_type':_0xcab3('0x28'),'client_id':_0x40341d[_0xcab3('0xe')],'client_secret':_0x40341d[_0xcab3('0x16')],'redirect_uri':_0x40341d[_0xcab3('0xd')],'code':_0x40341d[_0xcab3('0xf')]},'headers':{'Content-Type':_0xcab3('0x17')}};return rp(_0x2ca996)[_0xcab3('0x18')](function(_0x2c2e62){var _0x482e81=JSON[_0xcab3('0x19')](_0x2c2e62);if(_0x482e81[_0xcab3('0x1a')]){_0x40341d[_0xcab3('0x1b')]=_0x482e81[_0xcab3('0x1a')];logger[_0xcab3('0x1c')](util[_0xcab3('0x1d')](_0xcab3('0x1e'),_0x40341d[_0xcab3('0x1f')],_0x40341d['id']));}else{logger[_0xcab3('0x20')](util[_0xcab3('0x1d')](_0xcab3('0x21'),_0x40341d[_0xcab3('0x1f')],_0x40341d['id']),util[_0xcab3('0x22')](_0x482e81,{'showHidden':![],'depth':null}));}return _0x5e824e(null,_0x85ebaf);})[_0xcab3('0x29')](function(_0x5cc97e){logger[_0xcab3('0x20')](util[_0xcab3('0x1d')]('ZohoAccount,\x20Unable\x20to\x20generate\x20the\x20refresh\x20token\x20for\x20account\x20\x22%s\x22\x20#%d',_0x40341d['name'],_0x40341d['id']),util['inspect'](parsedResult,{'showHidden':![],'depth':null}));return _0x5e824e(_0x5cc97e);});}else{return _0x5e824e(null,_0x85ebaf);}}}});};