Major update to chorus.services platform
- 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>
This commit is contained in:
63
modules/dashboard/app/api/leads/route.ts
Normal file
63
modules/dashboard/app/api/leads/route.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { pool, Lead } from '@/lib/db'
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url)
|
||||
const page = parseInt(searchParams.get('page') || '1')
|
||||
const limit = parseInt(searchParams.get('limit') || '50')
|
||||
const sortBy = searchParams.get('sortBy') || 'created_at'
|
||||
const sortOrder = searchParams.get('sortOrder') || 'DESC'
|
||||
const search = searchParams.get('search') || ''
|
||||
|
||||
const offset = (page - 1) * limit
|
||||
|
||||
let whereClause = ''
|
||||
let queryParams: any[] = [limit, offset]
|
||||
|
||||
if (search) {
|
||||
whereClause = `WHERE (first_name ILIKE $3 OR last_name ILIKE $3 OR email ILIKE $3 OR company_name ILIKE $3)`
|
||||
queryParams.push(`%${search}%`)
|
||||
}
|
||||
|
||||
const validSortColumns = ['created_at', 'first_name', 'last_name', 'email', 'company_name', 'lead_source']
|
||||
const sortColumn = validSortColumns.includes(sortBy) ? sortBy : 'created_at'
|
||||
const order = sortOrder.toUpperCase() === 'ASC' ? 'ASC' : 'DESC'
|
||||
|
||||
const leadsQuery = `
|
||||
SELECT * FROM leads
|
||||
${whereClause}
|
||||
ORDER BY ${sortColumn} ${order}
|
||||
LIMIT $1 OFFSET $2
|
||||
`
|
||||
|
||||
const countQuery = `SELECT COUNT(*) as total FROM leads ${whereClause}`
|
||||
|
||||
const [leadsResult, countResult] = await Promise.all([
|
||||
pool.query(leadsQuery, queryParams),
|
||||
pool.query(countQuery, search ? [`%${search}%`] : [])
|
||||
])
|
||||
|
||||
const leads: Lead[] = leadsResult.rows
|
||||
const total = parseInt(countResult.rows[0].total)
|
||||
const totalPages = Math.ceil(total / limit)
|
||||
|
||||
return NextResponse.json({
|
||||
leads,
|
||||
pagination: {
|
||||
page,
|
||||
limit,
|
||||
total,
|
||||
totalPages,
|
||||
hasNext: page < totalPages,
|
||||
hasPrev: page > 1
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Error fetching leads:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch leads' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
64
modules/dashboard/app/api/stats/route.ts
Normal file
64
modules/dashboard/app/api/stats/route.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
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 }
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user