Gitea Actions Runner Test / test-job (push) Successful in 1s
CI / check (push) Successful in 28s
CI / tests-integration (push) Successful in 1m43s
CI / tests-unit (push) Successful in 1m48s
CI / tests-ui (push) Successful in 2m37s
CI / preflight (push) Skipped
CI / deploy (push) Successful in 2m23s
A bulk offer edit is the catalog mutation that rewrites hundreds of rows at once, and it was the only one writing nothing to the staff activity log: 22 of the 43 catalog actions logged, this one did not. The entry it now writes says what changed, not just that something did, because the log has no undo of its own and "bulk updated 200 offers" cannot answer the question it exists for. Deleting offers had no inverse at all. Every removed row is now kept at delete time and the caller gets a restoreId back, so an accidental multi-select is a click rather than a hand-edit of the table. The undo toast covers the common case; a RecentDeletionsPanel holds the same records so a delete noticed later is still reachable. Three refusals guard it: an id that another offer has since taken, a category that no longer exists (which would leave an offer that sells nowhere and shows under no page), and a delete whose restore record cannot be written — that one rolls back rather than deleting without a way back. Reading the audit row FOR UPDATE is also what stops two restores of one deletion from both inserting. sendCatalogUpdate() overwrote hotel-status.json on every write, so "which imports reached the hotel" was answerable for the last attempt only, and a failure two imports ago was gone by the time anyone looked. That file is now also appended to as a bounded 50-entry tail. The tree route carried four copies of the same page-select-plus-counts shaping, of which the BC branches had already drifted: one counted offers through the VARCHAR-tolerant helper, the other inline and swallowing errors. All of it is one readPages() now, and readFullTree sends both catalogs through one depth computation instead of delegating normal to getTreeFlat while computing BC here — a split that left two implementations behind one function name. getTreeFlat is gone. The BC ancestor walk also went from 20 levels to 50, matching getAncestors, so a deeply nested catalog no longer loses its breadcrumb. Bulk editing reaches the BC catalog, which previously had no way to edit or duplicate offers in bulk. The catalog is part of the operation identity now, so replaying one request key against the other catalog is not mistaken for the same work. Integration tests failed to import: the next/cache mock supplied only revalidatePath, and catalog-totals calls unstable_cache at module scope.
177 lines
4.3 KiB
TypeScript
177 lines
4.3 KiB
TypeScript
import { eq, sql } from "drizzle-orm";
|
|
import {
|
|
deletePageCommand,
|
|
updatePageCommand,
|
|
} from "@/features/catalog/server/page-commands";
|
|
import { CatalogPages, db, queryRows } from "@/lib/db";
|
|
|
|
export type { NestedTreeNode, TreeNode } from "@/types/catalog";
|
|
|
|
import type { NestedTreeNode, TreeNode } from "@/types/catalog";
|
|
|
|
function toInt(value: unknown, fallback = 0): number {
|
|
const n = typeof value === "number" ? value : Number(value);
|
|
return Number.isFinite(n) ? n : fallback;
|
|
}
|
|
|
|
/**
|
|
* Count catalog_items per page via raw SQL.
|
|
* Real Habbo DBs often store page_id as VARCHAR. String literals match both
|
|
* INT and VARCHAR columns while keeping the page_id index usable (CAST(... AS CHAR)
|
|
* would force a full index scan).
|
|
*/
|
|
export async function getCatalogItemCounts(
|
|
pageIds?: number[],
|
|
): Promise<Map<number, number>> {
|
|
const map = new Map<number, number>();
|
|
try {
|
|
if (pageIds && pageIds.length === 0) return map;
|
|
|
|
if (pageIds) {
|
|
const idStrs = pageIds.map(String);
|
|
const rows = await queryRows<{
|
|
page_id: string | number;
|
|
cnt: number | bigint;
|
|
}>(sql`
|
|
SELECT page_id, COUNT(*) as cnt
|
|
FROM catalog_items
|
|
WHERE page_id IN (${sql.join(idStrs, sql`, `)})
|
|
GROUP BY page_id
|
|
`);
|
|
for (const r of rows) {
|
|
map.set(toInt(r.page_id), toInt(r.cnt));
|
|
}
|
|
return map;
|
|
}
|
|
|
|
const rows = await queryRows<{
|
|
page_id: string | number;
|
|
cnt: number | bigint;
|
|
}>(sql`
|
|
SELECT page_id, COUNT(*) as cnt FROM catalog_items GROUP BY page_id
|
|
`);
|
|
for (const r of rows) {
|
|
map.set(toInt(r.page_id), toInt(r.cnt));
|
|
}
|
|
} catch {
|
|
// Best-effort — never break the tree for count failures.
|
|
}
|
|
return map;
|
|
}
|
|
|
|
/**
|
|
* Build a nested tree structure from flat nodes.
|
|
*/
|
|
export function buildNestedTree(
|
|
flat: TreeNode[],
|
|
rootParentId = -1,
|
|
): NestedTreeNode[] {
|
|
const childrenMap = new Map<number, NestedTreeNode[]>();
|
|
const nodeMap = new Map<number, NestedTreeNode>();
|
|
|
|
for (const node of flat) {
|
|
const nested: NestedTreeNode = { ...node, children: [] };
|
|
nodeMap.set(node.id, nested);
|
|
if (!childrenMap.has(node.parentId)) childrenMap.set(node.parentId, []);
|
|
childrenMap.get(node.parentId)?.push(nested);
|
|
}
|
|
|
|
for (const node of nodeMap.values()) {
|
|
node.children = childrenMap.get(node.id) ?? [];
|
|
}
|
|
|
|
return childrenMap.get(rootParentId) ?? [];
|
|
}
|
|
|
|
/**
|
|
* Create a new catalog page.
|
|
*/
|
|
export async function createPage(data: {
|
|
parentId: number;
|
|
caption: string;
|
|
captionSave?: string;
|
|
pageLayout?: string;
|
|
iconImage?: number;
|
|
iconColor?: number;
|
|
minRank?: number;
|
|
orderNum?: number;
|
|
visible?: string | boolean;
|
|
enabled?: string | boolean;
|
|
}) {
|
|
const visible =
|
|
typeof data.visible === "boolean"
|
|
? data.visible
|
|
? "1"
|
|
: "0"
|
|
: (data.visible ?? "1");
|
|
const enabled =
|
|
typeof data.enabled === "boolean"
|
|
? data.enabled
|
|
? "1"
|
|
: "0"
|
|
: (data.enabled ?? "1");
|
|
|
|
return db.insert(CatalogPages).values({
|
|
caption: data.caption,
|
|
parentId: data.parentId,
|
|
pageLayout: data.pageLayout ?? "default_3x3",
|
|
captionSave: (data.captionSave ?? data.caption).slice(0, 25),
|
|
iconColor: data.iconColor ?? 0,
|
|
iconImage: data.iconImage ?? 0,
|
|
minRank: data.minRank ?? 1,
|
|
orderNum: data.orderNum ?? 0,
|
|
visible,
|
|
enabled,
|
|
clubOnly: "0",
|
|
vipOnly: "0",
|
|
pageHeadline: "",
|
|
pageTeaser: "",
|
|
includes: "",
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Move a page to a new parent.
|
|
* Validates against circular hierarchy (A → B → C → A).
|
|
*/
|
|
export async function movePage(
|
|
pageId: number,
|
|
newParentId: number,
|
|
): Promise<void> {
|
|
await updatePageCommand("normal", pageId, { parentId: newParentId });
|
|
}
|
|
export async function deletePage(
|
|
pageId: number,
|
|
mode: "cascade" | "reparent" = "reparent",
|
|
) {
|
|
return deletePageCommand("normal", pageId, mode);
|
|
}
|
|
|
|
/**
|
|
* Get ancestors (breadcrumb) from root to the given page.
|
|
*/
|
|
export async function getAncestors(
|
|
pageId: number,
|
|
): Promise<Array<{ id: number; caption: string }>> {
|
|
const ancestors: Array<{ id: number; caption: string }> = [];
|
|
let currentId = pageId;
|
|
for (let i = 0; i < 50; i++) {
|
|
const [page] = await db
|
|
.select({
|
|
id: CatalogPages.id,
|
|
caption: CatalogPages.caption,
|
|
parentId: CatalogPages.parentId,
|
|
})
|
|
.from(CatalogPages)
|
|
.where(eq(CatalogPages.id, currentId))
|
|
.limit(1);
|
|
if (!page) break;
|
|
ancestors.unshift({ id: page.id, caption: page.caption });
|
|
if (page.parentId <= 0) break;
|
|
currentId = page.parentId;
|
|
}
|
|
return ancestors;
|
|
}
|
|
|
|
export { toInt };
|