echos-ocr/src/app/api/organizations/route.ts
Randall Stillwell d3e7374439 Add SaaS foundation: Auth.js, dashboard shell, org model, auto-assignment
Major architectural upgrade preparing Echo OCR for self-hosted SaaS deployment:

- Auth: Built-in Auth.js v5 with credentials + Authentik OIDC SSO, JWT sessions,
  middleware route protection, login/signup/setup pages, registration API
- UI: Dashboard layout with collapsible sidebar nav, AppShell wrapper, route
  groups for (dashboard) and (auth), new pages for events/people/reports
- Schema: Auth.js tables (Account, Session, VerificationToken), Organization,
  OrgMember, Location, CollectionDay, Invitation, SystemConfig, ApiKey models;
  proper User relations to ResponseCard/ActivityLog/Notification
- Permissions: Role hierarchy (owner/admin/editor/reviewer/viewer) with
  action-based permission map and requirePermission/requireAuth helpers
- Onboarding: Multi-step setup wizard for first-user bootstrap (account, org,
  location) with SystemConfig tracking
- Events: CollectionDay model with rrule support for recurring church services
- Auto-assign: Event-aware card assignment engine replacing getPreviousSunday()
- Migration: seed-migration.ts script for upgrading existing deployments

Made-with: Cursor
2026-04-14 23:59:38 -05:00

57 lines
1.7 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/db";
import { requireAuth } from "@/lib/auth";
import { PermissionError } from "@/lib/permissions";
export async function GET() {
try {
const user = await requireAuth();
if (!user.orgId) {
return NextResponse.json([]);
}
const org = await prisma.organization.findUnique({
where: { id: user.orgId },
include: {
locations: { orderBy: { name: "asc" } },
_count: { select: { members: true } },
},
});
return NextResponse.json(org);
} catch (error) {
if (error instanceof PermissionError) {
return NextResponse.json({ error: error.message }, { status: 403 });
}
return NextResponse.json({ error: "Failed to fetch organization" }, { status: 500 });
}
}
export async function PUT(req: NextRequest) {
try {
const user = await requireAuth("org.manage");
if (!user.orgId) {
return NextResponse.json({ error: "No organization" }, { status: 400 });
}
const body = await req.json();
const { name, type, timezone, settings } = body;
const org = await prisma.organization.update({
where: { id: user.orgId },
data: {
...(name !== undefined && { name }),
...(type !== undefined && { type }),
...(timezone !== undefined && { timezone }),
...(settings !== undefined && { settings }),
},
});
return NextResponse.json(org);
} catch (error) {
if (error instanceof PermissionError) {
return NextResponse.json({ error: error.message }, { status: 403 });
}
return NextResponse.json({ error: "Failed to update organization" }, { status: 500 });
}
}