51 lines
1.7 KiB
TypeScript

import { NextRequest } from 'next/server'
import { prisma } from '@/src/lib/prisma'
import { isAuthorizedDelete } from '@/src/lib/utils'
type BatchBody =
| { action: 'delete'; ids: string[] }
| { action: 'updateTimestamp'; ids: string[]; timestamp: string }
export async function POST(req: NextRequest) {
try {
if (!isAuthorizedDelete(req)) {
return Response.json({ error: 'unauthorized' }, { status: 401 })
}
const body = (await req.json()) as BatchBody
if (!body || !Array.isArray((body as any).ids) || (body as any).ids.length === 0) {
return Response.json({ error: 'ids required' }, { status: 400 })
}
const ids = (body as any).ids as string[]
if (body.action === 'delete') {
const result = await prisma.recording.deleteMany({ where: { id: { in: ids } } })
return Response.json({ success: true, deletedCount: result.count, deletedIds: ids })
}
if (body.action === 'updateTimestamp') {
const ts = new Date(body.timestamp)
if (Number.isNaN(ts.getTime())) {
return Response.json({ error: 'invalid timestamp' }, { status: 400 })
}
await prisma.recording.updateMany({ where: { id: { in: ids } }, data: { timestamp: ts } })
const updated = await prisma.recording.findMany({
where: { id: { in: ids } },
select: { id: true, timestamp: true },
})
return Response.json({
success: true,
updated: updated.map((r) => ({ id: r.id, timestamp: r.timestamp.toISOString() })),
})
}
return Response.json({ error: 'unsupported action' }, { status: 400 })
} catch (err) {
console.error(err)
return Response.json({ error: 'internal error' }, { status: 500 })
}
}