Files
vibn-frontend/app/api/projects/delete/route.ts
Mark Henderson 6f79a88abd fix(gitea-bot): add write:organization scope so bot can create repos
Without this the bot PAT 403s on POST /orgs/{org}/repos, which is
the single most important operation — creating new project repos
inside the workspace's Gitea org.

Made-with: Cursor
2026-04-21 11:05:55 -07:00

57 lines
1.8 KiB
TypeScript

import { NextResponse } from 'next/server';
import { authSession } from "@/lib/auth/session-server";
import { query } from '@/lib/db-postgres';
export async function POST(request: Request) {
try {
const session = await authSession();
if (!session?.user?.email) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const { projectId } = await request.json();
if (!projectId) {
return NextResponse.json({ error: 'Project ID is required' }, { status: 400 });
}
// Verify ownership
const rows = await query<{ id: string; data: any }>(`
SELECT p.id, 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 or unauthorized' }, { status: 404 });
}
// Unlink sessions
const sessionResult = await query(`
SELECT COUNT(*)::int AS count FROM fs_sessions WHERE data->>'projectId' = $1
`, [projectId]);
const sessionCount = sessionResult[0]?.count || 0;
await query(`
UPDATE fs_sessions
SET data = jsonb_set(
jsonb_set(data, '{projectId}', 'null'),
'{needsProjectAssociation}', 'true'
)
WHERE data->>'projectId' = $1
`, [projectId]);
// Delete the project
await query(`DELETE FROM fs_projects WHERE id = $1`, [projectId]);
return NextResponse.json({ success: true, message: 'Project deleted successfully', sessionsPreserved: sessionCount });
} catch (error) {
console.error('[POST /api/projects/delete] Error:', error);
return NextResponse.json(
{ error: 'Failed to delete project', details: error instanceof Error ? error.message : String(error) },
{ status: 500 }
);
}
}