forked from fahrPool/fahrPool
feat: add Nutzerliste query (merges profiles with auth.admin.listUsers)
This commit is contained in:
parent
371312fc89
commit
3013a7d52c
|
|
@ -0,0 +1,35 @@
|
|||
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()),
|
||||
};
|
||||
});
|
||||
}
|
||||
Loading…
Reference in New Issue