Introspected the live DB and fixed real drift between the hand-modeled
schema and the actual columns:
- Added missing @map("snake_case") on camelCase fields that silently
failed at query time: website_teams (rank_name/hidden_rank/...),
website_paypal_transactions/user_guestbooks/help_center_tickets(+replies)/
used_shop_vouchers/maintenance_tasks (user_id), website_rare_values
(currency_type).
- website_permissions was modeled as key/value/comment but is actually
permission/min_rank/description — fixed the model + the admin page/action.
- website_shop_articles.icon -> icon_url (+ added category_id, is_giftable);
updated the shop page + admin shop CRUD.
- website_shop_categories: dropped non-existent slug/timestamps, added the
real description/order columns; updated the shop page.
- website_shop_article_features.features(Json) -> content(Text).
Verified against amx_test: all website_* query errors gone; home renders
the real hotel_name ("Habbo"), rankings shows real users, /staff + /shop
200. tsc 0, vitest 49/49, next build 0. Remaining missing tables
(radio_*/game_*/staff_activities/alert_logs/article_comments+reactions)
don't exist in this DB and degrade gracefully via try/catch.
92 lines
3.1 KiB
TypeScript
92 lines
3.1 KiB
TypeScript
"use server";
|
|
|
|
import { revalidatePath } from "next/cache";
|
|
import { requireStaff } from "@/lib/admin/guard";
|
|
import { prisma } from "@/lib/prisma";
|
|
import { logStaffActivity } from "@/lib/services/staff-activity";
|
|
|
|
// website_permissions (model WebsitePermissions) is the CMS-owned permission ->
|
|
// minimum-rank mapping. Editable columns are exactly: permission (unique name),
|
|
// min_rank (lowest rank that may use it), description. id is BigInt and
|
|
// created_at/updated_at are managed here.
|
|
|
|
function parseMinRank(formData: FormData): number {
|
|
const n = Number(String(formData.get("minRank") ?? "").trim());
|
|
return Number.isInteger(n) && n >= 0 ? n : 1;
|
|
}
|
|
|
|
export async function createPermission(formData: FormData): Promise<void> {
|
|
const staff = await requireStaff();
|
|
const permission = String(formData.get("permission") ?? "").trim().slice(0, 255);
|
|
const minRank = parseMinRank(formData);
|
|
const description = String(formData.get("description") ?? "").trim() || null;
|
|
if (!permission) return;
|
|
|
|
const now = new Date();
|
|
try {
|
|
await prisma.websitePermissions.upsert({
|
|
where: { permission },
|
|
update: { minRank, description, updatedAt: now },
|
|
create: { permission, minRank, description, createdAt: now, updatedAt: now },
|
|
});
|
|
await logStaffActivity({
|
|
staffId: staff.id,
|
|
action: "permission_create",
|
|
description: `Saved permission "${permission}" (min rank ${minRank})`,
|
|
targetType: "permission",
|
|
});
|
|
} catch {
|
|
// ignore (e.g. constraint failure) — page re-renders current state
|
|
}
|
|
revalidatePath("/admin/permissions");
|
|
}
|
|
|
|
export async function updatePermission(formData: FormData): Promise<void> {
|
|
const staff = await requireStaff();
|
|
const raw = String(formData.get("id") ?? "");
|
|
if (!raw) return;
|
|
const id = BigInt(raw);
|
|
const permission = String(formData.get("permission") ?? "").trim().slice(0, 255);
|
|
const minRank = parseMinRank(formData);
|
|
const description = String(formData.get("description") ?? "").trim() || null;
|
|
if (!permission) return;
|
|
|
|
try {
|
|
await prisma.websitePermissions.update({
|
|
where: { id },
|
|
data: { permission, minRank, description, updatedAt: new Date() },
|
|
});
|
|
await logStaffActivity({
|
|
staffId: staff.id,
|
|
action: "permission_update",
|
|
description: `Updated permission #${raw} ("${permission}" min rank ${minRank})`,
|
|
targetType: "permission",
|
|
});
|
|
} catch {
|
|
// ignore (e.g. duplicate) — page re-renders current state
|
|
}
|
|
revalidatePath("/admin/permissions");
|
|
}
|
|
|
|
export async function deletePermission(formData: FormData): Promise<void> {
|
|
const staff = await requireStaff();
|
|
const raw = String(formData.get("id") ?? "");
|
|
if (!raw) return;
|
|
|
|
try {
|
|
const deleted = await prisma.websitePermissions.delete({
|
|
where: { id: BigInt(raw) },
|
|
select: { permission: true },
|
|
});
|
|
await logStaffActivity({
|
|
staffId: staff.id,
|
|
action: "permission_delete",
|
|
description: `Deleted permission #${raw} ("${deleted.permission}")`,
|
|
targetType: "permission",
|
|
});
|
|
} catch {
|
|
// ignore (e.g. already removed)
|
|
}
|
|
revalidatePath("/admin/permissions");
|
|
}
|