b9795b884e5df3a939484bb6f8f4d1067b43a0d5
[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 _0x59c4=['inspect','catch','substring','https://accounts.zoho.eu','ZohoAccount,\x20Refresh\x20token\x20successfully\x20generated\x20for\x20account\x20\x22%s\x22\x20#%d','lodash','util','../../config/logger','moment','bluebird','path','./intZohoAccount.attributes','../../components/integrations/configuration','exports','define','ZohoAccount','int_zoho_accounts','changed','zone','serverUrl','clientId','clientSecret','host','slice','lastIndexOf','https://accounts.zoho.com','https://accounts.zoho.com.au','https://accounts.zoho.in','https://accounts.zoho.com.cn','POST','/oauth/v2/token','authorization_code','code','application/x-www-form-urlencoded','then','parse','refresh_token','refreshToken','info','name','error','format','ZohoAccount,\x20Unable\x20to\x20generate\x20the\x20refresh\x20token\x20for\x20account\x20\x22%s\x22\x20#%d'];(function(_0x3a2a3c,_0x37bd31){var _0x50ac9a=function(_0x494a7f){while(--_0x494a7f){_0x3a2a3c['push'](_0x3a2a3c['shift']());}};_0x50ac9a(++_0x37bd31);}(_0x59c4,0xb1));var _0x459c=function(_0x5d458d,_0x9d4f0a){_0x5d458d=_0x5d458d-0x0;var _0x17f4aa=_0x59c4[_0x5d458d];return _0x17f4aa;};'use strict';var _=require(_0x459c('0x0'));var util=require(_0x459c('0x1'));var logger=require(_0x459c('0x2'))('api');var moment=require(_0x459c('0x3'));var BPromise=require(_0x459c('0x4'));var rp=require('request-promise');var fs=require('fs');var path=require(_0x459c('0x5'));var rimraf=require('rimraf');var config=require('../../config/environment');var attributes=require(_0x459c('0x6'));var integrations=require(_0x459c('0x7'));module[_0x459c('0x8')]=function(_0x97ee9a,_0x324980){return _0x97ee9a[_0x459c('0x9')](_0x459c('0xa'),attributes,{'tableName':_0x459c('0xb'),'paranoid':![],'indexes':[],'timestamps':!![],'hooks':{'beforeCreate':function(_0x160cff,_0x243f94,_0xb76e5e){if(_0x160cff[_0x459c('0xc')](_0x459c('0xd'))||_0x160cff['changed'](_0x459c('0xe'))||_0x160cff[_0x459c('0xc')](_0x459c('0xf'))||_0x160cff[_0x459c('0xc')](_0x459c('0x10'))||_0x160cff['changed']('code')||_0x160cff[_0x459c('0xc')](_0x459c('0x11'))){var _0x25d9f1,_0x14c69f;_0x25d9f1=_0x160cff[_0x459c('0xe')];var _0x555ab2=_0x25d9f1[_0x459c('0x12')](-0x1);if(_0x555ab2==='/'||_0x555ab2==='\x5c'||_0x555ab2===':'){_0x25d9f1=_0x25d9f1['substring'](0x0,_0x25d9f1[_0x459c('0x13')](_0x555ab2));}switch(_0x160cff[_0x459c('0xd')]){case'US':_0x14c69f=_0x459c('0x14');break;case'AU':_0x14c69f=_0x459c('0x15');break;case'IN':_0x14c69f=_0x459c('0x16');break;case'CN':_0x14c69f=_0x459c('0x17');break;default:_0x14c69f='https://accounts.zoho.eu';break;}var _0x29485e={'method':_0x459c('0x18'),'uri':_0x14c69f+_0x459c('0x19'),'form':{'grant_type':_0x459c('0x1a'),'client_id':_0x160cff[_0x459c('0xf')],'client_secret':_0x160cff[_0x459c('0x10')],'redirect_uri':_0x160cff[_0x459c('0xe')],'code':_0x160cff[_0x459c('0x1b')]},'headers':{'Content-Type':_0x459c('0x1c')}};return rp(_0x29485e)[_0x459c('0x1d')](function(_0x25ce30){var _0x347b9a=JSON[_0x459c('0x1e')](_0x25ce30);if(_0x347b9a[_0x459c('0x1f')]){_0x160cff[_0x459c('0x20')]=_0x347b9a[_0x459c('0x1f')];logger[_0x459c('0x21')](util['format']('ZohoAccount,\x20Refresh\x20token\x20successfully\x20generated\x20for\x20account\x20\x22%s\x22\x20#%d',_0x160cff[_0x459c('0x22')],_0x160cff['id']));}else{logger[_0x459c('0x23')](util[_0x459c('0x24')](_0x459c('0x25'),_0x160cff[_0x459c('0x22')],_0x160cff['id']),util[_0x459c('0x26')](_0x347b9a,{'showHidden':![],'depth':null}));}return _0xb76e5e(null,_0x243f94);})[_0x459c('0x27')](function(_0x2ef5f1){logger[_0x459c('0x23')](util['format']('ZohoAccount,\x20Unable\x20to\x20generate\x20the\x20refresh\x20token\x20for\x20account\x20\x22%s\x22\x20#%d',_0x160cff[_0x459c('0x22')],_0x160cff['id']),util['inspect'](parsedResult,{'showHidden':![],'depth':null}));return _0xb76e5e(_0x2ef5f1);});}else{return _0xb76e5e(null,_0x243f94);}},'beforeUpdate':function(_0x4cac39,_0x369e90,_0x41406c){if(_0x4cac39[_0x459c('0xc')](_0x459c('0xd'))||_0x4cac39['changed'](_0x459c('0xe'))||_0x4cac39[_0x459c('0xc')](_0x459c('0xf'))||_0x4cac39['changed']('clientSecret')||_0x4cac39['changed'](_0x459c('0x1b'))||_0x4cac39['changed'](_0x459c('0x11'))){var _0x4f4fd8,_0x2dee06;_0x4f4fd8=_0x4cac39[_0x459c('0xe')];var _0x344018=_0x4f4fd8[_0x459c('0x12')](-0x1);if(_0x344018==='/'||_0x344018==='\x5c'||_0x344018===':'){_0x4f4fd8=_0x4f4fd8[_0x459c('0x28')](0x0,_0x4f4fd8[_0x459c('0x13')](_0x344018));}switch(_0x4cac39[_0x459c('0xd')]){case'US':_0x2dee06=_0x459c('0x14');break;case'AU':_0x2dee06=_0x459c('0x15');break;case'IN':_0x2dee06=_0x459c('0x16');break;case'CN':_0x2dee06=_0x459c('0x17');break;default:_0x2dee06=_0x459c('0x29');break;}var _0x599cd2={'method':_0x459c('0x18'),'uri':_0x2dee06+_0x459c('0x19'),'form':{'grant_type':'authorization_code','client_id':_0x4cac39[_0x459c('0xf')],'client_secret':_0x4cac39[_0x459c('0x10')],'redirect_uri':_0x4cac39['serverUrl'],'code':_0x4cac39[_0x459c('0x1b')]},'headers':{'Content-Type':_0x459c('0x1c')}};return rp(_0x599cd2)['then'](function(_0x28f077){var _0x5bbdcd=JSON['parse'](_0x28f077);if(_0x5bbdcd[_0x459c('0x1f')]){_0x4cac39[_0x459c('0x20')]=_0x5bbdcd['refresh_token'];logger[_0x459c('0x21')](util[_0x459c('0x24')](_0x459c('0x2a'),_0x4cac39[_0x459c('0x22')],_0x4cac39['id']));}else{logger[_0x459c('0x23')](util[_0x459c('0x24')](_0x459c('0x25'),_0x4cac39[_0x459c('0x22')],_0x4cac39['id']),util['inspect'](_0x5bbdcd,{'showHidden':![],'depth':null}));}return _0x41406c(null,_0x369e90);})[_0x459c('0x27')](function(_0x3db4c4){logger[_0x459c('0x23')](util[_0x459c('0x24')](_0x459c('0x25'),_0x4cac39['name'],_0x4cac39['id']),util['inspect'](parsedResult,{'showHidden':![],'depth':null}));return _0x41406c(_0x3db4c4);});}else{return _0x41406c(null,_0x369e90);}}}});};