refactor(db): migrate app pages and APIs from Prisma facade to Drizzle (3)

Co-authored-by: Cursor <[email protected]>
This commit is contained in:
SimoandCursor committed 2026-08-01 14:15:32 +02:00
1 parent 2cd0863cb8
commit 580972c0a0
40 files changed
+1089 -808

No files matched your search

+61 -35
View File
@@ -1,11 +1,18 @@
import { and, count, desc, gte, inArray, like } from "drizzle-orm";
import Link from "next/link";
import { redirect } from "next/navigation";
import { AvatarImage } from "@/components/shared/avatar-image";
import { Badge } from "@/components/ui/badge";
import { Card, CardContent } from "@/components/ui/card";
import { getMinStaffRank } from "@/lib/admin/min-staff-rank";
import {
AdminAuditLog,
db,
SupportTickets,
User,
WebsiteTicket,
} from "@/lib/db";
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
import { prisma } from "@/lib/prisma";
export default async function ModTeamPage() {
const { session, permissions } = await getAdminContext();
@@ -13,48 +20,67 @@ export default async function ModTeamPage() {
redirect("/admin");
const minStaffRank = await getMinStaffRank();
const staff = await prisma.user.findMany({
where: { rank: { gte: minStaffRank } },
select: {
id: true,
username: true,
look: true,
rank: true,
online: true,
lastLogin: true,
lastOnline: true,
},
orderBy: [{ online: "desc" }, { rank: "desc" }],
});
const staff = await db
.select({
id: User.id,
username: User.username,
look: User.look,
rank: User.rank,
online: User.online,
lastLogin: User.lastLogin,
lastOnline: User.lastOnline,
})
.from(User)
.where(gte(User.rank, minStaffRank))
.orderBy(desc(User.online), desc(User.rank));
const staffIds = staff.map((s) => s.id);
// Get CFH ticket counts per mod and web ticket counts
const [cfhCounts, webTicketCounts, modActionCounts] = await Promise.all([
prisma.supportTickets.groupBy({
by: ["modId"],
where: { modId: { in: staff.map((s) => s.id) } },
_count: { _all: true },
}),
prisma.websiteTicket.groupBy({
by: ["assigneeId"],
where: { assigneeId: { in: staff.map((s) => s.id) } },
_count: { _all: true },
}),
prisma.adminAuditLog.groupBy({
by: ["userId"],
where: {
userId: { in: staff.map((s) => s.id) },
action: { startsWith: "mod_" },
},
_count: { _all: true },
}),
staffIds.length
? db
.select({
modId: SupportTickets.modId,
total: count(),
})
.from(SupportTickets)
.where(inArray(SupportTickets.modId, staffIds))
.groupBy(SupportTickets.modId)
: Promise.resolve([]),
staffIds.length
? db
.select({
assigneeId: WebsiteTicket.assigneeId,
total: count(),
})
.from(WebsiteTicket)
.where(inArray(WebsiteTicket.assigneeId, staffIds))
.groupBy(WebsiteTicket.assigneeId)
: Promise.resolve([]),
staffIds.length
? db
.select({
userId: AdminAuditLog.userId,
total: count(),
})
.from(AdminAuditLog)
.where(
and(
inArray(AdminAuditLog.userId, staffIds),
like(AdminAuditLog.action, "mod_%"),
),
)
.groupBy(AdminAuditLog.userId)
: Promise.resolve([]),
]);
const cfhMap = new Map(cfhCounts.map((c) => [c.modId, c._count._all]));
const cfhMap = new Map(cfhCounts.map((c) => [c.modId, Number(c.total)]));
const webMap = new Map(
webTicketCounts.map((c) => [c.assigneeId, c._count._all]),
webTicketCounts.map((c) => [c.assigneeId, Number(c.total)]),
);
const actionMap = new Map(
modActionCounts.map((c) => [c.userId, c._count._all]),
modActionCounts.map((c) => [c.userId, Number(c.total)]),
);
return (