wulf-pulse/app/api/dashboard/stats/route.ts

119 lines
3.5 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server';
import { postgresClient } from '@/lib/services/postgres-client';
async function tableExists(tableName: string): Promise<boolean> {
try {
const result = await postgresClient.query(
"SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = $1)",
[tableName]
);
return result.rows[0]?.exists || false;
} catch {
return false;
}
}
export async function GET(request: NextRequest) {
try {
// Get companies count from database
const companiesResult = await postgresClient.query(
'SELECT COUNT(*) as total, COUNT(CASE WHEN is_active = true THEN 1 END) as active FROM companies WHERE company_type = 1'
);
// Get Auvik mapping stats
let auvikMapped = 0;
try {
const auvikResult = await postgresClient.query(
'SELECT COUNT(*) as mapped FROM auvik_tenant_mappings WHERE autotask_company_id IS NOT NULL'
);
auvikMapped = parseInt(auvikResult.rows[0]?.mapped || '0');
} catch {
// Table may not exist
}
// Get total Auvik tenants count (check if table exists first)
let auvikTotal = auvikMapped;
if (await tableExists('auvik_tenants')) {
try {
const auvikTotalResult = await postgresClient.query(
'SELECT COUNT(*) as total FROM auvik_tenants'
);
auvikTotal = parseInt(auvikTotalResult.rows[0]?.total || '0');
} catch {
// Ignore error
}
}
// Get RMM mapping stats
let rmmMapped = 0;
try {
const rmmResult = await postgresClient.query(
'SELECT COUNT(*) as mapped FROM rmm_site_mappings WHERE company_id IS NOT NULL'
);
rmmMapped = parseInt(rmmResult.rows[0]?.mapped || '0');
} catch {
// Table may not exist
}
// Get total RMM sites count (check if table exists first)
let rmmTotal = rmmMapped;
if (await tableExists('rmm_sites')) {
try {
const rmmTotalResult = await postgresClient.query(
'SELECT COUNT(*) as total FROM rmm_sites'
);
rmmTotal = parseInt(rmmTotalResult.rows[0]?.total || '0');
} catch {
// Ignore error
}
}
// Get quotes count (if quotes table exists)
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
}
}
const stats = {
companies: {
total: parseInt(companiesResult.rows[0]?.total || '0'),
active: parseInt(companiesResult.rows[0]?.active || '0'),
},
mappings: {
auvik: {
mapped: auvikMapped,
unmapped: Math.max(0, auvikTotal - auvikMapped),
},
rmm: {
mapped: rmmMapped,
unmapped: Math.max(0, rmmTotal - rmmMapped),
},
},
quotes: {
open: quotesOpen,
total: quotesTotal,
},
};
return NextResponse.json(stats);
} catch (error) {
console.error('Error fetching dashboard stats:', error);
return NextResponse.json(
{ error: 'Failed to fetch dashboard stats' },
{ status: 500 }
);
}
}