2025-11-23 10:55:04 +08:00
|
|
|
import { NextResponse } from "next/server";
|
|
|
|
|
import { createSupabaseRouteClient } from "@/lib/supabase/server";
|
2026-01-17 10:12:53 +08:00
|
|
|
import { isConvexEnabled } from "@/lib/convex/enabled";
|
|
|
|
|
import { requireAuthContext } from "@/lib/auth/authContext";
|
|
|
|
|
import { getConvexHttpClient } from "@/lib/convex/server";
|
|
|
|
|
import { api } from "@/lib/convex/api";
|
2025-11-23 10:55:04 +08:00
|
|
|
|
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
|
|
|
|
|
|
export async function POST(request: Request) {
|
2026-01-17 10:12:53 +08:00
|
|
|
if (isConvexEnabled()) {
|
|
|
|
|
const auth = requireAuthContext();
|
|
|
|
|
const payload = await request.json().catch(() => ({}));
|
|
|
|
|
const workspaceId = payload.workspaceId as string | undefined;
|
|
|
|
|
if (!workspaceId) {
|
|
|
|
|
return NextResponse.json({ error: "缺少 workspaceId" }, { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const client = getConvexHttpClient();
|
|
|
|
|
await client.mutation(api.workspaces.switchDefaultWorkspace, {
|
|
|
|
|
userId: auth.userId,
|
|
|
|
|
workspaceId,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return NextResponse.json({ success: true });
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-23 10:55:04 +08:00
|
|
|
const supabase = await createSupabaseRouteClient();
|
|
|
|
|
const {
|
|
|
|
|
data: { session },
|
|
|
|
|
} = await supabase.auth.getSession();
|
|
|
|
|
|
|
|
|
|
if (!session) {
|
|
|
|
|
return NextResponse.json({ error: "未登录" }, { status: 401 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const payload = await request.json().catch(() => ({}));
|
|
|
|
|
const workspaceId = payload.workspaceId as string | undefined;
|
|
|
|
|
|
|
|
|
|
if (!workspaceId) {
|
|
|
|
|
return NextResponse.json({ error: "缺少 workspaceId" }, { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { data: membership, error: membershipError } = await supabase
|
|
|
|
|
.from("workspace_members")
|
|
|
|
|
.select("id")
|
|
|
|
|
.eq("workspace_id", workspaceId)
|
|
|
|
|
.eq("user_id", session.user.id)
|
|
|
|
|
.single();
|
|
|
|
|
|
|
|
|
|
if (membershipError || !membership) {
|
|
|
|
|
return NextResponse.json({ error: "无权切换至该工作空间" }, { status: 403 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { error: resetError } = await supabase
|
|
|
|
|
.from("workspace_members")
|
|
|
|
|
.update({ is_default: false })
|
|
|
|
|
.eq("user_id", session.user.id);
|
|
|
|
|
|
|
|
|
|
if (resetError) {
|
|
|
|
|
return NextResponse.json({ error: resetError.message }, { status: 500 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { error: switchError } = await supabase
|
|
|
|
|
.from("workspace_members")
|
|
|
|
|
.update({ is_default: true })
|
|
|
|
|
.eq("user_id", session.user.id)
|
|
|
|
|
.eq("workspace_id", workspaceId);
|
|
|
|
|
|
|
|
|
|
if (switchError) {
|
|
|
|
|
return NextResponse.json({ error: switchError.message }, { status: 500 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NextResponse.json({ success: true });
|
|
|
|
|
}
|