0.0.1.2
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
import { NextRequest, NextResponse } from "next/server"
|
||||
import { getServerSession } from "next-auth"
|
||||
import { authOptions } from "@/lib/auth"
|
||||
import { prisma } from "@/lib/prisma"
|
||||
|
||||
// 获取协作者列表
|
||||
export async function GET(
|
||||
req: NextRequest,
|
||||
{ params }: { params: Promise<{ treeId: string }> }
|
||||
) {
|
||||
try {
|
||||
const session = await getServerSession(authOptions)
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: "未授权" }, { status: 401 })
|
||||
}
|
||||
|
||||
const { treeId } = await params
|
||||
|
||||
// 检查权限(所有者或协作者都可以看)
|
||||
const tree = await prisma.familyTree.findFirst({
|
||||
where: {
|
||||
id: treeId,
|
||||
OR: [
|
||||
{ ownerId: session.user.id },
|
||||
{
|
||||
collaborators: {
|
||||
some: {
|
||||
userId: session.user.id
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
})
|
||||
|
||||
if (!tree) {
|
||||
return NextResponse.json({ error: "家族树不存在或无权访问" }, { status: 404 })
|
||||
}
|
||||
|
||||
// 获取协作者
|
||||
const collaborators = await prisma.treeCollaborator.findMany({
|
||||
where: { treeId },
|
||||
include: {
|
||||
user: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
email: true,
|
||||
avatar: true
|
||||
}
|
||||
}
|
||||
},
|
||||
orderBy: {
|
||||
invitedAt: 'desc'
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json({ collaborators })
|
||||
} catch (error) {
|
||||
console.error("获取协作者失败:", error)
|
||||
return NextResponse.json(
|
||||
{ error: "获取协作者失败" },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// 移除协作者
|
||||
export async function DELETE(
|
||||
req: NextRequest,
|
||||
{ params }: { params: Promise<{ treeId: string }> }
|
||||
) {
|
||||
try {
|
||||
const session = await getServerSession(authOptions)
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: "未授权" }, { status: 401 })
|
||||
}
|
||||
|
||||
const { treeId } = await params
|
||||
const { searchParams } = new URL(req.url)
|
||||
const userId = searchParams.get('userId')
|
||||
|
||||
if (!userId) {
|
||||
return NextResponse.json({ error: "缺少用户ID" }, { status: 400 })
|
||||
}
|
||||
|
||||
// 检查权限:只有所有者可以移除协作者,或者是协作者自己退出
|
||||
const tree = await prisma.familyTree.findFirst({
|
||||
where: {
|
||||
id: treeId
|
||||
}
|
||||
})
|
||||
|
||||
if (!tree) {
|
||||
return NextResponse.json({ error: "家族树不存在" }, { status: 404 })
|
||||
}
|
||||
|
||||
const isOwner = tree.ownerId === session.user.id
|
||||
const isSelf = userId === session.user.id
|
||||
|
||||
if (!isOwner && !isSelf) {
|
||||
return NextResponse.json({ error: "无权移除此协作者" }, { status: 403 })
|
||||
}
|
||||
|
||||
// 移除协作者
|
||||
await prisma.treeCollaborator.deleteMany({
|
||||
where: {
|
||||
treeId,
|
||||
userId
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json({ success: true })
|
||||
} catch (error) {
|
||||
console.error("移除协作者失败:", error)
|
||||
return NextResponse.json(
|
||||
{ error: "移除协作者失败" },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user