fix(frontend): preserve owner profile when switching charts

This commit is contained in:
Jesse_Chen
2026-08-30 15:55:45 +08:00
parent 796e4cf8cf
commit f9a00dc568
3 changed files with 173 additions and 35 deletions
@@ -36,3 +36,34 @@ export async function DELETE(_request: Request, context: RouteContext) {
return NextResponse.json({ error: errorMessage(error, "星盘删除失败") }, { status: 500 });
}
}
export async function PUT(request: Request, context: RouteContext) {
try {
const { id } = await context.params;
const supabase = await createServerSupabaseClient();
const { data: { user } } = await supabase.auth.getUser();
if (!user) return NextResponse.json({ error: "请先登录" }, { status: 401 });
const body = await request.json().catch(() => null) as { profile?: unknown } | null;
if (!body?.profile || typeof body.profile !== "object" || Array.isArray(body.profile)) {
return NextResponse.json({ error: "星盘资料格式不正确" }, { status: 400 });
}
const { data, error } = await supabase
.from("chart_profiles")
.update({ profile: body.profile, updated_at: new Date().toISOString() })
.eq("id", id)
.eq("user_id", user.id)
.eq("role", "other")
.select("id, role, profile, updated_at")
.maybeSingle();
if (error) throw error;
if (!data) return NextResponse.json({ error: "星盘不存在或无权更新" }, { status: 404 });
return NextResponse.json({ profile: data });
} catch (error) {
if (isSupabaseConfigurationError(error)) {
return NextResponse.json({ error: "数据库尚未配置", code: "DATABASE_NOT_CONFIGURED" }, { status: 503 });
}
return NextResponse.json({ error: errorMessage(error, "星盘更新失败") }, { status: 500 });
}
}