Built motion from commit 0900f080.|2.5.45
[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 _0x523e=['lastIndexOf','https://accounts.zoho.com','https://accounts.zoho.com.au','https://accounts.zoho.com.cn','https://accounts.zoho.eu','POST','/oauth/v2/token','authorization_code','code','application/x-www-form-urlencoded','then','parse','refreshToken','refresh_token','format','ZohoAccount,\x20Refresh\x20token\x20successfully\x20generated\x20for\x20account\x20\x22%s\x22\x20#%d','name','error','inspect','catch','host','slice','https://accounts.zoho.in','info','ZohoAccount,\x20Unable\x20to\x20generate\x20the\x20refresh\x20token\x20for\x20account\x20\x22%s\x22\x20#%d','lodash','util','../../config/logger','api','moment','bluebird','request-promise','path','rimraf','../../config/environment','../../components/integrations/configuration','exports','ZohoAccount','changed','zone','serverUrl','clientId','clientSecret'];(function(_0x44bcb0,_0x235aa9){var _0xfaf881=function(_0x25d81d){while(--_0x25d81d){_0x44bcb0['push'](_0x44bcb0['shift']());}};_0xfaf881(++_0x235aa9);}(_0x523e,0x146));var _0xe523=function(_0x21a9d9,_0x4d5ac8){_0x21a9d9=_0x21a9d9-0x0;var _0x46adce=_0x523e[_0x21a9d9];return _0x46adce;};'use strict';var _=require(_0xe523('0x0'));var util=require(_0xe523('0x1'));var logger=require(_0xe523('0x2'))(_0xe523('0x3'));var moment=require(_0xe523('0x4'));var BPromise=require(_0xe523('0x5'));var rp=require(_0xe523('0x6'));var fs=require('fs');var path=require(_0xe523('0x7'));var rimraf=require(_0xe523('0x8'));var config=require(_0xe523('0x9'));var attributes=require('./intZohoAccount.attributes');var integrations=require(_0xe523('0xa'));module[_0xe523('0xb')]=function(_0x5720c7,_0x4dc2a5){return _0x5720c7['define'](_0xe523('0xc'),attributes,{'tableName':'int_zoho_accounts','paranoid':![],'indexes':[],'timestamps':!![],'hooks':{'beforeCreate':function(_0x4f801c,_0x52e5af,_0x55726a){if(_0x4f801c[_0xe523('0xd')](_0xe523('0xe'))||_0x4f801c[_0xe523('0xd')](_0xe523('0xf'))||_0x4f801c[_0xe523('0xd')](_0xe523('0x10'))||_0x4f801c[_0xe523('0xd')](_0xe523('0x11'))||_0x4f801c['changed']('code')||_0x4f801c[_0xe523('0xd')]('host')){var _0x37cc65,_0x2f43a4;_0x37cc65=_0x4f801c[_0xe523('0xf')];var _0x25d7da=_0x37cc65['slice'](-0x1);if(_0x25d7da==='/'||_0x25d7da==='\x5c'||_0x25d7da===':'){_0x37cc65=_0x37cc65['substring'](0x0,_0x37cc65[_0xe523('0x12')](_0x25d7da));}switch(_0x4f801c[_0xe523('0xe')]){case'US':_0x2f43a4=_0xe523('0x13');break;case'AU':_0x2f43a4=_0xe523('0x14');break;case'IN':_0x2f43a4='https://accounts.zoho.in';break;case'CN':_0x2f43a4=_0xe523('0x15');break;default:_0x2f43a4=_0xe523('0x16');break;}var _0x462d60={'method':_0xe523('0x17'),'uri':_0x2f43a4+_0xe523('0x18'),'form':{'grant_type':_0xe523('0x19'),'client_id':_0x4f801c[_0xe523('0x10')],'client_secret':_0x4f801c[_0xe523('0x11')],'redirect_uri':_0x4f801c[_0xe523('0xf')],'code':_0x4f801c[_0xe523('0x1a')]},'headers':{'Content-Type':_0xe523('0x1b')}};return rp(_0x462d60)[_0xe523('0x1c')](function(_0x2edb40){var _0x1f562c=JSON[_0xe523('0x1d')](_0x2edb40);if(_0x1f562c['refresh_token']){_0x4f801c[_0xe523('0x1e')]=_0x1f562c[_0xe523('0x1f')];logger['info'](util[_0xe523('0x20')](_0xe523('0x21'),_0x4f801c[_0xe523('0x22')],_0x4f801c['id']));}else{logger[_0xe523('0x23')](util[_0xe523('0x20')]('ZohoAccount,\x20Unable\x20to\x20generate\x20the\x20refresh\x20token\x20for\x20account\x20\x22%s\x22\x20#%d',_0x4f801c['name'],_0x4f801c['id']),util[_0xe523('0x24')](_0x1f562c,{'showHidden':![],'depth':null}));}return _0x55726a(null,_0x52e5af);})[_0xe523('0x25')](function(_0x16bfa1){logger[_0xe523('0x23')](util['format']('ZohoAccount,\x20Unable\x20to\x20generate\x20the\x20refresh\x20token\x20for\x20account\x20\x22%s\x22\x20#%d',_0x4f801c[_0xe523('0x22')],_0x4f801c['id']),util[_0xe523('0x24')](parsedResult,{'showHidden':![],'depth':null}));return _0x55726a(_0x16bfa1);});}else{return _0x55726a(null,_0x52e5af);}},'beforeUpdate':function(_0x1dd3d4,_0x2a5105,_0x32d848){if(_0x1dd3d4[_0xe523('0xd')](_0xe523('0xe'))||_0x1dd3d4[_0xe523('0xd')](_0xe523('0xf'))||_0x1dd3d4[_0xe523('0xd')]('clientId')||_0x1dd3d4[_0xe523('0xd')](_0xe523('0x11'))||_0x1dd3d4[_0xe523('0xd')](_0xe523('0x1a'))||_0x1dd3d4['changed'](_0xe523('0x26'))){var _0x4704af,_0x17e79a;_0x4704af=_0x1dd3d4[_0xe523('0xf')];var _0x1864cc=_0x4704af[_0xe523('0x27')](-0x1);if(_0x1864cc==='/'||_0x1864cc==='\x5c'||_0x1864cc===':'){_0x4704af=_0x4704af['substring'](0x0,_0x4704af[_0xe523('0x12')](_0x1864cc));}switch(_0x1dd3d4[_0xe523('0xe')]){case'US':_0x17e79a=_0xe523('0x13');break;case'AU':_0x17e79a='https://accounts.zoho.com.au';break;case'IN':_0x17e79a=_0xe523('0x28');break;case'CN':_0x17e79a='https://accounts.zoho.com.cn';break;default:_0x17e79a=_0xe523('0x16');break;}var _0x334304={'method':'POST','uri':_0x17e79a+'/oauth/v2/token','form':{'grant_type':_0xe523('0x19'),'client_id':_0x1dd3d4[_0xe523('0x10')],'client_secret':_0x1dd3d4[_0xe523('0x11')],'redirect_uri':_0x1dd3d4['serverUrl'],'code':_0x1dd3d4[_0xe523('0x1a')]},'headers':{'Content-Type':_0xe523('0x1b')}};return rp(_0x334304)[_0xe523('0x1c')](function(_0x112fec){var _0x5f2872=JSON[_0xe523('0x1d')](_0x112fec);if(_0x5f2872[_0xe523('0x1f')]){_0x1dd3d4[_0xe523('0x1e')]=_0x5f2872[_0xe523('0x1f')];logger[_0xe523('0x29')](util[_0xe523('0x20')](_0xe523('0x21'),_0x1dd3d4['name'],_0x1dd3d4['id']));}else{logger[_0xe523('0x23')](util['format'](_0xe523('0x2a'),_0x1dd3d4[_0xe523('0x22')],_0x1dd3d4['id']),util[_0xe523('0x24')](_0x5f2872,{'showHidden':![],'depth':null}));}return _0x32d848(null,_0x2a5105);})['catch'](function(_0x3c9210){logger[_0xe523('0x23')](util[_0xe523('0x20')](_0xe523('0x2a'),_0x1dd3d4['name'],_0x1dd3d4['id']),util['inspect'](parsedResult,{'showHidden':![],'depth':null}));return _0x32d848(_0x3c9210);});}else{return _0x32d848(null,_0x2a5105);}}}});};