fix: insert other chart profiles directly (#15)

This commit is contained in:
732642856
2026-07-20 17:45:31 +08:00
committed by GitHub
parent cdfd389662
commit b3bca954da
4 changed files with 17 additions and 12 deletions
+1 -8
View File
@@ -70,16 +70,9 @@ export async function POST(request: Request) {
.single();
({ data, error } = await query);
} else {
const record = {
...(body.id ? { id: body.id } : {}),
user_id: user.id,
role,
profile: body.profile,
updated_at: updatedAt,
};
({ data, error } = await supabase
.from("chart_profiles")
.upsert(record, { onConflict: "id" })
.insert({ user_id: user.id, role, profile: body.profile, updated_at: updatedAt })
.select("id, role, profile, updated_at")
.single());
}
+2 -3
View File
@@ -337,13 +337,12 @@ async function saveCloudChartProfile(record: ChartLibraryRecord) {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
id: record.role === "self" ? undefined : record.id,
role: record.role,
profile: record.profile,
}),
});
if (!response.ok) throw new Error("cloud_chart_profile_save_failed");
const payload = await response.json().catch(() => null) as { profile?: ChartLibraryApiRecord } | null;
const payload = await response.json().catch(() => null) as { profile?: ChartLibraryApiRecord; error?: string } | null;
if (!response.ok) throw new Error(payload?.error || "cloud_chart_profile_save_failed");
return payload?.profile ? normalizeChartLibraryApiRecord(payload.profile) : record;
}
@@ -3,6 +3,7 @@ import { readFileSync } from "node:fs";
import test from "node:test";
const source = readFileSync(new URL("../src/app/page.tsx", import.meta.url), "utf8");
const route = readFileSync(new URL("../src/app/api/chart-profiles/route.ts", import.meta.url), "utf8");
test("other chart saves do not require the owner's rectification state", () => {
assert.match(source, /function missingOtherProfileStep\(profile: Profile\)/);
@@ -35,3 +36,14 @@ test("a successful cloud read replaces stale local other charts", () => {
);
assert.doesNotMatch(source, /fetchCloudChartLibrary\(\)[\s\S]{0,800}new Map\(\[[\s\S]{0,500}current\.filter\(\(record\) => record\.role === "other"\)/);
});
test("other chart creation lets the database create its UUID", () => {
assert.match(route, /\.insert\(\{ user_id: user\.id, role, profile: body\.profile, updated_at: updatedAt \}\)/);
assert.doesNotMatch(route, /\.upsert\(record, \{ onConflict: "id" \}\)/);
assert.doesNotMatch(source, /id: record\.role === "self" \? undefined : record\.id/);
});
test("cloud save failures preserve the server error message", () => {
assert.match(source, /const payload = await response\.json\(\)\.catch\(\(\) => null\) as \{ error\?: string \} \| null;/);
assert.match(source, /throw new Error\(payload\?\.error \|\| "cloud_chart_profile_save_failed"\);/);
});
+2 -1
View File
@@ -141,9 +141,10 @@ def test_chart_profile_library_has_cloud_table_api_and_local_fallback() -> None:
'eq("user_id", user.id)',
'eq("role", "self")',
'insert({ user_id: user.id, role, profile: body.profile',
'upsert(record, { onConflict: "id" })',
'insert({ user_id: user.id, role, profile: body.profile, updated_at: updatedAt })',
):
assert token in route
assert 'upsert(record, { onConflict: "id" })' not in route
assert 'eq("role", "other")' in delete_route
for token in (