490e0db91b62b80235efd0a2d6746c88218e3634
[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 _0x9471=['./intZohoAccount.attributes','../../components/integrations/configuration','define','ZohoAccount','int_zoho_accounts','changed','zone','serverUrl','clientId','clientSecret','code','host','slice','substring','lastIndexOf','https://accounts.zoho.com','https://accounts.zoho.com.au','https://accounts.zoho.in','https://accounts.zoho.com.cn','https://accounts.zoho.eu','POST','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','/oauth/v2/token','application/x-www-form-urlencoded','catch','util','../../config/logger','api','moment','bluebird','request-promise','path','rimraf','../../config/environment'];(function(_0x12f688,_0x585c25){var _0x59c3de=function(_0x4ac301){while(--_0x4ac301){_0x12f688['push'](_0x12f688['shift']());}};_0x59c3de(++_0x585c25);}(_0x9471,0xd3));var _0x1947=function(_0x3dd15e,_0x1f0015){_0x3dd15e=_0x3dd15e-0x0;var _0x231fd0=_0x9471[_0x3dd15e];return _0x231fd0;};'use strict';var _=require('lodash');var util=require(_0x1947('0x0'));var logger=require(_0x1947('0x1'))(_0x1947('0x2'));var moment=require(_0x1947('0x3'));var BPromise=require(_0x1947('0x4'));var rp=require(_0x1947('0x5'));var fs=require('fs');var path=require(_0x1947('0x6'));var rimraf=require(_0x1947('0x7'));var config=require(_0x1947('0x8'));var attributes=require(_0x1947('0x9'));var integrations=require(_0x1947('0xa'));module['exports']=function(_0x268cab,_0x1f8389){return _0x268cab[_0x1947('0xb')](_0x1947('0xc'),attributes,{'tableName':_0x1947('0xd'),'paranoid':![],'indexes':[],'timestamps':!![],'hooks':{'beforeCreate':function(_0x3b5d04,_0x255f2c,_0x5b7a8b){if(_0x3b5d04[_0x1947('0xe')](_0x1947('0xf'))||_0x3b5d04[_0x1947('0xe')](_0x1947('0x10'))||_0x3b5d04[_0x1947('0xe')](_0x1947('0x11'))||_0x3b5d04[_0x1947('0xe')](_0x1947('0x12'))||_0x3b5d04['changed'](_0x1947('0x13'))||_0x3b5d04['changed'](_0x1947('0x14'))){var _0x39dfd5,_0x5cfc47;_0x39dfd5=_0x3b5d04['serverUrl'];var _0x4c050d=_0x39dfd5[_0x1947('0x15')](-0x1);if(_0x4c050d==='/'||_0x4c050d==='\x5c'||_0x4c050d===':'){_0x39dfd5=_0x39dfd5[_0x1947('0x16')](0x0,_0x39dfd5[_0x1947('0x17')](_0x4c050d));}switch(_0x3b5d04[_0x1947('0xf')]){case'US':_0x5cfc47=_0x1947('0x18');break;case'AU':_0x5cfc47=_0x1947('0x19');break;case'IN':_0x5cfc47=_0x1947('0x1a');break;case'CN':_0x5cfc47=_0x1947('0x1b');break;default:_0x5cfc47=_0x1947('0x1c');break;}var _0x1e1f3c={'method':_0x1947('0x1d'),'uri':_0x5cfc47+'/oauth/v2/token','form':{'grant_type':'authorization_code','client_id':_0x3b5d04['clientId'],'client_secret':_0x3b5d04[_0x1947('0x12')],'redirect_uri':_0x3b5d04['serverUrl'],'code':_0x3b5d04[_0x1947('0x13')]},'headers':{'Content-Type':'application/x-www-form-urlencoded'}};return rp(_0x1e1f3c)[_0x1947('0x1e')](function(_0x49d52e){var _0x1b4a86=JSON[_0x1947('0x1f')](_0x49d52e);if(_0x1b4a86[_0x1947('0x20')]){_0x3b5d04[_0x1947('0x21')]=_0x1b4a86['refresh_token'];logger[_0x1947('0x22')](util[_0x1947('0x23')](_0x1947('0x24'),_0x3b5d04[_0x1947('0x25')],_0x3b5d04['id']));}else{logger[_0x1947('0x26')](util[_0x1947('0x23')](_0x1947('0x27'),_0x3b5d04[_0x1947('0x25')],_0x3b5d04['id']),util[_0x1947('0x28')](_0x1b4a86,{'showHidden':![],'depth':null}));}return _0x5b7a8b(null,_0x255f2c);})['catch'](function(_0x20c45a){logger[_0x1947('0x26')](util[_0x1947('0x23')](_0x1947('0x27'),_0x3b5d04[_0x1947('0x25')],_0x3b5d04['id']),util['inspect'](parsedResult,{'showHidden':![],'depth':null}));return _0x5b7a8b(_0x20c45a);});}else{return _0x5b7a8b(null,_0x255f2c);}},'beforeUpdate':function(_0x508067,_0x13a6fd,_0x1ca886){if(_0x508067['changed']('zone')||_0x508067[_0x1947('0xe')](_0x1947('0x10'))||_0x508067[_0x1947('0xe')]('clientId')||_0x508067['changed'](_0x1947('0x12'))||_0x508067['changed'](_0x1947('0x13'))||_0x508067[_0x1947('0xe')](_0x1947('0x14'))){var _0x476245,_0x403ff7;_0x476245=_0x508067[_0x1947('0x10')];var _0x2704f8=_0x476245['slice'](-0x1);if(_0x2704f8==='/'||_0x2704f8==='\x5c'||_0x2704f8===':'){_0x476245=_0x476245[_0x1947('0x16')](0x0,_0x476245[_0x1947('0x17')](_0x2704f8));}switch(_0x508067[_0x1947('0xf')]){case'US':_0x403ff7=_0x1947('0x18');break;case'AU':_0x403ff7=_0x1947('0x19');break;case'IN':_0x403ff7='https://accounts.zoho.in';break;case'CN':_0x403ff7=_0x1947('0x1b');break;default:_0x403ff7=_0x1947('0x1c');break;}var _0x2fcac2={'method':_0x1947('0x1d'),'uri':_0x403ff7+_0x1947('0x29'),'form':{'grant_type':'authorization_code','client_id':_0x508067[_0x1947('0x11')],'client_secret':_0x508067[_0x1947('0x12')],'redirect_uri':_0x508067['serverUrl'],'code':_0x508067[_0x1947('0x13')]},'headers':{'Content-Type':_0x1947('0x2a')}};return rp(_0x2fcac2)[_0x1947('0x1e')](function(_0x449679){var _0x4e2a6c=JSON[_0x1947('0x1f')](_0x449679);if(_0x4e2a6c[_0x1947('0x20')]){_0x508067[_0x1947('0x21')]=_0x4e2a6c[_0x1947('0x20')];logger[_0x1947('0x22')](util[_0x1947('0x23')](_0x1947('0x24'),_0x508067['name'],_0x508067['id']));}else{logger['error'](util['format'](_0x1947('0x27'),_0x508067[_0x1947('0x25')],_0x508067['id']),util['inspect'](_0x4e2a6c,{'showHidden':![],'depth':null}));}return _0x1ca886(null,_0x13a6fd);})[_0x1947('0x2b')](function(_0x31e3fa){logger['error'](util['format']('ZohoAccount,\x20Unable\x20to\x20generate\x20the\x20refresh\x20token\x20for\x20account\x20\x22%s\x22\x20#%d',_0x508067['name'],_0x508067['id']),util[_0x1947('0x28')](parsedResult,{'showHidden':![],'depth':null}));return _0x1ca886(_0x31e3fa);});}else{return _0x1ca886(null,_0x13a6fd);}}}});};