1
0
Fork 0
fahrPool/web/lib/nutzerverwaltung/queries.ts

36 lines
1.0 KiB
TypeScript

import { createClient } from "@/lib/supabase/server";
import { createAdminClient } from "@/lib/supabase/admin";
import type { Role } from "@/lib/auth/roles";
export type Nutzer = {
id: string;
name: string;
email: string | null;
role: Role;
gesperrt: boolean;
};
export async function listNutzer(): Promise<Nutzer[]> {
const supabase = await createClient();
const adminClient = createAdminClient();
const [{ data: profiles }, { data: authData }] = await Promise.all([
supabase.from("profiles").select("id, name, role").order("name"),
adminClient.auth.admin.listUsers(),
]);
const authById = new Map((authData?.users ?? []).map((nutzer) => [nutzer.id, nutzer]));
return (profiles ?? []).map((profile) => {
const authUser = authById.get(profile.id);
const bannedUntil = authUser?.banned_until;
return {
id: profile.id,
name: profile.name,
email: authUser?.email ?? null,
role: profile.role as Role,
gesperrt: Boolean(bannedUntil && new Date(bannedUntil).getTime() > Date.now()),
};
});
}