f7e36b722a03bcfb3a0f1491d6848b6d1d9f35b4
[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 _0x1f14=['ZohoAccount,\x20Refresh\x20token\x20successfully\x20generated\x20for\x20account\x20\x22%s\x22\x20#%d','lodash','util','../../config/logger','api','bluebird','request-promise','rimraf','./intZohoAccount.attributes','../../components/integrations/configuration','exports','define','ZohoAccount','int_zoho_accounts','changed','serverUrl','clientId','code','host','slice','substring','lastIndexOf','zone','https://accounts.zoho.com','POST','/oauth/v2/token','authorization_code','application/x-www-form-urlencoded','then','parse','refresh_token','info','format','name','error','inspect','catch','ZohoAccount,\x20Unable\x20to\x20generate\x20the\x20refresh\x20token\x20for\x20account\x20\x22%s\x22\x20#%d','clientSecret','https://accounts.zoho.com.au','https://accounts.zoho.in','https://accounts.zoho.com.cn','https://accounts.zoho.eu'];(function(_0x4470ff,_0xeb7ca7){var _0x876273=function(_0xc5ae97){while(--_0xc5ae97){_0x4470ff['push'](_0x4470ff['shift']());}};_0x876273(++_0xeb7ca7);}(_0x1f14,0x12e));var _0x41f1=function(_0x1d1872,_0x2b134d){_0x1d1872=_0x1d1872-0x0;var _0xdf1400=_0x1f14[_0x1d1872];return _0xdf1400;};'use strict';var _=require(_0x41f1('0x0'));var util=require(_0x41f1('0x1'));var logger=require(_0x41f1('0x2'))(_0x41f1('0x3'));var moment=require('moment');var BPromise=require(_0x41f1('0x4'));var rp=require(_0x41f1('0x5'));var fs=require('fs');var path=require('path');var rimraf=require(_0x41f1('0x6'));var config=require('../../config/environment');var attributes=require(_0x41f1('0x7'));var integrations=require(_0x41f1('0x8'));module[_0x41f1('0x9')]=function(_0x1a41fd,_0x16db87){return _0x1a41fd[_0x41f1('0xa')](_0x41f1('0xb'),attributes,{'tableName':_0x41f1('0xc'),'paranoid':![],'indexes':[],'timestamps':!![],'hooks':{'beforeCreate':function(_0x4ee3ca,_0x1d45be,_0x739f1b){if(_0x4ee3ca[_0x41f1('0xd')]('zone')||_0x4ee3ca['changed'](_0x41f1('0xe'))||_0x4ee3ca[_0x41f1('0xd')](_0x41f1('0xf'))||_0x4ee3ca[_0x41f1('0xd')]('clientSecret')||_0x4ee3ca[_0x41f1('0xd')](_0x41f1('0x10'))||_0x4ee3ca[_0x41f1('0xd')](_0x41f1('0x11'))){var _0x175a3e,_0x4219ce;_0x175a3e=_0x4ee3ca[_0x41f1('0xe')];var _0x221fd9=_0x175a3e[_0x41f1('0x12')](-0x1);if(_0x221fd9==='/'||_0x221fd9==='\x5c'||_0x221fd9===':'){_0x175a3e=_0x175a3e[_0x41f1('0x13')](0x0,_0x175a3e[_0x41f1('0x14')](_0x221fd9));}switch(_0x4ee3ca[_0x41f1('0x15')]){case'US':_0x4219ce=_0x41f1('0x16');break;case'AU':_0x4219ce='https://accounts.zoho.com.au';break;case'IN':_0x4219ce='https://accounts.zoho.in';break;case'CN':_0x4219ce='https://accounts.zoho.com.cn';break;default:_0x4219ce='https://accounts.zoho.eu';break;}var _0x32c960={'method':_0x41f1('0x17'),'uri':_0x4219ce+_0x41f1('0x18'),'form':{'grant_type':_0x41f1('0x19'),'client_id':_0x4ee3ca[_0x41f1('0xf')],'client_secret':_0x4ee3ca['clientSecret'],'redirect_uri':_0x4ee3ca['serverUrl'],'code':_0x4ee3ca[_0x41f1('0x10')]},'headers':{'Content-Type':_0x41f1('0x1a')}};return rp(_0x32c960)[_0x41f1('0x1b')](function(_0x3acf49){var _0x404940=JSON[_0x41f1('0x1c')](_0x3acf49);if(_0x404940[_0x41f1('0x1d')]){_0x4ee3ca['refreshToken']=_0x404940[_0x41f1('0x1d')];logger[_0x41f1('0x1e')](util[_0x41f1('0x1f')]('ZohoAccount,\x20Refresh\x20token\x20successfully\x20generated\x20for\x20account\x20\x22%s\x22\x20#%d',_0x4ee3ca[_0x41f1('0x20')],_0x4ee3ca['id']));}else{logger[_0x41f1('0x21')](util['format']('ZohoAccount,\x20Unable\x20to\x20generate\x20the\x20refresh\x20token\x20for\x20account\x20\x22%s\x22\x20#%d',_0x4ee3ca['name'],_0x4ee3ca['id']),util[_0x41f1('0x22')](_0x404940,{'showHidden':![],'depth':null}));}return _0x739f1b(null,_0x1d45be);})[_0x41f1('0x23')](function(_0x3ed76a){logger[_0x41f1('0x21')](util[_0x41f1('0x1f')](_0x41f1('0x24'),_0x4ee3ca['name'],_0x4ee3ca['id']),util[_0x41f1('0x22')](parsedResult,{'showHidden':![],'depth':null}));return _0x739f1b(_0x3ed76a);});}else{return _0x739f1b(null,_0x1d45be);}},'beforeUpdate':function(_0x1fdd68,_0x530d8d,_0x220050){if(_0x1fdd68[_0x41f1('0xd')](_0x41f1('0x15'))||_0x1fdd68['changed']('serverUrl')||_0x1fdd68[_0x41f1('0xd')]('clientId')||_0x1fdd68[_0x41f1('0xd')](_0x41f1('0x25'))||_0x1fdd68['changed'](_0x41f1('0x10'))||_0x1fdd68['changed'](_0x41f1('0x11'))){var _0x449b90,_0x51b375;_0x449b90=_0x1fdd68[_0x41f1('0xe')];var _0xf6ceb6=_0x449b90['slice'](-0x1);if(_0xf6ceb6==='/'||_0xf6ceb6==='\x5c'||_0xf6ceb6===':'){_0x449b90=_0x449b90['substring'](0x0,_0x449b90[_0x41f1('0x14')](_0xf6ceb6));}switch(_0x1fdd68[_0x41f1('0x15')]){case'US':_0x51b375=_0x41f1('0x16');break;case'AU':_0x51b375=_0x41f1('0x26');break;case'IN':_0x51b375=_0x41f1('0x27');break;case'CN':_0x51b375=_0x41f1('0x28');break;default:_0x51b375=_0x41f1('0x29');break;}var _0x25fec3={'method':_0x41f1('0x17'),'uri':_0x51b375+_0x41f1('0x18'),'form':{'grant_type':_0x41f1('0x19'),'client_id':_0x1fdd68[_0x41f1('0xf')],'client_secret':_0x1fdd68[_0x41f1('0x25')],'redirect_uri':_0x1fdd68[_0x41f1('0xe')],'code':_0x1fdd68['code']},'headers':{'Content-Type':_0x41f1('0x1a')}};return rp(_0x25fec3)[_0x41f1('0x1b')](function(_0x3abb19){var _0x7b6e60=JSON[_0x41f1('0x1c')](_0x3abb19);if(_0x7b6e60['refresh_token']){_0x1fdd68['refreshToken']=_0x7b6e60[_0x41f1('0x1d')];logger[_0x41f1('0x1e')](util[_0x41f1('0x1f')](_0x41f1('0x2a'),_0x1fdd68[_0x41f1('0x20')],_0x1fdd68['id']));}else{logger[_0x41f1('0x21')](util[_0x41f1('0x1f')](_0x41f1('0x24'),_0x1fdd68[_0x41f1('0x20')],_0x1fdd68['id']),util[_0x41f1('0x22')](_0x7b6e60,{'showHidden':![],'depth':null}));}return _0x220050(null,_0x530d8d);})[_0x41f1('0x23')](function(_0xed268){logger['error'](util[_0x41f1('0x1f')](_0x41f1('0x24'),_0x1fdd68[_0x41f1('0x20')],_0x1fdd68['id']),util[_0x41f1('0x22')](parsedResult,{'showHidden':![],'depth':null}));return _0x220050(_0xed268);});}else{return _0x220050(null,_0x530d8d);}}}});};