Built motion from commit 639a1a7f.|2.6.19
[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 _0x2e6a=['ZohoAccount','int_zoho_accounts','changed','zone','serverUrl','clientId','clientSecret','host','slice','lastIndexOf','https://accounts.zoho.com','https://accounts.zoho.eu','POST','/oauth/v2/token','code','application/x-www-form-urlencoded','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','error','substring','https://accounts.zoho.com.au','https://accounts.zoho.in','https://accounts.zoho.com.cn','authorization_code','catch','util','../../config/logger','bluebird','request-promise','path','../../components/integrations/configuration','exports','define'];(function(_0x2db3eb,_0x415d67){var _0x16ec81=function(_0x355dd2){while(--_0x355dd2){_0x2db3eb['push'](_0x2db3eb['shift']());}};_0x16ec81(++_0x415d67);}(_0x2e6a,0xee));var _0xa2e6=function(_0x465028,_0x4570ac){_0x465028=_0x465028-0x0;var _0xb783d4=_0x2e6a[_0x465028];return _0xb783d4;};'use strict';var _=require('lodash');var util=require(_0xa2e6('0x0'));var logger=require(_0xa2e6('0x1'))('api');var moment=require('moment');var BPromise=require(_0xa2e6('0x2'));var rp=require(_0xa2e6('0x3'));var fs=require('fs');var path=require(_0xa2e6('0x4'));var rimraf=require('rimraf');var config=require('../../config/environment');var attributes=require('./intZohoAccount.attributes');var integrations=require(_0xa2e6('0x5'));module[_0xa2e6('0x6')]=function(_0x2d0df8,_0x1ee828){return _0x2d0df8[_0xa2e6('0x7')](_0xa2e6('0x8'),attributes,{'tableName':_0xa2e6('0x9'),'paranoid':![],'indexes':[],'timestamps':!![],'hooks':{'beforeCreate':function(_0x9935bf,_0x4479fd,_0x3925ec){if(_0x9935bf[_0xa2e6('0xa')](_0xa2e6('0xb'))||_0x9935bf[_0xa2e6('0xa')](_0xa2e6('0xc'))||_0x9935bf[_0xa2e6('0xa')](_0xa2e6('0xd'))||_0x9935bf['changed'](_0xa2e6('0xe'))||_0x9935bf[_0xa2e6('0xa')]('code')||_0x9935bf[_0xa2e6('0xa')](_0xa2e6('0xf'))){var _0x521ac7,_0xf5d6d;_0x521ac7=_0x9935bf[_0xa2e6('0xc')];var _0x154438=_0x521ac7[_0xa2e6('0x10')](-0x1);if(_0x154438==='/'||_0x154438==='\x5c'||_0x154438===':'){_0x521ac7=_0x521ac7['substring'](0x0,_0x521ac7[_0xa2e6('0x11')](_0x154438));}switch(_0x9935bf[_0xa2e6('0xb')]){case'US':_0xf5d6d=_0xa2e6('0x12');break;case'AU':_0xf5d6d='https://accounts.zoho.com.au';break;case'IN':_0xf5d6d='https://accounts.zoho.in';break;case'CN':_0xf5d6d='https://accounts.zoho.com.cn';break;default:_0xf5d6d=_0xa2e6('0x13');break;}var _0x37a7c9={'method':_0xa2e6('0x14'),'uri':_0xf5d6d+_0xa2e6('0x15'),'form':{'grant_type':'authorization_code','client_id':_0x9935bf[_0xa2e6('0xd')],'client_secret':_0x9935bf['clientSecret'],'redirect_uri':_0x9935bf['serverUrl'],'code':_0x9935bf[_0xa2e6('0x16')]},'headers':{'Content-Type':_0xa2e6('0x17')}};return rp(_0x37a7c9)[_0xa2e6('0x18')](function(_0x3e37e5){var _0x3f7310=JSON[_0xa2e6('0x19')](_0x3e37e5);if(_0x3f7310[_0xa2e6('0x1a')]){_0x9935bf[_0xa2e6('0x1b')]=_0x3f7310[_0xa2e6('0x1a')];logger[_0xa2e6('0x1c')](util[_0xa2e6('0x1d')](_0xa2e6('0x1e'),_0x9935bf[_0xa2e6('0x1f')],_0x9935bf['id']));}else{logger['error'](util[_0xa2e6('0x1d')](_0xa2e6('0x20'),_0x9935bf[_0xa2e6('0x1f')],_0x9935bf['id']),util[_0xa2e6('0x21')](_0x3f7310,{'showHidden':![],'depth':null}));}return _0x3925ec(null,_0x4479fd);})['catch'](function(_0x5cbd90){logger[_0xa2e6('0x22')](util[_0xa2e6('0x1d')](_0xa2e6('0x20'),_0x9935bf[_0xa2e6('0x1f')],_0x9935bf['id']),util[_0xa2e6('0x21')](parsedResult,{'showHidden':![],'depth':null}));return _0x3925ec(_0x5cbd90);});}else{return _0x3925ec(null,_0x4479fd);}},'beforeUpdate':function(_0x56e46c,_0x415ac0,_0x2682c0){if(_0x56e46c[_0xa2e6('0xa')](_0xa2e6('0xb'))||_0x56e46c['changed'](_0xa2e6('0xc'))||_0x56e46c[_0xa2e6('0xa')](_0xa2e6('0xd'))||_0x56e46c[_0xa2e6('0xa')]('clientSecret')||_0x56e46c['changed'](_0xa2e6('0x16'))||_0x56e46c[_0xa2e6('0xa')](_0xa2e6('0xf'))){var _0x12d0f1,_0x37445a;_0x12d0f1=_0x56e46c[_0xa2e6('0xc')];var _0x21fc3a=_0x12d0f1[_0xa2e6('0x10')](-0x1);if(_0x21fc3a==='/'||_0x21fc3a==='\x5c'||_0x21fc3a===':'){_0x12d0f1=_0x12d0f1[_0xa2e6('0x23')](0x0,_0x12d0f1[_0xa2e6('0x11')](_0x21fc3a));}switch(_0x56e46c[_0xa2e6('0xb')]){case'US':_0x37445a=_0xa2e6('0x12');break;case'AU':_0x37445a=_0xa2e6('0x24');break;case'IN':_0x37445a=_0xa2e6('0x25');break;case'CN':_0x37445a=_0xa2e6('0x26');break;default:_0x37445a='https://accounts.zoho.eu';break;}var _0x272027={'method':_0xa2e6('0x14'),'uri':_0x37445a+_0xa2e6('0x15'),'form':{'grant_type':_0xa2e6('0x27'),'client_id':_0x56e46c[_0xa2e6('0xd')],'client_secret':_0x56e46c[_0xa2e6('0xe')],'redirect_uri':_0x56e46c[_0xa2e6('0xc')],'code':_0x56e46c[_0xa2e6('0x16')]},'headers':{'Content-Type':_0xa2e6('0x17')}};return rp(_0x272027)[_0xa2e6('0x18')](function(_0x2e8074){var _0x2fb2b3=JSON[_0xa2e6('0x19')](_0x2e8074);if(_0x2fb2b3['refresh_token']){_0x56e46c[_0xa2e6('0x1b')]=_0x2fb2b3[_0xa2e6('0x1a')];logger[_0xa2e6('0x1c')](util[_0xa2e6('0x1d')]('ZohoAccount,\x20Refresh\x20token\x20successfully\x20generated\x20for\x20account\x20\x22%s\x22\x20#%d',_0x56e46c[_0xa2e6('0x1f')],_0x56e46c['id']));}else{logger['error'](util['format'](_0xa2e6('0x20'),_0x56e46c[_0xa2e6('0x1f')],_0x56e46c['id']),util[_0xa2e6('0x21')](_0x2fb2b3,{'showHidden':![],'depth':null}));}return _0x2682c0(null,_0x415ac0);})[_0xa2e6('0x28')](function(_0x857513){logger[_0xa2e6('0x22')](util['format'](_0xa2e6('0x20'),_0x56e46c[_0xa2e6('0x1f')],_0x56e46c['id']),util['inspect'](parsedResult,{'showHidden':![],'depth':null}));return _0x2682c0(_0x857513);});}else{return _0x2682c0(null,_0x415ac0);}}}});};