Prmbr/src/app/api/simulator/[id]/share/route.ts
2025-09-02 00:11:45 +08:00

91 lines
2.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { NextResponse } from 'next/server'
import { prisma } from '@/lib/prisma'
import { auth } from '@/lib/auth'
import { headers } from 'next/headers'
export async function POST(
request: Request,
context: { params: Promise<{ id: string }> }
) {
try {
const params = await context.params
const session = await auth.api.getSession({
headers: await headers()
})
if (!session?.user) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
const { permissions } = await request.json()
if (!permissions || !['private', 'public'].includes(permissions)) {
return NextResponse.json(
{ error: 'Invalid permissions value' },
{ status: 400 }
)
}
// 检查 simulator run 是否存在且属于当前用户
const run = await prisma.simulatorRun.findFirst({
where: {
id: params.id,
userId: session.user.id
}
})
if (!run) {
return NextResponse.json(
{ error: 'Simulator run not found' },
{ status: 404 }
)
}
// 只有已完成的 run 才能共享
if (run.status !== 'completed') {
return NextResponse.json(
{ error: 'Only completed simulator runs can be shared' },
{ status: 400 }
)
}
// 更新权限设置
const updatedRun = await prisma.simulatorRun.update({
where: { id: params.id },
data: {
permissions,
// 如果设为 public重置 visibility 为等待审核状态
visibility: permissions === 'public' ? null : null
},
include: {
prompt: {
select: {
id: true,
name: true,
content: true
}
},
model: {
select: {
id: true,
name: true,
provider: true,
modelId: true,
outputType: true,
description: true,
maxTokens: true
}
}
}
})
return NextResponse.json(updatedRun)
} catch (error) {
console.error('Error toggling simulator run share status:', error)
return NextResponse.json(
{ error: 'Failed to update share status' },
{ status: 500 }
)
}
}