forked from fahrPool/fahrPool
feat: Rollen pro Firma zaehlen und formatieren
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
5e73b0b507
commit
1592c2f9c8
|
|
@ -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: <formatiereRollen>`.
|
||||||
|
- [ ] **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/<id>` 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`
|
||||||
|
|
@ -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");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
@ -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<keyof RollenAnzahl, [einzahl: string, mehrzahl: string]> = {
|
||||||
|
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(" · ");
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue