moved routes
This commit is contained in:
parent
0fbb298e89
commit
15d33a116c
46
src/index.js
46
src/index.js
|
|
@ -3,8 +3,13 @@
|
||||||
* Modularized Backend
|
* Modularized Backend
|
||||||
*/
|
*/
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
|
const path = require('path');
|
||||||
const puppeteer = require('puppeteer');
|
const puppeteer = require('puppeteer');
|
||||||
|
|
||||||
|
// Import config
|
||||||
|
const { pool } = require('./config/database');
|
||||||
|
const { OAuthClient, getOAuthClient, saveTokens } = require('./config/qbo');
|
||||||
|
|
||||||
// Import routes
|
// Import routes
|
||||||
const customerRoutes = require('./routes/customers');
|
const customerRoutes = require('./routes/customers');
|
||||||
const quoteRoutes = require('./routes/quotes');
|
const quoteRoutes = require('./routes/quotes');
|
||||||
|
|
@ -49,6 +54,7 @@ async function initBrowser() {
|
||||||
browser.on('disconnected', () => {
|
browser.on('disconnected', () => {
|
||||||
console.log('[BROWSER] Browser disconnected, restarting...');
|
console.log('[BROWSER] Browser disconnected, restarting...');
|
||||||
browser = null;
|
browser = null;
|
||||||
|
setBrowser(null);
|
||||||
initBrowser();
|
initBrowser();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -57,9 +63,42 @@ async function initBrowser() {
|
||||||
|
|
||||||
// Middleware
|
// Middleware
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
app.use(express.static('public'));
|
app.use(express.static(path.join(__dirname, '..', 'public')));
|
||||||
|
|
||||||
// Mount routes
|
// =====================================================
|
||||||
|
// QBO OAuth Routes — mounted at root level (not under /api/qbo)
|
||||||
|
// These must match the Intuit callback URL configuration
|
||||||
|
// =====================================================
|
||||||
|
app.get('/auth/qbo', (req, res) => {
|
||||||
|
const client = getOAuthClient();
|
||||||
|
const authUri = client.authorizeUri({
|
||||||
|
scope: [OAuthClient.scopes.Accounting],
|
||||||
|
state: 'intuit-qbo-auth'
|
||||||
|
});
|
||||||
|
console.log('🔗 Redirecting to QBO Authorization:', authUri);
|
||||||
|
res.redirect(authUri);
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get('/auth/qbo/callback', async (req, res) => {
|
||||||
|
const client = getOAuthClient();
|
||||||
|
try {
|
||||||
|
const authResponse = await client.createToken(req.url);
|
||||||
|
console.log('✅ QBO Authorization erfolgreich!');
|
||||||
|
saveTokens();
|
||||||
|
res.redirect('/#settings');
|
||||||
|
} catch (e) {
|
||||||
|
console.error('❌ QBO Authorization fehlgeschlagen:', e);
|
||||||
|
res.status(500).send(`
|
||||||
|
<h2>QBO Authorization Failed</h2>
|
||||||
|
<p>${e.message || e}</p>
|
||||||
|
<a href="/">Zurück zur App</a>
|
||||||
|
`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// =====================================================
|
||||||
|
// API Routes
|
||||||
|
// =====================================================
|
||||||
app.use('/api/customers', customerRoutes);
|
app.use('/api/customers', customerRoutes);
|
||||||
app.use('/api/quotes', quoteRoutes);
|
app.use('/api/quotes', quoteRoutes);
|
||||||
app.use('/api/invoices', invoiceRoutes);
|
app.use('/api/invoices', invoiceRoutes);
|
||||||
|
|
@ -81,11 +120,10 @@ process.on('SIGTERM', async () => {
|
||||||
if (browser) {
|
if (browser) {
|
||||||
await browser.close();
|
await browser.close();
|
||||||
}
|
}
|
||||||
const { pool } = require('./config/database');
|
|
||||||
await pool.end();
|
await pool.end();
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
startServer();
|
startServer();
|
||||||
|
|
||||||
module.exports = app;
|
module.exports = app;
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
/**
|
/**
|
||||||
* QBO Routes
|
* QBO Routes
|
||||||
* Handles QBO OAuth, sync, and data operations
|
* Handles QBO sync and data operations
|
||||||
|
* NOTE: OAuth auth/callback routes are in index.js (root-level paths)
|
||||||
*/
|
*/
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
@ -22,35 +23,6 @@ router.get('/status', (req, res) => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// GET auth URL - redirects to Intuit
|
|
||||||
router.get('/auth', (req, res) => {
|
|
||||||
const client = getOAuthClient();
|
|
||||||
const authUri = client.authorizeUri({
|
|
||||||
scope: [require('../config/qbo').OAuthClient.scopes.Accounting],
|
|
||||||
state: 'intuit-qbo-auth'
|
|
||||||
});
|
|
||||||
console.log('🔗 Redirecting to QBO Authorization:', authUri);
|
|
||||||
res.redirect(authUri);
|
|
||||||
});
|
|
||||||
|
|
||||||
// OAuth callback
|
|
||||||
router.get('/auth/callback', async (req, res) => {
|
|
||||||
const client = getOAuthClient();
|
|
||||||
try {
|
|
||||||
const authResponse = await client.createToken(req.url);
|
|
||||||
console.log('✅ QBO Authorization erfolgreich!');
|
|
||||||
saveTokens();
|
|
||||||
res.redirect('/#settings');
|
|
||||||
} catch (e) {
|
|
||||||
console.error('❌ QBO Authorization fehlgeschlagen:', e);
|
|
||||||
res.status(500).send(`
|
|
||||||
<h2>QBO Authorization Failed</h2>
|
|
||||||
<p>${e.message || e}</p>
|
|
||||||
<a href="/">Zurück zur App</a>
|
|
||||||
`);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// GET bank accounts from QBO
|
// GET bank accounts from QBO
|
||||||
router.get('/accounts', async (req, res) => {
|
router.get('/accounts', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
|
|
@ -597,4 +569,4 @@ router.post('/sync-payments', async (req, res) => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
Loading…
Reference in New Issue