diff --git a/app/api/dashboard/stats/route.ts b/app/api/dashboard/stats/route.ts index 2fb5f54..64bed44 100644 --- a/app/api/dashboard/stats/route.ts +++ b/app/api/dashboard/stats/route.ts @@ -1,5 +1,6 @@ import { NextRequest, NextResponse } from 'next/server'; import { postgresClient } from '@/lib/services/postgres-client'; +import { SalesBldrClient } from '@/lib/services/salesbldr-client'; async function tableExists(tableName: string): Promise { try { @@ -55,12 +56,12 @@ export async function GET(request: NextRequest) { // Table may not exist } - // Get total RMM sites count (check if table exists first) + // Get total RMM sites count from datto_rmm_sites let rmmTotal = rmmMapped; - if (await tableExists('rmm_sites')) { + if (await tableExists('datto_rmm_sites')) { try { const rmmTotalResult = await postgresClient.query( - 'SELECT COUNT(*) as total FROM rmm_sites' + 'SELECT COUNT(*) as total FROM datto_rmm_sites' ); rmmTotal = parseInt(rmmTotalResult.rows[0]?.total || '0'); } catch { @@ -68,23 +69,16 @@ export async function GET(request: NextRequest) { } } - // Get quotes count (if quotes table exists) + // Get quotes count from SalesBldr API let quotesOpen = 0; let quotesTotal = 0; - if (await tableExists('quotes')) { - try { - const quotesResult = await postgresClient.query( - "SELECT COUNT(*) as open FROM quotes WHERE status = 'open'" - ); - quotesOpen = parseInt(quotesResult.rows[0]?.open || '0'); - - const quotesTotalResult = await postgresClient.query( - 'SELECT COUNT(*) as total FROM quotes' - ); - quotesTotal = parseInt(quotesTotalResult.rows[0]?.total || '0'); - } catch { - // Ignore error - } + try { + const salesbldr = new SalesBldrClient(); + const openQuotes = await salesbldr.getOpenQuotes(1000); + quotesOpen = openQuotes.total ?? openQuotes.results?.length ?? 0; + quotesTotal = quotesOpen; + } catch { + // SalesBldr not configured or unavailable } const stats = {