forked from fahrPool/fahrPool
Merge feature/rollen-zaehlung: Nutzer je Rolle pro Firma in der Plattform-Verwaltung
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
ad2bc2da16
|
|
@ -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`
|
||||
|
|
@ -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 && <Alert>{error}</Alert>}
|
||||
|
||||
<Card>
|
||||
<p className="mb-4 text-asphalt-grey">
|
||||
<p className="text-asphalt-grey">
|
||||
Status: {firma.aktiv ? "aktiv" : <span className="text-status-danger">deaktiviert</span>}
|
||||
</p>
|
||||
<p className="mb-4 text-asphalt-grey">Nutzer: {formatiereRollen(firma.rollen)}</p>
|
||||
<form action={setzeFirmaAktiv.bind(null, firma.id, !firma.aktiv)}>
|
||||
<Button type="submit" variant={firma.aktiv ? "destructive" : "primary"}>
|
||||
{firma.aktiv ? "Firma deaktivieren" : "Firma aktivieren"}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import { createAdminClient } from "@/lib/supabase/admin";
|
|||
import { loadProfile } from "@/lib/auth/profile";
|
||||
import { pruefeSuperadmin } from "@/lib/plattform/superadmin";
|
||||
import { listFirmen } from "@/lib/plattform/queries";
|
||||
import { formatiereRollen } from "@/lib/plattform/rollen";
|
||||
import { legeFirmaAn } from "./actions";
|
||||
import { InstallHinweis } from "@/components/pwa/InstallHinweis";
|
||||
import { Alert } from "@/components/ui/Alert";
|
||||
|
|
@ -29,11 +30,12 @@ export default async function PlattformPage({ searchParams }: { searchParams: Pr
|
|||
<ul className="flex flex-col divide-y divide-aluminium-grey">
|
||||
{firmen.map((f) => (
|
||||
<li key={f.id}>
|
||||
<Link href={`/plattform/${f.id}`} className="flex min-h-11 items-center justify-between gap-4 py-2 text-asphalt-grey hover:text-performance-blue">
|
||||
<span className="font-medium">{f.name}</span>
|
||||
<span className="text-sm">
|
||||
{f.nutzerAnzahl} Nutzer · {f.aktiv ? "aktiv" : <span className="text-status-danger">deaktiviert</span>}
|
||||
<Link href={`/plattform/${f.id}`} className="flex min-h-11 flex-col justify-center gap-1 py-2 text-asphalt-grey hover:text-performance-blue">
|
||||
<span className="flex items-center justify-between gap-4">
|
||||
<span className="font-medium">{f.name}</span>
|
||||
<span className="text-sm">{f.aktiv ? "aktiv" : <span className="text-status-danger">deaktiviert</span>}</span>
|
||||
</span>
|
||||
<span className="text-sm">{formatiereRollen(f.rollen)}</span>
|
||||
</Link>
|
||||
</li>
|
||||
))}
|
||||
|
|
|
|||
|
|
@ -1,26 +1,25 @@
|
|||
import type { SupabaseClient } from "@supabase/supabase-js";
|
||||
import { zaehleRollen, type RollenAnzahl } from "./rollen";
|
||||
|
||||
export type FirmaUebersicht = { id: string; name: string; aktiv: boolean; nutzerAnzahl: number };
|
||||
export type FirmaUebersicht = { id: string; name: string; aktiv: boolean; rollen: RollenAnzahl };
|
||||
export type FirmenAdmin = { id: string; name: string; username: string; email: string | null };
|
||||
|
||||
type FirmaZeile = { id: string; name: string; aktiv: boolean; profiles: { role: string | null }[] | null };
|
||||
|
||||
function zuUebersicht(f: FirmaZeile): FirmaUebersicht {
|
||||
return { id: f.id, name: f.name, aktiv: f.aktiv, rollen: zaehleRollen(f.profiles) };
|
||||
}
|
||||
|
||||
// Nur mit dem Service-Client aufrufen.
|
||||
export async function listFirmen(admin: SupabaseClient): Promise<FirmaUebersicht[]> {
|
||||
const { data, error } = await admin.from("firmen").select("id, name, aktiv, profiles(count)").order("name");
|
||||
const { data, error } = await admin.from("firmen").select("id, name, aktiv, profiles(role)").order("name");
|
||||
if (error) throw new Error(`Firmen laden fehlgeschlagen: ${error.message}`);
|
||||
return (data ?? []).map((f) => ({
|
||||
id: f.id as string,
|
||||
name: f.name as string,
|
||||
aktiv: f.aktiv as boolean,
|
||||
nutzerAnzahl: ((f.profiles as { count: number }[] | null)?.[0]?.count ?? 0) as number,
|
||||
}));
|
||||
return ((data ?? []) as FirmaZeile[]).map(zuUebersicht);
|
||||
}
|
||||
|
||||
export async function ladeFirmaFuerPlattform(
|
||||
admin: SupabaseClient,
|
||||
firmaId: string
|
||||
): Promise<{ id: string; name: string; aktiv: boolean } | null> {
|
||||
const { data } = await admin.from("firmen").select("id, name, aktiv").eq("id", firmaId).maybeSingle();
|
||||
return (data as { id: string; name: string; aktiv: boolean } | null) ?? null;
|
||||
export async function ladeFirmaFuerPlattform(admin: SupabaseClient, firmaId: string): Promise<FirmaUebersicht | null> {
|
||||
const { data } = await admin.from("firmen").select("id, name, aktiv, profiles(role)").eq("id", firmaId).maybeSingle();
|
||||
return data ? zuUebersicht(data as FirmaZeile) : null;
|
||||
}
|
||||
|
||||
export async function listFirmenAdmins(admin: SupabaseClient, firmaId: string): Promise<FirmenAdmin[]> {
|
||||
|
|
|
|||
|
|
@ -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