- Add QBO OAuth2 client with token refresh (lib/services/qbo-client.ts) - Add QBO sync service for invoices, payments, deposits, purchases, journal entries, reports (lib/services/qbo-sync-service.ts) - Add QBO types (lib/types/qbo.ts) - Add API routes: /api/qbo/auth, /api/qbo/sync, /api/qbo/disconnect - Add /admin/qbo status and sync management page - Add legal pages: /legal/eula, /legal/privacy (Intuit app assessment) - Add QBO nav link under Admin - Fix reports: remove invalid summarize_column_by, add accounting_method from Preferences API, add showrows=all&showcols=all - Add CashFlow report type alongside P&L and BalanceSheet - Add NoReportData check to skip empty report months - Add intuit_tid capture in error messages - Add redirect: follow for cluster routing - Migration 051: qbo_tokens, qbo_invoices, qbo_payments, qbo_deposits, qbo_transactions, qbo_reports tables Also includes earlier work: - Ping flap suppression pipeline step - Ticket digest reports with LLM analysis - Zabbix WAN monitor and gap analysis - Kiosk is_deleted filter fixes - Datto RMM ping target enrichment - Entity sync soft-delete detection
48 lines
2 KiB
TypeScript
48 lines
2 KiB
TypeScript
/**
|
|
* QBO OAuth2 Authorization
|
|
* GET /api/qbo/auth — redirect to Intuit authorization page
|
|
* GET /api/qbo/auth/callback — exchange code for tokens
|
|
*/
|
|
|
|
import { NextRequest, NextResponse } from 'next/server';
|
|
import { QboClient } from '@/lib/services/qbo-client';
|
|
|
|
export async function GET(request: NextRequest) {
|
|
const { searchParams } = request.nextUrl;
|
|
const code = searchParams.get('code');
|
|
const realmId = searchParams.get('realmId');
|
|
const error = searchParams.get('error');
|
|
|
|
const redirectUri = `${process.env.NEXTAUTH_URL}/api/qbo/auth`;
|
|
|
|
// ── Callback from Intuit ──────────────────────────────────────────────────
|
|
const baseUrl = process.env.NEXTAUTH_URL || 'https://pulse.wulfconsulting.cloud';
|
|
|
|
if (code && realmId) {
|
|
try {
|
|
const client = new QboClient();
|
|
await client.exchangeCodeForToken(code, redirectUri);
|
|
console.log(`[QBO Auth] Tokens saved for realm ${realmId}`);
|
|
return NextResponse.redirect(`${baseUrl}/admin/qbo?connected=true`);
|
|
} catch (err) {
|
|
const msg = err instanceof Error ? err.message : String(err);
|
|
console.error('[QBO Auth] Token exchange failed:', msg);
|
|
return NextResponse.redirect(`${baseUrl}/admin/qbo?error=${encodeURIComponent(msg)}`);
|
|
}
|
|
}
|
|
|
|
if (error) {
|
|
return NextResponse.redirect(`${baseUrl}/admin/qbo?error=${encodeURIComponent(error)}`);
|
|
}
|
|
|
|
// ── Initiate authorization ────────────────────────────────────────────────
|
|
try {
|
|
const client = new QboClient();
|
|
const state = Math.random().toString(36).slice(2);
|
|
const authUrl = client.getAuthorizationUrl(redirectUri, state);
|
|
return NextResponse.redirect(authUrl);
|
|
} catch (err) {
|
|
const msg = err instanceof Error ? err.message : String(err);
|
|
return NextResponse.json({ error: msg }, { status: 500 });
|
|
}
|
|
}
|