- Extensive updates to system configuration and deployment - Enhanced documentation and architecture improvements - Updated dependencies and build configurations - Improved service integrations and workflows 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
64 lines
1.9 KiB
TypeScript
64 lines
1.9 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import { pool, LeadStats } from '@/lib/db'
|
|
|
|
export async function GET() {
|
|
try {
|
|
const queries = [
|
|
// Total leads
|
|
'SELECT COUNT(*) as count FROM leads',
|
|
|
|
// Leads today
|
|
'SELECT COUNT(*) as count FROM leads WHERE created_at >= CURRENT_DATE',
|
|
|
|
// Leads this week
|
|
'SELECT COUNT(*) as count FROM leads WHERE created_at >= date_trunc(\'week\', CURRENT_DATE)',
|
|
|
|
// Leads this month
|
|
'SELECT COUNT(*) as count FROM leads WHERE created_at >= date_trunc(\'month\', CURRENT_DATE)',
|
|
|
|
// By source
|
|
'SELECT lead_source, COUNT(*) as count FROM leads GROUP BY lead_source ORDER BY count DESC',
|
|
|
|
// By date (last 30 days)
|
|
`SELECT
|
|
DATE(created_at) as date,
|
|
COUNT(*) as count
|
|
FROM leads
|
|
WHERE created_at >= CURRENT_DATE - INTERVAL '30 days'
|
|
GROUP BY DATE(created_at)
|
|
ORDER BY date DESC`
|
|
]
|
|
|
|
const [
|
|
totalResult,
|
|
todayResult,
|
|
weekResult,
|
|
monthResult,
|
|
sourceResult,
|
|
dateResult
|
|
] = await Promise.all(queries.map(query => pool.query(query)))
|
|
|
|
const stats: LeadStats = {
|
|
total_leads: parseInt(totalResult.rows[0].count),
|
|
leads_today: parseInt(todayResult.rows[0].count),
|
|
leads_this_week: parseInt(weekResult.rows[0].count),
|
|
leads_this_month: parseInt(monthResult.rows[0].count),
|
|
by_source: sourceResult.rows.map(row => ({
|
|
lead_source: row.lead_source,
|
|
count: parseInt(row.count)
|
|
})),
|
|
by_date: dateResult.rows.map(row => ({
|
|
date: row.date,
|
|
count: parseInt(row.count)
|
|
}))
|
|
}
|
|
|
|
return NextResponse.json(stats)
|
|
} catch (error) {
|
|
console.error('Error fetching stats:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch statistics' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
} |