31 lines
989 B
TypeScript
31 lines
989 B
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { db } from '@/lib/db'
|
|
import { notifications } from '@/lib/db/schema'
|
|
import { eq, and, sql } from 'drizzle-orm'
|
|
import sdk from '@farcaster/frame-sdk'
|
|
|
|
export async function GET(req: NextRequest) {
|
|
try {
|
|
const context = await sdk.context
|
|
const userFid = context?.user?.fid?.toString()
|
|
|
|
if (!userFid) {
|
|
return NextResponse.json({ success: false, error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const result = await db.select({ count: sql<number>`count(*)` })
|
|
.from(notifications)
|
|
.where(and(
|
|
eq(notifications.userFid, userFid),
|
|
eq(notifications.isRead, false)
|
|
))
|
|
|
|
const count = Number(result[0]?.count || 0)
|
|
|
|
return NextResponse.json({ success: true, data: { count } })
|
|
} catch (error) {
|
|
console.error('Error fetching unread count:', error)
|
|
return NextResponse.json({ success: false, error: 'Failed to fetch unread count' }, { status: 500 })
|
|
}
|
|
}
|