5155ab87cd757a272956307540cb5d0191af4e38
[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 _0xc60b=['exports','define','ZohoAccount','int_zoho_accounts','changed','zone','clientSecret','code','host','serverUrl','slice','substring','https://accounts.zoho.com','https://accounts.zoho.com.cn','https://accounts.zoho.eu','POST','/oauth/v2/token','authorization_code','application/x-www-form-urlencoded','then','refreshToken','refresh_token','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','catch','format','clientId','https://accounts.zoho.com.au','info','lodash','util','../../config/logger','api','moment','request-promise','path','./intZohoAccount.attributes','../../components/integrations/configuration'];(function(_0x3ec8b7,_0x4f78a0){var _0x417aae=function(_0x3b4be9){while(--_0x3b4be9){_0x3ec8b7['push'](_0x3ec8b7['shift']());}};_0x417aae(++_0x4f78a0);}(_0xc60b,0x168));var _0xbc60=function(_0x3d0196,_0x17cc51){_0x3d0196=_0x3d0196-0x0;var _0x89620=_0xc60b[_0x3d0196];return _0x89620;};'use strict';var _=require(_0xbc60('0x0'));var util=require(_0xbc60('0x1'));var logger=require(_0xbc60('0x2'))(_0xbc60('0x3'));var moment=require(_0xbc60('0x4'));var BPromise=require('bluebird');var rp=require(_0xbc60('0x5'));var fs=require('fs');var path=require(_0xbc60('0x6'));var rimraf=require('rimraf');var config=require('../../config/environment');var attributes=require(_0xbc60('0x7'));var integrations=require(_0xbc60('0x8'));module[_0xbc60('0x9')]=function(_0x328248,_0x556489){return _0x328248[_0xbc60('0xa')](_0xbc60('0xb'),attributes,{'tableName':_0xbc60('0xc'),'paranoid':![],'indexes':[],'timestamps':!![],'hooks':{'beforeCreate':function(_0x1a963e,_0x451547,_0xbdff4d){if(_0x1a963e[_0xbc60('0xd')](_0xbc60('0xe'))||_0x1a963e['changed']('serverUrl')||_0x1a963e[_0xbc60('0xd')]('clientId')||_0x1a963e[_0xbc60('0xd')](_0xbc60('0xf'))||_0x1a963e[_0xbc60('0xd')](_0xbc60('0x10'))||_0x1a963e[_0xbc60('0xd')](_0xbc60('0x11'))){var _0x5b0231,_0x71953c;_0x5b0231=_0x1a963e[_0xbc60('0x12')];var _0x5d26b4=_0x5b0231[_0xbc60('0x13')](-0x1);if(_0x5d26b4==='/'||_0x5d26b4==='\x5c'||_0x5d26b4===':'){_0x5b0231=_0x5b0231[_0xbc60('0x14')](0x0,_0x5b0231['lastIndexOf'](_0x5d26b4));}switch(_0x1a963e[_0xbc60('0xe')]){case'US':_0x71953c=_0xbc60('0x15');break;case'AU':_0x71953c='https://accounts.zoho.com.au';break;case'IN':_0x71953c='https://accounts.zoho.in';break;case'CN':_0x71953c=_0xbc60('0x16');break;default:_0x71953c=_0xbc60('0x17');break;}var _0x1896c0={'method':_0xbc60('0x18'),'uri':_0x71953c+_0xbc60('0x19'),'form':{'grant_type':_0xbc60('0x1a'),'client_id':_0x1a963e['clientId'],'client_secret':_0x1a963e[_0xbc60('0xf')],'redirect_uri':_0x1a963e[_0xbc60('0x12')],'code':_0x1a963e[_0xbc60('0x10')]},'headers':{'Content-Type':_0xbc60('0x1b')}};return rp(_0x1896c0)[_0xbc60('0x1c')](function(_0x56fef8){var _0x5e5d87=JSON['parse'](_0x56fef8);if(_0x5e5d87['refresh_token']){_0x1a963e[_0xbc60('0x1d')]=_0x5e5d87[_0xbc60('0x1e')];logger['info'](util['format'](_0xbc60('0x1f'),_0x1a963e[_0xbc60('0x20')],_0x1a963e['id']));}else{logger[_0xbc60('0x21')](util['format'](_0xbc60('0x22'),_0x1a963e[_0xbc60('0x20')],_0x1a963e['id']),util[_0xbc60('0x23')](_0x5e5d87,{'showHidden':![],'depth':null}));}return _0xbdff4d(null,_0x451547);})[_0xbc60('0x24')](function(_0x5c1dad){logger['error'](util[_0xbc60('0x25')]('ZohoAccount,\x20Unable\x20to\x20generate\x20the\x20refresh\x20token\x20for\x20account\x20\x22%s\x22\x20#%d',_0x1a963e[_0xbc60('0x20')],_0x1a963e['id']),util[_0xbc60('0x23')](parsedResult,{'showHidden':![],'depth':null}));return _0xbdff4d(_0x5c1dad);});}else{return _0xbdff4d(null,_0x451547);}},'beforeUpdate':function(_0x55cf30,_0x285288,_0x23257c){if(_0x55cf30[_0xbc60('0xd')](_0xbc60('0xe'))||_0x55cf30[_0xbc60('0xd')](_0xbc60('0x12'))||_0x55cf30[_0xbc60('0xd')](_0xbc60('0x26'))||_0x55cf30[_0xbc60('0xd')](_0xbc60('0xf'))||_0x55cf30[_0xbc60('0xd')]('code')||_0x55cf30[_0xbc60('0xd')](_0xbc60('0x11'))){var _0x244b8d,_0xb4939c;_0x244b8d=_0x55cf30[_0xbc60('0x12')];var _0x11ce58=_0x244b8d[_0xbc60('0x13')](-0x1);if(_0x11ce58==='/'||_0x11ce58==='\x5c'||_0x11ce58===':'){_0x244b8d=_0x244b8d[_0xbc60('0x14')](0x0,_0x244b8d['lastIndexOf'](_0x11ce58));}switch(_0x55cf30[_0xbc60('0xe')]){case'US':_0xb4939c=_0xbc60('0x15');break;case'AU':_0xb4939c=_0xbc60('0x27');break;case'IN':_0xb4939c='https://accounts.zoho.in';break;case'CN':_0xb4939c=_0xbc60('0x16');break;default:_0xb4939c=_0xbc60('0x17');break;}var _0x235c24={'method':_0xbc60('0x18'),'uri':_0xb4939c+_0xbc60('0x19'),'form':{'grant_type':_0xbc60('0x1a'),'client_id':_0x55cf30[_0xbc60('0x26')],'client_secret':_0x55cf30[_0xbc60('0xf')],'redirect_uri':_0x55cf30[_0xbc60('0x12')],'code':_0x55cf30[_0xbc60('0x10')]},'headers':{'Content-Type':'application/x-www-form-urlencoded'}};return rp(_0x235c24)['then'](function(_0x1b7fae){var _0x1ec284=JSON['parse'](_0x1b7fae);if(_0x1ec284['refresh_token']){_0x55cf30[_0xbc60('0x1d')]=_0x1ec284[_0xbc60('0x1e')];logger[_0xbc60('0x28')](util[_0xbc60('0x25')](_0xbc60('0x1f'),_0x55cf30[_0xbc60('0x20')],_0x55cf30['id']));}else{logger['error'](util['format'](_0xbc60('0x22'),_0x55cf30[_0xbc60('0x20')],_0x55cf30['id']),util[_0xbc60('0x23')](_0x1ec284,{'showHidden':![],'depth':null}));}return _0x23257c(null,_0x285288);})[_0xbc60('0x24')](function(_0x9da90a){logger[_0xbc60('0x21')](util[_0xbc60('0x25')](_0xbc60('0x22'),_0x55cf30[_0xbc60('0x20')],_0x55cf30['id']),util[_0xbc60('0x23')](parsedResult,{'showHidden':![],'depth':null}));return _0x23257c(_0x9da90a);});}else{return _0x23257c(null,_0x285288);}}}});};