Files
ProjectE/apps/web/app/api/canvases/route.ts
T
Hermes c3bce0b9e3 fix(p0): resolve active domain automatically when onboarding skipped
- New resolveActiveDomain() helper in apps/web/lib/auth.ts: returns the
  user's first existing domain (ordered by sort_order then created_at),
  or auto-creates a default 'Personal' domain if they have none.
- 7 affected API routes (agents, canvases, domains, habits, projects,
  search, tasks) now fall back to resolveActiveDomain when the request
  omits a domain param. This eliminates the UNDEFINED_VALUE on domain_id
  and 22P02 invalid uuid errors that broke 6+ UI flows.
- New POST /api/quick-capture route: switches on type=task|habit|note|project
  to create the right entity, after resolving the active domain. This is
  the API the dashboard quick-capture widget calls.
- packages/db/src/schema.ts: added ownerId: uuid('owner_id') to the
  domains table so each user owns their domains.
- drizzle/0004_add_owner_id_to_domains.sql: matching migration with
  column add + index.
- apps/web/__tests__/lib/auth.test.ts: unit tests for both branches of
  resolveActiveDomain (returns existing / creates Personal).
- apps/web/app/(dashboard)/canvas/page.tsx: removed hardcoded
  domain: 'personal' from the quick-create payload so server-side
  resolution can do its job.

This unblocks all of: /tasks, /habits, /projects, /search, /settings/agents,
/settings/domains, /canvas, and dashboard quick-capture.
2026-07-30 23:51:29 +00:00

55 lines
1.9 KiB
TypeScript

// AGENT INSTRUCTION: After every write (INSERT/UPDATE/DELETE), you MUST:
// 1. Insert activity feed entry
// 2. Call pg.notify('project_e_events', JSON.stringify({ type, action, id, workspace_id }))
// See AGENTS.md for full rules.
import { NextRequest, NextResponse } from 'next/server';
import { withAuth, createErrorResponse, resolveActiveDomain } from '@/lib/auth';
import { createPocketBaseClient } from '@/lib/pocketbase';
import { createCanvasSchema } from '@project-e/shared';
import { z } from 'zod';
// GET /api/canvases — List canvases with filtering, sorting, pagination
export const GET = withAuth(async (request: NextRequest, _user) => {
const { searchParams } = new URL(request.url);
const page = parseInt(searchParams.get('page') || '1');
const perPage = parseInt(searchParams.get('perPage') || '50');
const filter = searchParams.get('filter') || undefined;
const sort = searchParams.get('sort') || '-created';
const pb = createPocketBaseClient();
const result = await pb.collection('canvases').getList(page, perPage, {
...(filter ? { filter } : {}),
sort,
});
return NextResponse.json({
items: result.items,
totalItems: result.totalItems,
totalPages: result.totalPages,
page: result.page,
perPage: result.perPage,
});
});
// POST /api/canvases — Create a canvas
export const POST = withAuth(async (request: NextRequest, user) => {
try {
const body = await request.json();
const data = createCanvasSchema.parse({
...body,
domain: body.domain || (await resolveActiveDomain(user)).id,
});
const pb = createPocketBaseClient();
const canvas = await pb.collection('canvases').create(data);
return NextResponse.json(canvas, { status: 201 });
} catch (error) {
if (error instanceof z.ZodError) {
return createErrorResponse('VALIDATION_ERROR', 'Invalid input', 400, error.issues);
}
throw error;
}
});