47 lines
1.3 KiB
TypeScript
47 lines
1.3 KiB
TypeScript
/**
|
|
* Admin read models — the people list behind /admin and /roster.
|
|
*
|
|
* Server-only data layer (no JSX). The admin page is a server component that
|
|
* fetches these rows and passes serialisable props to the client action
|
|
* panels, so the roster and the admin table never disagree about a user.
|
|
*/
|
|
|
|
import { sql } from "drizzle-orm";
|
|
import { db } from "@/db";
|
|
import { users } from "@/db/schema";
|
|
|
|
const userName = sql<string>`coalesce(${users.displayName}, ${users.firstName} || ' ' || ${users.lastName})`;
|
|
|
|
export interface AdminUserRow {
|
|
id: string;
|
|
email: string;
|
|
displayName: string;
|
|
systemRole: string;
|
|
status: string;
|
|
}
|
|
|
|
/**
|
|
* Every user, with the ones that need attention first: pending invites, then
|
|
* invited, active, inactive, and finally denied. Alphabetical within a group.
|
|
*/
|
|
export async function listUsers(): Promise<AdminUserRow[]> {
|
|
const statusRank = sql`CASE ${users.status}
|
|
WHEN 'pending_approval' THEN 0
|
|
WHEN 'invited' THEN 1
|
|
WHEN 'active' THEN 2
|
|
WHEN 'inactive' THEN 3
|
|
ELSE 4
|
|
END`;
|
|
|
|
return db
|
|
.select({
|
|
id: users.id,
|
|
email: users.email,
|
|
displayName: userName,
|
|
systemRole: users.systemRole,
|
|
status: users.status,
|
|
})
|
|
.from(users)
|
|
.orderBy(statusRank, users.lastName, users.firstName);
|
|
}
|