feat: add catalog audit page
Checks for: - items_base entries without catalog_items (not purchasable) - catalog_items referencing non-existent items_base - Items without .nitro or icon files on disk - Duplicate classnames - Items missing from all configured clone sources
This commit is contained in:
1 parent
90107c83d5
commit
19e4e10b1d
6 files changed
+607
-1
No files matched your search
@@ -0,0 +1,214 @@
|
||||
import { existsSync } from "node:fs";
|
||||
import path from "node:path";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { fetchSourceFurnidata } from "@/lib/services/clone-import";
|
||||
import { listSources } from "@/lib/services/clone-sources";
|
||||
import { getFurniAssetDirs } from "@/lib/services/furni-asset-dirs";
|
||||
import { ensureDirectories } from "./furni-import";
|
||||
|
||||
export interface AuditIssue {
|
||||
type: string;
|
||||
severity: "error" | "warning" | "info";
|
||||
classname?: string;
|
||||
itemId?: number;
|
||||
catalogId?: number;
|
||||
message: string;
|
||||
sourceName?: string;
|
||||
}
|
||||
|
||||
export interface MissingFromSource {
|
||||
sourceName: string;
|
||||
sourceId: string;
|
||||
classname: string;
|
||||
itemName: string;
|
||||
}
|
||||
|
||||
export interface AuditResult {
|
||||
issues: AuditIssue[];
|
||||
missingFromSources: MissingFromSource[];
|
||||
summary: {
|
||||
totalItemsBase: number;
|
||||
totalCatalogItems: number;
|
||||
missingCatalog: number;
|
||||
orphanedCatalog: number;
|
||||
missingNitro: number;
|
||||
missingIcon: number;
|
||||
duplicates: number;
|
||||
missingFromSourcesTotal: number;
|
||||
};
|
||||
}
|
||||
|
||||
export async function runCatalogAudit(
|
||||
onProgress?: (msg: string) => void,
|
||||
): Promise<AuditResult> {
|
||||
const issues: AuditIssue[] = [];
|
||||
const missingFromSources: MissingFromSource[] = [];
|
||||
|
||||
onProgress?.("Loading directories…");
|
||||
await ensureDirectories();
|
||||
const { iconDir, nitroDir } = await getFurniAssetDirs();
|
||||
|
||||
onProgress?.("Loading items_base…");
|
||||
const items = await prisma.$queryRaw<
|
||||
Array<{ id: number; item_name: string; public_name: string; type: string }>
|
||||
>`SELECT id, item_name, public_name, type FROM items_base ORDER BY id`;
|
||||
|
||||
onProgress?.("Loading catalog_items…");
|
||||
const catalogItems = await prisma.$queryRaw<
|
||||
Array<{ id: number; item_ids: string; catalog_name: string }>
|
||||
>`SELECT id, item_ids, catalog_name FROM catalog_items ORDER BY id`;
|
||||
|
||||
const summary = {
|
||||
totalItemsBase: items.length,
|
||||
totalCatalogItems: catalogItems.length,
|
||||
missingCatalog: 0,
|
||||
orphanedCatalog: 0,
|
||||
missingNitro: 0,
|
||||
missingIcon: 0,
|
||||
duplicates: 0,
|
||||
missingFromSourcesTotal: 0,
|
||||
};
|
||||
|
||||
// Build lookup: set of items_base IDs that have catalog entries
|
||||
const itemIdsWithCatalog = new Set<number>();
|
||||
const itemNamesWithCatalog = new Set<string>();
|
||||
for (const ci of catalogItems) {
|
||||
const ids = ci.item_ids.split(";").map(Number).filter(Boolean);
|
||||
for (const id of ids) {
|
||||
itemIdsWithCatalog.add(id);
|
||||
}
|
||||
itemNamesWithCatalog.add(ci.catalog_name);
|
||||
}
|
||||
|
||||
// Build items_base lookup by id and name
|
||||
const itemById = new Map<number, (typeof items)[0]>();
|
||||
const itemByName = new Map<string, (typeof items)[0]>();
|
||||
const classnameCount = new Map<string, number>();
|
||||
|
||||
for (const item of items) {
|
||||
itemById.set(item.id, item);
|
||||
itemByName.set(item.item_name, item);
|
||||
classnameCount.set(
|
||||
item.item_name,
|
||||
(classnameCount.get(item.item_name) ?? 0) + 1,
|
||||
);
|
||||
}
|
||||
|
||||
onProgress?.("Checking items_base entries…");
|
||||
for (const item of items) {
|
||||
if (!itemIdsWithCatalog.has(item.id)) {
|
||||
summary.missingCatalog++;
|
||||
issues.push({
|
||||
type: "missing_catalog",
|
||||
severity: "warning",
|
||||
classname: item.item_name,
|
||||
itemId: item.id,
|
||||
message: `"${item.item_name}" (ID ${item.id}) has no catalog_items entry — not purchasable`,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
onProgress?.("Checking for duplicate classnames…");
|
||||
for (const [name, count] of classnameCount) {
|
||||
if (count > 1) {
|
||||
summary.duplicates++;
|
||||
issues.push({
|
||||
type: "duplicate_classname",
|
||||
severity: "error",
|
||||
classname: name,
|
||||
message: `"${name}" appears ${count} times in items_base`,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
onProgress?.("Checking catalog_items for orphaned references…");
|
||||
for (const ci of catalogItems) {
|
||||
const ids = ci.item_ids.split(";").map(Number).filter(Boolean);
|
||||
for (const id of ids) {
|
||||
if (!itemById.has(id)) {
|
||||
summary.orphanedCatalog++;
|
||||
issues.push({
|
||||
type: "orphaned_catalog",
|
||||
severity: "error",
|
||||
catalogId: ci.id,
|
||||
message: `catalog_items ID ${ci.id} ("${ci.catalog_name}") references items_base ID ${id} which does not exist`,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
onProgress?.("Checking .nitro files…");
|
||||
for (const item of items) {
|
||||
const nitroPath = path.join(nitroDir, `${item.item_name}.nitro`);
|
||||
if (!existsSync(nitroPath)) {
|
||||
summary.missingNitro++;
|
||||
issues.push({
|
||||
type: "missing_nitro",
|
||||
severity: "error",
|
||||
classname: item.item_name,
|
||||
itemId: item.id,
|
||||
message: `"${item.item_name}" (ID ${item.id}) has no .nitro file`,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
onProgress?.("Checking icon files…");
|
||||
for (const item of items) {
|
||||
const iconPath = path.join(iconDir, `${item.item_name}_icon.png`);
|
||||
if (!existsSync(iconPath)) {
|
||||
summary.missingIcon++;
|
||||
issues.push({
|
||||
type: "missing_icon",
|
||||
severity: "warning",
|
||||
classname: item.item_name,
|
||||
itemId: item.id,
|
||||
message: `"${item.item_name}" (ID ${item.id}) has no icon file`,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
onProgress?.("Comparing with clone sources…");
|
||||
try {
|
||||
const sources = await listSources();
|
||||
for (const source of sources) {
|
||||
try {
|
||||
const entries = await fetchSourceFurnidata(source.furnidataUrl);
|
||||
for (const entry of entries) {
|
||||
if (!itemByName.has(entry.classname)) {
|
||||
missingFromSources.push({
|
||||
sourceName: source.name,
|
||||
sourceId: source.id,
|
||||
classname: entry.classname,
|
||||
itemName: entry.name,
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
issues.push({
|
||||
type: "source_fetch_failed",
|
||||
severity: "error",
|
||||
sourceName: source.name,
|
||||
message: `Failed to fetch furnidata from "${source.name}"`,
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
issues.push({
|
||||
type: "sources_list_failed",
|
||||
severity: "error",
|
||||
message: `Failed to list clone sources: ${(err as Error).message}`,
|
||||
});
|
||||
}
|
||||
|
||||
summary.missingFromSourcesTotal = missingFromSources.length;
|
||||
|
||||
// Sort issues: errors first, then by type
|
||||
issues.sort((a, b) => {
|
||||
const sev = { error: 0, warning: 1, info: 2 };
|
||||
const diff = (sev[a.severity] ?? 0) - (sev[b.severity] ?? 0);
|
||||
if (diff !== 0) return diff;
|
||||
return a.type.localeCompare(b.type);
|
||||
});
|
||||
|
||||
return { issues, missingFromSources, summary };
|
||||
}
|
||||
Reference in new issue
Block a user