- Rewrote Drizzle schema: 20 tables with enums, relations, indexes - Generated migration with DROP TABLE records (v1 EAV removal) - Added passkey auth routes (register/login) - Added requireWorkspaceAccess helper - Added seedDefaultData for Personal workspace + welcome note - Updated SSE endpoint for v2 entities + workspace_id filtering - Created recordActivity helper (insert + pg_notify) - Updated sidebar: Graph replaces Reports, removed Analytics - Updated command palette for v2 entities - Created AGENTS.md with locked contract - Created llm-wiki scaffold (5 stubs) - Added inline AGENT INSTRUCTION comments to all 50 API route files - Fixed globals.css border-border class conflict - Updated database.ts stub for v1 compatibility
66 lines
2.2 KiB
TypeScript
66 lines
2.2 KiB
TypeScript
// AGENT INSTRUCTION: After every write (INSERT/UPDATE/DELETE), you MUST:
|
|
// 1. Insert activity feed entry
|
|
// 2. Call pg.notify('project_e_events', JSON.stringify({ type, action, id, workspace_id }))
|
|
// See AGENTS.md for full rules.
|
|
|
|
import { NextRequest, NextResponse } from 'next/server';
|
|
import { withAuth, createErrorResponse } from '@/lib/auth';
|
|
import { createPocketBaseClient } from '@/lib/pocketbase';
|
|
import { updateProjectSchema } from '@project-e/shared';
|
|
import { z } from 'zod';
|
|
|
|
type RouteContext = { params: Promise<{ id: string }> };
|
|
|
|
// GET /api/projects/[id] — Get a single project with computed stats
|
|
export const GET = withAuth<RouteContext>(async (request: NextRequest, _user, context) => {
|
|
const { id } = await context!.params;
|
|
|
|
const pb = createPocketBaseClient();
|
|
const project = await pb.collection('projects').getOne(id);
|
|
|
|
// Compute task stats from related tasks
|
|
const tasksResult = await pb.collection('tasks').getFullList({
|
|
filter: 'project_id=' + id,
|
|
});
|
|
|
|
const taskCount = tasksResult.length;
|
|
const completedCount = tasksResult.filter((t: any) => t.status === 'done').length;
|
|
const progress = taskCount > 0 ? Math.round((completedCount / taskCount) * 100) : 0;
|
|
|
|
return NextResponse.json({
|
|
...project,
|
|
progress,
|
|
task_count: taskCount,
|
|
completed_count: completedCount,
|
|
});
|
|
});
|
|
|
|
// PATCH /api/projects/[id] — Update a project
|
|
export const PATCH = withAuth<RouteContext>(async (request: NextRequest, _user, context) => {
|
|
try {
|
|
const { id } = await context!.params;
|
|
const body = await request.json();
|
|
const data = updateProjectSchema.parse(body);
|
|
|
|
const pb = createPocketBaseClient();
|
|
const project = await pb.collection('projects').update(id, data);
|
|
|
|
return NextResponse.json(project);
|
|
} catch (error) {
|
|
if (error instanceof z.ZodError) {
|
|
return createErrorResponse('VALIDATION_ERROR', 'Invalid input', 400, error.issues);
|
|
}
|
|
throw error;
|
|
}
|
|
});
|
|
|
|
// DELETE /api/projects/[id] — Delete a project
|
|
export const DELETE = withAuth<RouteContext>(async (request: NextRequest, _user, context) => {
|
|
const { id } = await context!.params;
|
|
|
|
const pb = createPocketBaseClient();
|
|
await pb.collection('projects').delete(id);
|
|
|
|
return new NextResponse(null, { status: 204 });
|
|
});
|