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

Co-authored-by: Cursor <[email protected]>
This commit is contained in:
SimoandCursor committed 2026-08-01 14:15:39 +02:00
1 parent 9aa4f331bf
commit 30b54e99e7
40 files changed
+1183 -784

No files matched your search

+67 -53
View File
@@ -1,9 +1,29 @@
import {
and,
asc,
count,
desc,
eq,
inArray,
like,
or,
type SQL,
} from "drizzle-orm";
import { getTranslations } from "next-intl/server";
import { type CfhRow, CfhTable } from "@/app/admin/moderation/cfh/cfh-table";
import { requireModPermission } from "@/lib/admin/guard";
import { calcPagination, parseListParams } from "@/lib/admin-helpers";
import { db, SupportTickets, User } from "@/lib/db";
import { PERMS } from "@/lib/permissions";
import { prisma } from "@/lib/prisma";
const SORT_COLS = {
id: SupportTickets.id,
issue: SupportTickets.issue,
sender: SupportTickets.senderId,
reported: SupportTickets.reportedId,
mod: SupportTickets.modId,
state: SupportTickets.state,
} as const;
export default async function ModCfhListPage({
searchParams,
@@ -19,9 +39,9 @@ export default async function ModCfhListPage({
const stateFilter =
stateRaw !== undefined && stateRaw !== "" ? Number(stateRaw) : -1;
const conditions: any[] = [];
const conditions: SQL[] = [];
if (stateFilter >= 0) {
conditions.push({ state: stateFilter });
conditions.push(eq(SupportTickets.state, stateFilter));
}
if (parsed.search.trim()) {
@@ -29,63 +49,57 @@ export default async function ModCfhListPage({
const asId = Number(q);
let userIds: number[] = [];
try {
const users = await prisma.user.findMany({
where: { username: { contains: q } },
select: { id: true },
take: 50,
});
const users = await db
.select({ id: User.id })
.from(User)
.where(like(User.username, `%${q}%`))
.limit(50);
userIds = users.map((u) => u.id);
} catch {
userIds = [];
}
conditions.push({
OR: [
{ issue: { contains: q } },
...(Number.isFinite(asId) && asId > 0 ? [{ id: asId }] : []),
...(userIds.length
? [
{ senderId: { in: userIds } },
{ reportedId: { in: userIds } },
{ modId: { in: userIds } },
]
: []),
],
});
const ors: SQL[] = [like(SupportTickets.issue, `%${q}%`)];
if (Number.isFinite(asId) && asId > 0) {
ors.push(eq(SupportTickets.id, asId));
}
if (userIds.length) {
ors.push(inArray(SupportTickets.senderId, userIds));
ors.push(inArray(SupportTickets.reportedId, userIds));
ors.push(inArray(SupportTickets.modId, userIds));
}
conditions.push(or(...ors)!);
}
const where: any =
conditions.length === 0
? {}
: conditions.length === 1
? conditions[0]
: { AND: conditions };
const where = conditions.length > 0 ? and(...conditions) : undefined;
const SORT_MAP: Record<string, any> = {
id: { id: "desc" },
issue: { issue: "asc" },
sender: { senderId: "asc" },
reported: { reportedId: "asc" },
mod: { modId: "asc" },
state: { state: "asc" },
};
const sortKey =
parsed.sort && parsed.sort in SORT_COLS
? (parsed.sort as keyof typeof SORT_COLS)
: "id";
const sortCol = SORT_COLS[sortKey];
const finalOrder =
parsed.sort && parsed.sort in SORT_COLS
? parsed.order === "asc"
? asc(sortCol)
: desc(sortCol)
: desc(SupportTickets.id);
const baseOrder = SORT_MAP[parsed.sort ?? "id"] ?? { id: "desc" };
const orderField = Object.keys(baseOrder)[0] as keyof typeof baseOrder;
const orderBy = {
[orderField]: parsed.order,
} as any;
const total = await prisma.supportTickets.count({ where }).catch(() => 0);
const total = await db
.select({ total: count() })
.from(SupportTickets)
.where(where)
.then((rows) => rows[0]?.total ?? 0)
.catch(() => 0);
const pagination = calcPagination(total, parsed.page, parsed.perPage);
const tickets = await prisma.supportTickets
.findMany({
where,
orderBy,
skip: pagination.offset,
take: pagination.perPage,
})
const tickets = await db
.select()
.from(SupportTickets)
.where(where)
.orderBy(finalOrder)
.offset(pagination.offset)
.limit(pagination.perPage)
.catch(() => []);
const userIds = [
@@ -99,10 +113,10 @@ export default async function ModCfhListPage({
let userMap = new Map<number, string>();
if (userIds.length > 0) {
try {
const users = await prisma.user.findMany({
where: { id: { in: userIds } },
select: { id: true, username: true },
});
const users = await db
.select({ id: User.id, username: User.username })
.from(User)
.where(inArray(User.id, userIds));
userMap = new Map(users.map((u) => [u.id, u.username]));
} catch {
userMap = new Map();