b98ab2d6faeb742dbe196f77d6f5a4c66ad8a203
[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 _0xd474=['rimraf','../../config/environment','./intZohoAccount.attributes','exports','define','ZohoAccount','int_zoho_accounts','changed','zone','clientId','clientSecret','code','host','slice','substring','lastIndexOf','https://accounts.zoho.com','https://accounts.zoho.com.au','https://accounts.zoho.in','https://accounts.zoho.eu','POST','/oauth/v2/token','authorization_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','catch','error','serverUrl','https://accounts.zoho.com.cn','util','../../config/logger','api','bluebird','request-promise'];(function(_0x48558e,_0x1e3c71){var _0x32b05e=function(_0x3b5de0){while(--_0x3b5de0){_0x48558e['push'](_0x48558e['shift']());}};_0x32b05e(++_0x1e3c71);}(_0xd474,0x153));var _0x4d47=function(_0x5e51d0,_0x4fc72b){_0x5e51d0=_0x5e51d0-0x0;var _0x14879c=_0xd474[_0x5e51d0];return _0x14879c;};'use strict';var _=require('lodash');var util=require(_0x4d47('0x0'));var logger=require(_0x4d47('0x1'))(_0x4d47('0x2'));var moment=require('moment');var BPromise=require(_0x4d47('0x3'));var rp=require(_0x4d47('0x4'));var fs=require('fs');var path=require('path');var rimraf=require(_0x4d47('0x5'));var config=require(_0x4d47('0x6'));var attributes=require(_0x4d47('0x7'));var integrations=require('../../components/integrations/configuration');module[_0x4d47('0x8')]=function(_0x4c55c3,_0x1b6330){return _0x4c55c3[_0x4d47('0x9')](_0x4d47('0xa'),attributes,{'tableName':_0x4d47('0xb'),'paranoid':![],'indexes':[],'timestamps':!![],'hooks':{'beforeCreate':function(_0x5d7b37,_0x1c4c27,_0x21d42c){if(_0x5d7b37[_0x4d47('0xc')](_0x4d47('0xd'))||_0x5d7b37[_0x4d47('0xc')]('serverUrl')||_0x5d7b37[_0x4d47('0xc')](_0x4d47('0xe'))||_0x5d7b37['changed'](_0x4d47('0xf'))||_0x5d7b37[_0x4d47('0xc')](_0x4d47('0x10'))||_0x5d7b37[_0x4d47('0xc')](_0x4d47('0x11'))){var _0x2c8194,_0x114206;_0x2c8194=_0x5d7b37['serverUrl'];var _0x4002b1=_0x2c8194[_0x4d47('0x12')](-0x1);if(_0x4002b1==='/'||_0x4002b1==='\x5c'||_0x4002b1===':'){_0x2c8194=_0x2c8194[_0x4d47('0x13')](0x0,_0x2c8194[_0x4d47('0x14')](_0x4002b1));}switch(_0x5d7b37['zone']){case'US':_0x114206=_0x4d47('0x15');break;case'AU':_0x114206=_0x4d47('0x16');break;case'IN':_0x114206=_0x4d47('0x17');break;case'CN':_0x114206='https://accounts.zoho.com.cn';break;default:_0x114206=_0x4d47('0x18');break;}var _0x58425e={'method':_0x4d47('0x19'),'uri':_0x114206+_0x4d47('0x1a'),'form':{'grant_type':_0x4d47('0x1b'),'client_id':_0x5d7b37[_0x4d47('0xe')],'client_secret':_0x5d7b37[_0x4d47('0xf')],'redirect_uri':_0x5d7b37['serverUrl'],'code':_0x5d7b37[_0x4d47('0x10')]},'headers':{'Content-Type':_0x4d47('0x1c')}};return rp(_0x58425e)[_0x4d47('0x1d')](function(_0x438a70){var _0x4dc200=JSON[_0x4d47('0x1e')](_0x438a70);if(_0x4dc200[_0x4d47('0x1f')]){_0x5d7b37[_0x4d47('0x20')]=_0x4dc200[_0x4d47('0x1f')];logger[_0x4d47('0x21')](util[_0x4d47('0x22')](_0x4d47('0x23'),_0x5d7b37[_0x4d47('0x24')],_0x5d7b37['id']));}else{logger['error'](util[_0x4d47('0x22')](_0x4d47('0x25'),_0x5d7b37[_0x4d47('0x24')],_0x5d7b37['id']),util[_0x4d47('0x26')](_0x4dc200,{'showHidden':![],'depth':null}));}return _0x21d42c(null,_0x1c4c27);})[_0x4d47('0x27')](function(_0x9b8301){logger[_0x4d47('0x28')](util[_0x4d47('0x22')]('ZohoAccount,\x20Unable\x20to\x20generate\x20the\x20refresh\x20token\x20for\x20account\x20\x22%s\x22\x20#%d',_0x5d7b37[_0x4d47('0x24')],_0x5d7b37['id']),util[_0x4d47('0x26')](parsedResult,{'showHidden':![],'depth':null}));return _0x21d42c(_0x9b8301);});}else{return _0x21d42c(null,_0x1c4c27);}},'beforeUpdate':function(_0x23ac8c,_0x996778,_0x51b663){if(_0x23ac8c['changed'](_0x4d47('0xd'))||_0x23ac8c[_0x4d47('0xc')](_0x4d47('0x29'))||_0x23ac8c[_0x4d47('0xc')](_0x4d47('0xe'))||_0x23ac8c['changed'](_0x4d47('0xf'))||_0x23ac8c[_0x4d47('0xc')](_0x4d47('0x10'))||_0x23ac8c[_0x4d47('0xc')](_0x4d47('0x11'))){var _0x16d102,_0x3f689d;_0x16d102=_0x23ac8c[_0x4d47('0x29')];var _0x4ddc68=_0x16d102[_0x4d47('0x12')](-0x1);if(_0x4ddc68==='/'||_0x4ddc68==='\x5c'||_0x4ddc68===':'){_0x16d102=_0x16d102['substring'](0x0,_0x16d102[_0x4d47('0x14')](_0x4ddc68));}switch(_0x23ac8c[_0x4d47('0xd')]){case'US':_0x3f689d=_0x4d47('0x15');break;case'AU':_0x3f689d=_0x4d47('0x16');break;case'IN':_0x3f689d='https://accounts.zoho.in';break;case'CN':_0x3f689d=_0x4d47('0x2a');break;default:_0x3f689d='https://accounts.zoho.eu';break;}var _0x5c9851={'method':_0x4d47('0x19'),'uri':_0x3f689d+_0x4d47('0x1a'),'form':{'grant_type':_0x4d47('0x1b'),'client_id':_0x23ac8c['clientId'],'client_secret':_0x23ac8c['clientSecret'],'redirect_uri':_0x23ac8c[_0x4d47('0x29')],'code':_0x23ac8c[_0x4d47('0x10')]},'headers':{'Content-Type':_0x4d47('0x1c')}};return rp(_0x5c9851)[_0x4d47('0x1d')](function(_0x37a6f5){var _0x11ef7b=JSON[_0x4d47('0x1e')](_0x37a6f5);if(_0x11ef7b[_0x4d47('0x1f')]){_0x23ac8c[_0x4d47('0x20')]=_0x11ef7b[_0x4d47('0x1f')];logger[_0x4d47('0x21')](util['format'](_0x4d47('0x23'),_0x23ac8c[_0x4d47('0x24')],_0x23ac8c['id']));}else{logger[_0x4d47('0x28')](util['format'](_0x4d47('0x25'),_0x23ac8c[_0x4d47('0x24')],_0x23ac8c['id']),util[_0x4d47('0x26')](_0x11ef7b,{'showHidden':![],'depth':null}));}return _0x51b663(null,_0x996778);})['catch'](function(_0x2f3041){logger[_0x4d47('0x28')](util['format'](_0x4d47('0x25'),_0x23ac8c[_0x4d47('0x24')],_0x23ac8c['id']),util['inspect'](parsedResult,{'showHidden':![],'depth':null}));return _0x51b663(_0x2f3041);});}else{return _0x51b663(null,_0x996778);}}}});};