moved routes

This commit is contained in:
Andreas Knuth 2026-03-04 16:00:40 -06:00
parent 0fbb298e89
commit 15d33a116c
2 changed files with 45 additions and 35 deletions

View File

@ -3,8 +3,13 @@
* Modularized Backend
*/
const express = require('express');
const path = require('path');
const puppeteer = require('puppeteer');
// Import config
const { pool } = require('./config/database');
const { OAuthClient, getOAuthClient, saveTokens } = require('./config/qbo');
// Import routes
const customerRoutes = require('./routes/customers');
const quoteRoutes = require('./routes/quotes');
@ -49,6 +54,7 @@ async function initBrowser() {
browser.on('disconnected', () => {
console.log('[BROWSER] Browser disconnected, restarting...');
browser = null;
setBrowser(null);
initBrowser();
});
}
@ -57,9 +63,42 @@ async function initBrowser() {
// Middleware
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/quotes', quoteRoutes);
app.use('/api/invoices', invoiceRoutes);
@ -81,11 +120,10 @@ process.on('SIGTERM', async () => {
if (browser) {
await browser.close();
}
const { pool } = require('./config/database');
await pool.end();
process.exit(0);
});
startServer();
module.exports = app;
module.exports = app;

View File

@ -1,6 +1,7 @@
/**
* 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 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
router.get('/accounts', async (req, res) => {
try {
@ -597,4 +569,4 @@ router.post('/sync-payments', async (req, res) => {
}
});
module.exports = router;
module.exports = router;