flowstate / src /app /api /torque /events /route.ts
muthuk1's picture
feat: full Torque integration — live events, toast, bulk rescue, auto-scan
c6b6c96
import { NextResponse } from 'next/server'
import { sendCustomEvent, isTorqueConfigured } from '@/lib/torque-mcp'
import { pushEvent } from '@/lib/event-store'
export async function POST(req: Request) {
const body = await req.json()
const { wallet, eventType, eventName, metadata, data, risk, score } = body
const name = eventName || eventType
const payload = data || metadata || {}
if (!wallet || !name) {
return NextResponse.json({ error: 'wallet and eventName required' }, { status: 400 })
}
const result = await sendCustomEvent(wallet, name, payload)
if (!result.success) {
const status = isTorqueConfigured() ? 502 : 503
return NextResponse.json({ success: false, error: result.error }, { status })
}
pushEvent({
ingestionId: result.eventId || 'local-' + Date.now(),
wallet,
eventName: name,
risk,
score,
firedAt: new Date().toISOString(),
source: 'manual',
})
return NextResponse.json({ success: true, eventId: result.eventId })
}
export async function GET() {
return NextResponse.json({
status: isTorqueConfigured() ? 'ok' : 'unconfigured',
events: ['churn_risk_high', 'churn_risk_medium', 'comeback_detected', 'streak_maintained', 'volume_milestone', 'referral_from_saved', 'inactivity_detected'],
})
}