98716fca2ec2416e4dcd064c17039b281d6d9eb1
[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 _0x27f9=['serverUrl','clientId','clientSecret','host','substring','https://accounts.zoho.com','https://accounts.zoho.com.au','https://accounts.zoho.com.cn','https://accounts.zoho.eu','/oauth/v2/token','authorization_code','code','application/x-www-form-urlencoded','parse','refreshToken','refresh_token','info','format','ZohoAccount,\x20Refresh\x20token\x20successfully\x20generated\x20for\x20account\x20\x22%s\x22\x20#%d','name','error','inspect','catch','https://accounts.zoho.in','POST','then','ZohoAccount,\x20Unable\x20to\x20generate\x20the\x20refresh\x20token\x20for\x20account\x20\x22%s\x22\x20#%d','lodash','util','../../config/logger','api','moment','request-promise','path','rimraf','./intZohoAccount.attributes','../../components/integrations/configuration','ZohoAccount','int_zoho_accounts','changed','zone'];(function(_0x169c00,_0x46c6e8){var _0x40c8da=function(_0x1956f5){while(--_0x1956f5){_0x169c00['push'](_0x169c00['shift']());}};_0x40c8da(++_0x46c6e8);}(_0x27f9,0x1de));var _0x927f=function(_0x1e62d3,_0x45f7d5){_0x1e62d3=_0x1e62d3-0x0;var _0x247d73=_0x27f9[_0x1e62d3];return _0x247d73;};'use strict';var _=require(_0x927f('0x0'));var util=require(_0x927f('0x1'));var logger=require(_0x927f('0x2'))(_0x927f('0x3'));var moment=require(_0x927f('0x4'));var BPromise=require('bluebird');var rp=require(_0x927f('0x5'));var fs=require('fs');var path=require(_0x927f('0x6'));var rimraf=require(_0x927f('0x7'));var config=require('../../config/environment');var attributes=require(_0x927f('0x8'));var integrations=require(_0x927f('0x9'));module['exports']=function(_0x207d51,_0xcc5900){return _0x207d51['define'](_0x927f('0xa'),attributes,{'tableName':_0x927f('0xb'),'paranoid':![],'indexes':[],'timestamps':!![],'hooks':{'beforeCreate':function(_0x4c4ca6,_0x2ca21c,_0x437747){if(_0x4c4ca6[_0x927f('0xc')](_0x927f('0xd'))||_0x4c4ca6[_0x927f('0xc')](_0x927f('0xe'))||_0x4c4ca6[_0x927f('0xc')](_0x927f('0xf'))||_0x4c4ca6[_0x927f('0xc')](_0x927f('0x10'))||_0x4c4ca6['changed']('code')||_0x4c4ca6[_0x927f('0xc')](_0x927f('0x11'))){var _0x2d5d46,_0x295879;_0x2d5d46=_0x4c4ca6[_0x927f('0xe')];var _0x401593=_0x2d5d46['slice'](-0x1);if(_0x401593==='/'||_0x401593==='\x5c'||_0x401593===':'){_0x2d5d46=_0x2d5d46[_0x927f('0x12')](0x0,_0x2d5d46['lastIndexOf'](_0x401593));}switch(_0x4c4ca6[_0x927f('0xd')]){case'US':_0x295879=_0x927f('0x13');break;case'AU':_0x295879=_0x927f('0x14');break;case'IN':_0x295879='https://accounts.zoho.in';break;case'CN':_0x295879=_0x927f('0x15');break;default:_0x295879=_0x927f('0x16');break;}var _0x4728d8={'method':'POST','uri':_0x295879+_0x927f('0x17'),'form':{'grant_type':_0x927f('0x18'),'client_id':_0x4c4ca6[_0x927f('0xf')],'client_secret':_0x4c4ca6[_0x927f('0x10')],'redirect_uri':_0x4c4ca6[_0x927f('0xe')],'code':_0x4c4ca6[_0x927f('0x19')]},'headers':{'Content-Type':_0x927f('0x1a')}};return rp(_0x4728d8)['then'](function(_0xfa30fc){var _0x48d509=JSON[_0x927f('0x1b')](_0xfa30fc);if(_0x48d509['refresh_token']){_0x4c4ca6[_0x927f('0x1c')]=_0x48d509[_0x927f('0x1d')];logger[_0x927f('0x1e')](util[_0x927f('0x1f')](_0x927f('0x20'),_0x4c4ca6[_0x927f('0x21')],_0x4c4ca6['id']));}else{logger[_0x927f('0x22')](util['format']('ZohoAccount,\x20Unable\x20to\x20generate\x20the\x20refresh\x20token\x20for\x20account\x20\x22%s\x22\x20#%d',_0x4c4ca6['name'],_0x4c4ca6['id']),util[_0x927f('0x23')](_0x48d509,{'showHidden':![],'depth':null}));}return _0x437747(null,_0x2ca21c);})[_0x927f('0x24')](function(_0x920ec5){logger['error'](util[_0x927f('0x1f')]('ZohoAccount,\x20Unable\x20to\x20generate\x20the\x20refresh\x20token\x20for\x20account\x20\x22%s\x22\x20#%d',_0x4c4ca6['name'],_0x4c4ca6['id']),util[_0x927f('0x23')](parsedResult,{'showHidden':![],'depth':null}));return _0x437747(_0x920ec5);});}else{return _0x437747(null,_0x2ca21c);}},'beforeUpdate':function(_0x55b7ea,_0x5bce00,_0x546882){if(_0x55b7ea[_0x927f('0xc')](_0x927f('0xd'))||_0x55b7ea['changed'](_0x927f('0xe'))||_0x55b7ea['changed'](_0x927f('0xf'))||_0x55b7ea[_0x927f('0xc')](_0x927f('0x10'))||_0x55b7ea['changed'](_0x927f('0x19'))||_0x55b7ea[_0x927f('0xc')](_0x927f('0x11'))){var _0x1556be,_0x1c119c;_0x1556be=_0x55b7ea['serverUrl'];var _0x1ce31b=_0x1556be['slice'](-0x1);if(_0x1ce31b==='/'||_0x1ce31b==='\x5c'||_0x1ce31b===':'){_0x1556be=_0x1556be['substring'](0x0,_0x1556be['lastIndexOf'](_0x1ce31b));}switch(_0x55b7ea[_0x927f('0xd')]){case'US':_0x1c119c=_0x927f('0x13');break;case'AU':_0x1c119c='https://accounts.zoho.com.au';break;case'IN':_0x1c119c=_0x927f('0x25');break;case'CN':_0x1c119c=_0x927f('0x15');break;default:_0x1c119c=_0x927f('0x16');break;}var _0x4f3be3={'method':_0x927f('0x26'),'uri':_0x1c119c+_0x927f('0x17'),'form':{'grant_type':_0x927f('0x18'),'client_id':_0x55b7ea['clientId'],'client_secret':_0x55b7ea[_0x927f('0x10')],'redirect_uri':_0x55b7ea[_0x927f('0xe')],'code':_0x55b7ea[_0x927f('0x19')]},'headers':{'Content-Type':_0x927f('0x1a')}};return rp(_0x4f3be3)[_0x927f('0x27')](function(_0x232c69){var _0xe0f509=JSON[_0x927f('0x1b')](_0x232c69);if(_0xe0f509[_0x927f('0x1d')]){_0x55b7ea[_0x927f('0x1c')]=_0xe0f509['refresh_token'];logger[_0x927f('0x1e')](util['format'](_0x927f('0x20'),_0x55b7ea[_0x927f('0x21')],_0x55b7ea['id']));}else{logger[_0x927f('0x22')](util[_0x927f('0x1f')]('ZohoAccount,\x20Unable\x20to\x20generate\x20the\x20refresh\x20token\x20for\x20account\x20\x22%s\x22\x20#%d',_0x55b7ea['name'],_0x55b7ea['id']),util['inspect'](_0xe0f509,{'showHidden':![],'depth':null}));}return _0x546882(null,_0x5bce00);})[_0x927f('0x24')](function(_0x37df66){logger['error'](util[_0x927f('0x1f')](_0x927f('0x28'),_0x55b7ea[_0x927f('0x21')],_0x55b7ea['id']),util['inspect'](parsedResult,{'showHidden':![],'depth':null}));return _0x546882(_0x37df66);});}else{return _0x546882(null,_0x5bce00);}}}});};