From 1592c2f9c877623ff6be37122a13a78c887c3fc7 Mon Sep 17 00:00:00 2001 From: Jonny Date: Wed, 4 Nov 2026 11:57:43 +0100 Subject: [PATCH 1/2] feat: Rollen pro Firma zaehlen und formatieren Co-Authored-By: Claude Opus 5.5 --- .../plans/2026-11-04-rollen-zaehlung.md | 76 +++++++++++++++++++ web/lib/plattform/rollen.test.ts | 43 +++++++++++ web/lib/plattform/rollen.ts | 24 ++++++ 3 files changed, 143 insertions(+) create mode 100644 docs/superpowers/plans/2026-11-04-rollen-zaehlung.md create mode 100644 web/lib/plattform/rollen.test.ts create mode 100644 web/lib/plattform/rollen.ts diff --git a/docs/superpowers/plans/2026-11-04-rollen-zaehlung.md b/docs/superpowers/plans/2026-11-04-rollen-zaehlung.md new file mode 100644 index 0000000..37b02cf --- /dev/null +++ b/docs/superpowers/plans/2026-11-04-rollen-zaehlung.md @@ -0,0 +1,76 @@ +# Rollen-Zählung pro Firma Implementation Plan + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. + +**Goal:** Die Plattform-Verwaltung zeigt pro Firma, wie viele Admins, Disponenten, Fahrer und ReadOnly-Nutzer angelegt sind (Grundlage für rollenabhängige Abrechnung). + +**Architecture:** Eine reine Funktion zählt Rollen aus einer Liste von Profilen und formatiert sie. `listFirmen` und `ladeFirmaFuerPlattform` laden statt `profiles(count)` die Rollen der Profile (`profiles(role)`) und liefern die Zählung mit. Liste und Detailseite zeigen dieselbe Zeile. + +**Tech Stack:** Next.js (Server Components), supabase-js (Service-Client), Vitest. + +**Spec:** Kleine Änderung, im Chat abgestimmt (2026-11-03/04): +- Alle angelegten Nutzer zählen, auch gesperrte und ReadOnly. +- Rollen mit 0 werden angezeigt. +- Anzeige in der Firmenliste `/plattform` und auf der Detailseite `/plattform/[id]`. +- Superadmin zählt nicht (hat keine Firma). +- Keine Preise, kein Export, keine Historie, keine Summenzeile über alle Firmen, keine DB-Änderung. + +## Global Constraints + +- Reihenfolge und Beschriftung: `Admin(s) · Disponent(en) · Fahrer · ReadOnly`, Singular bei genau 1 (`1 Admin`, `1 Disponent`), sonst Plural (`0 Admins`, `2 Disponenten`); `Fahrer` und `ReadOnly` unverändert. +- Plattform-Seiten weiterhin nur für Superadmin (bestehende `pruefeSuperadmin`-Prüfung bleibt unverändert). +- Mobile first: Zählzeile darf umbrechen, keine horizontale Scrollbar. + +## Review Focus + +- Firma ohne Nutzer → `0 Admins · 0 Disponenten · 0 Fahrer · 0 ReadOnly` (Test in Task 1). +- Profil mit unerwarteter Rolle (z. B. `superadmin` mit Firma) → wird ignoriert, kein Absturz (Test in Task 1). +- `profiles` ist `null` in der Antwort → wie leere Liste (Test in Task 1). +- Gesperrte Nutzer werden mitgezählt — passiert automatisch, da nur `profiles` gezählt wird (kein Filter auf Auth-Status). +- Lange Zeile auf dem Handy bricht sauber um (manuelle Sichtprüfung in Task 2). + +--- + +### Task 1: Rollen zählen und formatieren + +**Files:** +- Create: `web/lib/plattform/rollen.ts` +- Test: `web/lib/plattform/rollen.test.ts` + +**Interfaces:** +- Produces: + - `type RollenAnzahl = { admin: number; disponent: number; fahrer: number; readonly: number }` + - `zaehleRollen(profile: { role: string | null }[] | null | undefined): RollenAnzahl` + - `formatiereRollen(a: RollenAnzahl): string` + +- [ ] **Step 1: Failing tests schreiben** + - `zaehleRollen([{role:"admin"},{role:"fahrer"},{role:"fahrer"},{role:"readonly"}])` → `{admin:1,disponent:0,fahrer:2,readonly:1}` + - `zaehleRollen(null)` und `zaehleRollen([])` → alles 0 + - `zaehleRollen([{role:"superadmin"},{role:null}])` → alles 0 + - `formatiereRollen({admin:2,disponent:3,fahrer:12,readonly:1})` → `"2 Admins · 3 Disponenten · 12 Fahrer · 1 ReadOnly"` + - `formatiereRollen({admin:1,disponent:1,fahrer:1,readonly:0})` → `"1 Admin · 1 Disponent · 1 Fahrer · 0 ReadOnly"` +- [ ] **Step 2:** `npx vitest run lib/plattform/rollen.test.ts` (aus `web/`) → FAIL (Modul fehlt) +- [ ] **Step 3:** `web/lib/plattform/rollen.ts` implementieren +- [ ] **Step 4:** Test erneut → PASS +- [ ] **Step 5:** Commit `feat: Rollen pro Firma zaehlen und formatieren` + +### Task 2: Anzeige in Plattform-Liste und Detailseite + +**Files:** +- Modify: `web/lib/plattform/queries.ts` (`FirmaUebersicht`, `listFirmen`, `ladeFirmaFuerPlattform`) +- Modify: `web/app/plattform/page.tsx` +- Modify: `web/app/plattform/[id]/page.tsx` + +**Interfaces:** +- Consumes: `zaehleRollen`, `formatiereRollen`, `RollenAnzahl` aus Task 1 +- Produces: + - `FirmaUebersicht = { id; name; aktiv; rollen: RollenAnzahl }` (`nutzerAnzahl` entfällt) + - `ladeFirmaFuerPlattform(...)` → `{ id; name; aktiv; rollen: RollenAnzahl } | null` + +- [ ] **Step 1:** In beiden Queries `profiles(role)` statt `profiles(count)` bzw. zusätzlich selektieren, mit `zaehleRollen` in `rollen` umwandeln. +- [ ] **Step 2:** Liste: Zeile 1 Firmenname + Status (`aktiv`/`deaktiviert`), Zeile 2 `formatiereRollen(f.rollen)` klein (`text-sm`), umbrechend. +- [ ] **Step 3:** Detailseite: in der Status-Karte unter „Status: …“ die Zeile `Nutzer: `. +- [ ] **Step 4:** Aus `web/`: `npx tsc --noEmit`, `npm run lint`, `npm test` → alles grün. +- [ ] **Step 5:** Live gegen lokalen Stack: als Superadmin `/plattform` und `/plattform/` abrufen (curl mit Session oder Browser), Zahlen gegen + `select f.name, p.role, count(*) from profiles p join firmen f on f.id=p.firma_id group by 1,2` prüfen; Handybreite (375 px) ohne horizontales Scrollen. +- [ ] **Step 6:** Commit `feat: Plattform-Verwaltung zeigt Nutzer je Rolle pro Firma` diff --git a/web/lib/plattform/rollen.test.ts b/web/lib/plattform/rollen.test.ts new file mode 100644 index 0000000..a4668f1 --- /dev/null +++ b/web/lib/plattform/rollen.test.ts @@ -0,0 +1,43 @@ +import { describe, expect, it } from "vitest"; +import { formatiereRollen, zaehleRollen } from "./rollen"; + +const leer = { admin: 0, disponent: 0, fahrer: 0, readonly: 0 }; + +describe("zaehleRollen", () => { + it("zaehlt jede Rolle", () => { + expect(zaehleRollen([{ role: "admin" }, { role: "fahrer" }, { role: "fahrer" }, { role: "readonly" }])).toEqual({ + admin: 1, + disponent: 0, + fahrer: 2, + readonly: 1, + }); + }); + + it("leere Liste oder null ergibt ueberall 0", () => { + expect(zaehleRollen([])).toEqual(leer); + expect(zaehleRollen(null)).toEqual(leer); + expect(zaehleRollen(undefined)).toEqual(leer); + }); + + it("ignoriert unbekannte Rollen", () => { + expect(zaehleRollen([{ role: "superadmin" }, { role: null }])).toEqual(leer); + }); +}); + +describe("formatiereRollen", () => { + it("Plural", () => { + expect(formatiereRollen({ admin: 2, disponent: 3, fahrer: 12, readonly: 1 })).toBe( + "2 Admins · 3 Disponenten · 12 Fahrer · 1 ReadOnly" + ); + }); + + it("Singular bei genau 1", () => { + expect(formatiereRollen({ admin: 1, disponent: 1, fahrer: 1, readonly: 0 })).toBe( + "1 Admin · 1 Disponent · 1 Fahrer · 0 ReadOnly" + ); + }); + + it("Null bleibt Plural", () => { + expect(formatiereRollen(leer)).toBe("0 Admins · 0 Disponenten · 0 Fahrer · 0 ReadOnly"); + }); +}); diff --git a/web/lib/plattform/rollen.ts b/web/lib/plattform/rollen.ts new file mode 100644 index 0000000..3acaf01 --- /dev/null +++ b/web/lib/plattform/rollen.ts @@ -0,0 +1,24 @@ +// Nutzer je Rolle einer Firma, Grundlage fuer die rollenabhaengige Abrechnung. +// Gezaehlt werden alle angelegten Profile, auch gesperrte. +export type RollenAnzahl = { admin: number; disponent: number; fahrer: number; readonly: number }; + +const ROLLEN = ["admin", "disponent", "fahrer", "readonly"] as const; + +const BESCHRIFTUNG: Record = { + admin: ["Admin", "Admins"], + disponent: ["Disponent", "Disponenten"], + fahrer: ["Fahrer", "Fahrer"], + readonly: ["ReadOnly", "ReadOnly"], +}; + +export function zaehleRollen(profile: { role: string | null }[] | null | undefined): RollenAnzahl { + const anzahl: RollenAnzahl = { admin: 0, disponent: 0, fahrer: 0, readonly: 0 }; + for (const p of profile ?? []) { + if ((ROLLEN as readonly string[]).includes(p.role ?? "")) anzahl[p.role as keyof RollenAnzahl]++; + } + return anzahl; +} + +export function formatiereRollen(a: RollenAnzahl): string { + return ROLLEN.map((r) => `${a[r]} ${BESCHRIFTUNG[r][a[r] === 1 ? 0 : 1]}`).join(" · "); +} From 8daa1b0a407f40a2dca0671e40061ddfac401ef0 Mon Sep 17 00:00:00 2001 From: Jonny Date: Wed, 4 Nov 2026 11:59:23 +0100 Subject: [PATCH 2/2] feat: Plattform-Verwaltung zeigt Nutzer je Rolle pro Firma Co-Authored-By: Claude Opus 5.5 --- web/app/plattform/[id]/page.tsx | 4 +++- web/app/plattform/page.tsx | 10 ++++++---- web/lib/plattform/queries.ts | 27 +++++++++++++-------------- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/web/app/plattform/[id]/page.tsx b/web/app/plattform/[id]/page.tsx index de28a83..748c25e 100644 --- a/web/app/plattform/[id]/page.tsx +++ b/web/app/plattform/[id]/page.tsx @@ -4,6 +4,7 @@ import { createAdminClient } from "@/lib/supabase/admin"; import { loadProfile } from "@/lib/auth/profile"; import { pruefeSuperadmin } from "@/lib/plattform/superadmin"; import { ladeFirmaFuerPlattform, listFirmenAdmins } from "@/lib/plattform/queries"; +import { formatiereRollen } from "@/lib/plattform/rollen"; import { legeFirmenAdminAn, setzeAdminPasswort, setzeFirmaAktiv } from "../actions"; import { Alert } from "@/components/ui/Alert"; import { Button } from "@/components/ui/Button"; @@ -36,9 +37,10 @@ export default async function PlattformFirmaPage({ {error && {error}} -

+

Status: {firma.aktiv ? "aktiv" : deaktiviert}

+

Nutzer: {formatiereRollen(firma.rollen)}