41 lines
1.3 KiB
TypeScript
41 lines
1.3 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { getDb } from '@/lib/db'
|
|
import { referrals } from '@/lib/db/schema'
|
|
import { eq, sql } from 'drizzle-orm'
|
|
import sdk from '@farcaster/frame-sdk'
|
|
|
|
export async function GET(req: NextRequest) {
|
|
try {
|
|
const context = await sdk.context
|
|
const fid = context?.user?.fid
|
|
|
|
if (!fid) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const db = getDb(process.env.DATABASE_URL!)
|
|
|
|
const stats = await db
|
|
.select({
|
|
total: sql<number>`count(*)`,
|
|
completed: sql<number>`count(*) filter (where status = 'completed')`,
|
|
earnings: sql<string>`coalesce(sum(reward_amount) filter (where status = 'completed'), 0)`,
|
|
})
|
|
.from(referrals)
|
|
.where(eq(referrals.referrerFid, fid.toString()))
|
|
|
|
const result = stats[0] || { total: 0, completed: 0, earnings: '0' }
|
|
|
|
return NextResponse.json({
|
|
totalReferrals: result.total,
|
|
completedReferrals: result.completed,
|
|
pendingReferrals: result.total - result.completed,
|
|
totalEarnings: result.earnings,
|
|
rank: 0,
|
|
})
|
|
} catch (error) {
|
|
console.error('Get referral stats error:', error)
|
|
return NextResponse.json({ error: 'Failed to get stats' }, { status: 500 })
|
|
}
|
|
}
|