ba9338b21b9b70dfc229885806c0d3a12e31d86f
[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 _0xdff9=['https://accounts.zoho.com.au','https://accounts.zoho.in','https://accounts.zoho.com.cn','https://accounts.zoho.eu','POST','/oauth/v2/token','authorization_code','clientSecret','application/x-www-form-urlencoded','then','refresh_token','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','../../config/logger','moment','bluebird','path','rimraf','../../config/environment','../../components/integrations/configuration','exports','define','ZohoAccount','int_zoho_accounts','changed','zone','serverUrl','clientId','code','host','slice','substring','lastIndexOf','https://accounts.zoho.com'];(function(_0x379eaf,_0x4eb489){var _0x326f1f=function(_0x4a8bc2){while(--_0x4a8bc2){_0x379eaf['push'](_0x379eaf['shift']());}};_0x326f1f(++_0x4eb489);}(_0xdff9,0xd5));var _0x9dff=function(_0x48abc9,_0x211c86){_0x48abc9=_0x48abc9-0x0;var _0x189950=_0xdff9[_0x48abc9];return _0x189950;};'use strict';var _=require('lodash');var util=require('util');var logger=require(_0x9dff('0x0'))('api');var moment=require(_0x9dff('0x1'));var BPromise=require(_0x9dff('0x2'));var rp=require('request-promise');var fs=require('fs');var path=require(_0x9dff('0x3'));var rimraf=require(_0x9dff('0x4'));var config=require(_0x9dff('0x5'));var attributes=require('./intZohoAccount.attributes');var integrations=require(_0x9dff('0x6'));module[_0x9dff('0x7')]=function(_0x5f347b,_0x59c995){return _0x5f347b[_0x9dff('0x8')](_0x9dff('0x9'),attributes,{'tableName':_0x9dff('0xa'),'paranoid':![],'indexes':[],'timestamps':!![],'hooks':{'beforeCreate':function(_0x2e3b0f,_0x537b50,_0x509a80){if(_0x2e3b0f[_0x9dff('0xb')](_0x9dff('0xc'))||_0x2e3b0f['changed'](_0x9dff('0xd'))||_0x2e3b0f[_0x9dff('0xb')](_0x9dff('0xe'))||_0x2e3b0f['changed']('clientSecret')||_0x2e3b0f[_0x9dff('0xb')](_0x9dff('0xf'))||_0x2e3b0f[_0x9dff('0xb')](_0x9dff('0x10'))){var _0x4a24b6,_0x485260;_0x4a24b6=_0x2e3b0f[_0x9dff('0xd')];var _0x379210=_0x4a24b6[_0x9dff('0x11')](-0x1);if(_0x379210==='/'||_0x379210==='\x5c'||_0x379210===':'){_0x4a24b6=_0x4a24b6[_0x9dff('0x12')](0x0,_0x4a24b6[_0x9dff('0x13')](_0x379210));}switch(_0x2e3b0f['zone']){case'US':_0x485260=_0x9dff('0x14');break;case'AU':_0x485260=_0x9dff('0x15');break;case'IN':_0x485260=_0x9dff('0x16');break;case'CN':_0x485260=_0x9dff('0x17');break;default:_0x485260=_0x9dff('0x18');break;}var _0xd84a53={'method':_0x9dff('0x19'),'uri':_0x485260+_0x9dff('0x1a'),'form':{'grant_type':_0x9dff('0x1b'),'client_id':_0x2e3b0f[_0x9dff('0xe')],'client_secret':_0x2e3b0f[_0x9dff('0x1c')],'redirect_uri':_0x2e3b0f['serverUrl'],'code':_0x2e3b0f[_0x9dff('0xf')]},'headers':{'Content-Type':_0x9dff('0x1d')}};return rp(_0xd84a53)[_0x9dff('0x1e')](function(_0x5c7de8){var _0x552456=JSON['parse'](_0x5c7de8);if(_0x552456[_0x9dff('0x1f')]){_0x2e3b0f[_0x9dff('0x20')]=_0x552456[_0x9dff('0x1f')];logger[_0x9dff('0x21')](util[_0x9dff('0x22')](_0x9dff('0x23'),_0x2e3b0f[_0x9dff('0x24')],_0x2e3b0f['id']));}else{logger[_0x9dff('0x25')](util[_0x9dff('0x22')]('ZohoAccount,\x20Unable\x20to\x20generate\x20the\x20refresh\x20token\x20for\x20account\x20\x22%s\x22\x20#%d',_0x2e3b0f[_0x9dff('0x24')],_0x2e3b0f['id']),util['inspect'](_0x552456,{'showHidden':![],'depth':null}));}return _0x509a80(null,_0x537b50);})['catch'](function(_0x1effb5){logger['error'](util['format'](_0x9dff('0x26'),_0x2e3b0f[_0x9dff('0x24')],_0x2e3b0f['id']),util['inspect'](parsedResult,{'showHidden':![],'depth':null}));return _0x509a80(_0x1effb5);});}else{return _0x509a80(null,_0x537b50);}},'beforeUpdate':function(_0x457505,_0x6699eb,_0x3650e3){if(_0x457505[_0x9dff('0xb')](_0x9dff('0xc'))||_0x457505['changed']('serverUrl')||_0x457505[_0x9dff('0xb')](_0x9dff('0xe'))||_0x457505['changed']('clientSecret')||_0x457505[_0x9dff('0xb')]('code')||_0x457505[_0x9dff('0xb')](_0x9dff('0x10'))){var _0x34121c,_0x2e5347;_0x34121c=_0x457505[_0x9dff('0xd')];var _0x19cdc4=_0x34121c['slice'](-0x1);if(_0x19cdc4==='/'||_0x19cdc4==='\x5c'||_0x19cdc4===':'){_0x34121c=_0x34121c['substring'](0x0,_0x34121c[_0x9dff('0x13')](_0x19cdc4));}switch(_0x457505[_0x9dff('0xc')]){case'US':_0x2e5347=_0x9dff('0x14');break;case'AU':_0x2e5347=_0x9dff('0x15');break;case'IN':_0x2e5347=_0x9dff('0x16');break;case'CN':_0x2e5347=_0x9dff('0x17');break;default:_0x2e5347='https://accounts.zoho.eu';break;}var _0x30f8fb={'method':_0x9dff('0x19'),'uri':_0x2e5347+_0x9dff('0x1a'),'form':{'grant_type':'authorization_code','client_id':_0x457505[_0x9dff('0xe')],'client_secret':_0x457505[_0x9dff('0x1c')],'redirect_uri':_0x457505[_0x9dff('0xd')],'code':_0x457505['code']},'headers':{'Content-Type':_0x9dff('0x1d')}};return rp(_0x30f8fb)[_0x9dff('0x1e')](function(_0x5799d7){var _0x497bd0=JSON['parse'](_0x5799d7);if(_0x497bd0[_0x9dff('0x1f')]){_0x457505[_0x9dff('0x20')]=_0x497bd0[_0x9dff('0x1f')];logger[_0x9dff('0x21')](util['format'](_0x9dff('0x23'),_0x457505[_0x9dff('0x24')],_0x457505['id']));}else{logger[_0x9dff('0x25')](util[_0x9dff('0x22')](_0x9dff('0x26'),_0x457505['name'],_0x457505['id']),util['inspect'](_0x497bd0,{'showHidden':![],'depth':null}));}return _0x3650e3(null,_0x6699eb);})['catch'](function(_0xbaafd6){logger[_0x9dff('0x25')](util['format'](_0x9dff('0x26'),_0x457505[_0x9dff('0x24')],_0x457505['id']),util['inspect'](parsedResult,{'showHidden':![],'depth':null}));return _0x3650e3(_0xbaafd6);});}else{return _0x3650e3(null,_0x6699eb);}}}});};