This commit is contained in:
2026-02-17 13:33:39 -06:00
parent 272f325d98
commit 52dcdce8bb
5 changed files with 195 additions and 12 deletions

View File

@@ -4,7 +4,8 @@ const path = require('path');
const puppeteer = require('puppeteer');
const fs = require('fs').promises;
const multer = require('multer');
const { makeQboApiCall, getOAuthClient } = require('./qbo_helper');
const OAuthClient = require('intuit-oauth');
const { makeQboApiCall, getOAuthClient, saveTokens, resetOAuthClient } = require('./qbo_helper');
const app = express();
const PORT = process.env.PORT || 3000;
@@ -1253,7 +1254,7 @@ app.post('/api/invoices/:id/export', async (req, res) => {
// 6. DB Update: Wir speichern AUCH die QBO-Nummer, damit wir wissen, wie sie drüben heißt
await client.query(
`UPDATE invoices SET qbo_id = $1, qbo_sync_token = $2, qbo_doc_number = $3 WHERE id = $4`,
`UPDATE invoices SET qbo_id = $1, qbo_sync_token = $2, qbo_doc_number = $3, invoice_number = $3 WHERE id = $4`,
[qboInvoice.Id, qboInvoice.SyncToken, qboInvoice.DocNumber, id]
);
@@ -1307,6 +1308,54 @@ app.get('/api/qbo/overdue', async (req, res) => {
res.status(500).json({ error: error.message });
}
});
// Schritt 1: User klickt "Authorize" → Redirect zu Intuit
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);
});
// Schritt 2: Intuit redirected zurück mit Code → Token holen
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();
// Redirect zurück zur App (Settings Tab)
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>
`);
}
});
// Status-Check Endpoint (für die UI)
app.get('/api/qbo/status', (req, res) => {
try {
const client = getOAuthClient();
const token = client.getToken();
const hasToken = !!(token && token.refresh_token);
res.json({
connected: hasToken,
realmId: token?.realmId || null
});
} catch (e) {
res.json({ connected: false });
}
});
// Start server and browser
async function startServer() {
await initBrowser();