Fix Lock In 500 error: fs_projects has no updated_at column
The PATCH handler used SQL 'updated_at = NOW()' which doesn't exist on fs_projects (all timestamps live inside the data JSONB blob). Rewrote to use the same read-merge-write pattern as other working routes: fetch current data, merge in JS, write back as data::jsonb. Made-with: Cursor
This commit is contained in:
@@ -3,16 +3,6 @@ import { getServerSession } from 'next-auth';
|
|||||||
import { authOptions } from '@/lib/auth/authOptions';
|
import { authOptions } from '@/lib/auth/authOptions';
|
||||||
import { query } from '@/lib/db-postgres';
|
import { query } from '@/lib/db-postgres';
|
||||||
|
|
||||||
async function verifyOwnership(projectId: string, email: string): Promise<boolean> {
|
|
||||||
const rows = await query<{ id: string }>(
|
|
||||||
`SELECT p.id FROM fs_projects p
|
|
||||||
JOIN fs_users u ON u.id = p.user_id
|
|
||||||
WHERE p.id = $1 AND u.data->>'email' = $2
|
|
||||||
LIMIT 1`,
|
|
||||||
[projectId, email]
|
|
||||||
);
|
|
||||||
return rows.length > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GET — returns surfaces[] and surfaceThemes{} for the project.
|
* GET — returns surfaces[] and surfaceThemes{} for the project.
|
||||||
@@ -63,36 +53,41 @@ export async function PATCH(
|
|||||||
const session = await getServerSession(authOptions);
|
const session = await getServerSession(authOptions);
|
||||||
if (!session?.user?.email) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
if (!session?.user?.email) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||||
|
|
||||||
const owned = await verifyOwnership(projectId, session.user.email);
|
// Read current data (also verifies ownership)
|
||||||
if (!owned) return NextResponse.json({ error: 'Project not found' }, { status: 404 });
|
const rows = await query<{ data: Record<string, unknown> }>(
|
||||||
|
`SELECT p.data FROM fs_projects p
|
||||||
|
JOIN fs_users u ON u.id = p.user_id
|
||||||
|
WHERE p.id = $1 AND u.data->>'email' = $2
|
||||||
|
LIMIT 1`,
|
||||||
|
[projectId, session.user.email]
|
||||||
|
);
|
||||||
|
if (rows.length === 0) return NextResponse.json({ error: 'Project not found' }, { status: 404 });
|
||||||
|
|
||||||
|
const current = rows[0].data ?? {};
|
||||||
const body = await req.json() as
|
const body = await req.json() as
|
||||||
| { surfaces: string[] }
|
| { surfaces: string[] }
|
||||||
| { surface: string; theme: string };
|
| { surface: string; theme: string };
|
||||||
|
|
||||||
|
let updated: Record<string, unknown>;
|
||||||
|
|
||||||
if ('surfaces' in body) {
|
if ('surfaces' in body) {
|
||||||
await query(
|
updated = { ...current, surfaces: body.surfaces, updatedAt: new Date().toISOString() };
|
||||||
`UPDATE fs_projects
|
|
||||||
SET data = data || jsonb_build_object('surfaces', $2::jsonb),
|
|
||||||
updated_at = NOW()
|
|
||||||
WHERE id = $1`,
|
|
||||||
[projectId, JSON.stringify(body.surfaces)]
|
|
||||||
);
|
|
||||||
} else if ('surface' in body && 'theme' in body) {
|
} else if ('surface' in body && 'theme' in body) {
|
||||||
await query(
|
const existing = (current.surfaceThemes ?? {}) as Record<string, string>;
|
||||||
`UPDATE fs_projects
|
updated = {
|
||||||
SET data = data || jsonb_build_object(
|
...current,
|
||||||
'surfaceThemes',
|
surfaceThemes: { ...existing, [body.surface]: body.theme },
|
||||||
COALESCE(data->'surfaceThemes', '{}'::jsonb) || jsonb_build_object($2, $3)
|
updatedAt: new Date().toISOString(),
|
||||||
),
|
};
|
||||||
updated_at = NOW()
|
|
||||||
WHERE id = $1`,
|
|
||||||
[projectId, body.surface, body.theme]
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
return NextResponse.json({ error: 'Invalid body' }, { status: 400 });
|
return NextResponse.json({ error: 'Invalid body' }, { status: 400 });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await query(
|
||||||
|
`UPDATE fs_projects SET data = $1::jsonb WHERE id = $2`,
|
||||||
|
[JSON.stringify(updated), projectId]
|
||||||
|
);
|
||||||
|
|
||||||
return NextResponse.json({ success: true });
|
return NextResponse.json({ success: true });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('[design-surfaces PATCH]', err);
|
console.error('[design-surfaces PATCH]', err);
|
||||||
|
|||||||
Reference in New Issue
Block a user