040d94e7f4ab119c78fa0c73775d4dca04a01bb8
[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 _0x441d=['../../config/environment','./intZohoAccount.attributes','../../components/integrations/configuration','exports','define','ZohoAccount','int_zoho_accounts','changed','zone','serverUrl','clientId','clientSecret','code','substring','lastIndexOf','https://accounts.zoho.com','https://accounts.zoho.in','https://accounts.zoho.eu','POST','/oauth/v2/token','application/x-www-form-urlencoded','then','parse','refresh_token','refreshToken','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','catch','error','https://accounts.zoho.com.au','https://accounts.zoho.com.cn','authorization_code','info','api','moment','request-promise','path'];(function(_0x1caf6a,_0x26a5bf){var _0x1bf493=function(_0x3982f5){while(--_0x3982f5){_0x1caf6a['push'](_0x1caf6a['shift']());}};_0x1bf493(++_0x26a5bf);}(_0x441d,0xec));var _0xd441=function(_0x4566f4,_0x2b6ebc){_0x4566f4=_0x4566f4-0x0;var _0x516e94=_0x441d[_0x4566f4];return _0x516e94;};'use strict';var _=require('lodash');var util=require('util');var logger=require('../../config/logger')(_0xd441('0x0'));var moment=require(_0xd441('0x1'));var BPromise=require('bluebird');var rp=require(_0xd441('0x2'));var fs=require('fs');var path=require(_0xd441('0x3'));var rimraf=require('rimraf');var config=require(_0xd441('0x4'));var attributes=require(_0xd441('0x5'));var integrations=require(_0xd441('0x6'));module[_0xd441('0x7')]=function(_0x463e45,_0x2ef3c4){return _0x463e45[_0xd441('0x8')](_0xd441('0x9'),attributes,{'tableName':_0xd441('0xa'),'paranoid':![],'indexes':[],'timestamps':!![],'hooks':{'beforeCreate':function(_0x45470d,_0x3b1253,_0x1366c3){if(_0x45470d[_0xd441('0xb')](_0xd441('0xc'))||_0x45470d['changed'](_0xd441('0xd'))||_0x45470d[_0xd441('0xb')](_0xd441('0xe'))||_0x45470d[_0xd441('0xb')](_0xd441('0xf'))||_0x45470d[_0xd441('0xb')](_0xd441('0x10'))||_0x45470d[_0xd441('0xb')]('host')){var _0xc05777,_0x5d772e;_0xc05777=_0x45470d[_0xd441('0xd')];var _0x566c8c=_0xc05777['slice'](-0x1);if(_0x566c8c==='/'||_0x566c8c==='\x5c'||_0x566c8c===':'){_0xc05777=_0xc05777[_0xd441('0x11')](0x0,_0xc05777[_0xd441('0x12')](_0x566c8c));}switch(_0x45470d[_0xd441('0xc')]){case'US':_0x5d772e=_0xd441('0x13');break;case'AU':_0x5d772e='https://accounts.zoho.com.au';break;case'IN':_0x5d772e=_0xd441('0x14');break;case'CN':_0x5d772e='https://accounts.zoho.com.cn';break;default:_0x5d772e=_0xd441('0x15');break;}var _0x413160={'method':_0xd441('0x16'),'uri':_0x5d772e+_0xd441('0x17'),'form':{'grant_type':'authorization_code','client_id':_0x45470d[_0xd441('0xe')],'client_secret':_0x45470d[_0xd441('0xf')],'redirect_uri':_0x45470d[_0xd441('0xd')],'code':_0x45470d[_0xd441('0x10')]},'headers':{'Content-Type':_0xd441('0x18')}};return rp(_0x413160)[_0xd441('0x19')](function(_0x4fc0ac){var _0x53c04e=JSON[_0xd441('0x1a')](_0x4fc0ac);if(_0x53c04e[_0xd441('0x1b')]){_0x45470d[_0xd441('0x1c')]=_0x53c04e[_0xd441('0x1b')];logger['info'](util[_0xd441('0x1d')](_0xd441('0x1e'),_0x45470d[_0xd441('0x1f')],_0x45470d['id']));}else{logger['error'](util[_0xd441('0x1d')](_0xd441('0x20'),_0x45470d[_0xd441('0x1f')],_0x45470d['id']),util[_0xd441('0x21')](_0x53c04e,{'showHidden':![],'depth':null}));}return _0x1366c3(null,_0x3b1253);})[_0xd441('0x22')](function(_0x4d9aef){logger[_0xd441('0x23')](util[_0xd441('0x1d')](_0xd441('0x20'),_0x45470d['name'],_0x45470d['id']),util[_0xd441('0x21')](parsedResult,{'showHidden':![],'depth':null}));return _0x1366c3(_0x4d9aef);});}else{return _0x1366c3(null,_0x3b1253);}},'beforeUpdate':function(_0x5d6ea1,_0x5a0b85,_0x3a3604){if(_0x5d6ea1[_0xd441('0xb')](_0xd441('0xc'))||_0x5d6ea1[_0xd441('0xb')](_0xd441('0xd'))||_0x5d6ea1[_0xd441('0xb')](_0xd441('0xe'))||_0x5d6ea1[_0xd441('0xb')](_0xd441('0xf'))||_0x5d6ea1['changed'](_0xd441('0x10'))||_0x5d6ea1[_0xd441('0xb')]('host')){var _0x36b96b,_0x4d45d2;_0x36b96b=_0x5d6ea1['serverUrl'];var _0x28c55a=_0x36b96b['slice'](-0x1);if(_0x28c55a==='/'||_0x28c55a==='\x5c'||_0x28c55a===':'){_0x36b96b=_0x36b96b[_0xd441('0x11')](0x0,_0x36b96b['lastIndexOf'](_0x28c55a));}switch(_0x5d6ea1[_0xd441('0xc')]){case'US':_0x4d45d2='https://accounts.zoho.com';break;case'AU':_0x4d45d2=_0xd441('0x24');break;case'IN':_0x4d45d2='https://accounts.zoho.in';break;case'CN':_0x4d45d2=_0xd441('0x25');break;default:_0x4d45d2=_0xd441('0x15');break;}var _0x15274e={'method':'POST','uri':_0x4d45d2+'/oauth/v2/token','form':{'grant_type':_0xd441('0x26'),'client_id':_0x5d6ea1['clientId'],'client_secret':_0x5d6ea1[_0xd441('0xf')],'redirect_uri':_0x5d6ea1[_0xd441('0xd')],'code':_0x5d6ea1[_0xd441('0x10')]},'headers':{'Content-Type':_0xd441('0x18')}};return rp(_0x15274e)[_0xd441('0x19')](function(_0x167237){var _0x5460df=JSON[_0xd441('0x1a')](_0x167237);if(_0x5460df[_0xd441('0x1b')]){_0x5d6ea1['refreshToken']=_0x5460df[_0xd441('0x1b')];logger[_0xd441('0x27')](util[_0xd441('0x1d')](_0xd441('0x1e'),_0x5d6ea1[_0xd441('0x1f')],_0x5d6ea1['id']));}else{logger['error'](util[_0xd441('0x1d')](_0xd441('0x20'),_0x5d6ea1[_0xd441('0x1f')],_0x5d6ea1['id']),util[_0xd441('0x21')](_0x5460df,{'showHidden':![],'depth':null}));}return _0x3a3604(null,_0x5a0b85);})[_0xd441('0x22')](function(_0x105fa4){logger[_0xd441('0x23')](util[_0xd441('0x1d')](_0xd441('0x20'),_0x5d6ea1[_0xd441('0x1f')],_0x5d6ea1['id']),util[_0xd441('0x21')](parsedResult,{'showHidden':![],'depth':null}));return _0x3a3604(_0x105fa4);});}else{return _0x3a3604(null,_0x5a0b85);}}}});};