feat: add official furnidata sync button for importing all missing items
- API endpoint: /api/admin/import/official/sync-all - Fetches all official Habbo furnidata - Compares with database to find missing items - Imports missing items via SSE batch with progress reporting - 500 item limit for safety - Proper abort signal support - Client component: OfficialSyncClient with progress UI - Updated StudioSyncPage to include Official Furnidata sync option - Uses existing importSingleFurni infrastructure
This commit is contained in:
1 parent
2920a6521b
commit
1dc8d1d46f
3 files changed
+334
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -1,4 +1,5 @@
|
|||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
|
import { OfficialSyncClient } from "@/app/admin/import/official/official-sync-client";
|
||||||
import { SyncAllClient } from "@/app/admin/import/sync/sync-all-client";
|
import { SyncAllClient } from "@/app/admin/import/sync/sync-all-client";
|
||||||
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
|
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
|
||||||
|
|
||||||
@@ -28,6 +29,17 @@ export default async function StudioSyncPage(_props: {
|
|||||||
</p>
|
</p>
|
||||||
<SyncAllClient />
|
<SyncAllClient />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<h2 className="text-lg font-semibold text-muted-foreground mb-2">
|
||||||
|
Official Furnidata
|
||||||
|
</h2>
|
||||||
|
<p className="text-sm text-muted-foreground">
|
||||||
|
Scan the official Habbo furnidata and automatically import any
|
||||||
|
furniture that your hotel doesn't have yet.
|
||||||
|
</p>
|
||||||
|
<OfficialSyncClient />
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -0,0 +1,97 @@
|
|||||||
|
import { withAdmin } from "@/lib/api-handler";
|
||||||
|
import { db, ItemsBase } from "@/lib/db";
|
||||||
|
import { PERMS } from "@/lib/permissions";
|
||||||
|
import { importSingleFurni } from "@/lib/services/furni-import";
|
||||||
|
import { getOfficialHabboFurnidata } from "@/lib/services/habbo-furnidata-cache";
|
||||||
|
import { runSseBatch } from "@/lib/services/import/core/sse-batch";
|
||||||
|
|
||||||
|
export const POST = withAdmin(
|
||||||
|
{ permission: PERMS.ASSETS_IMPORT },
|
||||||
|
async (request) => {
|
||||||
|
// Haal alle officiële furnidata
|
||||||
|
const furnidata = await getOfficialHabboFurnidata();
|
||||||
|
|
||||||
|
// Haal alle bestaande classnames uit de database
|
||||||
|
const existingClassnames = new Set(
|
||||||
|
await db
|
||||||
|
.select({ itemName: ItemsBase.itemName })
|
||||||
|
.from(ItemsBase)
|
||||||
|
.then((rows) => rows.map((r) => r.itemName)),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Filter naar alleen items die niet in de database staan
|
||||||
|
const missingItems = Array.from(furnidata.entries())
|
||||||
|
.filter(([, entry]) => !existingClassnames.has(entry.classname))
|
||||||
|
.map(([, entry]) => ({
|
||||||
|
classname: entry.classname,
|
||||||
|
name: entry.name,
|
||||||
|
description: entry.description ?? "",
|
||||||
|
type: entry.category === "wallitem" ? "wallitem" : "flooritem",
|
||||||
|
revision: entry.revision ?? 0,
|
||||||
|
category: entry.category ?? "unknown",
|
||||||
|
}));
|
||||||
|
|
||||||
|
if (missingItems.length === 0) {
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({
|
||||||
|
success: true,
|
||||||
|
message: "All items are already in the database",
|
||||||
|
imported: 0,
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (missingItems.length > 500) {
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({
|
||||||
|
success: false,
|
||||||
|
message: `Too many missing items (${missingItems.length}). Consider importing in smaller batches.`,
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
status: 400,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Importeren via SSE batch
|
||||||
|
return runSseBatch({
|
||||||
|
items: missingItems,
|
||||||
|
concurrency: 3,
|
||||||
|
signal: request.signal,
|
||||||
|
labelOf: (it) => it.classname,
|
||||||
|
worker: async (it, _index, report) => {
|
||||||
|
const result = await importSingleFurni({
|
||||||
|
id: 0,
|
||||||
|
classname: it.classname,
|
||||||
|
name: it.name,
|
||||||
|
description: it.description,
|
||||||
|
type: it.type,
|
||||||
|
revision: it.revision,
|
||||||
|
category: it.category,
|
||||||
|
onProgress: report,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (result.ok) {
|
||||||
|
return {
|
||||||
|
ok: true,
|
||||||
|
warnings: result.warnings,
|
||||||
|
error: result.error,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
ok: false,
|
||||||
|
warnings: result.warnings,
|
||||||
|
error: result.error,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
flush: async () => {
|
||||||
|
// FurnitureData.json wordt automatisch geschreven door importSingleFurni
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
Reference in new issue
Block a user