refactor(db): migrate app pages and APIs from Prisma facade to Drizzle (4)
Co-authored-by: Cursor <[email protected]>
This commit is contained in:
1 parent
580972c0a0
commit
9aa4f331bf
40 files changed
+1259
-927
No files matched your search
@@ -1,9 +1,10 @@
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { revalidateTag } from "next/cache";
|
||||
import { NextResponse } from "next/server";
|
||||
import { withAdmin } from "@/lib/api-handler";
|
||||
import { apiError } from "@/lib/api-response";
|
||||
import { AclModelRole, db } from "@/lib/db";
|
||||
import { PERMS } from "@/lib/permissions";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export const POST = withAdmin(
|
||||
{ permission: PERMS.PERMISSIONS_MANAGE },
|
||||
@@ -20,16 +21,32 @@ export const POST = withAdmin(
|
||||
return apiError("Valid userId and roleId required", 400);
|
||||
}
|
||||
if (body.action === "remove") {
|
||||
await prisma.aclModelRole.deleteMany({
|
||||
where: { modelType: "User", modelId: userId, roleId },
|
||||
});
|
||||
await db
|
||||
.delete(AclModelRole)
|
||||
.where(
|
||||
and(
|
||||
eq(AclModelRole.modelType, "User"),
|
||||
eq(AclModelRole.modelId, userId),
|
||||
eq(AclModelRole.roleId, roleId),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
const existing = await prisma.aclModelRole.findFirst({
|
||||
where: { modelType: "User", modelId: userId, roleId },
|
||||
});
|
||||
const [existing] = await db
|
||||
.select({ modelId: AclModelRole.modelId })
|
||||
.from(AclModelRole)
|
||||
.where(
|
||||
and(
|
||||
eq(AclModelRole.modelType, "User"),
|
||||
eq(AclModelRole.modelId, userId),
|
||||
eq(AclModelRole.roleId, roleId),
|
||||
),
|
||||
)
|
||||
.limit(1);
|
||||
if (!existing)
|
||||
await prisma.aclModelRole.create({
|
||||
data: { modelType: "User", modelId: userId, roleId },
|
||||
await db.insert(AclModelRole).values({
|
||||
modelType: "User",
|
||||
modelId: userId,
|
||||
roleId,
|
||||
});
|
||||
}
|
||||
revalidateTag("permissions", { expire: 0 });
|
||||
|
||||
Reference in new issue
Block a user