124 lines
3.2 KiB
TypeScript
124 lines
3.2 KiB
TypeScript
"use server";
|
|
|
|
import { and, eq, inArray } from "drizzle-orm";
|
|
import { revalidatePath } from "next/cache";
|
|
import { requirePermission } from "@/lib/admin/guard";
|
|
import { db, Items, Rooms } from "@/lib/db";
|
|
import { PERMS } from "@/lib/permissions";
|
|
import { rcon } from "@/lib/services/rcon";
|
|
import { logStaffActivity } from "@/lib/services/staff-activity";
|
|
|
|
export async function updateRoomItem(payload: Record<string, unknown>) {
|
|
const staff = await requirePermission(PERMS.ROOMS_EDIT);
|
|
const { roomId, itemId, ...data } = payload as {
|
|
roomId: number;
|
|
itemId: number;
|
|
[key: string]: unknown;
|
|
};
|
|
await db
|
|
.update(Items)
|
|
.set(data as Partial<typeof Items.$inferInsert>)
|
|
.where(eq(Items.id, itemId));
|
|
await logStaffActivity({
|
|
staffId: staff.id,
|
|
action: "room_item_update",
|
|
description: `Updated item #${itemId} in room #${roomId}`,
|
|
targetType: "room_item",
|
|
targetId: itemId,
|
|
});
|
|
revalidatePath(`/admin/rooms/${roomId}/furni`);
|
|
}
|
|
|
|
export async function bulkDeleteRoomItems({
|
|
roomId,
|
|
itemIds,
|
|
}: {
|
|
roomId: number;
|
|
itemIds: number[];
|
|
}) {
|
|
const staff = await requirePermission(PERMS.ROOMS_EDIT);
|
|
await db
|
|
.delete(Items)
|
|
.where(and(inArray(Items.id, itemIds), eq(Items.roomId, roomId)));
|
|
await logStaffActivity({
|
|
staffId: staff.id,
|
|
action: "room_items_bulk_delete",
|
|
description: `Deleted ${itemIds.length} item(s) from room #${roomId}`,
|
|
targetType: "room_item",
|
|
});
|
|
revalidatePath(`/admin/rooms/${roomId}/furni`);
|
|
}
|
|
|
|
export async function deleteRoomItem({
|
|
roomId,
|
|
itemId,
|
|
}: {
|
|
roomId: number;
|
|
itemId: number;
|
|
}) {
|
|
const staff = await requirePermission(PERMS.ROOMS_EDIT);
|
|
await db.delete(Items).where(eq(Items.id, itemId));
|
|
await logStaffActivity({
|
|
staffId: staff.id,
|
|
action: "room_item_delete",
|
|
description: `Deleted item #${itemId} from room #${roomId}`,
|
|
targetType: "room_item",
|
|
targetId: itemId,
|
|
});
|
|
revalidatePath(`/admin/rooms/${roomId}/furni`);
|
|
}
|
|
|
|
export async function roomRconAction({
|
|
roomId,
|
|
action,
|
|
}: {
|
|
roomId: number;
|
|
action: string;
|
|
}) {
|
|
await requirePermission(PERMS.ROOMS_EDIT);
|
|
if (action === "reload") {
|
|
await rcon.send("reloadroom", { room_id: roomId });
|
|
} else if (action === "kick") {
|
|
await rcon.send("kickall", { room_id: roomId });
|
|
} else if (action === "lock") {
|
|
await rcon.send("updateroom", { room_id: roomId, state: "locked" });
|
|
} else if (action === "unlock") {
|
|
await rcon.send("updateroom", { room_id: roomId, state: "open" });
|
|
}
|
|
}
|
|
|
|
export async function deleteRoom({ id }: { id: number }) {
|
|
const staff = await requirePermission(PERMS.ROOMS_DELETE);
|
|
await db.delete(Rooms).where(eq(Rooms.id, id));
|
|
await logStaffActivity({
|
|
staffId: staff.id,
|
|
action: "room_delete",
|
|
description: `Deleted room #${id}`,
|
|
targetType: "room",
|
|
targetId: id,
|
|
});
|
|
revalidatePath("/admin/rooms");
|
|
}
|
|
|
|
export async function updateRoom({
|
|
id,
|
|
...data
|
|
}: {
|
|
id: number;
|
|
name?: string;
|
|
description?: string;
|
|
state?: string;
|
|
usersMax?: number;
|
|
}) {
|
|
const staff = await requirePermission(PERMS.ROOMS_EDIT);
|
|
await db.update(Rooms).set(data).where(eq(Rooms.id, id));
|
|
await logStaffActivity({
|
|
staffId: staff.id,
|
|
action: "room_update",
|
|
description: `Updated room #${id}`,
|
|
targetType: "room",
|
|
targetId: id,
|
|
});
|
|
revalidatePath(`/admin/rooms/${id}`);
|
|
}
|