Built motion from commit (unavailable).|2.5.31
[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 _0x59f7=['api','moment','request-promise','path','rimraf','../../config/environment','../../components/integrations/configuration','exports','define','ZohoAccount','int_zoho_accounts','zone','changed','serverUrl','clientId','clientSecret','code','host','slice','substring','https://accounts.zoho.com.au','https://accounts.zoho.eu','POST','/oauth/v2/token','authorization_code','parse','refreshToken','info','format','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','lastIndexOf','https://accounts.zoho.com','https://accounts.zoho.in','https://accounts.zoho.com.cn','application/x-www-form-urlencoded','then','refresh_token','catch','util'];(function(_0x2077b2,_0x294462){var _0x43249c=function(_0xc3c85c){while(--_0xc3c85c){_0x2077b2['push'](_0x2077b2['shift']());}};_0x43249c(++_0x294462);}(_0x59f7,0x80));var _0x759f=function(_0x151f01,_0x4528bb){_0x151f01=_0x151f01-0x0;var _0x5aae39=_0x59f7[_0x151f01];return _0x5aae39;};'use strict';var _=require('lodash');var util=require(_0x759f('0x0'));var logger=require('../../config/logger')(_0x759f('0x1'));var moment=require(_0x759f('0x2'));var BPromise=require('bluebird');var rp=require(_0x759f('0x3'));var fs=require('fs');var path=require(_0x759f('0x4'));var rimraf=require(_0x759f('0x5'));var config=require(_0x759f('0x6'));var attributes=require('./intZohoAccount.attributes');var integrations=require(_0x759f('0x7'));module[_0x759f('0x8')]=function(_0x5f062c,_0x537609){return _0x5f062c[_0x759f('0x9')](_0x759f('0xa'),attributes,{'tableName':_0x759f('0xb'),'paranoid':![],'indexes':[],'timestamps':!![],'hooks':{'beforeCreate':function(_0x5d2615,_0x4395ff,_0x30adc6){if(_0x5d2615['changed'](_0x759f('0xc'))||_0x5d2615[_0x759f('0xd')](_0x759f('0xe'))||_0x5d2615[_0x759f('0xd')](_0x759f('0xf'))||_0x5d2615[_0x759f('0xd')](_0x759f('0x10'))||_0x5d2615[_0x759f('0xd')](_0x759f('0x11'))||_0x5d2615['changed'](_0x759f('0x12'))){var _0x231132,_0x3af30f;_0x231132=_0x5d2615['serverUrl'];var _0xbd339e=_0x231132[_0x759f('0x13')](-0x1);if(_0xbd339e==='/'||_0xbd339e==='\x5c'||_0xbd339e===':'){_0x231132=_0x231132[_0x759f('0x14')](0x0,_0x231132['lastIndexOf'](_0xbd339e));}switch(_0x5d2615['zone']){case'US':_0x3af30f='https://accounts.zoho.com';break;case'AU':_0x3af30f=_0x759f('0x15');break;case'IN':_0x3af30f='https://accounts.zoho.in';break;case'CN':_0x3af30f='https://accounts.zoho.com.cn';break;default:_0x3af30f=_0x759f('0x16');break;}var _0x3fdbba={'method':_0x759f('0x17'),'uri':_0x3af30f+_0x759f('0x18'),'form':{'grant_type':_0x759f('0x19'),'client_id':_0x5d2615[_0x759f('0xf')],'client_secret':_0x5d2615['clientSecret'],'redirect_uri':_0x5d2615[_0x759f('0xe')],'code':_0x5d2615['code']},'headers':{'Content-Type':'application/x-www-form-urlencoded'}};return rp(_0x3fdbba)['then'](function(_0x2b2e8e){var _0x5297c2=JSON[_0x759f('0x1a')](_0x2b2e8e);if(_0x5297c2['refresh_token']){_0x5d2615[_0x759f('0x1b')]=_0x5297c2['refresh_token'];logger[_0x759f('0x1c')](util[_0x759f('0x1d')](_0x759f('0x1e'),_0x5d2615[_0x759f('0x1f')],_0x5d2615['id']));}else{logger[_0x759f('0x20')](util['format'](_0x759f('0x21'),_0x5d2615[_0x759f('0x1f')],_0x5d2615['id']),util[_0x759f('0x22')](_0x5297c2,{'showHidden':![],'depth':null}));}return _0x30adc6(null,_0x4395ff);})['catch'](function(_0x39c04d){logger['error'](util['format'](_0x759f('0x21'),_0x5d2615[_0x759f('0x1f')],_0x5d2615['id']),util[_0x759f('0x22')](parsedResult,{'showHidden':![],'depth':null}));return _0x30adc6(_0x39c04d);});}else{return _0x30adc6(null,_0x4395ff);}},'beforeUpdate':function(_0x3791b1,_0x13007c,_0x44e789){if(_0x3791b1[_0x759f('0xd')]('zone')||_0x3791b1[_0x759f('0xd')](_0x759f('0xe'))||_0x3791b1[_0x759f('0xd')](_0x759f('0xf'))||_0x3791b1[_0x759f('0xd')]('clientSecret')||_0x3791b1[_0x759f('0xd')](_0x759f('0x11'))||_0x3791b1[_0x759f('0xd')](_0x759f('0x12'))){var _0x486f42,_0x38e2dc;_0x486f42=_0x3791b1[_0x759f('0xe')];var _0x2df67e=_0x486f42[_0x759f('0x13')](-0x1);if(_0x2df67e==='/'||_0x2df67e==='\x5c'||_0x2df67e===':'){_0x486f42=_0x486f42[_0x759f('0x14')](0x0,_0x486f42[_0x759f('0x23')](_0x2df67e));}switch(_0x3791b1[_0x759f('0xc')]){case'US':_0x38e2dc=_0x759f('0x24');break;case'AU':_0x38e2dc=_0x759f('0x15');break;case'IN':_0x38e2dc=_0x759f('0x25');break;case'CN':_0x38e2dc=_0x759f('0x26');break;default:_0x38e2dc='https://accounts.zoho.eu';break;}var _0x3f0a74={'method':_0x759f('0x17'),'uri':_0x38e2dc+'/oauth/v2/token','form':{'grant_type':'authorization_code','client_id':_0x3791b1[_0x759f('0xf')],'client_secret':_0x3791b1[_0x759f('0x10')],'redirect_uri':_0x3791b1[_0x759f('0xe')],'code':_0x3791b1[_0x759f('0x11')]},'headers':{'Content-Type':_0x759f('0x27')}};return rp(_0x3f0a74)[_0x759f('0x28')](function(_0x229396){var _0x5ae103=JSON['parse'](_0x229396);if(_0x5ae103[_0x759f('0x29')]){_0x3791b1[_0x759f('0x1b')]=_0x5ae103[_0x759f('0x29')];logger['info'](util[_0x759f('0x1d')]('ZohoAccount,\x20Refresh\x20token\x20successfully\x20generated\x20for\x20account\x20\x22%s\x22\x20#%d',_0x3791b1[_0x759f('0x1f')],_0x3791b1['id']));}else{logger[_0x759f('0x20')](util['format'](_0x759f('0x21'),_0x3791b1['name'],_0x3791b1['id']),util[_0x759f('0x22')](_0x5ae103,{'showHidden':![],'depth':null}));}return _0x44e789(null,_0x13007c);})[_0x759f('0x2a')](function(_0x3f98b1){logger['error'](util[_0x759f('0x1d')](_0x759f('0x21'),_0x3791b1[_0x759f('0x1f')],_0x3791b1['id']),util[_0x759f('0x22')](parsedResult,{'showHidden':![],'depth':null}));return _0x44e789(_0x3f98b1);});}else{return _0x44e789(null,_0x13007c);}}}});};