0e49e51ce54a19f98d86242d74617cb8583944a9
[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 _0x43ed=['name','error','inspect','catch','ZohoAccount,\x20Unable\x20to\x20generate\x20the\x20refresh\x20token\x20for\x20account\x20\x22%s\x22\x20#%d','authorization_code','parse','refreshToken','lodash','../../config/logger','api','moment','request-promise','path','rimraf','../../config/environment','./intZohoAccount.attributes','../../components/integrations/configuration','define','int_zoho_accounts','changed','serverUrl','clientId','clientSecret','host','slice','substring','lastIndexOf','zone','https://accounts.zoho.com','https://accounts.zoho.com.au','https://accounts.zoho.in','https://accounts.zoho.com.cn','https://accounts.zoho.eu','POST','/oauth/v2/token','code','then','refresh_token','info','format','ZohoAccount,\x20Refresh\x20token\x20successfully\x20generated\x20for\x20account\x20\x22%s\x22\x20#%d'];(function(_0x3c2956,_0xacaf09){var _0x125450=function(_0x267427){while(--_0x267427){_0x3c2956['push'](_0x3c2956['shift']());}};_0x125450(++_0xacaf09);}(_0x43ed,0x104));var _0xd43e=function(_0x13ea00,_0x12f610){_0x13ea00=_0x13ea00-0x0;var _0x1f7347=_0x43ed[_0x13ea00];return _0x1f7347;};'use strict';var _=require(_0xd43e('0x0'));var util=require('util');var logger=require(_0xd43e('0x1'))(_0xd43e('0x2'));var moment=require(_0xd43e('0x3'));var BPromise=require('bluebird');var rp=require(_0xd43e('0x4'));var fs=require('fs');var path=require(_0xd43e('0x5'));var rimraf=require(_0xd43e('0x6'));var config=require(_0xd43e('0x7'));var attributes=require(_0xd43e('0x8'));var integrations=require(_0xd43e('0x9'));module['exports']=function(_0x356168,_0x1ef7e3){return _0x356168[_0xd43e('0xa')]('ZohoAccount',attributes,{'tableName':_0xd43e('0xb'),'paranoid':![],'indexes':[],'timestamps':!![],'hooks':{'beforeCreate':function(_0x2ee8c6,_0x37985a,_0x2256a7){if(_0x2ee8c6['changed']('zone')||_0x2ee8c6[_0xd43e('0xc')](_0xd43e('0xd'))||_0x2ee8c6['changed'](_0xd43e('0xe'))||_0x2ee8c6[_0xd43e('0xc')](_0xd43e('0xf'))||_0x2ee8c6['changed']('code')||_0x2ee8c6[_0xd43e('0xc')](_0xd43e('0x10'))){var _0xe7d70b,_0x5944b1;_0xe7d70b=_0x2ee8c6[_0xd43e('0xd')];var _0x47c410=_0xe7d70b[_0xd43e('0x11')](-0x1);if(_0x47c410==='/'||_0x47c410==='\x5c'||_0x47c410===':'){_0xe7d70b=_0xe7d70b[_0xd43e('0x12')](0x0,_0xe7d70b[_0xd43e('0x13')](_0x47c410));}switch(_0x2ee8c6[_0xd43e('0x14')]){case'US':_0x5944b1=_0xd43e('0x15');break;case'AU':_0x5944b1=_0xd43e('0x16');break;case'IN':_0x5944b1=_0xd43e('0x17');break;case'CN':_0x5944b1=_0xd43e('0x18');break;default:_0x5944b1=_0xd43e('0x19');break;}var _0x1b9ca1={'method':_0xd43e('0x1a'),'uri':_0x5944b1+_0xd43e('0x1b'),'form':{'grant_type':'authorization_code','client_id':_0x2ee8c6['clientId'],'client_secret':_0x2ee8c6[_0xd43e('0xf')],'redirect_uri':_0x2ee8c6[_0xd43e('0xd')],'code':_0x2ee8c6[_0xd43e('0x1c')]},'headers':{'Content-Type':'application/x-www-form-urlencoded'}};return rp(_0x1b9ca1)[_0xd43e('0x1d')](function(_0x14a404){var _0x393979=JSON['parse'](_0x14a404);if(_0x393979[_0xd43e('0x1e')]){_0x2ee8c6['refreshToken']=_0x393979[_0xd43e('0x1e')];logger[_0xd43e('0x1f')](util[_0xd43e('0x20')](_0xd43e('0x21'),_0x2ee8c6[_0xd43e('0x22')],_0x2ee8c6['id']));}else{logger[_0xd43e('0x23')](util[_0xd43e('0x20')]('ZohoAccount,\x20Unable\x20to\x20generate\x20the\x20refresh\x20token\x20for\x20account\x20\x22%s\x22\x20#%d',_0x2ee8c6[_0xd43e('0x22')],_0x2ee8c6['id']),util[_0xd43e('0x24')](_0x393979,{'showHidden':![],'depth':null}));}return _0x2256a7(null,_0x37985a);})[_0xd43e('0x25')](function(_0x3757a3){logger[_0xd43e('0x23')](util[_0xd43e('0x20')](_0xd43e('0x26'),_0x2ee8c6['name'],_0x2ee8c6['id']),util[_0xd43e('0x24')](parsedResult,{'showHidden':![],'depth':null}));return _0x2256a7(_0x3757a3);});}else{return _0x2256a7(null,_0x37985a);}},'beforeUpdate':function(_0x16c55b,_0x46e969,_0x24c763){if(_0x16c55b[_0xd43e('0xc')](_0xd43e('0x14'))||_0x16c55b[_0xd43e('0xc')](_0xd43e('0xd'))||_0x16c55b['changed']('clientId')||_0x16c55b['changed'](_0xd43e('0xf'))||_0x16c55b['changed'](_0xd43e('0x1c'))||_0x16c55b[_0xd43e('0xc')](_0xd43e('0x10'))){var _0x2fb5e8,_0x5ebc25;_0x2fb5e8=_0x16c55b[_0xd43e('0xd')];var _0x4e67a8=_0x2fb5e8['slice'](-0x1);if(_0x4e67a8==='/'||_0x4e67a8==='\x5c'||_0x4e67a8===':'){_0x2fb5e8=_0x2fb5e8['substring'](0x0,_0x2fb5e8[_0xd43e('0x13')](_0x4e67a8));}switch(_0x16c55b[_0xd43e('0x14')]){case'US':_0x5ebc25='https://accounts.zoho.com';break;case'AU':_0x5ebc25=_0xd43e('0x16');break;case'IN':_0x5ebc25='https://accounts.zoho.in';break;case'CN':_0x5ebc25=_0xd43e('0x18');break;default:_0x5ebc25=_0xd43e('0x19');break;}var _0x37deba={'method':_0xd43e('0x1a'),'uri':_0x5ebc25+_0xd43e('0x1b'),'form':{'grant_type':_0xd43e('0x27'),'client_id':_0x16c55b[_0xd43e('0xe')],'client_secret':_0x16c55b[_0xd43e('0xf')],'redirect_uri':_0x16c55b[_0xd43e('0xd')],'code':_0x16c55b['code']},'headers':{'Content-Type':'application/x-www-form-urlencoded'}};return rp(_0x37deba)[_0xd43e('0x1d')](function(_0x310da7){var _0x7936ac=JSON[_0xd43e('0x28')](_0x310da7);if(_0x7936ac['refresh_token']){_0x16c55b[_0xd43e('0x29')]=_0x7936ac['refresh_token'];logger[_0xd43e('0x1f')](util[_0xd43e('0x20')]('ZohoAccount,\x20Refresh\x20token\x20successfully\x20generated\x20for\x20account\x20\x22%s\x22\x20#%d',_0x16c55b[_0xd43e('0x22')],_0x16c55b['id']));}else{logger[_0xd43e('0x23')](util['format']('ZohoAccount,\x20Unable\x20to\x20generate\x20the\x20refresh\x20token\x20for\x20account\x20\x22%s\x22\x20#%d',_0x16c55b['name'],_0x16c55b['id']),util[_0xd43e('0x24')](_0x7936ac,{'showHidden':![],'depth':null}));}return _0x24c763(null,_0x46e969);})[_0xd43e('0x25')](function(_0x261923){logger[_0xd43e('0x23')](util[_0xd43e('0x20')](_0xd43e('0x26'),_0x16c55b['name'],_0x16c55b['id']),util[_0xd43e('0x24')](parsedResult,{'showHidden':![],'depth':null}));return _0x24c763(_0x261923);});}else{return _0x24c763(null,_0x46e969);}}}});};