Built motion from commit 47436d59.|2.6.7
[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 _0x4d58=['inspect','https://accounts.zoho.com','https://accounts.zoho.in','https://accounts.zoho.com.cn','POST','then','../../config/logger','api','bluebird','path','rimraf','./intZohoAccount.attributes','exports','define','int_zoho_accounts','changed','zone','clientSecret','code','host','serverUrl','slice','substring','lastIndexOf','https://accounts.zoho.com.au','https://accounts.zoho.eu','/oauth/v2/token','authorization_code','clientId','application/x-www-form-urlencoded','parse','refreshToken','refresh_token','format','ZohoAccount,\x20Refresh\x20token\x20successfully\x20generated\x20for\x20account\x20\x22%s\x22\x20#%d','error','ZohoAccount,\x20Unable\x20to\x20generate\x20the\x20refresh\x20token\x20for\x20account\x20\x22%s\x22\x20#%d','name','catch'];(function(_0x477575,_0xf4784b){var _0x3b3751=function(_0x7074d6){while(--_0x7074d6){_0x477575['push'](_0x477575['shift']());}};_0x3b3751(++_0xf4784b);}(_0x4d58,0x7b));var _0x84d5=function(_0x244b89,_0x4eea72){_0x244b89=_0x244b89-0x0;var _0x1a4c2f=_0x4d58[_0x244b89];return _0x1a4c2f;};'use strict';var _=require('lodash');var util=require('util');var logger=require(_0x84d5('0x0'))(_0x84d5('0x1'));var moment=require('moment');var BPromise=require(_0x84d5('0x2'));var rp=require('request-promise');var fs=require('fs');var path=require(_0x84d5('0x3'));var rimraf=require(_0x84d5('0x4'));var config=require('../../config/environment');var attributes=require(_0x84d5('0x5'));var integrations=require('../../components/integrations/configuration');module[_0x84d5('0x6')]=function(_0x21e154,_0x5d5337){return _0x21e154[_0x84d5('0x7')]('ZohoAccount',attributes,{'tableName':_0x84d5('0x8'),'paranoid':![],'indexes':[],'timestamps':!![],'hooks':{'beforeCreate':function(_0x56d055,_0x46efc6,_0x4d46cd){if(_0x56d055[_0x84d5('0x9')](_0x84d5('0xa'))||_0x56d055[_0x84d5('0x9')]('serverUrl')||_0x56d055['changed']('clientId')||_0x56d055[_0x84d5('0x9')](_0x84d5('0xb'))||_0x56d055[_0x84d5('0x9')](_0x84d5('0xc'))||_0x56d055['changed'](_0x84d5('0xd'))){var _0x7fa568,_0x2e1e08;_0x7fa568=_0x56d055[_0x84d5('0xe')];var _0x3bcbd3=_0x7fa568[_0x84d5('0xf')](-0x1);if(_0x3bcbd3==='/'||_0x3bcbd3==='\x5c'||_0x3bcbd3===':'){_0x7fa568=_0x7fa568[_0x84d5('0x10')](0x0,_0x7fa568[_0x84d5('0x11')](_0x3bcbd3));}switch(_0x56d055[_0x84d5('0xa')]){case'US':_0x2e1e08='https://accounts.zoho.com';break;case'AU':_0x2e1e08=_0x84d5('0x12');break;case'IN':_0x2e1e08='https://accounts.zoho.in';break;case'CN':_0x2e1e08='https://accounts.zoho.com.cn';break;default:_0x2e1e08=_0x84d5('0x13');break;}var _0x496e66={'method':'POST','uri':_0x2e1e08+_0x84d5('0x14'),'form':{'grant_type':_0x84d5('0x15'),'client_id':_0x56d055[_0x84d5('0x16')],'client_secret':_0x56d055[_0x84d5('0xb')],'redirect_uri':_0x56d055[_0x84d5('0xe')],'code':_0x56d055[_0x84d5('0xc')]},'headers':{'Content-Type':_0x84d5('0x17')}};return rp(_0x496e66)['then'](function(_0xcff251){var _0x28e4f0=JSON[_0x84d5('0x18')](_0xcff251);if(_0x28e4f0['refresh_token']){_0x56d055[_0x84d5('0x19')]=_0x28e4f0[_0x84d5('0x1a')];logger['info'](util[_0x84d5('0x1b')](_0x84d5('0x1c'),_0x56d055['name'],_0x56d055['id']));}else{logger[_0x84d5('0x1d')](util[_0x84d5('0x1b')](_0x84d5('0x1e'),_0x56d055[_0x84d5('0x1f')],_0x56d055['id']),util['inspect'](_0x28e4f0,{'showHidden':![],'depth':null}));}return _0x4d46cd(null,_0x46efc6);})[_0x84d5('0x20')](function(_0x580547){logger['error'](util[_0x84d5('0x1b')](_0x84d5('0x1e'),_0x56d055['name'],_0x56d055['id']),util[_0x84d5('0x21')](parsedResult,{'showHidden':![],'depth':null}));return _0x4d46cd(_0x580547);});}else{return _0x4d46cd(null,_0x46efc6);}},'beforeUpdate':function(_0x3356aa,_0xc98b1f,_0x1b2078){if(_0x3356aa['changed']('zone')||_0x3356aa[_0x84d5('0x9')]('serverUrl')||_0x3356aa[_0x84d5('0x9')](_0x84d5('0x16'))||_0x3356aa[_0x84d5('0x9')]('clientSecret')||_0x3356aa['changed'](_0x84d5('0xc'))||_0x3356aa['changed'](_0x84d5('0xd'))){var _0x1d0cbc,_0x383b20;_0x1d0cbc=_0x3356aa[_0x84d5('0xe')];var _0xcb6849=_0x1d0cbc['slice'](-0x1);if(_0xcb6849==='/'||_0xcb6849==='\x5c'||_0xcb6849===':'){_0x1d0cbc=_0x1d0cbc[_0x84d5('0x10')](0x0,_0x1d0cbc[_0x84d5('0x11')](_0xcb6849));}switch(_0x3356aa[_0x84d5('0xa')]){case'US':_0x383b20=_0x84d5('0x22');break;case'AU':_0x383b20=_0x84d5('0x12');break;case'IN':_0x383b20=_0x84d5('0x23');break;case'CN':_0x383b20=_0x84d5('0x24');break;default:_0x383b20=_0x84d5('0x13');break;}var _0xa54c42={'method':_0x84d5('0x25'),'uri':_0x383b20+_0x84d5('0x14'),'form':{'grant_type':_0x84d5('0x15'),'client_id':_0x3356aa[_0x84d5('0x16')],'client_secret':_0x3356aa[_0x84d5('0xb')],'redirect_uri':_0x3356aa[_0x84d5('0xe')],'code':_0x3356aa[_0x84d5('0xc')]},'headers':{'Content-Type':_0x84d5('0x17')}};return rp(_0xa54c42)[_0x84d5('0x26')](function(_0x4a3ef4){var _0x594f7a=JSON['parse'](_0x4a3ef4);if(_0x594f7a[_0x84d5('0x1a')]){_0x3356aa['refreshToken']=_0x594f7a[_0x84d5('0x1a')];logger['info'](util['format'](_0x84d5('0x1c'),_0x3356aa[_0x84d5('0x1f')],_0x3356aa['id']));}else{logger[_0x84d5('0x1d')](util[_0x84d5('0x1b')](_0x84d5('0x1e'),_0x3356aa[_0x84d5('0x1f')],_0x3356aa['id']),util[_0x84d5('0x21')](_0x594f7a,{'showHidden':![],'depth':null}));}return _0x1b2078(null,_0xc98b1f);})[_0x84d5('0x20')](function(_0x45375d){logger[_0x84d5('0x1d')](util['format'](_0x84d5('0x1e'),_0x3356aa[_0x84d5('0x1f')],_0x3356aa['id']),util['inspect'](parsedResult,{'showHidden':![],'depth':null}));return _0x1b2078(_0x45375d);});}else{return _0x1b2078(null,_0xc98b1f);}}}});};