Files
agent-park/src/app/api/discovery/tasks/batch-reset/route.ts
T

73 lines
2.0 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { prisma } from '@/lib/prisma'
import { BatchResetTasksSchema } from '@/lib/validations'
import { isValidApiKey } from '@/lib/auth'
/**
* POST /api/discovery/tasks/batch-reset
* 批量重置任务状态为 PENDING
*
* 用于 n8n 探索流程失败后重置任务,支持两种模式:
* 1. 按任务 ID 列表重置:{ taskIds: ["id1", "id2"] }
* 2. 按状态筛选重置:{ statuses: ["IN_PROGRESS", "FAILED"] }
* 不传 statuses 则默认重置 IN_PROGRESS 和 FAILED
*/
export async function POST(request: NextRequest) {
try {
const body = await request.json()
const validation = BatchResetTasksSchema.safeParse(body)
if (!validation.success) {
return NextResponse.json(
{
success: false,
error: 'Validation error',
details: validation.error.errors.map((e) => e.message),
},
{ status: 400 }
)
}
const { apiKey, taskIds, statuses } = validation.data
// 验证API密钥
if (!isValidApiKey(apiKey)) {
return NextResponse.json(
{ success: false, error: 'Unauthorized' },
{ status: 401 }
)
}
// 构建查询条件
const where = taskIds
? { id: { in: taskIds } }
: { status: { in: statuses || ['IN_PROGRESS', 'FAILED'] } }
// 批量更新任务状态
const result = await prisma.projectDiscoveryTask.updateMany({
where,
data: {
status: 'PENDING',
startedAt: null,
completedAt: null,
errorMessage: null,
},
})
console.warn(
`[Discovery] Batch reset ${result.count} tasks to PENDING. Condition: ${JSON.stringify(where)}`
)
return NextResponse.json({
success: true,
reset: result.count,
})
} catch (error) {
console.error('[Discovery] Error batch resetting tasks:', error)
return NextResponse.json(
{ success: false, error: 'Internal server error' },
{ status: 500 }
)
}
}