/opt/canhelp/apps/api/src/routes
Edit: /opt/canhelp/apps/api/src/routes/stats.ts (1093B)
import { Hono } from 'hono'
import { count, countDistinct, eq } from 'drizzle-orm'
import { db } from '../db.js'
import { tasks, categories, specialistCards } from '@canhelp/db'
import { redis } from '../redis.js'
const app = new Hono()
const CACHE_KEY = 'public:stats:v2'
const CACHE_TTL = 60 * 10 // 10 minutes
// GET /stats — public platform statistics
app.get('/', async (c) => {
const cached = await redis.get(CACHE_KEY).catch(() => null)
if (cached) return c.json(JSON.parse(cached))
const [[{ specialistCount }], [{ taskCount }], [{ categoryCount }]] =
await Promise.all([
db.select({ specialistCount: countDistinct(specialistCards.specialistId) }).from(specialistCards).where(eq(specialistCards.isActive, true)),
db.select({ taskCount: count() }).from(tasks),
db.select({ categoryCount: count() }).from(categories).where(eq(categories.isActive, true)),
])
const result = { specialistCount, taskCount, categoryCount }
redis.set(CACHE_KEY, JSON.stringify(result), 'EX', CACHE_TTL).catch(() => {})
return c.json(result)
})
export default app