T1/Phase 1: scaffold Vite SPA + Hono API + Bun worker

- apps/web: Vite + React 19 + TanStack Router/Query + shadcn/ui
   - apps/api: Hono + Bun on :3001 with /api/health, /api/auth/*, /mcp stubs
   - apps/worker: Bun worker stub, DB connection, graceful SIGTERM
   - apps/web-legacy/: old Next.js code moved aside (preserved for T2-T8 reference)
   - Dockerfiles: api (Bun), worker (Bun), spa (multi-stage Caddy)
   - Caddyfile: serves dist + reverse-proxies /api/* + /mcp to api
   - docker-compose.yml: 4-service target (api, spa, db, worker)
   - packages/db/src/client.ts: shared Drizzle client for api + worker
   - db/client.ts: root-level alias for convenience

   Parent: t_e1cbd87d -> t_24c9c3fd (T0)
This commit is contained in:
Hermes
2026-08-01 01:15:31 +00:00
parent 9203aee758
commit fca56ab77e
312 changed files with 3489 additions and 196 deletions
@@ -0,0 +1,47 @@
// 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, requireWorkspaceAccess, createErrorResponse } from '@/lib/auth';
import { db, activityFeed } from '@project-e/db';
import { and, desc, eq, sql } from 'drizzle-orm';
type RouteContext = { params: Promise<{ domainId: string }> };
// GET /api/domains/[domainId]/activity — List activity feed for a workspace
export const GET = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId } = await context!.params;
await requireWorkspaceAccess(domainId);
const { searchParams } = new URL(request.url);
const entityType = searchParams.get('entity_type');
const limit = Math.min(parseInt(searchParams.get('limit') || '20'), 100);
const offset = parseInt(searchParams.get('offset') || '0');
const conditions: any[] = [eq(activityFeed.workspaceId, domainId)];
if (entityType) {
conditions.push(eq(activityFeed.entityType, entityType));
}
const [items, countResult] = await Promise.all([
db.select()
.from(activityFeed)
.where(and(...conditions))
.orderBy(desc(activityFeed.createdAt))
.limit(limit)
.offset(offset),
db.select({ count: sql<number>`count(*)` })
.from(activityFeed)
.where(and(...conditions)),
]);
return NextResponse.json({
items,
totalItems: Number(countResult[0]?.count || 0),
limit,
offset,
});
});
@@ -0,0 +1,217 @@
// 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, requireWorkspaceAccess, createErrorResponse } from '@/lib/auth';
import { db, tasks, habits, habitCompletions, projects, sections, domains } from '@project-e/db';
import { and, asc, between, eq, gte, isNull, lte, sql } from 'drizzle-orm';
type RouteContext = { params: Promise<{ domainId: string }> };
interface CalendarEvent {
id: string;
title: string;
start: string;
end: string;
type: 'task' | 'habit' | 'project' | 'milestone';
entityType: string;
entityId: string;
color: string;
domainId: string;
href: string;
priority?: string;
difficulty?: string;
status?: string;
}
// GET /api/domains/[domainId]/calendar/events?from=&to=
// Returns all events (tasks with due_date, habits scheduled for date range, project target dates)
// joined with domain for color/title
export const GET = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId } = await context!.params;
await requireWorkspaceAccess(domainId);
const { searchParams } = new URL(request.url);
const from = searchParams.get('from');
const to = searchParams.get('to');
const types = searchParams.get('types')?.split(',').filter(Boolean) || ['task', 'habit', 'project', 'milestone'];
if (!from || !to) {
return createErrorResponse('VALIDATION_ERROR', 'from and to query params are required (ISO dates)', 400);
}
const fromDate = new Date(from);
const toDate = new Date(to);
// Get domain for color
const [domain] = await db.select({ color: domains.color, name: domains.name })
.from(domains)
.where(eq(domains.id, domainId))
.limit(1);
const domainColor = domain?.color || '#3b82f6';
const events: CalendarEvent[] = [];
// 1. Tasks with due_date in range
if (types.includes('task')) {
const taskRows = await db.select()
.from(tasks)
.where(and(
eq(tasks.domainId, domainId),
isNull(tasks.deletedAt),
gte(tasks.dueDate, fromDate),
lte(tasks.dueDate, toDate),
))
.orderBy(asc(tasks.dueDate));
for (const task of taskRows) {
if (!task.dueDate) continue;
const color = task.priority === 'urgent' ? '#ef4444'
: task.priority === 'high' ? '#f97316'
: task.priority === 'medium' ? '#3b82f6'
: '#6b7280';
events.push({
id: `task-${task.id}`,
title: task.title,
start: task.dueDate.toISOString(),
end: task.dueDate.toISOString(),
type: 'task',
entityType: 'task',
entityId: task.id,
color,
domainId,
href: `/tasks/${task.id}`,
priority: task.priority,
status: task.status,
});
}
}
// 2. Habits — check if they have completions in range (scheduled habits)
if (types.includes('habit')) {
const habitRows = await db.select()
.from(habits)
.where(and(
eq(habits.domainId, domainId),
eq(habits.active, true),
isNull(habits.deletedAt),
));
for (const habit of habitRows) {
const color = habit.difficulty === 'hard' ? '#ef4444'
: habit.difficulty === 'medium' ? '#f97316'
: '#22c55e';
// Check if habit has completions in range
const completions = await db.select({ date: habitCompletions.date })
.from(habitCompletions)
.where(and(
eq(habitCompletions.habitId, habit.id),
gte(habitCompletions.date, fromDate),
lte(habitCompletions.date, toDate),
));
const completedDates = new Set(completions.map(c => c.date.toISOString().split('T')[0]));
// Generate events for each day in range (for daily habits)
// For weekly/custom, just show the habit as a recurring event
const current = new Date(fromDate);
while (current <= toDate) {
const dayOfWeek = current.getDay();
const skipDays = (habit.skipDays || []) as number[];
const dateStr = current.toISOString().split('T')[0];
if (!skipDays.includes(dayOfWeek)) {
const isCompleted = completedDates.has(dateStr);
events.push({
id: `habit-${habit.id}-${dateStr}`,
title: `${isCompleted ? '✅ ' : '○ '}${habit.name}`,
start: current.toISOString(),
end: current.toISOString(),
type: 'habit',
entityType: 'habit',
entityId: habit.id,
color,
domainId,
href: '/habits',
difficulty: habit.difficulty,
status: isCompleted ? 'completed' : 'pending',
});
}
current.setDate(current.getDate() + 1);
}
}
}
// 3. Projects with target_date in range
if (types.includes('project')) {
const projectRows = await db.select()
.from(projects)
.where(and(
eq(projects.domainId, domainId),
isNull(projects.deletedAt),
gte(projects.targetDate, fromDate),
lte(projects.targetDate, toDate),
))
.orderBy(asc(projects.targetDate));
for (const project of projectRows) {
if (!project.targetDate) continue;
events.push({
id: `project-${project.id}`,
title: `📁 ${project.name}`,
start: project.targetDate.toISOString(),
end: project.targetDate.toISOString(),
type: 'project',
entityType: 'project',
entityId: project.id,
color: project.color || '#8b5cf6',
domainId,
href: `/projects/${project.id}`,
status: project.status,
});
}
}
// 4. Sections (milestones) with target_date in range
if (types.includes('milestone')) {
const milestoneRows = await db.select({
id: sections.id,
name: sections.name,
targetDate: sections.targetDate,
projectId: sections.projectId,
status: sections.status,
kind: sections.kind,
})
.from(sections)
.innerJoin(projects, eq(sections.projectId, projects.id))
.where(and(
eq(projects.domainId, domainId),
eq(sections.kind, 'milestone'),
gte(sections.targetDate, fromDate),
lte(sections.targetDate, toDate),
))
.orderBy(asc(sections.targetDate));
for (const milestone of milestoneRows) {
if (!milestone.targetDate) continue;
events.push({
id: `milestone-${milestone.id}`,
title: `🏁 ${milestone.name}`,
start: milestone.targetDate.toISOString(),
end: milestone.targetDate.toISOString(),
type: 'milestone',
entityType: 'section',
entityId: milestone.id,
color: '#f59e0b',
domainId,
href: `/projects/${milestone.projectId}`,
status: milestone.status,
});
}
}
return NextResponse.json({ events });
});
@@ -0,0 +1,73 @@
// 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, requireWorkspaceAccess, createErrorResponse, ApiError } from '@/lib/auth';
import { recordActivity } from '@/lib/activity';
import { db, domains } from '@project-e/db';
import { eq } from 'drizzle-orm';
import { z } from 'zod';
type RouteContext = { params: Promise<{ domainId: string }> };
const layoutItemSchema = z.object({
widgetId: z.string(),
order: z.number().int(),
enabled: z.boolean(),
config: z.record(z.string(), z.unknown()).optional(),
});
const updateLayoutSchema = z.object({
layout: z.array(layoutItemSchema),
});
// PUT /api/domains/[domainId]/dashboard/layout — Update dashboard layout
export const PUT = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId } = await context!.params;
await requireWorkspaceAccess(domainId);
try {
const body = await request.json();
const data = updateLayoutSchema.parse(body);
const [domain] = await db.select({ id: domains.id, customFields: domains.customFields })
.from(domains)
.where(eq(domains.id, domainId))
.limit(1);
if (!domain) {
return createErrorResponse('NOT_FOUND', 'Domain not found', 404);
}
// Store layout in domain's custom_fields
const existingFields = (domain.customFields as Record<string, unknown>) || {};
await db.update(domains)
.set({
customFields: { ...existingFields, dashboard_layout: data.layout },
updatedAt: new Date(),
})
.where(eq(domains.id, domainId));
await recordActivity({
actor: user.name,
action: 'updated',
entityType: 'domain',
entityId: domainId,
changes: { dashboardLayout: data.layout },
workspaceId: domainId,
});
return NextResponse.json({ layout: data.layout });
} catch (error) {
if (error instanceof z.ZodError) {
return createErrorResponse('VALIDATION_ERROR', 'Invalid input', 400, error.issues);
}
if (error instanceof ApiError) {
return createErrorResponse(error.code, error.message, error.status);
}
console.error('[dashboard/layout PUT] error:', error);
return createErrorResponse('INTERNAL_ERROR', 'Failed to update dashboard layout', 500);
}
});
@@ -0,0 +1,106 @@
// 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, requireWorkspaceAccess, createErrorResponse, ApiError } from '@/lib/auth';
import { recordActivity } from '@/lib/activity';
import { db, domains } from '@project-e/db';
import { eq } from 'drizzle-orm';
import { z } from 'zod';
type RouteContext = { params: Promise<{ domainId: string }> };
const layoutItemSchema = z.object({
widgetId: z.string(),
order: z.number().int(),
enabled: z.boolean(),
config: z.record(z.string(), z.unknown()).optional(),
});
const updateLayoutSchema = z.object({
layout: z.array(layoutItemSchema),
});
// GET /api/domains/[domainId]/dashboard — Returns layout (widget order) + widget data
export const GET = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId } = await context!.params;
await requireWorkspaceAccess(domainId);
// Verify domain exists
const [domain] = await db.select({ id: domains.id, name: domains.name, color: domains.color, customFields: domains.customFields })
.from(domains)
.where(eq(domains.id, domainId))
.limit(1);
if (!domain) {
return createErrorResponse('NOT_FOUND', 'Domain not found', 404);
}
// Dashboard layout is stored in the domain's custom_fields as jsonb
// We use a convention: dashboard_layout key in custom_fields
const storedLayout = (domain.customFields as Record<string, unknown>)?.dashboard_layout as Array<{ widgetId: string; order: number; enabled: boolean; config?: Record<string, unknown> }> | undefined;
const defaultLayout = [
{ widgetId: 'today-tasks', order: 0, enabled: true },
{ widgetId: 'habit-checklist', order: 1, enabled: true },
{ widgetId: 'weekly-stats', order: 2, enabled: true },
{ widgetId: 'project-progress', order: 3, enabled: true },
{ widgetId: 'upcoming-calendar', order: 4, enabled: true },
{ widgetId: 'recent-notes', order: 5, enabled: true },
{ widgetId: 'activity-feed', order: 6, enabled: true },
{ widgetId: 'quick-capture', order: 7, enabled: true },
];
return NextResponse.json({ layout: storedLayout || defaultLayout });
});
// PUT /api/domains/[domainId]/dashboard/layout — Update dashboard layout
export const PUT = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId } = await context!.params;
await requireWorkspaceAccess(domainId);
try {
const body = await request.json();
const data = updateLayoutSchema.parse(body);
const [domain] = await db.select({ id: domains.id, customFields: domains.customFields })
.from(domains)
.where(eq(domains.id, domainId))
.limit(1);
if (!domain) {
return createErrorResponse('NOT_FOUND', 'Domain not found', 404);
}
// Store layout in domain's custom_fields
const existingFields = (domain.customFields as Record<string, unknown>) || {};
await db.update(domains)
.set({
customFields: { ...existingFields, dashboard_layout: data.layout },
updatedAt: new Date(),
})
.where(eq(domains.id, domainId));
await recordActivity({
actor: user.name,
action: 'updated',
entityType: 'domain',
entityId: domainId,
changes: { dashboardLayout: data.layout },
workspaceId: domainId,
});
return NextResponse.json({ layout: data.layout });
} catch (error) {
if (error instanceof z.ZodError) {
return createErrorResponse('VALIDATION_ERROR', 'Invalid input', 400, error.issues);
}
if (error instanceof ApiError) {
return createErrorResponse(error.code, error.message, error.status);
}
console.error('[dashboard PUT] error:', error);
return createErrorResponse('INTERNAL_ERROR', 'Failed to update dashboard layout', 500);
}
});
@@ -0,0 +1,25 @@
// 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, requireWorkspaceAccess } from '@/lib/auth';
import { db, activityFeed } from '@project-e/db';
import { and, desc, eq } from 'drizzle-orm';
type RouteContext = { params: Promise<{ domainId: string }> };
// GET /api/domains/[domainId]/dashboard/widgets/activity-feed
export const GET = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId } = await context!.params;
await requireWorkspaceAccess(domainId);
const items = await db.select()
.from(activityFeed)
.where(eq(activityFeed.workspaceId, domainId))
.orderBy(desc(activityFeed.createdAt))
.limit(20);
return NextResponse.json({ items });
});
@@ -0,0 +1,50 @@
// 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, requireWorkspaceAccess } from '@/lib/auth';
import { db, habits, habitCompletions } from '@project-e/db';
import { and, eq, gte, isNull, lte, sql } from 'drizzle-orm';
type RouteContext = { params: Promise<{ domainId: string }> };
// GET /api/domains/[domainId]/dashboard/widgets/habit-checklist
export const GET = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId } = await context!.params;
await requireWorkspaceAccess(domainId);
const today = new Date();
today.setHours(0, 0, 0, 0);
const tomorrow = new Date(today);
tomorrow.setDate(tomorrow.getDate() + 1);
const activeHabits = await db.select()
.from(habits)
.where(and(
eq(habits.domainId, domainId),
eq(habits.active, true),
isNull(habits.deletedAt),
));
// Check which habits are completed today
const items = [];
for (const habit of activeHabits) {
const [completion] = await db.select({ count: sql<number>`count(*)` })
.from(habitCompletions)
.where(and(
eq(habitCompletions.habitId, habit.id),
gte(habitCompletions.date, today),
lte(habitCompletions.date, tomorrow),
));
const completed = Number(completion?.count || 0) > 0;
items.push({
...habit,
completedToday: completed,
});
}
return NextResponse.json({ items });
});
@@ -0,0 +1,54 @@
// 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, requireWorkspaceAccess } from '@/lib/auth';
import { db, projects, tasks } from '@project-e/db';
import { and, eq, inArray, isNull, sql } from 'drizzle-orm';
type RouteContext = { params: Promise<{ domainId: string }> };
// GET /api/domains/[domainId]/dashboard/widgets/project-progress
export const GET = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId } = await context!.params;
await requireWorkspaceAccess(domainId);
const activeProjects = await db.select()
.from(projects)
.where(and(
eq(projects.domainId, domainId),
inArray(projects.status, ['active', 'paused']),
isNull(projects.deletedAt),
));
// Compute progress for each project
const items = [];
for (const project of activeProjects) {
const [totalResult] = await db.select({ count: sql<number>`count(*)` })
.from(tasks)
.where(and(eq(tasks.projectId, project.id), isNull(tasks.deletedAt)));
const [completedResult] = await db.select({ count: sql<number>`count(*)` })
.from(tasks)
.where(and(eq(tasks.projectId, project.id), eq(tasks.status, 'done'), isNull(tasks.deletedAt)));
const total = Number(totalResult?.count || 0);
const completed = Number(completedResult?.count || 0);
const progress = total > 0 ? Math.round((completed / total) * 100) : 0;
items.push({
id: project.id,
name: project.name,
status: project.status,
color: project.color,
targetDate: project.targetDate,
taskCount: total,
completedCount: completed,
progress,
});
}
return NextResponse.json({ items });
});
@@ -0,0 +1,34 @@
// 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, requireWorkspaceAccess } from '@/lib/auth';
import { db, notes } from '@project-e/db';
import { and, desc, eq, isNull } from 'drizzle-orm';
type RouteContext = { params: Promise<{ domainId: string }> };
// GET /api/domains/[domainId]/dashboard/widgets/recent-notes
export const GET = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId } = await context!.params;
await requireWorkspaceAccess(domainId);
const items = await db.select({
id: notes.id,
title: notes.title,
updatedAt: notes.updatedAt,
isPinned: notes.isPinned,
})
.from(notes)
.where(and(
eq(notes.domainId, domainId),
eq(notes.isArchived, false),
isNull(notes.deletedAt),
))
.orderBy(desc(notes.updatedAt))
.limit(5);
return NextResponse.json({ items });
});
@@ -0,0 +1,35 @@
// 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, requireWorkspaceAccess } from '@/lib/auth';
import { db, tasks, habits, habitCompletions, projects, notes, activityFeed, domains } from '@project-e/db';
import { and, asc, desc, eq, gte, inArray, isNull, lte, sql } from 'drizzle-orm';
type RouteContext = { params: Promise<{ domainId: string }> };
// GET /api/domains/[domainId]/dashboard/widgets/today-tasks
export const GET = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId } = await context!.params;
await requireWorkspaceAccess(domainId);
const today = new Date();
today.setHours(0, 0, 0, 0);
const tomorrow = new Date(today);
tomorrow.setDate(tomorrow.getDate() + 1);
const items = await db.select()
.from(tasks)
.where(and(
eq(tasks.domainId, domainId),
isNull(tasks.deletedAt),
gte(tasks.dueDate, today),
lte(tasks.dueDate, tomorrow),
))
.orderBy(asc(tasks.priority))
.limit(10);
return NextResponse.json({ items });
});
@@ -0,0 +1,63 @@
// 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, requireWorkspaceAccess } from '@/lib/auth';
import { db, tasks, projects, sections } from '@project-e/db';
import { and, asc, eq, gte, isNull, lte, sql } from 'drizzle-orm';
type RouteContext = { params: Promise<{ domainId: string }> };
// GET /api/domains/[domainId]/dashboard/widgets/upcoming-calendar
export const GET = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId } = await context!.params;
await requireWorkspaceAccess(domainId);
const today = new Date();
today.setHours(0, 0, 0, 0);
const nextWeek = new Date(today);
nextWeek.setDate(nextWeek.getDate() + 7);
// Tasks due in next 7 days
const upcomingTasks = await db.select({
id: tasks.id,
title: tasks.title,
dueDate: tasks.dueDate,
priority: tasks.priority,
status: tasks.status,
})
.from(tasks)
.where(and(
eq(tasks.domainId, domainId),
isNull(tasks.deletedAt),
gte(tasks.dueDate, today),
lte(tasks.dueDate, nextWeek),
))
.orderBy(asc(tasks.dueDate))
.limit(10);
// Projects with target dates in next 7 days
const upcomingProjects = await db.select({
id: projects.id,
name: projects.name,
targetDate: projects.targetDate,
status: projects.status,
color: projects.color,
})
.from(projects)
.where(and(
eq(projects.domainId, domainId),
isNull(projects.deletedAt),
gte(projects.targetDate, today),
lte(projects.targetDate, nextWeek),
))
.orderBy(asc(projects.targetDate))
.limit(5);
return NextResponse.json({
tasks: upcomingTasks,
projects: upcomingProjects,
});
});
@@ -0,0 +1,66 @@
// 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, requireWorkspaceAccess } from '@/lib/auth';
import { db, tasks, habits, habitCompletions } from '@project-e/db';
import { and, eq, gte, isNull, lte, sql } from 'drizzle-orm';
type RouteContext = { params: Promise<{ domainId: string }> };
// GET /api/domains/[domainId]/dashboard/widgets/weekly-stats
export const GET = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId } = await context!.params;
await requireWorkspaceAccess(domainId);
const now = new Date();
const weekStart = new Date(now);
weekStart.setDate(weekStart.getDate() - weekStart.getDay());
weekStart.setHours(0, 0, 0, 0);
const weekEnd = new Date(weekStart);
weekEnd.setDate(weekEnd.getDate() + 7);
// Task completions this week
const [taskCompletions] = await db.select({ count: sql<number>`count(*)` })
.from(tasks)
.where(and(
eq(tasks.domainId, domainId),
eq(tasks.status, 'done'),
gte(tasks.completedAt, weekStart),
lte(tasks.completedAt, weekEnd),
isNull(tasks.deletedAt),
));
// Habit completions this week
const [habitCompletionsCount] = await db.select({ count: sql<number>`count(*)` })
.from(habitCompletions)
.innerJoin(habits, eq(habitCompletions.habitId, habits.id))
.where(and(
eq(habits.domainId, domainId),
gte(habitCompletions.date, weekStart),
lte(habitCompletions.date, weekEnd),
isNull(habits.deletedAt),
));
// Streak counts
const activeHabits = await db.select({ id: habits.id, streakCount: habits.streakCount, bestStreak: habits.bestStreak })
.from(habits)
.where(and(
eq(habits.domainId, domainId),
eq(habits.active, true),
isNull(habits.deletedAt),
));
const totalStreak = activeHabits.reduce((sum, h) => sum + (h.streakCount || 0), 0);
const bestStreak = Math.max(...activeHabits.map(h => h.bestStreak || 0), 0);
return NextResponse.json({
taskCompletions: Number(taskCompletions?.count || 0),
habitCompletions: Number(habitCompletionsCount?.count || 0),
totalStreak,
bestStreak,
activeHabits: activeHabits.length,
});
});
@@ -0,0 +1,24 @@
// 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, requireWorkspaceAccess } from '@/lib/auth';
import { getGraphData } from '@/lib/graph-service';
type RouteContext = { params: Promise<{ domainId: string }> };
// GET /api/domains/[domainId]/graph — Get graph data for one domain
export const GET = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId } = await context!.params;
await requireWorkspaceAccess(domainId);
const graphData = await getGraphData(domainId);
return NextResponse.json(graphData, {
headers: {
'Cache-Control': 'private, max-age=30, stale-while-revalidate=120',
},
});
});
@@ -0,0 +1,138 @@
// 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, requireWorkspaceAccess, createErrorResponse, ApiError } from '@/lib/auth';
import { recordActivity } from '@/lib/activity';
import { db, habits, habitCompletions, sql } from '@project-e/db';
import { and, eq, isNull, gte, desc, count } from 'drizzle-orm';
import { z } from 'zod';
const completeHabitSchema = z.object({
value: z.number().int().positive().optional().default(1),
mood: z.number().int().min(1).max(5).optional().nullable(),
notes: z.string().optional().nullable(),
});
type RouteContext = { params: Promise<{ domainId: string; id: string }> };
/**
* Calculate the current streak for a habit.
* Streak = consecutive days with at least one completion, going backwards from today.
* Skip days (e.g. weekends) are excluded from the streak count.
*/
async function calculateStreak(habitId: string, skipDays: number[]): Promise<number> {
// Get all completion dates for this habit, ordered desc
const completions = await db.select({ date: habitCompletions.date })
.from(habitCompletions)
.where(eq(habitCompletions.habitId, habitId))
.orderBy(desc(habitCompletions.date));
if (completions.length === 0) return 0;
const completionDates = new Set(
completions.map(c => c.date.toISOString().split('T')[0])
);
let streak = 0;
const today = new Date();
today.setHours(0, 0, 0, 0);
const checkDate = new Date(today);
// Check up to 365 days back
for (let i = 0; i < 365; i++) {
const dateStr = checkDate.toISOString().split('T')[0];
const dayOfWeek = checkDate.getDay(); // 0=Sun, 6=Sat
if (skipDays.includes(dayOfWeek)) {
// Skip day — move on without breaking streak
checkDate.setDate(checkDate.getDate() - 1);
continue;
}
if (completionDates.has(dateStr)) {
streak++;
checkDate.setDate(checkDate.getDate() - 1);
} else {
break;
}
}
return streak;
}
// POST /api/domains/[domainId]/habits/[id]/complete — Complete a habit
export const POST = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, id } = await context!.params;
await requireWorkspaceAccess(domainId);
try {
const body = await request.json();
const data = completeHabitSchema.parse(body);
// Verify habit exists
const [habit] = await db.select()
.from(habits)
.where(and(eq(habits.id, id), eq(habits.domainId, domainId), isNull(habits.deletedAt)))
.limit(1);
if (!habit) {
return createErrorResponse('NOT_FOUND', 'Habit not found', 404);
}
// Create completion
const [completion] = await db.insert(habitCompletions).values({
habitId: id,
date: new Date(),
value: data.value,
mood: data.mood ?? null,
notes: data.notes ?? null,
}).returning();
// Recalculate streak
const skipDays = habit.skipDays || [];
const newStreak = await calculateStreak(id, skipDays);
// Update habit with new streak
const updateData: Record<string, unknown> = {
streakCount: newStreak,
updatedAt: new Date(),
};
// Update best streak if current is higher
if (newStreak > (habit.bestStreak || 0)) {
updateData.bestStreak = newStreak;
}
await db.update(habits)
.set(updateData)
.where(eq(habits.id, id));
// Record activity
await recordActivity({
actor: user.name,
action: 'completed',
entityType: 'habit',
entityId: id,
changes: { value: data.value, mood: data.mood, streak: newStreak },
workspaceId: domainId,
});
return NextResponse.json({
completion,
streakCount: newStreak,
bestStreak: Math.max(newStreak, habit.bestStreak || 0),
}, { status: 201 });
} catch (error) {
if (error instanceof z.ZodError) {
return createErrorResponse('VALIDATION_ERROR', 'Invalid input', 400, error.issues);
}
if (error instanceof ApiError) {
return createErrorResponse(error.code, error.message, error.status);
}
console.error('[habit complete POST] error:', error);
return createErrorResponse('INTERNAL_ERROR', 'Failed to complete habit', 500);
}
});
@@ -0,0 +1,60 @@
// 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, requireWorkspaceAccess, createErrorResponse } from '@/lib/auth';
import { db, habits, habitCompletions } from '@project-e/db';
import { and, asc, desc, eq, gte, isNull, lte } from 'drizzle-orm';
type RouteContext = { params: Promise<{ domainId: string; id: string }> };
// GET /api/domains/[domainId]/habits/[id]/completions — List completions with date range
export const GET = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, id } = await context!.params;
await requireWorkspaceAccess(domainId);
// Verify habit exists
const [habit] = await db.select({ id: habits.id })
.from(habits)
.where(and(eq(habits.id, id), eq(habits.domainId, domainId), isNull(habits.deletedAt)))
.limit(1);
if (!habit) {
return createErrorResponse('NOT_FOUND', 'Habit not found', 404);
}
const { searchParams } = new URL(request.url);
const from = searchParams.get('from');
const to = searchParams.get('to');
const limit = Math.min(parseInt(searchParams.get('limit') || '365'), 1000);
const offset = parseInt(searchParams.get('offset') || '0');
const order = searchParams.get('order') || 'desc';
const conditions: any[] = [eq(habitCompletions.habitId, id)];
if (from) conditions.push(gte(habitCompletions.date, new Date(from)));
if (to) conditions.push(lte(habitCompletions.date, new Date(to)));
const orderFn = order === 'asc' ? asc : desc;
const [items, countResult] = await Promise.all([
db.select()
.from(habitCompletions)
.where(and(...conditions))
.orderBy(orderFn(habitCompletions.date))
.limit(limit)
.offset(offset),
db.select({ count: db.$count(habitCompletions) })
.from(habitCompletions)
.where(and(...conditions)),
]);
return NextResponse.json({
items,
totalItems: Number(countResult[0]?.count || 0),
limit,
offset,
});
});
@@ -0,0 +1,160 @@
// 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, requireWorkspaceAccess, createErrorResponse, ApiError } from '@/lib/auth';
import { recordActivity } from '@/lib/activity';
import { db, habits, habitCompletions, habitTags, tags as tagsTable } from '@project-e/db';
import { and, asc, desc, eq, gte, inArray, isNull, lte, sql } from 'drizzle-orm';
import { z } from 'zod';
const habitFrequencyEnum = z.enum(['daily', 'weekly', 'custom']);
const habitDifficultyEnum = z.enum(['easy', 'medium', 'hard']);
const updateHabitSchema = z.object({
name: z.string().min(1).optional(),
description: z.string().optional().nullable(),
frequency: habitFrequencyEnum.optional(),
difficulty: habitDifficultyEnum.optional(),
goalPerPeriod: z.number().int().positive().optional(),
unit: z.string().optional().nullable(),
reminderTime: z.string().optional().nullable(),
skipDays: z.array(z.number().int().min(0).max(6)).optional(),
moodTracking: z.boolean().optional(),
active: z.boolean().optional(),
});
type RouteContext = { params: Promise<{ domainId: string; id: string }> };
// GET /api/domains/[domainId]/habits/[id] — Get a single habit with streak + recent completions
export const GET = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, id } = await context!.params;
await requireWorkspaceAccess(domainId);
const [habit] = await db.select()
.from(habits)
.where(and(eq(habits.id, id), eq(habits.domainId, domainId), isNull(habits.deletedAt)))
.limit(1);
if (!habit) {
return createErrorResponse('NOT_FOUND', 'Habit not found', 404);
}
// Fetch recent completions (last 30 days)
const thirtyDaysAgo = new Date();
thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30);
const recentCompletions = await db.select()
.from(habitCompletions)
.where(and(
eq(habitCompletions.habitId, id),
gte(habitCompletions.date, thirtyDaysAgo),
))
.orderBy(desc(habitCompletions.date));
// Fetch tags
const tagRows = await db.select({
id: tagsTable.id,
name: tagsTable.name,
color: tagsTable.color,
})
.from(habitTags)
.innerJoin(tagsTable, eq(habitTags.tagId, tagsTable.id))
.where(eq(habitTags.habitId, id));
return NextResponse.json({
...habit,
recentCompletions,
tags: tagRows,
});
});
// PATCH /api/domains/[domainId]/habits/[id] — Update a habit
export const PATCH = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, id } = await context!.params;
await requireWorkspaceAccess(domainId);
try {
const body = await request.json();
const data = updateHabitSchema.parse(body);
const [existing] = await db.select()
.from(habits)
.where(and(eq(habits.id, id), eq(habits.domainId, domainId), isNull(habits.deletedAt)))
.limit(1);
if (!existing) {
return createErrorResponse('NOT_FOUND', 'Habit not found', 404);
}
const updateValues: Record<string, unknown> = {};
if (data.name !== undefined) updateValues.name = data.name;
if (data.description !== undefined) updateValues.description = data.description;
if (data.frequency !== undefined) updateValues.frequency = data.frequency;
if (data.difficulty !== undefined) updateValues.difficulty = data.difficulty;
if (data.goalPerPeriod !== undefined) updateValues.goalPerPeriod = data.goalPerPeriod;
if (data.unit !== undefined) updateValues.unit = data.unit;
if (data.reminderTime !== undefined) updateValues.reminderTime = data.reminderTime;
if (data.skipDays !== undefined) updateValues.skipDays = data.skipDays;
if (data.moodTracking !== undefined) updateValues.moodTracking = data.moodTracking;
if (data.active !== undefined) updateValues.active = data.active;
updateValues.updatedAt = new Date();
const [updated] = await db.update(habits)
.set(updateValues)
.where(eq(habits.id, id))
.returning();
await recordActivity({
actor: user.name,
action: 'updated',
entityType: 'habit',
entityId: id,
changes: { ...data, previousName: existing.name },
workspaceId: domainId,
});
return NextResponse.json(updated);
} catch (error) {
if (error instanceof z.ZodError) {
return createErrorResponse('VALIDATION_ERROR', 'Invalid input', 400, error.issues);
}
if (error instanceof ApiError) {
return createErrorResponse(error.code, error.message, error.status);
}
console.error('[habits PATCH] error:', error);
return createErrorResponse('INTERNAL_ERROR', 'Failed to update habit', 500);
}
});
// DELETE /api/domains/[domainId]/habits/[id] — Soft delete a habit
export const DELETE = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, id } = await context!.params;
await requireWorkspaceAccess(domainId);
const [existing] = await db.select()
.from(habits)
.where(and(eq(habits.id, id), eq(habits.domainId, domainId), isNull(habits.deletedAt)))
.limit(1);
if (!existing) {
return createErrorResponse('NOT_FOUND', 'Habit not found', 404);
}
await db.update(habits)
.set({ deletedAt: new Date(), updatedAt: new Date() })
.where(eq(habits.id, id));
await recordActivity({
actor: user.name,
action: 'deleted',
entityType: 'habit',
entityId: id,
changes: { name: existing.name },
workspaceId: domainId,
});
return new NextResponse(null, { status: 204 });
});
@@ -0,0 +1,123 @@
// 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, requireWorkspaceAccess, createErrorResponse, ApiError } from '@/lib/auth';
import { recordActivity } from '@/lib/activity';
import { db, habits, habitTags, tags as tagsTable } from '@project-e/db';
import { and, eq, isNull } from 'drizzle-orm';
import { z } from 'zod';
const tagActionSchema = z.object({
tagId: z.string().uuid(),
});
type RouteContext = { params: Promise<{ domainId: string; id: string }> };
// POST /api/domains/[domainId]/habits/[id]/tags — Add a tag to a habit
export const POST = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, id } = await context!.params;
await requireWorkspaceAccess(domainId);
try {
const body = await request.json();
const data = tagActionSchema.parse(body);
// Verify habit exists
const [habit] = await db.select()
.from(habits)
.where(and(eq(habits.id, id), eq(habits.domainId, domainId), isNull(habits.deletedAt)))
.limit(1);
if (!habit) {
return createErrorResponse('NOT_FOUND', 'Habit not found', 404);
}
// Verify tag exists
const [tag] = await db.select()
.from(tagsTable)
.where(eq(tagsTable.id, data.tagId))
.limit(1);
if (!tag) {
return createErrorResponse('NOT_FOUND', 'Tag not found', 404);
}
// Check if already tagged
const [existing] = await db.select()
.from(habitTags)
.where(and(eq(habitTags.habitId, id), eq(habitTags.tagId, data.tagId)))
.limit(1);
if (existing) {
return createErrorResponse('CONFLICT', 'Tag already added to this habit', 409);
}
await db.insert(habitTags).values({ habitId: id, tagId: data.tagId });
await recordActivity({
actor: user.name,
action: 'tag_added',
entityType: 'habit',
entityId: id,
changes: { tagId: data.tagId, tagName: tag.name },
workspaceId: domainId,
});
return NextResponse.json({ success: true }, { status: 201 });
} catch (error) {
if (error instanceof z.ZodError) {
return createErrorResponse('VALIDATION_ERROR', 'Invalid input', 400, error.issues);
}
if (error instanceof ApiError) {
return createErrorResponse(error.code, error.message, error.status);
}
console.error('[habit tags POST] error:', error);
return createErrorResponse('INTERNAL_ERROR', 'Failed to add tag', 500);
}
});
// DELETE /api/domains/[domainId]/habits/[id]/tags — Remove a tag from a habit
export const DELETE = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, id } = await context!.params;
await requireWorkspaceAccess(domainId);
try {
const body = await request.json();
const data = tagActionSchema.parse(body);
const [existing] = await db.select()
.from(habitTags)
.where(and(eq(habitTags.habitId, id), eq(habitTags.tagId, data.tagId)))
.limit(1);
if (!existing) {
return createErrorResponse('NOT_FOUND', 'Tag not found on this habit', 404);
}
await db.delete(habitTags)
.where(and(eq(habitTags.habitId, id), eq(habitTags.tagId, data.tagId)));
await recordActivity({
actor: user.name,
action: 'tag_removed',
entityType: 'habit',
entityId: id,
changes: { tagId: data.tagId },
workspaceId: domainId,
});
return NextResponse.json({ success: true });
} catch (error) {
if (error instanceof z.ZodError) {
return createErrorResponse('VALIDATION_ERROR', 'Invalid input', 400, error.issues);
}
if (error instanceof ApiError) {
return createErrorResponse(error.code, error.message, error.status);
}
console.error('[habit tags DELETE] error:', error);
return createErrorResponse('INTERNAL_ERROR', 'Failed to remove tag', 500);
}
});
@@ -0,0 +1,167 @@
// 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, requireWorkspaceAccess, createErrorResponse, ApiError } from '@/lib/auth';
import { recordActivity } from '@/lib/activity';
import { db, habits, habitTags, tags as tagsTable } from '@project-e/db';
import { and, asc, desc, eq, ilike, inArray, isNull, or, sql } from 'drizzle-orm';
import { z } from 'zod';
const habitFrequencyEnum = z.enum(['daily', 'weekly', 'custom']);
const habitDifficultyEnum = z.enum(['easy', 'medium', 'hard']);
const createHabitSchema = z.object({
name: z.string().min(1, 'Name is required'),
description: z.string().optional().nullable(),
frequency: habitFrequencyEnum.optional().default('daily'),
difficulty: habitDifficultyEnum.optional().default('medium'),
goalPerPeriod: z.number().int().positive().optional().default(1),
unit: z.string().optional().nullable(),
reminderTime: z.string().optional().nullable(),
skipDays: z.array(z.number().int().min(0).max(6)).optional().default([]),
moodTracking: z.boolean().optional().default(false),
active: z.boolean().optional().default(true),
tagIds: z.array(z.string().uuid()).optional(),
});
type RouteContext = { params: Promise<{ domainId: string }> };
// GET /api/domains/[domainId]/habits — List habits with filtering
export const GET = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId } = await context!.params;
await requireWorkspaceAccess(domainId);
const { searchParams } = new URL(request.url);
const active = searchParams.get('active');
const frequency = searchParams.get('frequency');
const difficulty = searchParams.get('difficulty');
const search = searchParams.get('search');
const limit = Math.min(parseInt(searchParams.get('limit') || '50'), 200);
const offset = parseInt(searchParams.get('offset') || '0');
const sort = searchParams.get('sort') || 'name';
const order = searchParams.get('order') || 'asc';
const conditions: any[] = [
eq(habits.domainId, domainId),
isNull(habits.deletedAt),
];
if (active === 'true') conditions.push(eq(habits.active, true));
else if (active === 'false') conditions.push(eq(habits.active, false));
if (frequency) conditions.push(eq(habits.frequency, frequency as any));
if (difficulty) conditions.push(eq(habits.difficulty, difficulty as any));
if (search) conditions.push(ilike(habits.name, `%${search}%`));
const orderFn = order === 'desc' ? desc : asc;
let orderColumn;
switch (sort) {
case 'frequency': orderColumn = orderFn(habits.frequency); break;
case 'difficulty': orderColumn = orderFn(habits.difficulty); break;
case 'streak_count': orderColumn = orderFn(habits.streakCount); break;
case 'created_at': orderColumn = orderFn(habits.createdAt); break;
case 'updated_at': orderColumn = orderFn(habits.updatedAt); break;
default: orderColumn = orderFn(habits.name); break;
}
const [items, countResult] = await Promise.all([
db.select()
.from(habits)
.where(and(...conditions))
.orderBy(orderColumn)
.limit(limit)
.offset(offset),
db.select({ count: sql<number>`count(*)` })
.from(habits)
.where(and(...conditions)),
]);
const totalItems = Number(countResult[0]?.count || 0);
// Fetch tags for all habits
let habitTagMap = new Map<string, { id: string; name: string; color: string | null }[]>();
if (items.length > 0) {
const habitIds = items.map(h => h.id);
const tagRows = await db.select({
habitId: habitTags.habitId,
id: tagsTable.id,
name: tagsTable.name,
color: tagsTable.color,
})
.from(habitTags)
.innerJoin(tagsTable, eq(habitTags.tagId, tagsTable.id))
.where(inArray(habitTags.habitId, habitIds));
for (const row of tagRows) {
if (!habitTagMap.has(row.habitId)) habitTagMap.set(row.habitId, []);
habitTagMap.get(row.habitId)!.push({ id: row.id, name: row.name, color: row.color });
}
}
const itemsWithTags = items.map(h => ({
...h,
tags: habitTagMap.get(h.id) || [],
}));
return NextResponse.json({
items: itemsWithTags,
totalItems,
limit,
offset,
});
});
// POST /api/domains/[domainId]/habits — Create a habit
export const POST = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId } = await context!.params;
await requireWorkspaceAccess(domainId);
try {
const body = await request.json();
const data = createHabitSchema.parse(body);
const [habit] = await db.insert(habits).values({
name: data.name,
description: data.description ?? null,
domainId,
frequency: data.frequency,
difficulty: data.difficulty,
goalPerPeriod: data.goalPerPeriod,
unit: data.unit ?? null,
reminderTime: data.reminderTime ?? null,
skipDays: data.skipDays,
moodTracking: data.moodTracking,
active: data.active,
}).returning();
// Insert tags if provided
if (data.tagIds && data.tagIds.length > 0) {
await db.insert(habitTags).values(
data.tagIds.map(tagId => ({ habitId: habit.id, tagId }))
);
}
// Record activity
await recordActivity({
actor: user.name,
action: 'created',
entityType: 'habit',
entityId: habit.id,
changes: { name: habit.name, frequency: habit.frequency, difficulty: habit.difficulty },
workspaceId: domainId,
});
return NextResponse.json(habit, { status: 201 });
} catch (error) {
if (error instanceof z.ZodError) {
return createErrorResponse('VALIDATION_ERROR', 'Invalid input', 400, error.issues);
}
if (error instanceof ApiError) {
return createErrorResponse(error.code, error.message, error.status);
}
console.error('[habits POST] error:', error);
return createErrorResponse('INTERNAL_ERROR', 'Failed to create habit', 500);
}
});
@@ -0,0 +1,23 @@
// 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, requireWorkspaceAccess, createErrorResponse } from '@/lib/auth';
import { getBacklinks } from '@/lib/note-link-service';
type RouteContext = { params: Promise<{ domainId: string; id: string }> };
// GET /api/domains/[domainId]/notes/[id]/backlinks — List notes that link to this one
export const GET = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, id } = await context!.params;
await requireWorkspaceAccess(domainId);
const backlinks = await getBacklinks(id);
return NextResponse.json({
items: backlinks,
totalItems: backlinks.length,
});
});
@@ -0,0 +1,147 @@
// 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, requireWorkspaceAccess, createErrorResponse, ApiError } from '@/lib/auth';
import { recordActivity } from '@/lib/activity';
import { db, notes, noteTags, tags as tagsTable } from '@project-e/db';
import { and, eq, inArray, isNull } from 'drizzle-orm';
import { z } from 'zod';
import { syncNoteLinks, getBacklinks, getOutgoingLinks } from '@/lib/note-link-service';
const updateNoteSchema = z.object({
title: z.string().min(1).optional(),
content: z.string().optional().nullable(),
isPinned: z.boolean().optional(),
isArchived: z.boolean().optional(),
});
type RouteContext = { params: Promise<{ domainId: string; id: string }> };
// GET /api/domains/[domainId]/notes/[id] — Get a single note with computed links
export const GET = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, id } = await context!.params;
await requireWorkspaceAccess(domainId);
const [note] = await db.select()
.from(notes)
.where(and(eq(notes.id, id), eq(notes.domainId, domainId), isNull(notes.deletedAt)))
.limit(1);
if (!note) {
return createErrorResponse('NOT_FOUND', 'Note not found', 404);
}
// Fetch tags
const tagRows = await db.select({
id: tagsTable.id,
name: tagsTable.name,
color: tagsTable.color,
})
.from(noteTags)
.innerJoin(tagsTable, eq(noteTags.tagId, tagsTable.id))
.where(eq(noteTags.noteId, id));
// Fetch backlinks and outgoing links
const [backlinks, outgoingLinks] = await Promise.all([
getBacklinks(id),
getOutgoingLinks(id),
]);
return NextResponse.json({
...note,
tags: tagRows,
backlinks,
outgoingLinks,
});
});
// PATCH /api/domains/[domainId]/notes/[id] — Update a note, re-parse wikilinks
export const PATCH = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, id } = await context!.params;
await requireWorkspaceAccess(domainId);
try {
const body = await request.json();
const data = updateNoteSchema.parse(body);
const [existing] = await db.select()
.from(notes)
.where(and(eq(notes.id, id), eq(notes.domainId, domainId), isNull(notes.deletedAt)))
.limit(1);
if (!existing) {
return createErrorResponse('NOT_FOUND', 'Note not found', 404);
}
const updateValues: Record<string, unknown> = {};
if (data.title !== undefined) updateValues.title = data.title;
if (data.content !== undefined) updateValues.content = data.content;
if (data.isPinned !== undefined) updateValues.isPinned = data.isPinned;
if (data.isArchived !== undefined) updateValues.isArchived = data.isArchived;
updateValues.updatedAt = new Date();
const [updated] = await db.update(notes)
.set(updateValues)
.where(eq(notes.id, id))
.returning();
// Re-sync wikilinks if content changed
const content = data.content ?? existing.content;
if (content) {
await syncNoteLinks(id, content);
}
await recordActivity({
actor: user.name,
action: 'updated',
entityType: 'note',
entityId: id,
changes: { ...data, previousTitle: existing.title },
workspaceId: domainId,
});
return NextResponse.json(updated);
} catch (error) {
if (error instanceof z.ZodError) {
return createErrorResponse('VALIDATION_ERROR', 'Invalid input', 400, error.issues);
}
if (error instanceof ApiError) {
return createErrorResponse(error.code, error.message, error.status);
}
console.error('[notes PATCH] error:', error);
return createErrorResponse('INTERNAL_ERROR', 'Failed to update note', 500);
}
});
// DELETE /api/domains/[domainId]/notes/[id] — Soft delete a note
export const DELETE = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, id } = await context!.params;
await requireWorkspaceAccess(domainId);
const [existing] = await db.select()
.from(notes)
.where(and(eq(notes.id, id), eq(notes.domainId, domainId), isNull(notes.deletedAt)))
.limit(1);
if (!existing) {
return createErrorResponse('NOT_FOUND', 'Note not found', 404);
}
await db.update(notes)
.set({ deletedAt: new Date(), updatedAt: new Date() })
.where(eq(notes.id, id));
await recordActivity({
actor: user.name,
action: 'deleted',
entityType: 'note',
entityId: id,
changes: { title: existing.title },
workspaceId: domainId,
});
return new NextResponse(null, { status: 204 });
});
@@ -0,0 +1,123 @@
// 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, requireWorkspaceAccess, createErrorResponse, ApiError } from '@/lib/auth';
import { recordActivity } from '@/lib/activity';
import { db, notes, noteTags, tags as tagsTable } from '@project-e/db';
import { and, eq, isNull } from 'drizzle-orm';
import { z } from 'zod';
const tagActionSchema = z.object({
tagId: z.string().uuid(),
});
type RouteContext = { params: Promise<{ domainId: string; id: string }> };
// POST /api/domains/[domainId]/notes/[id]/tags — Add a tag to a note
export const POST = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, id } = await context!.params;
await requireWorkspaceAccess(domainId);
try {
const body = await request.json();
const data = tagActionSchema.parse(body);
// Verify note exists
const [note] = await db.select()
.from(notes)
.where(and(eq(notes.id, id), eq(notes.domainId, domainId), isNull(notes.deletedAt)))
.limit(1);
if (!note) {
return createErrorResponse('NOT_FOUND', 'Note not found', 404);
}
// Verify tag exists
const [tag] = await db.select()
.from(tagsTable)
.where(eq(tagsTable.id, data.tagId))
.limit(1);
if (!tag) {
return createErrorResponse('NOT_FOUND', 'Tag not found', 404);
}
// Check if already tagged
const [existing] = await db.select()
.from(noteTags)
.where(and(eq(noteTags.noteId, id), eq(noteTags.tagId, data.tagId)))
.limit(1);
if (existing) {
return createErrorResponse('CONFLICT', 'Tag already added to this note', 409);
}
await db.insert(noteTags).values({ noteId: id, tagId: data.tagId });
await recordActivity({
actor: user.name,
action: 'tag_added',
entityType: 'note',
entityId: id,
changes: { tagId: data.tagId, tagName: tag.name },
workspaceId: domainId,
});
return NextResponse.json({ success: true }, { status: 201 });
} catch (error) {
if (error instanceof z.ZodError) {
return createErrorResponse('VALIDATION_ERROR', 'Invalid input', 400, error.issues);
}
if (error instanceof ApiError) {
return createErrorResponse(error.code, error.message, error.status);
}
console.error('[note tags POST] error:', error);
return createErrorResponse('INTERNAL_ERROR', 'Failed to add tag', 500);
}
});
// DELETE /api/domains/[domainId]/notes/[id]/tags — Remove a tag from a note
export const DELETE = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, id } = await context!.params;
await requireWorkspaceAccess(domainId);
try {
const body = await request.json();
const data = tagActionSchema.parse(body);
const [existing] = await db.select()
.from(noteTags)
.where(and(eq(noteTags.noteId, id), eq(noteTags.tagId, data.tagId)))
.limit(1);
if (!existing) {
return createErrorResponse('NOT_FOUND', 'Tag not found on this note', 404);
}
await db.delete(noteTags)
.where(and(eq(noteTags.noteId, id), eq(noteTags.tagId, data.tagId)));
await recordActivity({
actor: user.name,
action: 'tag_removed',
entityType: 'note',
entityId: id,
changes: { tagId: data.tagId },
workspaceId: domainId,
});
return NextResponse.json({ success: true });
} catch (error) {
if (error instanceof z.ZodError) {
return createErrorResponse('VALIDATION_ERROR', 'Invalid input', 400, error.issues);
}
if (error instanceof ApiError) {
return createErrorResponse(error.code, error.message, error.status);
}
console.error('[note tags DELETE] error:', error);
return createErrorResponse('INTERNAL_ERROR', 'Failed to remove tag', 500);
}
});
@@ -0,0 +1,154 @@
// 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, requireWorkspaceAccess, createErrorResponse, ApiError } from '@/lib/auth';
import { recordActivity } from '@/lib/activity';
import { db, notes, noteTags, tags as tagsTable } from '@project-e/db';
import { and, asc, desc, eq, ilike, inArray, isNull, or, sql } from 'drizzle-orm';
import { z } from 'zod';
import { syncNoteLinks } from '@/lib/note-link-service';
const createNoteSchema = z.object({
title: z.string().min(1, 'Title is required'),
content: z.string().optional().nullable(),
isPinned: z.boolean().optional().default(false),
isArchived: z.boolean().optional().default(false),
tagIds: z.array(z.string().uuid()).optional(),
});
type RouteContext = { params: Promise<{ domainId: string }> };
// GET /api/domains/[domainId]/notes — List notes with filtering
export const GET = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId } = await context!.params;
await requireWorkspaceAccess(domainId);
const { searchParams } = new URL(request.url);
const pinned = searchParams.get('pinned');
const archived = searchParams.get('archived');
const search = searchParams.get('search');
const limit = Math.min(parseInt(searchParams.get('limit') || '50'), 200);
const offset = parseInt(searchParams.get('offset') || '0');
const sort = searchParams.get('sort') || 'updated_at';
const order = searchParams.get('order') || 'desc';
const conditions: any[] = [
eq(notes.domainId, domainId),
isNull(notes.deletedAt),
];
if (pinned === 'true') conditions.push(eq(notes.isPinned, true));
if (archived === 'true') conditions.push(eq(notes.isArchived, true));
else if (archived !== 'all') conditions.push(eq(notes.isArchived, false));
if (search) conditions.push(ilike(notes.title, `%${search}%`));
const orderFn = order === 'desc' ? desc : asc;
let orderColumn;
switch (sort) {
case 'title': orderColumn = orderFn(notes.title); break;
case 'created_at': orderColumn = orderFn(notes.createdAt); break;
case 'is_pinned': orderColumn = orderFn(notes.isPinned); break;
default: orderColumn = orderFn(notes.updatedAt); break;
}
const [items, countResult] = await Promise.all([
db.select()
.from(notes)
.where(and(...conditions))
.orderBy(orderColumn)
.limit(limit)
.offset(offset),
db.select({ count: sql<number>`count(*)` })
.from(notes)
.where(and(...conditions)),
]);
const totalItems = Number(countResult[0]?.count || 0);
// Fetch tags for all notes
let noteTagMap = new Map<string, { id: string; name: string; color: string | null }[]>();
if (items.length > 0) {
const noteIds = items.map(n => n.id);
const tagRows = await db.select({
noteId: noteTags.noteId,
id: tagsTable.id,
name: tagsTable.name,
color: tagsTable.color,
})
.from(noteTags)
.innerJoin(tagsTable, eq(noteTags.tagId, tagsTable.id))
.where(inArray(noteTags.noteId, noteIds));
for (const row of tagRows) {
if (!noteTagMap.has(row.noteId)) noteTagMap.set(row.noteId, []);
noteTagMap.get(row.noteId)!.push({ id: row.id, name: row.name, color: row.color });
}
}
const itemsWithTags = items.map(n => ({
...n,
tags: noteTagMap.get(n.id) || [],
}));
return NextResponse.json({
items: itemsWithTags,
totalItems,
limit,
offset,
});
});
// POST /api/domains/[domainId]/notes — Create a note
export const POST = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId } = await context!.params;
await requireWorkspaceAccess(domainId);
try {
const body = await request.json();
const data = createNoteSchema.parse(body);
const [note] = await db.insert(notes).values({
title: data.title,
content: data.content ?? null,
domainId,
isPinned: data.isPinned,
isArchived: data.isArchived,
}).returning();
// Insert tags if provided
if (data.tagIds && data.tagIds.length > 0) {
await db.insert(noteTags).values(
data.tagIds.map(tagId => ({ noteId: note.id, tagId }))
);
}
// Sync wikilinks from content
if (data.content) {
await syncNoteLinks(note.id, data.content);
}
// Record activity
await recordActivity({
actor: user.name,
action: 'created',
entityType: 'note',
entityId: note.id,
changes: { title: note.title },
workspaceId: domainId,
});
return NextResponse.json(note, { status: 201 });
} catch (error) {
if (error instanceof z.ZodError) {
return createErrorResponse('VALIDATION_ERROR', 'Invalid input', 400, error.issues);
}
if (error instanceof ApiError) {
return createErrorResponse(error.code, error.message, error.status);
}
console.error('[notes POST] error:', error);
return createErrorResponse('INTERNAL_ERROR', 'Failed to create note', 500);
}
});
@@ -0,0 +1,160 @@
// 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, requireWorkspaceAccess, createErrorResponse, ApiError } from '@/lib/auth';
import { recordActivity } from '@/lib/activity';
import { db, projects, tasks, sections, projectTags, tags as tagsTable } from '@project-e/db';
import { and, asc, eq, inArray, isNull, sql } from 'drizzle-orm';
import { z } from 'zod';
const projectStatusEnum = z.enum(['active', 'paused', 'completed', 'archived']);
const updateProjectSchema = z.object({
name: z.string().min(1).optional(),
description: z.string().optional().nullable(),
status: projectStatusEnum.optional(),
color: z.string().optional().nullable(),
icon: z.string().optional().nullable(),
targetDate: z.string().datetime().optional().nullable(),
});
type RouteContext = { params: Promise<{ domainId: string; projectId: string }> };
// GET /api/domains/[domainId]/projects/[id] — Get a single project with sections, task counts, progress
export const GET = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, projectId: id } = await context!.params;
await requireWorkspaceAccess(domainId);
const [project] = await db.select()
.from(projects)
.where(and(eq(projects.id, id), eq(projects.domainId, domainId), isNull(projects.deletedAt)))
.limit(1);
if (!project) {
return createErrorResponse('NOT_FOUND', 'Project not found', 404);
}
// Fetch sections
const projectSections = await db.select()
.from(sections)
.where(eq(sections.projectId, id))
.orderBy(asc(sections.sortOrder));
// Fetch tasks grouped by section
const projectTasks = await db.select()
.from(tasks)
.where(and(eq(tasks.projectId, id), isNull(tasks.deletedAt)))
.orderBy(asc(tasks.order));
// Fetch tags
const tagRows = await db.select({
id: tagsTable.id,
name: tagsTable.name,
color: tagsTable.color,
})
.from(projectTags)
.innerJoin(tagsTable, eq(projectTags.tagId, tagsTable.id))
.where(eq(projectTags.projectId, id));
// Compute counts
const totalTasks = projectTasks.length;
const completedTasks = projectTasks.filter(t => t.status === 'done').length;
const progress = totalTasks > 0 ? Math.round((completedTasks / totalTasks) * 100) : 0;
return NextResponse.json({
...project,
sections: projectSections,
tasks: projectTasks,
tags: tagRows,
taskCount: totalTasks,
completedCount: completedTasks,
progress,
});
});
// PATCH /api/domains/[domainId]/projects/[id] — Update a project
export const PATCH = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, projectId: id } = await context!.params;
await requireWorkspaceAccess(domainId);
try {
const body = await request.json();
const data = updateProjectSchema.parse(body);
const [existing] = await db.select()
.from(projects)
.where(and(eq(projects.id, id), eq(projects.domainId, domainId), isNull(projects.deletedAt)))
.limit(1);
if (!existing) {
return createErrorResponse('NOT_FOUND', 'Project not found', 404);
}
const updateValues: Record<string, unknown> = {};
if (data.name !== undefined) updateValues.name = data.name;
if (data.description !== undefined) updateValues.description = data.description;
if (data.status !== undefined) updateValues.status = data.status;
if (data.color !== undefined) updateValues.color = data.color;
if (data.icon !== undefined) updateValues.icon = data.icon;
if (data.targetDate !== undefined) updateValues.targetDate = data.targetDate ? new Date(data.targetDate) : null;
updateValues.updatedAt = new Date();
const [updated] = await db.update(projects)
.set(updateValues)
.where(eq(projects.id, id))
.returning();
await recordActivity({
actor: user.name,
action: 'updated',
entityType: 'project',
entityId: id,
changes: { ...data, previousName: existing.name },
workspaceId: domainId,
});
return NextResponse.json(updated);
} catch (error) {
if (error instanceof z.ZodError) {
return createErrorResponse('VALIDATION_ERROR', 'Invalid input', 400, error.issues);
}
if (error instanceof ApiError) {
return createErrorResponse(error.code, error.message, error.status);
}
console.error('[projects PATCH] error:', error);
return createErrorResponse('INTERNAL_ERROR', 'Failed to update project', 500);
}
});
// DELETE /api/domains/[domainId]/projects/[id] — Soft delete a project
export const DELETE = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, projectId: id } = await context!.params;
await requireWorkspaceAccess(domainId);
const [existing] = await db.select()
.from(projects)
.where(and(eq(projects.id, id), eq(projects.domainId, domainId), isNull(projects.deletedAt)))
.limit(1);
if (!existing) {
return createErrorResponse('NOT_FOUND', 'Project not found', 404);
}
await db.update(projects)
.set({ deletedAt: new Date(), updatedAt: new Date() })
.where(eq(projects.id, id));
await recordActivity({
actor: user.name,
action: 'deleted',
entityType: 'project',
entityId: id,
changes: { name: existing.name },
workspaceId: domainId,
});
return new NextResponse(null, { status: 204 });
});
@@ -0,0 +1,123 @@
// 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, requireWorkspaceAccess, createErrorResponse, ApiError } from '@/lib/auth';
import { recordActivity } from '@/lib/activity';
import { db, projects, sections } from '@project-e/db';
import { and, eq, isNull } from 'drizzle-orm';
import { z } from 'zod';
const sectionKindEnum = z.enum(['section', 'milestone']);
const sectionStatusEnum = z.enum(['planned', 'in_progress', 'complete']);
const updateSectionSchema = z.object({
name: z.string().min(1).optional(),
kind: sectionKindEnum.optional(),
status: sectionStatusEnum.optional(),
targetDate: z.string().datetime().optional().nullable(),
sortOrder: z.number().int().optional(),
});
type RouteContext = { params: Promise<{ domainId: string; projectId: string; id: string }> };
// GET /api/domains/[domainId]/projects/[projectId]/sections/[id] — Get a single section
export const GET = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, projectId, id } = await context!.params;
await requireWorkspaceAccess(domainId);
const [section] = await db.select()
.from(sections)
.where(and(eq(sections.id, id), eq(sections.projectId, projectId)))
.limit(1);
if (!section) {
return createErrorResponse('NOT_FOUND', 'Section not found', 404);
}
return NextResponse.json(section);
});
// PATCH /api/domains/[domainId]/projects/[projectId]/sections/[id] — Update a section
export const PATCH = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, projectId, id } = await context!.params;
await requireWorkspaceAccess(domainId);
try {
const body = await request.json();
const data = updateSectionSchema.parse(body);
const [existing] = await db.select()
.from(sections)
.where(and(eq(sections.id, id), eq(sections.projectId, projectId)))
.limit(1);
if (!existing) {
return createErrorResponse('NOT_FOUND', 'Section not found', 404);
}
const updateValues: Record<string, unknown> = {};
if (data.name !== undefined) updateValues.name = data.name;
if (data.kind !== undefined) updateValues.kind = data.kind;
if (data.status !== undefined) updateValues.status = data.status;
if (data.targetDate !== undefined) updateValues.targetDate = data.targetDate ? new Date(data.targetDate) : null;
if (data.sortOrder !== undefined) updateValues.sortOrder = data.sortOrder;
updateValues.updatedAt = new Date();
const [updated] = await db.update(sections)
.set(updateValues)
.where(eq(sections.id, id))
.returning();
await recordActivity({
actor: user.name,
action: 'updated',
entityType: 'section',
entityId: id,
changes: { ...data, previousName: existing.name, projectId },
workspaceId: domainId,
});
return NextResponse.json(updated);
} catch (error) {
if (error instanceof z.ZodError) {
return createErrorResponse('VALIDATION_ERROR', 'Invalid input', 400, error.issues);
}
if (error instanceof ApiError) {
return createErrorResponse(error.code, error.message, error.status);
}
console.error('[sections PATCH] error:', error);
return createErrorResponse('INTERNAL_ERROR', 'Failed to update section', 500);
}
});
// DELETE /api/domains/[domainId]/projects/[projectId]/sections/[id] — Delete a section
export const DELETE = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, projectId, id } = await context!.params;
await requireWorkspaceAccess(domainId);
const [existing] = await db.select()
.from(sections)
.where(and(eq(sections.id, id), eq(sections.projectId, projectId)))
.limit(1);
if (!existing) {
return createErrorResponse('NOT_FOUND', 'Section not found', 404);
}
await db.delete(sections)
.where(eq(sections.id, id));
await recordActivity({
actor: user.name,
action: 'deleted',
entityType: 'section',
entityId: id,
changes: { name: existing.name, projectId },
workspaceId: domainId,
});
return new NextResponse(null, { status: 204 });
});
@@ -0,0 +1,106 @@
// 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, requireWorkspaceAccess, createErrorResponse, ApiError } from '@/lib/auth';
import { recordActivity } from '@/lib/activity';
import { db, projects, sections } from '@project-e/db';
import { and, asc, eq, isNull, sql } from 'drizzle-orm';
import { z } from 'zod';
const sectionKindEnum = z.enum(['section', 'milestone']);
const sectionStatusEnum = z.enum(['planned', 'in_progress', 'complete']);
const createSectionSchema = z.object({
name: z.string().min(1, 'Name is required'),
kind: sectionKindEnum.optional().default('section'),
status: sectionStatusEnum.optional().default('planned'),
targetDate: z.string().datetime().optional().nullable(),
sortOrder: z.number().int().optional(),
});
type RouteContext = { params: Promise<{ domainId: string; projectId: string }> };
// GET /api/domains/[domainId]/projects/[projectId]/sections — List sections for a project
export const GET = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, projectId } = await context!.params;
await requireWorkspaceAccess(domainId);
// Verify project exists and belongs to domain
const [project] = await db.select({ id: projects.id })
.from(projects)
.where(and(eq(projects.id, projectId), eq(projects.domainId, domainId), isNull(projects.deletedAt)))
.limit(1);
if (!project) {
return createErrorResponse('NOT_FOUND', 'Project not found', 404);
}
const items = await db.select()
.from(sections)
.where(eq(sections.projectId, projectId))
.orderBy(asc(sections.sortOrder));
return NextResponse.json({ items });
});
// POST /api/domains/[domainId]/projects/[projectId]/sections — Create a section
export const POST = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, projectId } = await context!.params;
await requireWorkspaceAccess(domainId);
try {
const body = await request.json();
const data = createSectionSchema.parse(body);
// Verify project exists
const [project] = await db.select({ id: projects.id, name: projects.name })
.from(projects)
.where(and(eq(projects.id, projectId), eq(projects.domainId, domainId), isNull(projects.deletedAt)))
.limit(1);
if (!project) {
return createErrorResponse('NOT_FOUND', 'Project not found', 404);
}
// Determine sort order if not provided
let sortOrder = data.sortOrder;
if (sortOrder === undefined) {
const [maxOrder] = await db.select({ max: sql<number>`COALESCE(MAX(sort_order), -1)` })
.from(sections)
.where(eq(sections.projectId, projectId));
sortOrder = Number(maxOrder?.max || -1) + 1;
}
const [section] = await db.insert(sections).values({
name: data.name,
projectId,
kind: data.kind,
status: data.status,
targetDate: data.targetDate ? new Date(data.targetDate) : null,
sortOrder,
}).returning();
await recordActivity({
actor: user.name,
action: 'created',
entityType: 'section',
entityId: section.id,
changes: { name: section.name, projectId, projectName: project.name, kind: section.kind },
workspaceId: domainId,
});
return NextResponse.json(section, { status: 201 });
} catch (error) {
if (error instanceof z.ZodError) {
return createErrorResponse('VALIDATION_ERROR', 'Invalid input', 400, error.issues);
}
if (error instanceof ApiError) {
return createErrorResponse(error.code, error.message, error.status);
}
console.error('[sections POST] error:', error);
return createErrorResponse('INTERNAL_ERROR', 'Failed to create section', 500);
}
});
@@ -0,0 +1,181 @@
// 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, requireWorkspaceAccess, createErrorResponse, ApiError } from '@/lib/auth';
import { recordActivity } from '@/lib/activity';
import { db, projects, tasks, sections, projectTags, tags as tagsTable } from '@project-e/db';
import { and, asc, desc, eq, ilike, inArray, isNull, sql } from 'drizzle-orm';
import { z } from 'zod';
const projectStatusEnum = z.enum(['active', 'paused', 'completed', 'archived']);
const createProjectSchema = z.object({
name: z.string().min(1, 'Name is required'),
description: z.string().optional().nullable(),
status: projectStatusEnum.optional().default('active'),
color: z.string().optional().nullable(),
icon: z.string().optional().nullable(),
targetDate: z.string().datetime().optional().nullable(),
tagIds: z.array(z.string().uuid()).optional(),
});
type RouteContext = { params: Promise<{ domainId: string }> };
// GET /api/domains/[domainId]/projects — List projects with filtering
export const GET = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId } = await context!.params;
await requireWorkspaceAccess(domainId);
const { searchParams } = new URL(request.url);
const status = searchParams.get('status');
const search = searchParams.get('search');
const limit = Math.min(parseInt(searchParams.get('limit') || '50'), 200);
const offset = parseInt(searchParams.get('offset') || '0');
const sort = searchParams.get('sort') || 'name';
const order = searchParams.get('order') || 'asc';
const conditions: any[] = [
eq(projects.domainId, domainId),
isNull(projects.deletedAt),
];
if (status) {
const statuses = status.split(',');
conditions.push(inArray(projects.status, statuses as any));
}
if (search) conditions.push(ilike(projects.name, `%${search}%`));
const orderFn = order === 'desc' ? desc : asc;
let orderColumn;
switch (sort) {
case 'status': orderColumn = orderFn(projects.status); break;
case 'target_date': orderColumn = orderFn(projects.targetDate); break;
case 'created_at': orderColumn = orderFn(projects.createdAt); break;
case 'updated_at': orderColumn = orderFn(projects.updatedAt); break;
default: orderColumn = orderFn(projects.name); break;
}
const [items, countResult] = await Promise.all([
db.select()
.from(projects)
.where(and(...conditions))
.orderBy(orderColumn)
.limit(limit)
.offset(offset),
db.select({ count: sql<number>`count(*)` })
.from(projects)
.where(and(...conditions)),
]);
const totalItems = Number(countResult[0]?.count || 0);
// Fetch task counts and tags for all projects
let projectTagMap = new Map<string, { id: string; name: string; color: string | null }[]>();
let taskCountMap = new Map<string, { total: number; completed: number }>();
if (items.length > 0) {
const projectIds = items.map(p => p.id);
// Tags
const tagRows = await db.select({
projectId: projectTags.projectId,
id: tagsTable.id,
name: tagsTable.name,
color: tagsTable.color,
})
.from(projectTags)
.innerJoin(tagsTable, eq(projectTags.tagId, tagsTable.id))
.where(inArray(projectTags.projectId, projectIds));
for (const row of tagRows) {
if (!projectTagMap.has(row.projectId)) projectTagMap.set(row.projectId, []);
projectTagMap.get(row.projectId)!.push({ id: row.id, name: row.name, color: row.color });
}
// Task counts
for (const projectId of projectIds) {
const [totalResult] = await db.select({ count: sql<number>`count(*)` })
.from(tasks)
.where(and(eq(tasks.projectId, projectId), isNull(tasks.deletedAt)));
const [completedResult] = await db.select({ count: sql<number>`count(*)` })
.from(tasks)
.where(and(eq(tasks.projectId, projectId), eq(tasks.status, 'done'), isNull(tasks.deletedAt)));
taskCountMap.set(projectId, {
total: Number(totalResult?.count || 0),
completed: Number(completedResult?.count || 0),
});
}
}
const itemsWithMeta = items.map(p => {
const counts = taskCountMap.get(p.id) || { total: 0, completed: 0 };
return {
...p,
tags: projectTagMap.get(p.id) || [],
taskCount: counts.total,
completedCount: counts.completed,
progress: counts.total > 0 ? Math.round((counts.completed / counts.total) * 100) : 0,
};
});
return NextResponse.json({
items: itemsWithMeta,
totalItems,
limit,
offset,
});
});
// POST /api/domains/[domainId]/projects — Create a project
export const POST = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId } = await context!.params;
await requireWorkspaceAccess(domainId);
try {
const body = await request.json();
const data = createProjectSchema.parse(body);
const [project] = await db.insert(projects).values({
name: data.name,
description: data.description ?? null,
status: data.status,
domainId,
color: data.color ?? null,
icon: data.icon ?? null,
targetDate: data.targetDate ? new Date(data.targetDate) : null,
}).returning();
// Insert tags if provided
if (data.tagIds && data.tagIds.length > 0) {
await db.insert(projectTags).values(
data.tagIds.map(tagId => ({ projectId: project.id, tagId }))
);
}
// Record activity
await recordActivity({
actor: user.name,
action: 'created',
entityType: 'project',
entityId: project.id,
changes: { name: project.name, status: project.status },
workspaceId: domainId,
});
return NextResponse.json(project, { status: 201 });
} catch (error) {
if (error instanceof z.ZodError) {
return createErrorResponse('VALIDATION_ERROR', 'Invalid input', 400, error.issues);
}
if (error instanceof ApiError) {
return createErrorResponse(error.code, error.message, error.status);
}
console.error('[projects POST] error:', error);
return createErrorResponse('INTERNAL_ERROR', 'Failed to create project', 500);
}
});
@@ -0,0 +1,51 @@
// 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 { updateDomainSchema } from '@project-e/shared';
import { z } from 'zod';
type RouteContext = { params: Promise<{ domainId: string }> };
// GET /api/domains/[id] — Get a single domain
export const GET = withAuth<RouteContext>(async (request: NextRequest, _user, context) => {
const { domainId: id } = await context!.params;
const pb = createPocketBaseClient();
const domain = await pb.collection('domains').getOne(id);
return NextResponse.json(domain);
});
// PATCH /api/domains/[id] — Update a domain
export const PATCH = withAuth<RouteContext>(async (request: NextRequest, _user, context) => {
try {
const { domainId: id } = await context!.params;
const body = await request.json();
const data = updateDomainSchema.parse(body);
const pb = createPocketBaseClient();
const domain = await pb.collection('domains').update(id, data);
return NextResponse.json(domain);
} catch (error) {
if (error instanceof z.ZodError) {
return createErrorResponse('VALIDATION_ERROR', 'Invalid input', 400, error.issues);
}
throw error;
}
});
// DELETE /api/domains/[id] — Delete a domain
export const DELETE = withAuth<RouteContext>(async (request: NextRequest, _user, context) => {
const { domainId: id } = await context!.params;
const pb = createPocketBaseClient();
await pb.collection('domains').delete(id);
return new NextResponse(null, { status: 204 });
});
@@ -0,0 +1,90 @@
// 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, requireWorkspaceAccess, createErrorResponse } from '@/lib/auth';
import { recordActivity } from '@/lib/activity';
import { db, tasks } from '@project-e/db';
import { and, eq, isNull } from 'drizzle-orm';
import { RRule } from 'rrule';
type RouteContext = { params: Promise<{ domainId: string; id: string }> };
// POST /api/domains/[domainId]/tasks/[id]/complete — Mark task as done
export const POST = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, id } = await context!.params;
await requireWorkspaceAccess(domainId);
const [existing] = await db.select()
.from(tasks)
.where(and(eq(tasks.id, id), eq(tasks.domainId, domainId), isNull(tasks.deletedAt)))
.limit(1);
if (!existing) {
return createErrorResponse('NOT_FOUND', 'Task not found', 404);
}
const [updated] = await db.update(tasks)
.set({
status: 'done',
completedAt: new Date(),
updatedAt: new Date(),
})
.where(eq(tasks.id, id))
.returning();
await recordActivity({
actor: user.name,
action: 'completed',
entityType: 'task',
entityId: id,
changes: { previousStatus: existing.status },
workspaceId: domainId,
});
// Auto-create next recurring instance if recurrenceRule is set
if (existing.recurrenceRule) {
try {
const rule = RRule.fromString(existing.recurrenceRule);
const nextOccurrence = rule.after(new Date(), true);
if (nextOccurrence) {
const [spawned] = await db.insert(tasks).values({
title: existing.title,
description: existing.description,
status: 'todo',
priority: existing.priority,
domainId: existing.domainId,
projectId: existing.projectId,
sectionId: existing.sectionId,
parentId: existing.parentId,
dueDate: nextOccurrence,
estimatedMinutes: existing.estimatedMinutes,
order: existing.order,
customFields: existing.customFields ?? {},
recurrenceRule: existing.recurrenceRule,
}).returning();
await recordActivity({
actor: user.name,
action: 'created',
entityType: 'task',
entityId: spawned.id,
changes: {
title: spawned.title,
note: 'Auto-created from recurring task',
sourceTaskId: id,
},
workspaceId: domainId,
});
}
} catch (err) {
console.error('[tasks complete] Failed to spawn recurring instance:', err);
// Don't fail the completion — the original task is already marked done
}
}
return NextResponse.json(updated);
});
@@ -0,0 +1,162 @@
// 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, requireWorkspaceAccess, createErrorResponse, ApiError } from '@/lib/auth';
import { recordActivity } from '@/lib/activity';
import { db, tasks, taskDependencies } from '@project-e/db';
import { and, eq, isNull } from 'drizzle-orm';
import { z } from 'zod';
const addDependencySchema = z.object({
taskId: z.string().uuid(),
});
type RouteContext = { params: Promise<{ domainId: string; id: string }> };
/**
* Cycle detection: check if adding dep (taskId -> dependsOnTaskId) would create a cycle.
* Uses BFS from dependsOnTaskId following the dependency chain.
*/
async function wouldCreateCycle(taskId: string, dependsOnTaskId: string): Promise<boolean> {
if (taskId === dependsOnTaskId) return true;
// BFS: follow dependencies from dependsOnTaskId to see if we reach taskId
const visited = new Set<string>();
const queue = [dependsOnTaskId];
while (queue.length > 0) {
const current = queue.shift()!;
if (current === taskId) return true;
if (visited.has(current)) continue;
visited.add(current);
const deps = await db.select({ dependsOnTaskId: taskDependencies.dependsOnTaskId })
.from(taskDependencies)
.where(eq(taskDependencies.taskId, current));
for (const dep of deps) {
if (!visited.has(dep.dependsOnTaskId)) {
queue.push(dep.dependsOnTaskId);
}
}
}
return false;
}
// POST /api/domains/[domainId]/tasks/[id]/dependencies — Add a dependency
export const POST = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, id } = await context!.params;
await requireWorkspaceAccess(domainId);
try {
const body = await request.json();
const data = addDependencySchema.parse(body);
// Verify both tasks exist
const [task] = await db.select()
.from(tasks)
.where(and(eq(tasks.id, id), eq(tasks.domainId, domainId), isNull(tasks.deletedAt)))
.limit(1);
if (!task) {
return createErrorResponse('NOT_FOUND', 'Task not found', 404);
}
const [depTask] = await db.select()
.from(tasks)
.where(and(eq(tasks.id, data.taskId), eq(tasks.domainId, domainId), isNull(tasks.deletedAt)))
.limit(1);
if (!depTask) {
return createErrorResponse('NOT_FOUND', 'Dependency task not found', 404);
}
// Cycle detection
const cycle = await wouldCreateCycle(id, data.taskId);
if (cycle) {
return createErrorResponse('CONFLICT', 'Adding this dependency would create a cycle', 400);
}
// Check if dependency already exists
const [existing] = await db.select()
.from(taskDependencies)
.where(and(eq(taskDependencies.taskId, id), eq(taskDependencies.dependsOnTaskId, data.taskId)))
.limit(1);
if (existing) {
return createErrorResponse('CONFLICT', 'Dependency already exists', 409);
}
await db.insert(taskDependencies).values({
taskId: id,
dependsOnTaskId: data.taskId,
});
await recordActivity({
actor: user.name,
action: 'dependency_added',
entityType: 'task',
entityId: id,
changes: { dependsOnTaskId: data.taskId, dependsOnTitle: depTask.title },
workspaceId: domainId,
});
return NextResponse.json({ success: true }, { status: 201 });
} catch (error) {
if (error instanceof z.ZodError) {
return createErrorResponse('VALIDATION_ERROR', 'Invalid input', 400, error.issues);
}
if (error instanceof ApiError) {
return createErrorResponse(error.code, error.message, error.status);
}
console.error('[dependencies POST] error:', error);
return createErrorResponse('INTERNAL_ERROR', 'Failed to add dependency', 500);
}
});
// DELETE /api/domains/[domainId]/tasks/[id]/dependencies — Remove a dependency
export const DELETE = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, id } = await context!.params;
await requireWorkspaceAccess(domainId);
try {
const body = await request.json();
const data = addDependencySchema.parse(body);
const [existing] = await db.select()
.from(taskDependencies)
.where(and(eq(taskDependencies.taskId, id), eq(taskDependencies.dependsOnTaskId, data.taskId)))
.limit(1);
if (!existing) {
return createErrorResponse('NOT_FOUND', 'Dependency not found', 404);
}
await db.delete(taskDependencies)
.where(and(eq(taskDependencies.taskId, id), eq(taskDependencies.dependsOnTaskId, data.taskId)));
await recordActivity({
actor: user.name,
action: 'dependency_removed',
entityType: 'task',
entityId: id,
changes: { dependsOnTaskId: data.taskId },
workspaceId: domainId,
});
return NextResponse.json({ success: true });
} catch (error) {
if (error instanceof z.ZodError) {
return createErrorResponse('VALIDATION_ERROR', 'Invalid input', 400, error.issues);
}
if (error instanceof ApiError) {
return createErrorResponse(error.code, error.message, error.status);
}
console.error('[dependencies DELETE] error:', error);
return createErrorResponse('INTERNAL_ERROR', 'Failed to remove dependency', 500);
}
});
@@ -0,0 +1,205 @@
// 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, requireWorkspaceAccess, createErrorResponse, ApiError } from '@/lib/auth';
import { recordActivity } from '@/lib/activity';
import { db, tasks, taskTags, tags as tagsTable, taskDependencies } from '@project-e/db';
import { and, asc, eq, inArray, isNull, sql } from 'drizzle-orm';
import { z } from 'zod';
const taskStatusEnum = z.enum(['todo', 'in_progress', 'done', 'cancelled']);
const taskPriorityEnum = z.enum(['low', 'medium', 'high', 'urgent']);
const updateTaskSchema = z.object({
title: z.string().min(1).optional(),
description: z.string().optional().nullable(),
status: taskStatusEnum.optional(),
priority: taskPriorityEnum.optional(),
projectId: z.string().uuid().optional().nullable(),
sectionId: z.string().uuid().optional().nullable(),
parentId: z.string().uuid().optional().nullable(),
dueDate: z.string().datetime().optional().nullable(),
estimatedMinutes: z.number().int().positive().optional().nullable(),
order: z.number().int().optional(),
customFields: z.record(z.string(), z.unknown()).optional(),
recurrenceRule: z.string().optional().nullable(),
});
type RouteContext = { params: Promise<{ domainId: string; id: string }> };
// GET /api/domains/[domainId]/tasks/[id] — Get a single task with subtasks + dependencies
export const GET = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, id } = await context!.params;
await requireWorkspaceAccess(domainId);
const [task] = await db.select()
.from(tasks)
.where(and(eq(tasks.id, id), eq(tasks.domainId, domainId), isNull(tasks.deletedAt)))
.limit(1);
if (!task) {
return createErrorResponse('NOT_FOUND', 'Task not found', 404);
}
// Fetch subtasks
const subtasks = await db.select()
.from(tasks)
.where(and(eq(tasks.parentId, id), isNull(tasks.deletedAt)))
.orderBy(asc(tasks.order));
// Fetch tags
const tagRows = await db.select({
id: tagsTable.id,
name: tagsTable.name,
color: tagsTable.color,
})
.from(taskTags)
.innerJoin(tagsTable, eq(taskTags.tagId, tagsTable.id))
.where(eq(taskTags.taskId, id));
// Fetch dependencies (tasks this task depends on)
const depRows = await db.select({
id: tasks.id,
title: tasks.title,
status: tasks.status,
})
.from(taskDependencies)
.innerJoin(tasks, eq(taskDependencies.dependsOnTaskId, tasks.id))
.where(and(eq(taskDependencies.taskId, id), isNull(tasks.deletedAt)));
// Fetch dependents (tasks that depend on this task)
const dependentRows = await db.select({
id: tasks.id,
title: tasks.title,
status: tasks.status,
})
.from(taskDependencies)
.innerJoin(tasks, eq(taskDependencies.taskId, tasks.id))
.where(and(eq(taskDependencies.dependsOnTaskId, id), isNull(tasks.deletedAt)));
return NextResponse.json({
...task,
subtasks,
tags: tagRows,
dependencies: depRows,
dependents: dependentRows,
});
});
// PATCH /api/domains/[domainId]/tasks/[id] — Update a task
export const PATCH = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, id } = await context!.params;
await requireWorkspaceAccess(domainId);
try {
const body = await request.json();
const data = updateTaskSchema.parse(body);
// Verify task exists
const [existing] = await db.select()
.from(tasks)
.where(and(eq(tasks.id, id), eq(tasks.domainId, domainId), isNull(tasks.deletedAt)))
.limit(1);
if (!existing) {
return createErrorResponse('NOT_FOUND', 'Task not found', 404);
}
// Cycle detection for parentId (can't set parent to self or descendant)
if (data.parentId && data.parentId === id) {
return createErrorResponse('VALIDATION_ERROR', 'A task cannot be its own parent', 400);
}
if (data.parentId) {
// Check for cycles in parent chain
let currentParentId: string | null = data.parentId;
const visited = new Set<string>([id]);
while (currentParentId) {
if (visited.has(currentParentId)) {
return createErrorResponse('VALIDATION_ERROR', 'Circular parent reference detected', 400);
}
visited.add(currentParentId);
const [parent] = await db.select({ parentId: tasks.parentId })
.from(tasks)
.where(eq(tasks.id, currentParentId))
.limit(1);
currentParentId = parent?.parentId ?? null;
}
}
// Build update object
const updateValues: Record<string, unknown> = {};
if (data.title !== undefined) updateValues.title = data.title;
if (data.description !== undefined) updateValues.description = data.description;
if (data.status !== undefined) updateValues.status = data.status;
if (data.priority !== undefined) updateValues.priority = data.priority;
if (data.projectId !== undefined) updateValues.projectId = data.projectId;
if (data.sectionId !== undefined) updateValues.sectionId = data.sectionId;
if (data.parentId !== undefined) updateValues.parentId = data.parentId;
if (data.dueDate !== undefined) updateValues.dueDate = data.dueDate ? new Date(data.dueDate) : null;
if (data.estimatedMinutes !== undefined) updateValues.estimatedMinutes = data.estimatedMinutes;
if (data.order !== undefined) updateValues.order = data.order;
if (data.customFields !== undefined) updateValues.customFields = data.customFields;
if (data.recurrenceRule !== undefined) updateValues.recurrenceRule = data.recurrenceRule;
updateValues.updatedAt = new Date();
const [updated] = await db.update(tasks)
.set(updateValues)
.where(eq(tasks.id, id))
.returning();
// Record activity
await recordActivity({
actor: user.name,
action: 'updated',
entityType: 'task',
entityId: id,
changes: { ...data, previousStatus: existing.status },
workspaceId: domainId,
});
return NextResponse.json(updated);
} catch (error) {
if (error instanceof z.ZodError) {
return createErrorResponse('VALIDATION_ERROR', 'Invalid input', 400, error.issues);
}
if (error instanceof ApiError) {
return createErrorResponse(error.code, error.message, error.status);
}
console.error('[tasks PATCH] error:', error);
return createErrorResponse('INTERNAL_ERROR', 'Failed to update task', 500);
}
});
// DELETE /api/domains/[domainId]/tasks/[id] — Soft delete a task
export const DELETE = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, id } = await context!.params;
await requireWorkspaceAccess(domainId);
const [existing] = await db.select()
.from(tasks)
.where(and(eq(tasks.id, id), eq(tasks.domainId, domainId), isNull(tasks.deletedAt)))
.limit(1);
if (!existing) {
return createErrorResponse('NOT_FOUND', 'Task not found', 404);
}
await db.update(tasks)
.set({ deletedAt: new Date(), updatedAt: new Date() })
.where(eq(tasks.id, id));
// Record activity
await recordActivity({
actor: user.name,
action: 'deleted',
entityType: 'task',
entityId: id,
changes: { title: existing.title },
workspaceId: domainId,
});
return new NextResponse(null, { status: 204 });
});
@@ -0,0 +1,67 @@
// 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, requireWorkspaceAccess, createErrorResponse, ApiError } from '@/lib/auth';
import { recordActivity } from '@/lib/activity';
import { db, tasks } from '@project-e/db';
import { and, eq, isNull } from 'drizzle-orm';
import { z } from 'zod';
const scheduleSchema = z.object({
dueDate: z.string().datetime().nullable(),
});
type RouteContext = { params: Promise<{ domainId: string; id: string }> };
// PATCH /api/domains/[domainId]/tasks/[id]/schedule — Reschedule a task via drag
export const PATCH = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, id } = await context!.params;
await requireWorkspaceAccess(domainId);
try {
const body = await request.json();
const data = scheduleSchema.parse(body);
// Verify task exists
const [existing] = await db.select()
.from(tasks)
.where(and(eq(tasks.id, id), eq(tasks.domainId, domainId), isNull(tasks.deletedAt)))
.limit(1);
if (!existing) {
return createErrorResponse('NOT_FOUND', 'Task not found', 404);
}
const [updated] = await db.update(tasks)
.set({
dueDate: data.dueDate ? new Date(data.dueDate) : null,
updatedAt: new Date(),
})
.where(eq(tasks.id, id))
.returning();
// Record activity
await recordActivity({
actor: user.name,
action: 'updated',
entityType: 'task',
entityId: id,
changes: { dueDate: data.dueDate, previousDueDate: existing.dueDate?.toISOString() || null },
workspaceId: domainId,
});
return NextResponse.json(updated);
} catch (error) {
if (error instanceof z.ZodError) {
return createErrorResponse('VALIDATION_ERROR', 'Invalid input', 400, error.issues);
}
if (error instanceof ApiError) {
return createErrorResponse(error.code, error.message, error.status);
}
console.error('[schedule PATCH] error:', error);
return createErrorResponse('INTERNAL_ERROR', 'Failed to reschedule task', 500);
}
});
@@ -0,0 +1,123 @@
// 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, requireWorkspaceAccess, createErrorResponse, ApiError } from '@/lib/auth';
import { recordActivity } from '@/lib/activity';
import { db, tasks, taskTags, tags as tagsTable } from '@project-e/db';
import { and, eq, isNull } from 'drizzle-orm';
import { z } from 'zod';
const tagActionSchema = z.object({
tagId: z.string().uuid(),
});
type RouteContext = { params: Promise<{ domainId: string; id: string }> };
// POST /api/domains/[domainId]/tasks/[id]/tags — Add a tag to a task
export const POST = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, id } = await context!.params;
await requireWorkspaceAccess(domainId);
try {
const body = await request.json();
const data = tagActionSchema.parse(body);
// Verify task exists
const [task] = await db.select()
.from(tasks)
.where(and(eq(tasks.id, id), eq(tasks.domainId, domainId), isNull(tasks.deletedAt)))
.limit(1);
if (!task) {
return createErrorResponse('NOT_FOUND', 'Task not found', 404);
}
// Verify tag exists
const [tag] = await db.select()
.from(tagsTable)
.where(eq(tagsTable.id, data.tagId))
.limit(1);
if (!tag) {
return createErrorResponse('NOT_FOUND', 'Tag not found', 404);
}
// Check if already tagged
const [existing] = await db.select()
.from(taskTags)
.where(and(eq(taskTags.taskId, id), eq(taskTags.tagId, data.tagId)))
.limit(1);
if (existing) {
return createErrorResponse('CONFLICT', 'Tag already added to this task', 409);
}
await db.insert(taskTags).values({ taskId: id, tagId: data.tagId });
await recordActivity({
actor: user.name,
action: 'tag_added',
entityType: 'task',
entityId: id,
changes: { tagId: data.tagId, tagName: tag.name },
workspaceId: domainId,
});
return NextResponse.json({ success: true }, { status: 201 });
} catch (error) {
if (error instanceof z.ZodError) {
return createErrorResponse('VALIDATION_ERROR', 'Invalid input', 400, error.issues);
}
if (error instanceof ApiError) {
return createErrorResponse(error.code, error.message, error.status);
}
console.error('[tags POST] error:', error);
return createErrorResponse('INTERNAL_ERROR', 'Failed to add tag', 500);
}
});
// DELETE /api/domains/[domainId]/tasks/[id]/tags — Remove a tag from a task
export const DELETE = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, id } = await context!.params;
await requireWorkspaceAccess(domainId);
try {
const body = await request.json();
const data = tagActionSchema.parse(body);
const [existing] = await db.select()
.from(taskTags)
.where(and(eq(taskTags.taskId, id), eq(taskTags.tagId, data.tagId)))
.limit(1);
if (!existing) {
return createErrorResponse('NOT_FOUND', 'Tag not found on this task', 404);
}
await db.delete(taskTags)
.where(and(eq(taskTags.taskId, id), eq(taskTags.tagId, data.tagId)));
await recordActivity({
actor: user.name,
action: 'tag_removed',
entityType: 'task',
entityId: id,
changes: { tagId: data.tagId },
workspaceId: domainId,
});
return NextResponse.json({ success: true });
} catch (error) {
if (error instanceof z.ZodError) {
return createErrorResponse('VALIDATION_ERROR', 'Invalid input', 400, error.issues);
}
if (error instanceof ApiError) {
return createErrorResponse(error.code, error.message, error.status);
}
console.error('[tags DELETE] error:', error);
return createErrorResponse('INTERNAL_ERROR', 'Failed to remove tag', 500);
}
});
@@ -0,0 +1,47 @@
// 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, requireWorkspaceAccess, createErrorResponse } from '@/lib/auth';
import { recordActivity } from '@/lib/activity';
import { db, tasks } from '@project-e/db';
import { and, eq, isNull } from 'drizzle-orm';
type RouteContext = { params: Promise<{ domainId: string; id: string }> };
// POST /api/domains/[domainId]/tasks/[id]/uncomplete — Revert task from done
export const POST = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, id } = await context!.params;
await requireWorkspaceAccess(domainId);
const [existing] = await db.select()
.from(tasks)
.where(and(eq(tasks.id, id), eq(tasks.domainId, domainId), isNull(tasks.deletedAt)))
.limit(1);
if (!existing) {
return createErrorResponse('NOT_FOUND', 'Task not found', 404);
}
const [updated] = await db.update(tasks)
.set({
status: 'todo',
completedAt: null,
updatedAt: new Date(),
})
.where(eq(tasks.id, id))
.returning();
await recordActivity({
actor: user.name,
action: 'uncompleted',
entityType: 'task',
entityId: id,
changes: { previousStatus: existing.status },
workspaceId: domainId,
});
return NextResponse.json(updated);
});
@@ -0,0 +1,131 @@
// 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, requireWorkspaceAccess, createErrorResponse, ApiError } from '@/lib/auth';
import { recordActivity } from '@/lib/activity';
import { db, tasks } from '@project-e/db';
import { and, eq, inArray, isNull } from 'drizzle-orm';
import { z } from 'zod';
const bulkUpdateSchema = z.object({
ids: z.array(z.string().uuid()).min(1).max(200),
updates: z.object({
status: z.enum(['todo', 'in_progress', 'done', 'cancelled']).optional(),
priority: z.enum(['low', 'medium', 'high', 'urgent']).optional(),
order: z.number().int().optional(),
projectId: z.string().uuid().optional().nullable(),
sectionId: z.string().uuid().optional().nullable(),
}),
});
const bulkDeleteSchema = z.object({
ids: z.array(z.string().uuid()).min(1).max(200),
});
type RouteContext = { params: Promise<{ domainId: string }> };
// POST /api/domains/[domainId]/tasks/bulk — Bulk update tasks (order, status)
export const POST = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId } = await context!.params;
await requireWorkspaceAccess(domainId);
try {
const body = await request.json();
const data = bulkUpdateSchema.parse(body);
// Verify all tasks belong to this domain
const existingTasks = await db.select({ id: tasks.id })
.from(tasks)
.where(and(inArray(tasks.id, data.ids), eq(tasks.domainId, domainId), isNull(tasks.deletedAt)));
if (existingTasks.length !== data.ids.length) {
return createErrorResponse('NOT_FOUND', 'One or more tasks not found', 404);
}
const updateValues: Record<string, unknown> = { updatedAt: new Date() };
if (data.updates.status !== undefined) updateValues.status = data.updates.status;
if (data.updates.priority !== undefined) updateValues.priority = data.updates.priority;
if (data.updates.order !== undefined) updateValues.order = data.updates.order;
if (data.updates.projectId !== undefined) updateValues.projectId = data.updates.projectId;
if (data.updates.sectionId !== undefined) updateValues.sectionId = data.updates.sectionId;
const updated = await db.update(tasks)
.set(updateValues)
.where(inArray(tasks.id, data.ids))
.returning();
// Record activity for each task
for (const task of updated) {
await recordActivity({
actor: user.name,
action: 'bulk_updated',
entityType: 'task',
entityId: task.id,
changes: data.updates,
workspaceId: domainId,
});
}
return NextResponse.json({ updated: updated.length, items: updated });
} catch (error) {
if (error instanceof z.ZodError) {
return createErrorResponse('VALIDATION_ERROR', 'Invalid input', 400, error.issues);
}
if (error instanceof ApiError) {
return createErrorResponse(error.code, error.message, error.status);
}
console.error('[tasks bulk POST] error:', error);
return createErrorResponse('INTERNAL_ERROR', 'Failed to bulk update tasks', 500);
}
});
// DELETE /api/domains/[domainId]/tasks/bulk — Bulk soft-delete tasks
export const DELETE = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId } = await context!.params;
await requireWorkspaceAccess(domainId);
try {
const body = await request.json();
const data = bulkDeleteSchema.parse(body);
// Verify all tasks belong to this domain
const existingTasks = await db.select({ id: tasks.id, title: tasks.title })
.from(tasks)
.where(and(inArray(tasks.id, data.ids), eq(tasks.domainId, domainId), isNull(tasks.deletedAt)));
if (existingTasks.length !== data.ids.length) {
return createErrorResponse('NOT_FOUND', 'One or more tasks not found', 404);
}
// Soft delete
await db.update(tasks)
.set({ deletedAt: new Date(), updatedAt: new Date() })
.where(inArray(tasks.id, data.ids));
// Record activity for each task
for (const task of existingTasks) {
await recordActivity({
actor: user.name,
action: 'bulk_deleted',
entityType: 'task',
entityId: task.id,
changes: { title: task.title },
workspaceId: domainId,
});
}
return NextResponse.json({ deleted: existingTasks.length });
} catch (error) {
if (error instanceof z.ZodError) {
return createErrorResponse('VALIDATION_ERROR', 'Invalid input', 400, error.issues);
}
if (error instanceof ApiError) {
return createErrorResponse(error.code, error.message, error.status);
}
console.error('[tasks bulk DELETE] error:', error);
return createErrorResponse('INTERNAL_ERROR', 'Failed to bulk delete tasks', 500);
}
});
@@ -0,0 +1,220 @@
// 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, requireWorkspaceAccess, createErrorResponse, ApiError } from '@/lib/auth';
import { recordActivity } from '@/lib/activity';
import { db, tasks, taskTags, tags as tagsTable, taskDependencies, domains } from '@project-e/db';
import { and, asc, desc, eq, ilike, inArray, isNull, or, sql } from 'drizzle-orm';
import { z } from 'zod';
const taskStatusEnum = z.enum(['todo', 'in_progress', 'done', 'cancelled']);
const taskPriorityEnum = z.enum(['low', 'medium', 'high', 'urgent']);
const createTaskSchema = z.object({
title: z.string().min(1, 'Title is required'),
description: z.string().optional().nullable(),
status: taskStatusEnum.optional().default('todo'),
priority: taskPriorityEnum.optional().default('medium'),
projectId: z.string().uuid().optional().nullable(),
sectionId: z.string().uuid().optional().nullable(),
parentId: z.string().uuid().optional().nullable(),
dueDate: z.string().datetime().optional().nullable(),
estimatedMinutes: z.number().int().positive().optional().nullable(),
order: z.number().int().optional(),
customFields: z.record(z.string(), z.unknown()).optional(),
recurrenceRule: z.string().optional().nullable(),
tagIds: z.array(z.string().uuid()).optional(),
});
type RouteContext = { params: Promise<{ domainId: string }> };
// GET /api/domains/[domainId]/tasks — List tasks with filtering, sorting, pagination
export const GET = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId } = await context!.params;
await requireWorkspaceAccess(domainId);
const { searchParams } = new URL(request.url);
const status = searchParams.get('status');
const priority = searchParams.get('priority');
const tag = searchParams.get('tag');
const search = searchParams.get('search');
const parentId = searchParams.get('parent_id');
const projectId = searchParams.get('project_id');
const sectionId = searchParams.get('section_id');
const limit = Math.min(parseInt(searchParams.get('limit') || '50'), 200);
const offset = parseInt(searchParams.get('offset') || '0');
const sort = searchParams.get('sort') || 'order';
const order = searchParams.get('order') || 'asc';
// Build where conditions
const conditions: any[] = [
eq(tasks.domainId, domainId),
isNull(tasks.deletedAt),
];
if (status) {
const statuses = status.split(',');
conditions.push(inArray(tasks.status, statuses as any));
}
if (priority) {
const priorities = priority.split(',');
conditions.push(inArray(tasks.priority, priorities as any));
}
if (search) {
conditions.push(ilike(tasks.title, `%${search}%`));
}
if (parentId === 'null') {
conditions.push(isNull(tasks.parentId));
} else if (parentId) {
conditions.push(eq(tasks.parentId, parentId));
}
if (projectId) {
conditions.push(eq(tasks.projectId, projectId));
}
if (sectionId) {
conditions.push(eq(tasks.sectionId, sectionId));
}
// Build order
const orderFn = order === 'desc' ? desc : asc;
let orderColumn;
switch (sort) {
case 'title': orderColumn = orderFn(tasks.title); break;
case 'status': orderColumn = orderFn(tasks.status); break;
case 'priority': orderColumn = orderFn(tasks.priority); break;
case 'due_date': orderColumn = orderFn(tasks.dueDate); break;
case 'created_at': orderColumn = orderFn(tasks.createdAt); break;
case 'updated_at': orderColumn = orderFn(tasks.updatedAt); break;
default: orderColumn = orderFn(tasks.order); break;
}
const [items, countResult] = await Promise.all([
db.select()
.from(tasks)
.where(and(...conditions))
.orderBy(orderColumn)
.limit(limit)
.offset(offset),
db.select({ count: sql<number>`count(*)` })
.from(tasks)
.where(and(...conditions)),
]);
const totalItems = Number(countResult[0]?.count || 0);
// If tag filter is specified, filter in-memory (or we could do a subquery)
let filteredItems = items;
if (tag) {
const tagIds = tag.split(',');
const taskTagRows = await db.select({ taskId: taskTags.taskId })
.from(taskTags)
.where(inArray(taskTags.tagId, tagIds));
const matchingTaskIds = new Set(taskTagRows.map(r => r.taskId));
filteredItems = items.filter(t => matchingTaskIds.has(t.id));
}
// Fetch tags for all tasks
let taskTagMap = new Map<string, { id: string; name: string; color: string | null }[]>();
if (filteredItems.length > 0) {
const taskIds = filteredItems.map(t => t.id);
const tagRows = await db.select({
taskId: taskTags.taskId,
id: tagsTable.id,
name: tagsTable.name,
color: tagsTable.color,
})
.from(taskTags)
.innerJoin(tagsTable, eq(taskTags.tagId, tagsTable.id))
.where(inArray(taskTags.taskId, taskIds));
for (const row of tagRows) {
if (!taskTagMap.has(row.taskId)) taskTagMap.set(row.taskId, []);
taskTagMap.get(row.taskId)!.push({ id: row.id, name: row.name, color: row.color });
}
}
const itemsWithTags = filteredItems.map(t => ({
...t,
tags: taskTagMap.get(t.id) || [],
}));
return NextResponse.json({
items: itemsWithTags,
totalItems,
limit,
offset,
});
});
// POST /api/domains/[domainId]/tasks — Create a task
export const POST = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId } = await context!.params;
await requireWorkspaceAccess(domainId);
try {
const body = await request.json();
const data = createTaskSchema.parse(body);
// Validate domain_id matches route param
// domainId is already validated via requireWorkspaceAccess
// Cycle detection for parentId (subtask)
if (data.parentId) {
// Verify parent exists and is not deleted
const [parent] = await db.select({ id: tasks.id })
.from(tasks)
.where(and(eq(tasks.id, data.parentId), isNull(tasks.deletedAt)))
.limit(1);
if (!parent) {
return createErrorResponse('NOT_FOUND', 'Parent task not found', 404);
}
}
const [task] = await db.insert(tasks).values({
title: data.title,
description: data.description ?? null,
status: data.status,
priority: data.priority,
domainId,
projectId: data.projectId ?? null,
sectionId: data.sectionId ?? null,
parentId: data.parentId ?? null,
dueDate: data.dueDate ? new Date(data.dueDate) : null,
estimatedMinutes: data.estimatedMinutes ?? null,
order: data.order ?? 0,
customFields: data.customFields ?? {},
recurrenceRule: data.recurrenceRule ?? null,
}).returning();
// Insert tags if provided
if (data.tagIds && data.tagIds.length > 0) {
await db.insert(taskTags).values(
data.tagIds.map(tagId => ({ taskId: task.id, tagId }))
);
}
// Record activity
await recordActivity({
actor: user.name,
action: 'created',
entityType: 'task',
entityId: task.id,
changes: { title: task.title, status: task.status, priority: task.priority },
workspaceId: domainId,
});
return NextResponse.json(task, { status: 201 });
} catch (error) {
if (error instanceof z.ZodError) {
return createErrorResponse('VALIDATION_ERROR', 'Invalid input', 400, error.issues);
}
if (error instanceof ApiError) {
return createErrorResponse(error.code, error.message, error.status);
}
console.error('[tasks POST] error:', error);
return createErrorResponse('INTERNAL_ERROR', 'Failed to create task', 500);
}
});
@@ -0,0 +1,50 @@
// 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, requireWorkspaceAccess, createErrorResponse } from '@/lib/auth';
import { db, webhooks, webhookDeliveries } from '@project-e/db';
import { and, desc, eq, sql } from 'drizzle-orm';
type RouteContext = { params: Promise<{ domainId: string; id: string }> };
// GET /api/domains/[domainId]/webhooks/[id]/deliveries — List deliveries for a webhook
export const GET = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, id } = await context!.params;
await requireWorkspaceAccess(domainId);
// Verify webhook exists in this workspace
const [webhook] = await db.select({ id: webhooks.id })
.from(webhooks)
.where(and(eq(webhooks.id, id), eq(webhooks.workspaceId, domainId)))
.limit(1);
if (!webhook) {
return createErrorResponse('NOT_FOUND', 'Webhook not found', 404);
}
const { searchParams } = new URL(request.url);
const limit = Math.min(parseInt(searchParams.get('limit') || '50'), 200);
const offset = parseInt(searchParams.get('offset') || '0');
const [items, countResult] = await Promise.all([
db.select()
.from(webhookDeliveries)
.where(eq(webhookDeliveries.webhookId, id))
.orderBy(desc(webhookDeliveries.createdAt))
.limit(limit)
.offset(offset),
db.select({ count: sql<number>`count(*)` })
.from(webhookDeliveries)
.where(eq(webhookDeliveries.webhookId, id)),
]);
return NextResponse.json({
items,
totalItems: Number(countResult[0]?.count || 0),
limit,
offset,
});
});
@@ -0,0 +1,118 @@
// 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, requireWorkspaceAccess, createErrorResponse, ApiError } from '@/lib/auth';
import { recordActivity } from '@/lib/activity';
import { db, webhooks } from '@project-e/db';
import { and, eq } from 'drizzle-orm';
import { z } from 'zod';
const updateWebhookSchema = z.object({
name: z.string().optional(),
url: z.string().url('Must be a valid URL').optional(),
events: z.array(z.string()).optional(),
active: z.boolean().optional(),
});
type RouteContext = { params: Promise<{ domainId: string; id: string }> };
// GET /api/domains/[domainId]/webhooks/[id] — Get a single webhook
export const GET = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, id } = await context!.params;
await requireWorkspaceAccess(domainId);
const [webhook] = await db.select()
.from(webhooks)
.where(and(eq(webhooks.id, id), eq(webhooks.workspaceId, domainId)))
.limit(1);
if (!webhook) {
return createErrorResponse('NOT_FOUND', 'Webhook not found', 404);
}
// Never return the secret on GET
const { secret: _, ...safe } = webhook;
return NextResponse.json(safe);
});
// PATCH /api/domains/[domainId]/webhooks/[id] — Update a webhook
export const PATCH = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, id } = await context!.params;
await requireWorkspaceAccess(domainId);
try {
const body = await request.json();
const data = updateWebhookSchema.parse(body);
const [existing] = await db.select()
.from(webhooks)
.where(and(eq(webhooks.id, id), eq(webhooks.workspaceId, domainId)))
.limit(1);
if (!existing) {
return createErrorResponse('NOT_FOUND', 'Webhook not found', 404);
}
const updateData: Record<string, unknown> = { updatedAt: new Date() };
if (data.name !== undefined) updateData.name = data.name;
if (data.url !== undefined) updateData.url = data.url;
if (data.events !== undefined) updateData.events = data.events;
if (data.active !== undefined) updateData.active = data.active;
const [updated] = await db.update(webhooks)
.set(updateData)
.where(eq(webhooks.id, id))
.returning();
await recordActivity({
actor: user.name,
action: 'updated',
entityType: 'webhook',
entityId: updated.id,
changes: updateData,
workspaceId: domainId,
});
const { secret: _, ...safe } = updated;
return NextResponse.json(safe);
} catch (error) {
if (error instanceof z.ZodError) {
return createErrorResponse('VALIDATION_ERROR', 'Invalid input', 400, error.issues);
}
if (error instanceof ApiError) {
return createErrorResponse(error.code, error.message, error.status);
}
console.error('[webhook PATCH] error:', error);
return createErrorResponse('INTERNAL_ERROR', 'Failed to update webhook', 500);
}
});
// DELETE /api/domains/[domainId]/webhooks/[id] — Delete a webhook
export const DELETE = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, id } = await context!.params;
await requireWorkspaceAccess(domainId);
const [existing] = await db.select()
.from(webhooks)
.where(and(eq(webhooks.id, id), eq(webhooks.workspaceId, domainId)))
.limit(1);
if (!existing) {
return createErrorResponse('NOT_FOUND', 'Webhook not found', 404);
}
await db.delete(webhooks).where(eq(webhooks.id, id));
await recordActivity({
actor: user.name,
action: 'deleted',
entityType: 'webhook',
entityId: id,
workspaceId: domainId,
});
return new NextResponse(null, { status: 204 });
});
@@ -0,0 +1,99 @@
// 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, requireWorkspaceAccess, createErrorResponse } from '@/lib/auth';
import { db, webhooks, webhookDeliveries } from '@project-e/db';
import { and, eq } from 'drizzle-orm';
import { createHmac } from 'node:crypto';
type RouteContext = { params: Promise<{ domainId: string; id: string }> };
// POST /api/domains/[domainId]/webhooks/[id]/test — Send a test event
export const POST = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId, id } = await context!.params;
await requireWorkspaceAccess(domainId);
const [webhook] = await db.select()
.from(webhooks)
.where(and(eq(webhooks.id, id), eq(webhooks.workspaceId, domainId)))
.limit(1);
if (!webhook) {
return createErrorResponse('NOT_FOUND', 'Webhook not found', 404);
}
if (!webhook.active) {
return createErrorResponse('WEBHOOK_DISABLED', 'Cannot test a disabled webhook', 400);
}
const testPayload = {
event: 'test.ping',
entity_type: 'test',
entity_id: 'test-001',
data: { message: 'This is a test webhook delivery from Project E.', webhook_id: webhook.id },
timestamp: new Date().toISOString(),
workspace_id: domainId,
};
const headers: Record<string, string> = {
'Content-Type': 'application/json',
'X-Event-Type': 'test.ping',
};
if (webhook.secret) {
const body = JSON.stringify(testPayload);
const signature = createHmac('sha256', webhook.secret)
.update(body)
.digest('hex');
headers['X-ProjectE-Signature'] = signature;
}
try {
const response = await fetch(webhook.url, {
method: 'POST',
headers,
body: JSON.stringify(testPayload),
signal: AbortSignal.timeout(10000),
});
const responseBody = await response.text();
// Record the delivery
await db.insert(webhookDeliveries).values({
webhookId: webhook.id,
event: 'test.ping',
payload: testPayload as Record<string, unknown>,
status: response.ok ? 'success' : 'failed',
statusCode: response.status,
responseBody: responseBody.substring(0, 1000),
attempts: 1,
});
return NextResponse.json({
success: response.ok,
status: response.status,
response: responseBody.substring(0, 500),
});
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
await db.insert(webhookDeliveries).values({
webhookId: webhook.id,
event: 'test.ping',
payload: testPayload as Record<string, unknown>,
status: 'failed',
statusCode: 0,
responseBody: errorMessage,
attempts: 1,
});
return NextResponse.json({
success: false,
status: 0,
response: errorMessage,
});
}
});
@@ -0,0 +1,102 @@
// 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, requireWorkspaceAccess, createErrorResponse, ApiError } from '@/lib/auth';
import { recordActivity } from '@/lib/activity';
import { db, webhooks, webhookDeliveries } from '@project-e/db';
import { and, asc, desc, eq, isNull, sql } from 'drizzle-orm';
import { createHash, randomBytes } from 'node:crypto';
import { z } from 'zod';
const createWebhookSchema = z.object({
name: z.string().optional(),
url: z.string().url('Must be a valid URL'),
events: z.array(z.string()).default([]),
active: z.boolean().optional().default(true),
});
const updateWebhookSchema = z.object({
name: z.string().optional(),
url: z.string().url('Must be a valid URL').optional(),
events: z.array(z.string()).optional(),
active: z.boolean().optional(),
});
type RouteContext = { params: Promise<{ domainId: string }> };
type IdRouteContext = { params: Promise<{ domainId: string; id: string }> };
// GET /api/domains/[domainId]/webhooks — List webhooks
export const GET = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId } = await context!.params;
await requireWorkspaceAccess(domainId);
const { searchParams } = new URL(request.url);
const limit = Math.min(parseInt(searchParams.get('limit') || '50'), 200);
const offset = parseInt(searchParams.get('offset') || '0');
const [items, countResult] = await Promise.all([
db.select()
.from(webhooks)
.where(eq(webhooks.workspaceId, domainId))
.orderBy(desc(webhooks.createdAt))
.limit(limit)
.offset(offset),
db.select({ count: sql<number>`count(*)` })
.from(webhooks)
.where(eq(webhooks.workspaceId, domainId)),
]);
return NextResponse.json({
items,
totalItems: Number(countResult[0]?.count || 0),
limit,
offset,
});
});
// POST /api/domains/[domainId]/webhooks — Create a webhook
export const POST = withAuth<RouteContext>(async (request: NextRequest, user, context) => {
const { domainId } = await context!.params;
await requireWorkspaceAccess(domainId);
try {
const body = await request.json();
const data = createWebhookSchema.parse(body);
// Generate a webhook secret (shown only once on create)
const secret = `whsec_${randomBytes(24).toString('hex')}`;
const [webhook] = await db.insert(webhooks).values({
name: data.name ?? null,
url: data.url,
secret,
events: data.events,
active: data.active ?? true,
workspaceId: domainId,
}).returning();
await recordActivity({
actor: user.name,
action: 'created',
entityType: 'webhook',
entityId: webhook.id,
changes: { name: webhook.name, url: webhook.url, events: webhook.events },
workspaceId: domainId,
});
// Return the secret on create — it won't be shown again
return NextResponse.json({ ...webhook, secret }, { status: 201 });
} catch (error) {
if (error instanceof z.ZodError) {
return createErrorResponse('VALIDATION_ERROR', 'Invalid input', 400, error.issues);
}
if (error instanceof ApiError) {
return createErrorResponse(error.code, error.message, error.status);
}
console.error('[webhooks POST] error:', error);
return createErrorResponse('INTERNAL_ERROR', 'Failed to create webhook', 500);
}
});
+129
View File
@@ -0,0 +1,129 @@
// 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, resolveActiveDomain } from '@/lib/auth';
import { db, domains } from '@project-e/db';
import { and, asc, desc, eq, ilike, or, sql } from 'drizzle-orm';
import { z } from 'zod';
const createDomainSchema = z.object({
name: z.string().min(1, 'Name is required'),
slug: z.string().min(1).optional(),
color: z.string().optional().nullable(),
icon: z.string().optional().nullable(),
parentId: z.string().uuid().optional().nullable(),
});
// GET /api/domains — List domains with filtering, sorting, pagination
export const GET = withAuth(async (request: NextRequest, user) => {
const { searchParams } = new URL(request.url);
const page = Math.max(1, parseInt(searchParams.get('page') || '1'));
const perPage = Math.min(100, Math.max(1, parseInt(searchParams.get('perPage') || '50')));
const sortParam = searchParams.get('sort') || 'sort_order';
const filter = searchParams.get('filter') || undefined;
// Build order by — whitelist safe column names
const sortDir = sortParam.startsWith('-') ? 'desc' : 'asc';
const sortField = sortParam.replace(/^-/, '');
const sortColumns: Record<string, any> = {
name: domains.name,
slug: domains.slug,
sort_order: domains.sortOrder,
created_at: domains.createdAt,
updated_at: domains.updatedAt,
};
const orderBy = sortDir === 'asc'
? asc(sortColumns[sortField] || domains.sortOrder)
: desc(sortColumns[sortField] || domains.sortOrder);
// Build where clause — filter by owner
const conditions: any[] = [eq(domains.ownerId, user.id)];
if (filter) {
conditions.push(
or(
ilike(domains.name, `%${filter}%`),
ilike(domains.slug, `%${filter}%`),
)!
);
}
const offset = (page - 1) * perPage;
const [items, countResult] = await Promise.all([
db.select()
.from(domains)
.where(and(...conditions))
.orderBy(orderBy)
.limit(perPage)
.offset(offset),
db.select({ count: sql<number>`count(*)` })
.from(domains)
.where(and(...conditions)),
]);
let totalItems = Number(countResult[0]?.count || 0);
// If user has no domains, auto-create a default "Personal" domain
if (totalItems === 0) {
const active = await resolveActiveDomain(user);
// Re-fetch to include the newly created domain
const [newItems, newCount] = await Promise.all([
db.select()
.from(domains)
.where(eq(domains.ownerId, user.id))
.orderBy(orderBy)
.limit(perPage)
.offset(offset),
db.select({ count: sql<number>`count(*)` })
.from(domains)
.where(eq(domains.ownerId, user.id)),
]);
return NextResponse.json({
items: newItems,
totalItems: Number(newCount[0]?.count || 0),
totalPages: Math.ceil(Number(newCount[0]?.count || 0) / perPage),
page,
perPage,
});
}
return NextResponse.json({
items,
totalItems,
totalPages: Math.ceil(totalItems / perPage),
page,
perPage,
});
});
// POST /api/domains — Create a domain
export const POST = withAuth(async (request: NextRequest, user) => {
try {
const body = await request.json();
const data = createDomainSchema.parse(body);
// Auto-generate slug from name if not provided
const slug = data.slug || data.name.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/(^-|-$)/g, '') || 'domain';
const [domain] = await db.insert(domains)
.values({
name: data.name,
slug,
color: data.color || null,
icon: data.icon || null,
parentId: data.parentId || null,
ownerId: user.id,
})
.returning();
return NextResponse.json(domain, { status: 201 });
} catch (error) {
if (error instanceof z.ZodError) {
return createErrorResponse('VALIDATION_ERROR', 'Invalid input', 400, error.issues);
}
throw error;
}
});