route.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // app/api/branches/route.js
  2. import { NextResponse } from "next/server";
  3. import { listBranches } from "@/lib/storage";
  4. import { getSession } from "@/lib/auth/session";
  5. import { filterBranchesForSession } from "@/lib/auth/permissions";
  6. /**
  7. * GET /api/branches
  8. *
  9. * Returns the list of branches (e.g. ["NL01", "NL02", ...]) based on the
  10. * directory names under NAS_ROOT_PATH.
  11. *
  12. * RBAC:
  13. * - 401 if no session
  14. * - branch role: only returns its own branch
  15. * - admin/dev: returns all branches
  16. */
  17. export async function GET() {
  18. const session = await getSession();
  19. if (!session) {
  20. return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  21. }
  22. try {
  23. const branches = await listBranches();
  24. const visibleBranches = filterBranchesForSession(session, branches);
  25. return NextResponse.json({ branches: visibleBranches });
  26. } catch (error) {
  27. console.error("[api/branches] Error reading branches:", error);
  28. return NextResponse.json(
  29. { error: "Fehler beim Lesen der Niederlassungen" },
  30. { status: 500 }
  31. );
  32. }
  33. }