Files
vibn-frontend/app/api/projects/[projectId]/design-surfaces/route.ts
Mark Henderson 53b098ce6a Fix Lock In 42P18: cast id::text to resolve parameter type ambiguity
PostgreSQL could not determine the type of $2 in 'WHERE id = $2'
when id column type is UUID. Casting the column (id::text = $1)
sidesteps the extended-protocol type inference issue. Also moves
projectId to $1 to match the proven working pattern in other routes.

Made-with: Cursor
2026-03-06 11:23:31 -08:00

99 lines
3.3 KiB
TypeScript

import { NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth/authOptions';
import { query } from '@/lib/db-postgres';
/**
* GET — returns surfaces[] and surfaceThemes{} for the project.
*/
export async function GET(
_req: Request,
{ params }: { params: Promise<{ projectId: string }> }
) {
try {
const { projectId } = await params;
const session = await getServerSession(authOptions);
if (!session?.user?.email) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
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 data = rows[0].data ?? {};
return NextResponse.json({
surfaces: (data.surfaces ?? []) as string[],
surfaceThemes: (data.surfaceThemes ?? {}) as Record<string, string>,
});
} catch (err) {
console.error('[design-surfaces GET]', err);
return NextResponse.json({ error: 'Internal error' }, { status: 500 });
}
}
/**
* PATCH — two operations:
* { surfaces: string[] } — save the active surface list
* { surface: string, theme: string } — lock in a theme for one surface
*/
export async function PATCH(
req: Request,
{ params }: { params: Promise<{ projectId: string }> }
) {
try {
const { projectId } = await params;
const session = await getServerSession(authOptions);
if (!session?.user?.email) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
// Read current data (also verifies ownership)
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
| { surfaces: string[] }
| { surface: string; theme: string };
let updated: Record<string, unknown>;
if ('surfaces' in body) {
updated = { ...current, surfaces: body.surfaces, updatedAt: new Date().toISOString() };
} else if ('surface' in body && 'theme' in body) {
const existing = (current.surfaceThemes ?? {}) as Record<string, string>;
updated = {
...current,
surfaceThemes: { ...existing, [body.surface]: body.theme },
updatedAt: new Date().toISOString(),
};
} else {
return NextResponse.json({ error: 'Invalid body' }, { status: 400 });
}
// Put projectId as $1 (matches known-working pattern in other routes)
// and cast id::text to avoid "could not determine data type of parameter" (42P18)
await query(
`UPDATE fs_projects SET data = $2::jsonb WHERE id::text = $1`,
[projectId, JSON.stringify(updated)]
);
return NextResponse.json({ success: true });
} catch (err) {
console.error('[design-surfaces PATCH]', err);
return NextResponse.json({ error: 'Internal error' }, { status: 500 });
}
}