refactor(db): finish Drizzle migration for remaining actions and services

Co-authored-by: Cursor <[email protected]>
This commit is contained in:
SimoandCursor committed 2026-08-01 13:27:59 +02:00
1 parent 22234fe102
commit 65b2fbee6a
19 files changed
+422 -342

No files matched your search

+12 -7
View File
@@ -1,7 +1,8 @@
import "server-only";
import { count, eq, sql } from "drizzle-orm";
import { env } from "@/env";
import { prisma } from "@/lib/prisma";
import { db, User } from "@/lib/db";
import { redis } from "@/lib/redis";
import { rcon } from "@/lib/services/rcon";
@@ -19,11 +20,11 @@ export type OpsHealth = {
* Semantics match `/api/health` for Redis (null when not configured).
*/
export async function fetchOpsHealth(): Promise<OpsHealth> {
const [db, redisOk, emulatorOk, onlineUsers] = await Promise.all([
const [dbProbe, redisOk, emulatorOk, onlineUsers] = await Promise.all([
(async () => {
try {
const start = Date.now();
await prisma.$queryRaw`SELECT 1`;
await db.execute(sql`SELECT 1`);
return { dbOk: true, dbLatencyMs: Date.now() - start };
} catch {
return { dbOk: false, dbLatencyMs: 0 };
@@ -40,12 +41,16 @@ export async function fetchOpsHealth(): Promise<OpsHealth> {
}
})(),
rcon.send("ping", null).catch(() => false),
prisma.user.count({ where: { online: "1" } }).catch(() => 0),
db
.select({ total: count() })
.from(User)
.where(eq(User.online, "1"))
.then((rows) => rows[0]?.total ?? 0)
.catch(() => 0),
]);
return {
dbOk: db.dbOk,
dbLatencyMs: db.dbLatencyMs,
dbOk: dbProbe.dbOk,
dbLatencyMs: dbProbe.dbLatencyMs,
redisOk,
emulatorOk: Boolean(emulatorOk),
onlineUsers,
+30 -16
View File
@@ -1,25 +1,39 @@
import { count, desc, eq } from "drizzle-orm";
import type { OnlineUser } from "@/components/admin/dashboard";
import { prisma } from "@/lib/prisma";
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 [count, onlineUsersRaw] = await Promise.all([
prisma.user.count({ where: { online: "1" } }).catch(() => 0),
prisma.user
.findMany({
where: { online: "1" },
select: {
id: true,
username: true,
look: true,
lastOnline: true,
},
orderBy: { lastOnline: "desc" },
take: limit,
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,
})
.catch(() => []),
.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;
}>,
),
]);
const users: OnlineUser[] = onlineUsersRaw.map((u) => ({
@@ -31,5 +45,5 @@ export async function fetchOpsOnlineUsers(
lastActivity: u.lastOnline > 0 ? new Date(u.lastOnline * 1000) : new Date(),
}));
return { count, users };
return { count: countRow, users };
}
+12 -5
View File
@@ -1,6 +1,7 @@
import "server-only";
import { prisma } from "@/lib/prisma";
import { count, eq, ne } from "drizzle-orm";
import { db, WebsiteHelpCenterTickets, WebsiteTicket } from "@/lib/db";
/** Open-queue sizes for dual ticket products (CMS desk vs help-center). */
export async function fetchTicketQueueOpenCounts(): Promise<{
@@ -8,11 +9,17 @@ export async function fetchTicketQueueOpenCounts(): Promise<{
helpOpen: number;
}> {
const [cmsOpen, helpOpen] = await Promise.all([
prisma.websiteTicket
.count({ where: { status: { not: "closed" } } })
db
.select({ total: count() })
.from(WebsiteTicket)
.where(ne(WebsiteTicket.status, "closed"))
.then((rows) => rows[0]?.total ?? 0)
.catch(() => 0),
prisma.websiteHelpCenterTickets
.count({ where: { open: true } })
db
.select({ total: count() })
.from(WebsiteHelpCenterTickets)
.where(eq(WebsiteHelpCenterTickets.open, true))
.then((rows) => rows[0]?.total ?? 0)
.catch(() => 0),
]);
return { cmsOpen, helpOpen };