Built motion from commit da617fac.|2.6.6
[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 _0xf63a=['request-promise','path','rimraf','../../config/environment','./intZohoAccount.attributes','ZohoAccount','int_zoho_accounts','zone','changed','serverUrl','clientId','clientSecret','host','slice','substring','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','then','parse','refresh_token','refreshToken','info','format','ZohoAccount,\x20Refresh\x20token\x20successfully\x20generated\x20for\x20account\x20\x22%s\x22\x20#%d','name','ZohoAccount,\x20Unable\x20to\x20generate\x20the\x20refresh\x20token\x20for\x20account\x20\x22%s\x22\x20#%d','inspect','catch','https://accounts.zoho.eu','error','util','../../config/logger','moment'];(function(_0x4a5ff5,_0x4ee65b){var _0x5c464f=function(_0xadcc79){while(--_0xadcc79){_0x4a5ff5['push'](_0x4a5ff5['shift']());}};_0x5c464f(++_0x4ee65b);}(_0xf63a,0x9d));var _0xaf63=function(_0x56f55b,_0x1c02aa){_0x56f55b=_0x56f55b-0x0;var _0x232941=_0xf63a[_0x56f55b];return _0x232941;};'use strict';var _=require('lodash');var util=require(_0xaf63('0x0'));var logger=require(_0xaf63('0x1'))('api');var moment=require(_0xaf63('0x2'));var BPromise=require('bluebird');var rp=require(_0xaf63('0x3'));var fs=require('fs');var path=require(_0xaf63('0x4'));var rimraf=require(_0xaf63('0x5'));var config=require(_0xaf63('0x6'));var attributes=require(_0xaf63('0x7'));var integrations=require('../../components/integrations/configuration');module['exports']=function(_0xfea527,_0x37f748){return _0xfea527['define'](_0xaf63('0x8'),attributes,{'tableName':_0xaf63('0x9'),'paranoid':![],'indexes':[],'timestamps':!![],'hooks':{'beforeCreate':function(_0x5a1ac4,_0x1bd87f,_0x46d2d4){if(_0x5a1ac4['changed'](_0xaf63('0xa'))||_0x5a1ac4[_0xaf63('0xb')](_0xaf63('0xc'))||_0x5a1ac4[_0xaf63('0xb')](_0xaf63('0xd'))||_0x5a1ac4[_0xaf63('0xb')](_0xaf63('0xe'))||_0x5a1ac4['changed']('code')||_0x5a1ac4['changed'](_0xaf63('0xf'))){var _0x272233,_0x22e002;_0x272233=_0x5a1ac4[_0xaf63('0xc')];var _0x9328ef=_0x272233[_0xaf63('0x10')](-0x1);if(_0x9328ef==='/'||_0x9328ef==='\x5c'||_0x9328ef===':'){_0x272233=_0x272233[_0xaf63('0x11')](0x0,_0x272233[_0xaf63('0x12')](_0x9328ef));}switch(_0x5a1ac4[_0xaf63('0xa')]){case'US':_0x22e002=_0xaf63('0x13');break;case'AU':_0x22e002=_0xaf63('0x14');break;case'IN':_0x22e002=_0xaf63('0x15');break;case'CN':_0x22e002=_0xaf63('0x16');break;default:_0x22e002='https://accounts.zoho.eu';break;}var _0x207ab9={'method':_0xaf63('0x17'),'uri':_0x22e002+_0xaf63('0x18'),'form':{'grant_type':_0xaf63('0x19'),'client_id':_0x5a1ac4[_0xaf63('0xd')],'client_secret':_0x5a1ac4[_0xaf63('0xe')],'redirect_uri':_0x5a1ac4[_0xaf63('0xc')],'code':_0x5a1ac4[_0xaf63('0x1a')]},'headers':{'Content-Type':'application/x-www-form-urlencoded'}};return rp(_0x207ab9)[_0xaf63('0x1b')](function(_0x50a220){var _0x59c873=JSON[_0xaf63('0x1c')](_0x50a220);if(_0x59c873[_0xaf63('0x1d')]){_0x5a1ac4[_0xaf63('0x1e')]=_0x59c873[_0xaf63('0x1d')];logger[_0xaf63('0x1f')](util[_0xaf63('0x20')](_0xaf63('0x21'),_0x5a1ac4[_0xaf63('0x22')],_0x5a1ac4['id']));}else{logger['error'](util[_0xaf63('0x20')](_0xaf63('0x23'),_0x5a1ac4[_0xaf63('0x22')],_0x5a1ac4['id']),util[_0xaf63('0x24')](_0x59c873,{'showHidden':![],'depth':null}));}return _0x46d2d4(null,_0x1bd87f);})[_0xaf63('0x25')](function(_0x2fcc73){logger['error'](util[_0xaf63('0x20')]('ZohoAccount,\x20Unable\x20to\x20generate\x20the\x20refresh\x20token\x20for\x20account\x20\x22%s\x22\x20#%d',_0x5a1ac4[_0xaf63('0x22')],_0x5a1ac4['id']),util['inspect'](parsedResult,{'showHidden':![],'depth':null}));return _0x46d2d4(_0x2fcc73);});}else{return _0x46d2d4(null,_0x1bd87f);}},'beforeUpdate':function(_0x1be244,_0xc909b0,_0x4dcdf9){if(_0x1be244[_0xaf63('0xb')]('zone')||_0x1be244[_0xaf63('0xb')](_0xaf63('0xc'))||_0x1be244[_0xaf63('0xb')]('clientId')||_0x1be244[_0xaf63('0xb')](_0xaf63('0xe'))||_0x1be244[_0xaf63('0xb')](_0xaf63('0x1a'))||_0x1be244[_0xaf63('0xb')](_0xaf63('0xf'))){var _0x9f675a,_0x5db2c8;_0x9f675a=_0x1be244[_0xaf63('0xc')];var _0x5334c2=_0x9f675a[_0xaf63('0x10')](-0x1);if(_0x5334c2==='/'||_0x5334c2==='\x5c'||_0x5334c2===':'){_0x9f675a=_0x9f675a[_0xaf63('0x11')](0x0,_0x9f675a[_0xaf63('0x12')](_0x5334c2));}switch(_0x1be244['zone']){case'US':_0x5db2c8=_0xaf63('0x13');break;case'AU':_0x5db2c8=_0xaf63('0x14');break;case'IN':_0x5db2c8=_0xaf63('0x15');break;case'CN':_0x5db2c8=_0xaf63('0x16');break;default:_0x5db2c8=_0xaf63('0x26');break;}var _0x39c9f5={'method':_0xaf63('0x17'),'uri':_0x5db2c8+_0xaf63('0x18'),'form':{'grant_type':'authorization_code','client_id':_0x1be244['clientId'],'client_secret':_0x1be244[_0xaf63('0xe')],'redirect_uri':_0x1be244[_0xaf63('0xc')],'code':_0x1be244[_0xaf63('0x1a')]},'headers':{'Content-Type':'application/x-www-form-urlencoded'}};return rp(_0x39c9f5)['then'](function(_0x32ba55){var _0x3fe281=JSON[_0xaf63('0x1c')](_0x32ba55);if(_0x3fe281[_0xaf63('0x1d')]){_0x1be244[_0xaf63('0x1e')]=_0x3fe281['refresh_token'];logger[_0xaf63('0x1f')](util[_0xaf63('0x20')](_0xaf63('0x21'),_0x1be244[_0xaf63('0x22')],_0x1be244['id']));}else{logger[_0xaf63('0x27')](util[_0xaf63('0x20')](_0xaf63('0x23'),_0x1be244[_0xaf63('0x22')],_0x1be244['id']),util[_0xaf63('0x24')](_0x3fe281,{'showHidden':![],'depth':null}));}return _0x4dcdf9(null,_0xc909b0);})[_0xaf63('0x25')](function(_0xa77637){logger[_0xaf63('0x27')](util[_0xaf63('0x20')](_0xaf63('0x23'),_0x1be244[_0xaf63('0x22')],_0x1be244['id']),util[_0xaf63('0x24')](parsedResult,{'showHidden':![],'depth':null}));return _0x4dcdf9(_0xa77637);});}else{return _0x4dcdf9(null,_0xc909b0);}}}});};