70 lines
1.6 KiB
TypeScript
70 lines
1.6 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { auth } from '@/auth'
|
|
import { prisma } from '@/lib/prisma'
|
|
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
const session = await auth()
|
|
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const url = new URL(request.url)
|
|
const page = parseInt(url.searchParams.get('page') || '1')
|
|
const limit = parseInt(url.searchParams.get('limit') || '50')
|
|
const status = url.searchParams.get('status')
|
|
const type = url.searchParams.get('type')
|
|
const level = url.searchParams.get('level')
|
|
|
|
const where: any = { userId: session.user.id }
|
|
|
|
if (status && status !== 'all') {
|
|
where.status = status
|
|
}
|
|
|
|
if (type && type !== 'all') {
|
|
where.type = type
|
|
}
|
|
|
|
if (level && level !== 'all') {
|
|
where.level = parseInt(level)
|
|
}
|
|
|
|
const commissions = await prisma.commission.findMany({
|
|
where,
|
|
include: {
|
|
fromUser: {
|
|
select: {
|
|
name: true,
|
|
email: true
|
|
}
|
|
}
|
|
},
|
|
orderBy: {
|
|
createdAt: 'desc'
|
|
},
|
|
skip: (page - 1) * limit,
|
|
take: limit
|
|
})
|
|
|
|
const total = await prisma.commission.count({ where })
|
|
|
|
return NextResponse.json({
|
|
commissions,
|
|
pagination: {
|
|
page,
|
|
limit,
|
|
total,
|
|
pages: Math.ceil(total / limit)
|
|
}
|
|
})
|
|
} catch (error) {
|
|
console.error('Error fetching commissions:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch commissions' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|