Files
EpicNext-Cms/src/app/admin/users/_lib/load-user.ts
T

70 lines
3.2 KiB
TypeScript

import { notFound } from "next/navigation";
import { prisma } from "@/lib/prisma";
export async function loadUserById(id: string) {
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 [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`,
]);
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: decoratedUser,
diamonds,
duckets,
roomCount,
chatCount: chatLogs.length,
tradeCount,
userRooms,
chatLogs,
commandLogs,
isOnline: user.online === "1",
activeBan,
};
}