refactor(db): migrate staff and app actions from Prisma facade to Drizzle
Co-authored-by: Cursor <[email protected]>
This commit is contained in:
1 parent
9854719cfd
commit
ed9c23c702
85 files changed
+2612
-1832
No files matched your search
+102
-72
@@ -1,9 +1,17 @@
|
||||
"use server";
|
||||
|
||||
import { and, eq, or } from "drizzle-orm";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { redirect } from "next/navigation";
|
||||
import { auth } from "@/lib/auth";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import {
|
||||
db,
|
||||
Guilds,
|
||||
GuildsForumsComments,
|
||||
GuildsForumsThreads,
|
||||
MessengerFriendrequests,
|
||||
MessengerFriendships,
|
||||
} from "@/lib/db";
|
||||
import { clientIp, rateLimit } from "@/lib/rate-limit";
|
||||
|
||||
// Guild forum subjects are VARCHAR(255); the comment/message body lives in
|
||||
@@ -117,25 +125,44 @@ export async function sendFriendRequest(formData: FormData): Promise<void> {
|
||||
outcome = "self";
|
||||
} else {
|
||||
// Guard against duplicate pending requests and already-existing friendships.
|
||||
const [outgoingRequest, incomingRequest, existingFriendship] =
|
||||
const [[outgoingRequest], [incomingRequest], [existingFriendship]] =
|
||||
await Promise.all([
|
||||
prisma.messengerFriendrequests.findFirst({
|
||||
where: { userFromId: fromId, userToId: toId },
|
||||
select: { id: true },
|
||||
}),
|
||||
prisma.messengerFriendrequests.findFirst({
|
||||
where: { userFromId: toId, userToId: fromId },
|
||||
select: { id: true },
|
||||
}),
|
||||
prisma.messengerFriendships.findFirst({
|
||||
where: {
|
||||
OR: [
|
||||
{ userOneId: fromId, userTwoId: toId },
|
||||
{ userOneId: toId, userTwoId: fromId },
|
||||
],
|
||||
},
|
||||
select: { id: true },
|
||||
}),
|
||||
db
|
||||
.select({ id: MessengerFriendrequests.id })
|
||||
.from(MessengerFriendrequests)
|
||||
.where(
|
||||
and(
|
||||
eq(MessengerFriendrequests.userFromId, fromId),
|
||||
eq(MessengerFriendrequests.userToId, toId),
|
||||
),
|
||||
)
|
||||
.limit(1),
|
||||
db
|
||||
.select({ id: MessengerFriendrequests.id })
|
||||
.from(MessengerFriendrequests)
|
||||
.where(
|
||||
and(
|
||||
eq(MessengerFriendrequests.userFromId, toId),
|
||||
eq(MessengerFriendrequests.userToId, fromId),
|
||||
),
|
||||
)
|
||||
.limit(1),
|
||||
db
|
||||
.select({ id: MessengerFriendships.id })
|
||||
.from(MessengerFriendships)
|
||||
.where(
|
||||
or(
|
||||
and(
|
||||
eq(MessengerFriendships.userOneId, fromId),
|
||||
eq(MessengerFriendships.userTwoId, toId),
|
||||
),
|
||||
and(
|
||||
eq(MessengerFriendships.userOneId, toId),
|
||||
eq(MessengerFriendships.userTwoId, fromId),
|
||||
),
|
||||
),
|
||||
)
|
||||
.limit(1),
|
||||
]);
|
||||
|
||||
if (existingFriendship) {
|
||||
@@ -147,8 +174,9 @@ export async function sendFriendRequest(formData: FormData): Promise<void> {
|
||||
// creating a duplicate reverse row.
|
||||
outcome = "incoming_pending";
|
||||
} else {
|
||||
await prisma.messengerFriendrequests.create({
|
||||
data: { userFromId: fromId, userToId: toId },
|
||||
await db.insert(MessengerFriendrequests).values({
|
||||
userFromId: fromId,
|
||||
userToId: toId,
|
||||
});
|
||||
outcome = "sent";
|
||||
}
|
||||
@@ -210,39 +238,36 @@ export async function postThread(formData: FormData): Promise<void> {
|
||||
} else {
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
|
||||
const guild = await prisma.guilds.findUnique({
|
||||
where: { id: guildId },
|
||||
select: { id: true },
|
||||
});
|
||||
const [guild] = await db
|
||||
.select({ id: Guilds.id })
|
||||
.from(Guilds)
|
||||
.where(eq(Guilds.id, guildId))
|
||||
.limit(1);
|
||||
if (!guild) {
|
||||
outcome = "not_found";
|
||||
} else {
|
||||
await prisma.$transaction(async (tx) => {
|
||||
const thread = await tx.guildsForumsThreads.create({
|
||||
data: {
|
||||
guildId,
|
||||
openerId,
|
||||
subject,
|
||||
postsCount: 1,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
state: 0,
|
||||
pinned: 0,
|
||||
locked: 0,
|
||||
adminId: 0,
|
||||
},
|
||||
select: { id: true },
|
||||
await db.transaction(async (tx) => {
|
||||
const [result] = await tx.insert(GuildsForumsThreads).values({
|
||||
guildId,
|
||||
openerId,
|
||||
subject,
|
||||
postsCount: 1,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
state: 0,
|
||||
pinned: 0,
|
||||
locked: 0,
|
||||
adminId: 0,
|
||||
});
|
||||
const threadId = Number(result.insertId);
|
||||
|
||||
await tx.guildsForumsComments.create({
|
||||
data: {
|
||||
threadId: thread.id,
|
||||
userId: openerId,
|
||||
message,
|
||||
createdAt: now,
|
||||
state: 0,
|
||||
adminId: 0,
|
||||
},
|
||||
await tx.insert(GuildsForumsComments).values({
|
||||
threadId,
|
||||
userId: openerId,
|
||||
message,
|
||||
createdAt: now,
|
||||
state: 0,
|
||||
adminId: 0,
|
||||
});
|
||||
});
|
||||
outcome = "posted";
|
||||
@@ -304,39 +329,44 @@ export async function replyToThread(formData: FormData): Promise<void> {
|
||||
} else {
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
|
||||
const thread = await prisma.guildsForumsThreads.findFirst({
|
||||
where: { id: threadId, guildId, state: 0 },
|
||||
select: {
|
||||
id: true,
|
||||
locked: true,
|
||||
postsCount: true,
|
||||
},
|
||||
});
|
||||
const [thread] = await db
|
||||
.select({
|
||||
id: GuildsForumsThreads.id,
|
||||
locked: GuildsForumsThreads.locked,
|
||||
postsCount: GuildsForumsThreads.postsCount,
|
||||
})
|
||||
.from(GuildsForumsThreads)
|
||||
.where(
|
||||
and(
|
||||
eq(GuildsForumsThreads.id, threadId),
|
||||
eq(GuildsForumsThreads.guildId, guildId),
|
||||
eq(GuildsForumsThreads.state, 0),
|
||||
),
|
||||
)
|
||||
.limit(1);
|
||||
|
||||
if (!thread) {
|
||||
outcome = "not_found";
|
||||
} else if (thread.locked) {
|
||||
outcome = "locked";
|
||||
} else {
|
||||
await prisma.$transaction(async (tx) => {
|
||||
await tx.guildsForumsComments.create({
|
||||
data: {
|
||||
threadId: thread.id,
|
||||
userId,
|
||||
message,
|
||||
createdAt: now,
|
||||
state: 0,
|
||||
adminId: 0,
|
||||
},
|
||||
await db.transaction(async (tx) => {
|
||||
await tx.insert(GuildsForumsComments).values({
|
||||
threadId: thread.id,
|
||||
userId,
|
||||
message,
|
||||
createdAt: now,
|
||||
state: 0,
|
||||
adminId: 0,
|
||||
});
|
||||
|
||||
await tx.guildsForumsThreads.update({
|
||||
where: { id: thread.id },
|
||||
data: {
|
||||
await tx
|
||||
.update(GuildsForumsThreads)
|
||||
.set({
|
||||
postsCount: (thread.postsCount ?? 0) + 1,
|
||||
updatedAt: now,
|
||||
},
|
||||
});
|
||||
})
|
||||
.where(eq(GuildsForumsThreads.id, thread.id));
|
||||
});
|
||||
outcome = "replied";
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user