fix: restore complete admin feature dependencies
This commit is contained in:
1 parent
5b4228261a
commit
0cd753c735
115 files changed
+18565
-4930
No files matched your search
@@ -1,73 +1,69 @@
|
||||
import { notFound } from 'next/navigation'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { notFound } from "next/navigation";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export async function loadUserById(id: string) {
|
||||
const numericId = parseInt(id, 10)
|
||||
const isNumeric = !Number.isNaN(numericId) && String(numericId) === id
|
||||
const numericId = Number(id);
|
||||
const isNumeric = Number.isSafeInteger(numericId) && numericId > 0 && String(numericId) === id;
|
||||
const user = await prisma.user.findUnique({ where: isNumeric ? { id: numericId } : { username: id } });
|
||||
if (!user) notFound();
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: isNumeric ? { id: numericId } : { username: id },
|
||||
include: {
|
||||
permission: true,
|
||||
settings: true,
|
||||
currencies: true,
|
||||
badges: true,
|
||||
bansReceived: {
|
||||
include: { staff: { select: { username: true } } },
|
||||
orderBy: { id: 'desc' },
|
||||
take: 5,
|
||||
},
|
||||
},
|
||||
})
|
||||
const [currencies, badges, bansReceived, permissionRows, roomCount, tradeCount, userRooms, chatLogs, commandLogs] =
|
||||
await Promise.all([
|
||||
prisma.usersCurrency.findMany({ where: { userId: user.id } }),
|
||||
prisma.usersBadges.findMany({ where: { userId: user.id }, orderBy: { slotId: "asc" } }),
|
||||
prisma.$queryRaw<Array<{ id: number; banReason: string; type: string; timestamp: number; banExpire: number; staffName: string }>>`
|
||||
SELECT b.id, b.ban_reason AS banReason, b.type, b.timestamp, b.ban_expire AS banExpire,
|
||||
COALESCE(s.username, 'Unknown') AS staffName
|
||||
FROM bans b LEFT JOIN users s ON s.id = b.user_staff_id
|
||||
WHERE b.user_id = ${user.id} ORDER BY b.id DESC LIMIT 5`,
|
||||
prisma.$queryRaw<Array<{ rankName: string }>>`
|
||||
SELECT COALESCE(rank_name, CONCAT('Rank ', id)) AS rankName FROM permissions WHERE id = ${user.rank} LIMIT 1`,
|
||||
prisma.rooms.count({ where: { ownerId: user.id } }),
|
||||
prisma.roomTradeLog.count({ where: { OR: [{ userOneId: user.id }, { userTwoId: user.id }] } }),
|
||||
prisma.rooms.findMany({
|
||||
where: { ownerId: user.id },
|
||||
orderBy: { users: "desc" },
|
||||
take: 20,
|
||||
select: { id: true, name: true, state: true, users: true, usersMax: true },
|
||||
}),
|
||||
prisma.$queryRaw<Array<{ id: number; roomId: number; timestamp: number; message: string }>>`
|
||||
SELECT ROW_NUMBER() OVER (ORDER BY timestamp DESC) AS id, room_id AS roomId, timestamp, message
|
||||
FROM chatlogs_room WHERE user_from_id = ${user.id} ORDER BY timestamp DESC LIMIT 20`,
|
||||
prisma.$queryRaw<Array<{ id: number; timestamp: number; command: string; params: string; succes: string }>>`
|
||||
SELECT ROW_NUMBER() OVER (ORDER BY timestamp DESC) AS id, timestamp, command, params, succes
|
||||
FROM commandlogs WHERE user_id = ${user.id} ORDER BY timestamp DESC LIMIT 10`,
|
||||
]);
|
||||
|
||||
if (!user) notFound()
|
||||
|
||||
const diamonds = user.currencies.find((c) => c.type === 5)?.amount ?? 0
|
||||
const duckets = user.currencies.find((c) => c.type === 0)?.amount ?? 0
|
||||
|
||||
const [roomCount, chatCount, tradeCount, userRooms, chatLogs, commandLogs] = await Promise.all([
|
||||
prisma.room.count({ where: { ownerId: user.id } }),
|
||||
prisma.chatLog.count({ where: { userId: user.id } }),
|
||||
prisma.trade.count({
|
||||
where: { OR: [{ userOneId: user.id }, { userTwoId: user.id }] },
|
||||
}),
|
||||
prisma.room.findMany({
|
||||
where: { ownerId: user.id },
|
||||
orderBy: { users: 'desc' },
|
||||
take: 20,
|
||||
select: { id: true, name: true, state: true, users: true, usersMax: true },
|
||||
}),
|
||||
prisma.chatLog.findMany({
|
||||
where: { userId: user.id },
|
||||
orderBy: { id: 'desc' },
|
||||
take: 20,
|
||||
select: { id: true, roomId: true, timestamp: true, message: true },
|
||||
}),
|
||||
prisma.commandLog.findMany({
|
||||
where: { userId: user.id },
|
||||
orderBy: { id: 'desc' },
|
||||
take: 10,
|
||||
select: { id: true, timestamp: true, command: true, params: true, succes: true },
|
||||
}),
|
||||
])
|
||||
|
||||
const isOnline = user.online === '1'
|
||||
const activeBan = user.bansReceived.find((b) => {
|
||||
const now = Math.floor(Date.now() / 1000)
|
||||
return b.banExpire === 0 || b.banExpire > now
|
||||
})
|
||||
const diamonds = currencies.find((currency) => currency.type === 5)?.amount ?? 0;
|
||||
const duckets = currencies.find((currency) => currency.type === 0)?.amount ?? 0;
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
const activeBan = bansReceived.find((ban) => ban.banExpire === 0 || ban.banExpire > now);
|
||||
const decoratedUser = {
|
||||
...user,
|
||||
mail: user.mail ?? "",
|
||||
gender: String(user.gender),
|
||||
machine_id: user.machineId,
|
||||
twoFactorConfirmed: user.twoFactorConfirmedAt ? 1 : 0,
|
||||
permission: { rankName: permissionRows[0]?.rankName ?? `Rank ${user.rank}` },
|
||||
currencies,
|
||||
badges,
|
||||
bansReceived: bansReceived.map((ban) => ({
|
||||
...ban,
|
||||
staff: { username: ban.staffName },
|
||||
})),
|
||||
};
|
||||
|
||||
return {
|
||||
user,
|
||||
user: decoratedUser,
|
||||
diamonds,
|
||||
duckets,
|
||||
roomCount,
|
||||
chatCount,
|
||||
chatCount: chatLogs.length,
|
||||
tradeCount,
|
||||
userRooms,
|
||||
chatLogs,
|
||||
commandLogs: commandLogs.map((l) => ({ ...l, succes: String(l.succes) })),
|
||||
isOnline,
|
||||
commandLogs,
|
||||
isOnline: user.online === "1",
|
||||
activeBan,
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user