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

+65 -54
View File
@@ -1,9 +1,20 @@
import {
and,
asc,
count,
desc,
eq,
inArray,
like,
or,
type SQL,
} from "drizzle-orm";
import { redirect } from "next/navigation";
import { getTranslations } from "next-intl/server";
import { calcPagination, parseListParams } from "@/lib/admin-helpers";
import { db, SupportTickets, User } from "@/lib/db";
import { formatDate } from "@/lib/format-date";
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
import { prisma } from "@/lib/prisma";
import { type CfhRow, CfhTable } from "./cfh-table";
export default async function CfhListPage({
@@ -23,9 +34,9 @@ export default async function CfhListPage({
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()) {
@@ -33,63 +44,63 @@ export default async function CfhListPage({
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 searchOr = [
like(SupportTickets.issue, `%${q}%`),
...(Number.isFinite(asId) && asId > 0
? [eq(SupportTickets.id, asId)]
: []),
...(userIds.length
? [
inArray(SupportTickets.senderId, userIds),
inArray(SupportTickets.reportedId, userIds),
inArray(SupportTickets.modId, userIds),
]
: []),
];
conditions.push(or(...searchOr)!);
}
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 orderCol =
parsed.sort === "issue"
? SupportTickets.issue
: parsed.sort === "sender"
? SupportTickets.senderId
: parsed.sort === "reported"
? SupportTickets.reportedId
: parsed.sort === "mod"
? SupportTickets.modId
: parsed.sort === "state"
? SupportTickets.state
: SupportTickets.id;
const orderBy = parsed.order === "asc" ? asc(orderCol) : desc(orderCol);
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 [totalRows] = await db
.select({ total: count() })
.from(SupportTickets)
.where(where)
.catch(() => [{ total: 0 }]);
const total = Number(totalRows?.total ?? 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(orderBy)
.limit(pagination.perPage)
.offset(pagination.offset)
.catch(() => []);
const userIds = [
@@ -98,15 +109,15 @@ export default async function CfhListPage({
...tickets.map((x) => x.reportedId),
...tickets.map((x) => x.modId),
]),
].filter(Boolean);
].filter(Boolean) as number[];
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();