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

Co-authored-by: Cursor <[email protected]>
This commit is contained in:
SimoandCursor committed 2026-08-01 14:15:36 +02:00
1 parent 580972c0a0
commit 9aa4f331bf
40 files changed
+1259 -927

No files matched your search

+88 -75
View File
@@ -1,6 +1,15 @@
import { and, desc, eq, inArray, or } from "drizzle-orm";
import { isBanActive, unixNow } from "@/lib/bans";
import {
AdminAuditLog,
Ban,
db,
StaffActivities,
SupportTickets,
User,
UsersSettings,
} from "@/lib/db";
import { formatDate } from "@/lib/format-date";
import { prisma } from "@/lib/prisma";
export type SanctionEvent = {
id: string;
@@ -37,84 +46,89 @@ export async function loadUserSanctions(
const now = unixNow();
const events: SanctionEvent[] = [];
const [bans, audits, staffActs, cfh, settings] = await Promise.all([
prisma.ban
.findMany({
where: { userId },
orderBy: { timestamp: "desc" },
take: 40,
select: {
id: true,
banReason: true,
type: true,
timestamp: true,
banExpire: true,
userStaffId: true,
},
const [bans, audits, staffActs, cfh, settingsRows] = await Promise.all([
db
.select({
id: Ban.id,
banReason: Ban.banReason,
type: Ban.type,
timestamp: Ban.timestamp,
banExpire: Ban.banExpire,
userStaffId: Ban.userStaffId,
})
.from(Ban)
.where(eq(Ban.userId, userId))
.orderBy(desc(Ban.timestamp))
.limit(40)
.catch(() => []),
prisma.adminAuditLog
.findMany({
where: {
target: "User",
targetId: userId,
action: { in: [...AUDIT_ACTIONS] },
},
orderBy: { id: "desc" },
take: 40,
select: {
id: true,
action: true,
details: true,
createdAt: true,
userId: true,
user: { select: { username: true } },
},
db
.select({
id: AdminAuditLog.id,
action: AdminAuditLog.action,
details: AdminAuditLog.details,
createdAt: AdminAuditLog.createdAt,
userId: AdminAuditLog.userId,
actorUsername: User.username,
})
.from(AdminAuditLog)
.leftJoin(User, eq(AdminAuditLog.userId, User.id))
.where(
and(
eq(AdminAuditLog.target, "User"),
eq(AdminAuditLog.targetId, userId),
inArray(AdminAuditLog.action, [...AUDIT_ACTIONS]),
),
)
.orderBy(desc(AdminAuditLog.id))
.limit(40)
.catch(() => []),
prisma.staffActivities
.findMany({
where: {
targetType: "user",
targetId: BigInt(userId),
},
orderBy: { id: "desc" },
take: 40,
select: {
id: true,
action: true,
description: true,
createdAt: true,
userId: true,
},
db
.select({
id: StaffActivities.id,
action: StaffActivities.action,
description: StaffActivities.description,
createdAt: StaffActivities.createdAt,
userId: StaffActivities.userId,
})
.from(StaffActivities)
.where(
and(
eq(StaffActivities.targetType, "user"),
eq(StaffActivities.targetId, BigInt(userId)),
),
)
.orderBy(desc(StaffActivities.id))
.limit(40)
.catch(() => []),
prisma.supportTickets
.findMany({
where: {
OR: [{ reportedId: userId }, { senderId: userId }],
},
orderBy: { id: "desc" },
take: 20,
select: {
id: true,
issue: true,
state: true,
timestamp: true,
senderId: true,
reportedId: true,
modId: true,
},
db
.select({
id: SupportTickets.id,
issue: SupportTickets.issue,
state: SupportTickets.state,
timestamp: SupportTickets.timestamp,
senderId: SupportTickets.senderId,
reportedId: SupportTickets.reportedId,
modId: SupportTickets.modId,
})
.from(SupportTickets)
.where(
or(
eq(SupportTickets.reportedId, userId),
eq(SupportTickets.senderId, userId),
),
)
.orderBy(desc(SupportTickets.id))
.limit(20)
.catch(() => []),
db
.select({ muteEndTimestamp: UsersSettings.muteEndTimestamp })
.from(UsersSettings)
.where(eq(UsersSettings.userId, userId))
.limit(1)
.catch(() => []),
prisma.usersSettings
.findUnique({
where: { userId },
select: { muteEndTimestamp: true },
})
.catch(() => null),
]);
const settings = settingsRows[0] ?? null;
const muteUntil = settings?.muteEndTimestamp ?? 0;
if (muteUntil > now) {
events.push({
@@ -134,11 +148,10 @@ export async function loadUserSanctions(
];
const staffUsers =
staffIds.length > 0
? await prisma.user
.findMany({
where: { id: { in: staffIds } },
select: { id: true, username: true },
})
? await db
.select({ id: User.id, username: User.username })
.from(User)
.where(inArray(User.id, staffIds))
.catch(() => [])
: [];
const staffName = new Map(staffUsers.map((u) => [u.id, u.username]));
@@ -172,7 +185,7 @@ export async function loadUserSanctions(
kind,
label: a.action,
detail: a.details?.slice(0, 200) || "—",
actor: a.user?.username,
actor: a.actorUsername ?? undefined,
});
}