first commit
This commit is contained in:
61
app/api/admin/products/bulk/route.ts
Normal file
61
app/api/admin/products/bulk/route.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { auth } from '@/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
|
||||
export async function PATCH(request: NextRequest) {
|
||||
try {
|
||||
const session = await auth()
|
||||
|
||||
if (!session?.user || session.user.role !== 'ADMIN') {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const body = await request.json()
|
||||
const { productIds, action } = body
|
||||
|
||||
if (!productIds || !Array.isArray(productIds) || productIds.length === 0) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Product IDs are required' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
let updateData: any = {}
|
||||
|
||||
switch (action) {
|
||||
case 'activate':
|
||||
updateData = { isActive: true }
|
||||
break
|
||||
case 'deactivate':
|
||||
updateData = { isActive: false }
|
||||
break
|
||||
case 'delete':
|
||||
await prisma.product.deleteMany({
|
||||
where: {
|
||||
id: { in: productIds }
|
||||
}
|
||||
})
|
||||
return NextResponse.json({ success: true })
|
||||
default:
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid action' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
await prisma.product.updateMany({
|
||||
where: {
|
||||
id: { in: productIds }
|
||||
},
|
||||
data: updateData
|
||||
})
|
||||
|
||||
return NextResponse.json({ success: true })
|
||||
} catch (error) {
|
||||
console.error('Error in bulk action:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to perform bulk action' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user