fix(housekeeping): harden system workflow boundaries
This commit is contained in:
1 parent
3788ecd9f1
commit
c325c53774
18 files changed
+809
-79
No files matched your search
@@ -1,41 +1,40 @@
|
||||
import "server-only";
|
||||
|
||||
import { count, desc, eq } from "drizzle-orm";
|
||||
import type { OnlineUser } from "@/components/admin/dashboard";
|
||||
import { db, User } from "@/lib/db";
|
||||
|
||||
/** Shared online roster used by CommandoCentrum (and linked from other ops hubs). */
|
||||
export async function fetchOpsOnlineUsers(
|
||||
limit = 40,
|
||||
): Promise<{ count: number; users: OnlineUser[] }> {
|
||||
const [countRow, onlineUsersRaw] = await Promise.all([
|
||||
db
|
||||
.select({ total: count() })
|
||||
.from(User)
|
||||
.where(eq(User.online, "1"))
|
||||
.then((rows) => rows[0]?.total ?? 0)
|
||||
.catch(() => 0),
|
||||
db
|
||||
.select({
|
||||
id: User.id,
|
||||
username: User.username,
|
||||
look: User.look,
|
||||
lastOnline: User.lastOnline,
|
||||
})
|
||||
.from(User)
|
||||
.where(eq(User.online, "1"))
|
||||
.orderBy(desc(User.lastOnline))
|
||||
.limit(limit)
|
||||
.then((rows) => rows)
|
||||
.catch(
|
||||
() =>
|
||||
[] as Array<{
|
||||
id: number;
|
||||
username: string;
|
||||
look: string;
|
||||
lastOnline: number;
|
||||
}>,
|
||||
),
|
||||
]);
|
||||
interface OpsOnlineUserRow {
|
||||
readonly id: number;
|
||||
readonly username: string;
|
||||
readonly look: string;
|
||||
readonly lastOnline: number;
|
||||
}
|
||||
|
||||
function loadOnlineCount(): Promise<number> {
|
||||
return db
|
||||
.select({ total: count() })
|
||||
.from(User)
|
||||
.where(eq(User.online, "1"))
|
||||
.then((rows) => rows[0]?.total ?? 0);
|
||||
}
|
||||
|
||||
function loadOnlineRows(limit: number): Promise<OpsOnlineUserRow[]> {
|
||||
return db
|
||||
.select({
|
||||
id: User.id,
|
||||
username: User.username,
|
||||
look: User.look,
|
||||
lastOnline: User.lastOnline,
|
||||
})
|
||||
.from(User)
|
||||
.where(eq(User.online, "1"))
|
||||
.orderBy(desc(User.lastOnline))
|
||||
.limit(limit)
|
||||
.then((rows) => rows);
|
||||
}
|
||||
|
||||
function onlineRoster(countRow: number, onlineUsersRaw: OpsOnlineUserRow[]) {
|
||||
const users: OnlineUser[] = onlineUsersRaw.map((u) => ({
|
||||
id: u.id,
|
||||
username: u.username,
|
||||
@@ -47,3 +46,25 @@ export async function fetchOpsOnlineUsers(
|
||||
|
||||
return { count: countRow, users };
|
||||
}
|
||||
|
||||
/** Fail-aware roster for guarded System workflows. */
|
||||
export async function fetchOpsOnlineUsersStrict(
|
||||
limit = 40,
|
||||
): Promise<{ count: number; users: OnlineUser[] }> {
|
||||
const [countRow, onlineUsersRaw] = await Promise.all([
|
||||
loadOnlineCount(),
|
||||
loadOnlineRows(limit),
|
||||
]);
|
||||
return onlineRoster(countRow, onlineUsersRaw);
|
||||
}
|
||||
|
||||
/** Shared tolerant roster used by the legacy CommandoCentrum. */
|
||||
export async function fetchOpsOnlineUsers(
|
||||
limit = 40,
|
||||
): Promise<{ count: number; users: OnlineUser[] }> {
|
||||
const [countRow, onlineUsersRaw] = await Promise.all([
|
||||
loadOnlineCount().catch(() => 0),
|
||||
loadOnlineRows(limit).catch(() => []),
|
||||
]);
|
||||
return onlineRoster(countRow, onlineUsersRaw);
|
||||
}
|
||||
Reference in new issue
Block a user