92 lines
2.3 KiB
TypeScript
92 lines
2.3 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 || session.user.role !== 'ADMIN') {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const { searchParams } = new URL(request.url)
|
|
const page = parseInt(searchParams.get('page') || '1')
|
|
const limit = parseInt(searchParams.get('limit') || '20')
|
|
const search = searchParams.get('search') || ''
|
|
const approved = searchParams.get('approved')
|
|
|
|
const skip = (page - 1) * limit
|
|
|
|
const where: any = {}
|
|
|
|
if (search) {
|
|
where.OR = [
|
|
{ title: { contains: search, mode: 'insensitive' } },
|
|
{ comment: { contains: search, mode: 'insensitive' } },
|
|
{ user: { name: { contains: search, mode: 'insensitive' } } },
|
|
{ product: { name: { contains: search, mode: 'insensitive' } } }
|
|
]
|
|
}
|
|
|
|
if (approved !== null && approved !== undefined) {
|
|
where.isApproved = approved === 'true'
|
|
}
|
|
|
|
const [reviews, total] = await Promise.all([
|
|
prisma.review.findMany({
|
|
where,
|
|
include: {
|
|
user: {
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
email: true
|
|
}
|
|
},
|
|
product: {
|
|
select: {
|
|
id: true,
|
|
name: true
|
|
}
|
|
},
|
|
_count: {
|
|
select: {
|
|
helpfulVotedBy: true,
|
|
reportedBy: true
|
|
}
|
|
}
|
|
},
|
|
orderBy: {
|
|
createdAt: 'desc'
|
|
},
|
|
skip,
|
|
take: limit
|
|
}),
|
|
prisma.review.count({ where })
|
|
])
|
|
|
|
const reviewsWithCounts = reviews.map(review => ({
|
|
...review,
|
|
helpfulVotes: review._count.helpfulVotedBy,
|
|
reportCount: review._count.reportedBy
|
|
}))
|
|
|
|
return NextResponse.json({
|
|
reviews: reviewsWithCounts,
|
|
pagination: {
|
|
page,
|
|
limit,
|
|
total,
|
|
pages: Math.ceil(total / limit)
|
|
}
|
|
})
|
|
} catch (error) {
|
|
console.error('Error fetching admin reviews:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch reviews' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|