echos-ocr/src/app/api/locations/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

58 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 locations = await prisma.location.findMany({
where: { organizationId: user.orgId },
include: {
_count: { select: { collectionDays: true, cards: true } },
},
orderBy: { name: "asc" },
});
return NextResponse.json(locations);
} catch (error) {
if (error instanceof PermissionError) {
return NextResponse.json({ error: error.message }, { status: 403 });
}
return NextResponse.json({ error: "Failed to fetch locations" }, { status: 500 });
}
}
export async function POST(req: NextRequest) {
try {
const user = await requireAuth("events.manage");
if (!user.orgId) {
return NextResponse.json({ error: "No organization" }, { status: 400 });
}
const body = await req.json();
const { name, address, timezone } = body;
if (!name) {
return NextResponse.json({ error: "Name is required" }, { status: 400 });
}
const location = await prisma.location.create({
data: {
name,
address: address || null,
timezone: timezone || null,
organizationId: user.orgId,
},
});
return NextResponse.json(location, { status: 201 });
} catch (error) {
if (error instanceof PermissionError) {
return NextResponse.json({ error: error.message }, { status: 403 });
}
return NextResponse.json({ error: "Failed to create location" }, { status: 500 });
}
}