feat(import): run catalog synchronizations as durable jobs
This commit is contained in:
1 parent
a2954e4408
commit
76f0420d64
14 files changed
+559
-801
No files matched your search
@@ -1,225 +1,5 @@
|
||||
"use client";
|
||||
|
||||
import { AlertCircle, Check, CloudDownload, Loader2 } from "lucide-react";
|
||||
import { useCallback, useRef, useState } from "react";
|
||||
import { toast } from "sonner";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { adminFetch } from "@/lib/admin-fetch";
|
||||
|
||||
interface SyncEvent {
|
||||
type: string;
|
||||
classname?: string;
|
||||
status?: string;
|
||||
succeeded?: number;
|
||||
failed?: number;
|
||||
message?: string;
|
||||
}
|
||||
|
||||
interface SyncResult {
|
||||
succeeded: number;
|
||||
failed: number;
|
||||
message: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Official furnidata sync client.
|
||||
* Imports all furniture from official Habbo furnidata that the hotel doesn't have yet.
|
||||
*/
|
||||
import { FurnitureSyncQueue } from "@/components/admin/studio/furniture-sync-queue";
|
||||
export function OfficialSyncClient() {
|
||||
const [syncing, setSyncing] = useState(false);
|
||||
const [progress, setProgress] = useState<{
|
||||
done: number;
|
||||
total: number;
|
||||
} | null>(null);
|
||||
const [log, setLog] = useState<string[]>([]);
|
||||
const [result, setResult] = useState<SyncResult | null>(null);
|
||||
const abortRef = useRef<AbortController | null>(null);
|
||||
|
||||
const startSync = useCallback(async () => {
|
||||
setSyncing(true);
|
||||
setProgress(null);
|
||||
setLog([]);
|
||||
setResult(null);
|
||||
|
||||
const abort = new AbortController();
|
||||
abortRef.current = abort;
|
||||
|
||||
try {
|
||||
const res = await adminFetch("/api/admin/import/official/sync-all", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({}),
|
||||
signal: abort.signal,
|
||||
});
|
||||
|
||||
if (!res.body) {
|
||||
toast.error("No response stream");
|
||||
setSyncing(false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!res.headers.get("content-type")?.includes("text/event-stream")) {
|
||||
let errMsg = "Sync failed";
|
||||
try {
|
||||
const json = await res.json();
|
||||
errMsg = json.message || errMsg;
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
toast.error(errMsg);
|
||||
setSyncing(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const reader = res.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
let buf = "";
|
||||
let succeeded = 0;
|
||||
let failed = 0;
|
||||
let totalItems = 0;
|
||||
|
||||
while (true) {
|
||||
const { value, done: streamDone } = await reader.read();
|
||||
if (streamDone) break;
|
||||
buf += decoder.decode(value, { stream: true });
|
||||
const parts = buf.split("\n\n");
|
||||
buf = parts.pop() ?? "";
|
||||
for (const part of parts) {
|
||||
if (!part.startsWith("data: ")) continue;
|
||||
try {
|
||||
const evt = JSON.parse(part.slice(6)) as SyncEvent;
|
||||
if (evt.type === "item_progress") {
|
||||
const classname = evt.classname ?? "";
|
||||
if (evt.status === "done") {
|
||||
succeeded++;
|
||||
totalItems++;
|
||||
setProgress({ done: succeeded + failed, total: totalItems });
|
||||
setLog((prev) => [...prev, `✓ ${classname}`]);
|
||||
} else if (evt.status === "failed") {
|
||||
failed++;
|
||||
totalItems++;
|
||||
setProgress({ done: succeeded + failed, total: totalItems });
|
||||
setLog((prev) => [...prev, `✗ ${classname} (failed)`]);
|
||||
}
|
||||
}
|
||||
if (evt.type === "batch_complete") {
|
||||
succeeded = evt.succeeded ?? succeeded;
|
||||
failed = evt.failed ?? failed;
|
||||
}
|
||||
if (evt.type === "error") {
|
||||
toast.error(
|
||||
typeof evt.message === "string"
|
||||
? evt.message
|
||||
: "Sync finished with errors",
|
||||
);
|
||||
}
|
||||
} catch {
|
||||
// skip parse errors
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
setResult({
|
||||
succeeded,
|
||||
failed,
|
||||
message:
|
||||
totalItems > 0
|
||||
? `${succeeded} items synced`
|
||||
: "All items were already up to date",
|
||||
});
|
||||
if (succeeded > 0) toast.success(`${succeeded} items synced`);
|
||||
if (failed > 0) toast.error(`${failed} items failed`);
|
||||
} catch (err) {
|
||||
if ((err as Error).name === "AbortError") {
|
||||
toast.info("Sync cancelled");
|
||||
} else {
|
||||
toast.error("Sync request failed");
|
||||
}
|
||||
} finally {
|
||||
setSyncing(false);
|
||||
abortRef.current = null;
|
||||
}
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
{!result && (
|
||||
<div className="flex items-center gap-2 flex-wrap">
|
||||
<Button onClick={startSync} disabled={syncing} size="lg">
|
||||
{syncing ? (
|
||||
<Loader2 className="w-4 h-4 animate-spin mr-2" />
|
||||
) : (
|
||||
<CloudDownload className="w-4 h-4 mr-2" />
|
||||
)}
|
||||
{syncing
|
||||
? `Syncing… ${progress ? `${progress.done}/${progress.total}` : ""}`
|
||||
: "Import All Missing from Official Furnidata"}
|
||||
</Button>
|
||||
{syncing && (
|
||||
<Button
|
||||
variant="outline"
|
||||
size="lg"
|
||||
onClick={() => abortRef.current?.abort()}
|
||||
>
|
||||
Cancel
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{progress && (
|
||||
<div className="text-sm text-muted-foreground">
|
||||
Processed {progress.done} items
|
||||
{progress.total > 0 ? ` out of ${progress.total}` : ""}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{log.length > 0 && (
|
||||
<div className="rounded-xl border bg-card p-4 max-h-80 overflow-x-auto overflow-y-auto space-y-0.5">
|
||||
{log.map((line) => (
|
||||
<div
|
||||
key={line}
|
||||
className="text-xs font-mono text-muted-foreground whitespace-nowrap"
|
||||
>
|
||||
{line}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{result && (
|
||||
<div className="rounded-xl border bg-card p-5 space-y-3">
|
||||
<div className="flex items-center gap-2">
|
||||
{result.failed === 0 ? (
|
||||
<Check className="w-5 h-5 text-[var(--admin-success)]" />
|
||||
) : (
|
||||
<AlertCircle className="w-5 h-5 text-[var(--admin-error)]" />
|
||||
)}
|
||||
<span className="text-lg font-semibold">Sync Complete</span>
|
||||
</div>
|
||||
<div className="text-sm space-y-1">
|
||||
<p>
|
||||
<span className="text-muted-foreground">Succeeded:</span>{" "}
|
||||
{result.succeeded}
|
||||
</p>
|
||||
<p>
|
||||
<span className="text-muted-foreground">Failed:</span>{" "}
|
||||
{result.failed}
|
||||
</p>
|
||||
<p className="text-sm text-muted-foreground">{result.message}</p>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!syncing && !result && (
|
||||
<div className="rounded-xl border bg-card p-6 text-center text-muted-foreground text-sm">
|
||||
<CloudDownload className="w-10 h-10 mx-auto mb-3 opacity-40" />
|
||||
<p>
|
||||
Click the button above to scan the official Habbo furnidata and
|
||||
automatically import any furniture that your hotel doesn't have yet.
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
return <FurnitureSyncQueue kind="official" />;
|
||||
}
|
||||
@@ -1,213 +1,5 @@
|
||||
"use client";
|
||||
|
||||
import { Check, CloudDownload, Loader2 } from "lucide-react";
|
||||
import { useCallback, useRef, useState } from "react";
|
||||
import { toast } from "sonner";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { adminFetch } from "@/lib/admin-fetch";
|
||||
|
||||
interface SyncEvent {
|
||||
type: string;
|
||||
classname?: string;
|
||||
status?: string;
|
||||
succeeded?: number;
|
||||
failed?: number;
|
||||
}
|
||||
|
||||
import { FurnitureSyncQueue } from "@/components/admin/studio/furniture-sync-queue";
|
||||
export function SyncAllClient() {
|
||||
const [syncing, setSyncing] = useState(false);
|
||||
const [progress, setProgress] = useState<{
|
||||
done: number;
|
||||
total: number;
|
||||
} | null>(null);
|
||||
const [log, setLog] = useState<string[]>([]);
|
||||
const [result, setResult] = useState<{
|
||||
succeeded: number;
|
||||
failed: number;
|
||||
sources: number;
|
||||
} | null>(null);
|
||||
const abortRef = useRef<AbortController | null>(null);
|
||||
|
||||
const startSync = useCallback(async () => {
|
||||
setSyncing(true);
|
||||
setProgress(null);
|
||||
setLog([]);
|
||||
setResult(null);
|
||||
|
||||
const abort = new AbortController();
|
||||
abortRef.current = abort;
|
||||
|
||||
try {
|
||||
const res = await adminFetch("/api/admin/import/clone/sync-all", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({}),
|
||||
signal: abort.signal,
|
||||
});
|
||||
|
||||
if (!res.body) {
|
||||
toast.error("No response stream");
|
||||
setSyncing(false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!res.headers.get("content-type")?.includes("text/event-stream")) {
|
||||
let errMsg = "Sync failed";
|
||||
try {
|
||||
const json = await res.json();
|
||||
errMsg = json.error || errMsg;
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
toast.error(errMsg);
|
||||
setSyncing(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const reader = res.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
let buf = "";
|
||||
let succeeded = 0;
|
||||
let failed = 0;
|
||||
let totalItems = 0;
|
||||
|
||||
while (true) {
|
||||
const { value, done: streamDone } = await reader.read();
|
||||
if (streamDone) break;
|
||||
buf += decoder.decode(value, { stream: true });
|
||||
const parts = buf.split("\n\n");
|
||||
buf = parts.pop() ?? "";
|
||||
for (const part of parts) {
|
||||
if (!part.startsWith("data: ")) continue;
|
||||
try {
|
||||
const evt = JSON.parse(part.slice(6)) as SyncEvent;
|
||||
if (
|
||||
evt.type === "item_progress" &&
|
||||
(evt.status === "done" || evt.status === "failed")
|
||||
) {
|
||||
totalItems++;
|
||||
if (evt.status === "done") succeeded++;
|
||||
else failed++;
|
||||
setProgress({ done: succeeded + failed, total: totalItems });
|
||||
setLog((prev) => [
|
||||
...prev,
|
||||
`${evt.status === "done" ? "✓" : "✗"} ${evt.classname ?? ""}`,
|
||||
]);
|
||||
}
|
||||
if (evt.type === "batch_complete") {
|
||||
succeeded = evt.succeeded ?? succeeded;
|
||||
failed = evt.failed ?? failed;
|
||||
}
|
||||
} catch {
|
||||
// skip parse errors
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check for skipped state (all up to date)
|
||||
try {
|
||||
const parsed = JSON.parse(
|
||||
buf.includes("data:") ? (buf.split("data:").pop() ?? "{}") : "{}",
|
||||
);
|
||||
if (parsed.skipped) {
|
||||
toast.info(parsed.message || "All sources are up to date");
|
||||
setResult({ succeeded: 0, failed: 0, sources: 0 });
|
||||
setSyncing(false);
|
||||
return;
|
||||
}
|
||||
} catch {
|
||||
// not a skipped response
|
||||
}
|
||||
|
||||
setResult({ succeeded, failed, sources: 1 });
|
||||
if (succeeded > 0) toast.success(`${succeeded} items synced`);
|
||||
if (failed > 0) toast.error(`${failed} items failed`);
|
||||
} catch (err) {
|
||||
if ((err as Error).name === "AbortError") {
|
||||
toast.info("Sync cancelled");
|
||||
} else {
|
||||
toast.error("Sync request failed");
|
||||
}
|
||||
} finally {
|
||||
setSyncing(false);
|
||||
abortRef.current = null;
|
||||
}
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
{!result && (
|
||||
<div className="flex items-center gap-2 flex-wrap">
|
||||
<Button onClick={startSync} disabled={syncing} size="lg">
|
||||
{syncing ? (
|
||||
<Loader2 className="w-4 h-4 animate-spin mr-2" />
|
||||
) : (
|
||||
<CloudDownload className="w-4 h-4 mr-2" />
|
||||
)}
|
||||
{syncing
|
||||
? `Syncing… ${progress ? `${progress.done}/${progress.total}` : ""}`
|
||||
: "Start Sync All Sources"}
|
||||
</Button>
|
||||
{syncing && (
|
||||
<Button
|
||||
variant="outline"
|
||||
size="lg"
|
||||
onClick={() => abortRef.current?.abort()}
|
||||
>
|
||||
Cancel
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{progress && (
|
||||
<div className="text-sm text-muted-foreground">
|
||||
Processed {progress.done} items
|
||||
{progress.total > 0 ? ` out of ${progress.total}` : ""}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{log.length > 0 && (
|
||||
<div className="rounded-xl border bg-card p-4 max-h-80 overflow-x-auto overflow-y-auto space-y-0.5">
|
||||
{log.map((line) => (
|
||||
<div
|
||||
key={line}
|
||||
className="text-xs font-mono text-muted-foreground whitespace-nowrap"
|
||||
>
|
||||
{line}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{result && result.sources > 0 && (
|
||||
<div className="rounded-xl border bg-card p-5 space-y-3">
|
||||
<div className="flex items-center gap-2">
|
||||
<Check className="w-5 h-5 text-[var(--admin-success)]" />
|
||||
<span className="text-lg font-semibold">Sync Complete</span>
|
||||
</div>
|
||||
<div className="text-sm space-y-1">
|
||||
<p>
|
||||
<span className="text-muted-foreground">Succeeded:</span>{" "}
|
||||
{result.succeeded}
|
||||
</p>
|
||||
<p>
|
||||
<span className="text-muted-foreground">Failed:</span>{" "}
|
||||
{result.failed}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!syncing && !result && (
|
||||
<div className="rounded-xl border bg-card p-6 text-center text-muted-foreground text-sm">
|
||||
<CloudDownload className="w-10 h-10 mx-auto mb-3 opacity-40" />
|
||||
<p>
|
||||
Click the button above to scan all clone sources for missing
|
||||
furniture and import it automatically.
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
return <FurnitureSyncQueue kind="clone" />;
|
||||
}
|
||||
@@ -1,205 +1,7 @@
|
||||
import { inArray } from "drizzle-orm";
|
||||
import { withAdmin } from "@/lib/api-handler";
|
||||
import { db, ItemsBase } from "@/lib/db";
|
||||
import { PERMS } from "@/lib/permissions";
|
||||
import { logServerError } from "@/lib/server-log";
|
||||
import { logAudit } from "@/lib/services/audit";
|
||||
import {
|
||||
cloneSingleFurni,
|
||||
fetchSourceFurnidata,
|
||||
type SourceFurni,
|
||||
} from "@/lib/services/clone-import";
|
||||
import { listSources } from "@/lib/services/clone-sources";
|
||||
import {
|
||||
appendFurniEntriesBatch,
|
||||
appendFurniEntry,
|
||||
} from "@/lib/services/furni-data";
|
||||
import { runSseBatch } from "@/lib/services/import/core/sse-batch";
|
||||
|
||||
function sseEvent(data: unknown): string {
|
||||
return `data: ${JSON.stringify(data)}\n\n`;
|
||||
}
|
||||
|
||||
import { enqueueFurnitureSync } from "@/lib/services/furni-sync-queue";
|
||||
export const POST = withAdmin(
|
||||
{ permission: PERMS.ASSETS_IMPORT },
|
||||
async (request, ctx) => {
|
||||
const body = await request.json().catch(() => ({}));
|
||||
const sourceIdFilter: string | undefined = body.sourceId || undefined;
|
||||
|
||||
const allSources = await listSources();
|
||||
const sources = sourceIdFilter
|
||||
? allSources.filter((s) => s.id === sourceIdFilter)
|
||||
: allSources;
|
||||
|
||||
if (sources.length === 0) {
|
||||
return new Response(
|
||||
sseEvent({ type: "error", message: "No sources configured" }),
|
||||
{
|
||||
status: 400,
|
||||
headers: { "Content-Type": "text/event-stream" },
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// Pre-fetch furnidata ONCE per source and build a lookup map.
|
||||
// The old code re-fetched inside the worker loop for every item,
|
||||
// causing 2000+ redundant DB reads + HTTP requests.
|
||||
const sourceFurniDataMap = new Map<string, Map<string, SourceFurni>>();
|
||||
|
||||
for (const source of sources) {
|
||||
try {
|
||||
const entries = await fetchSourceFurnidata(source.furnidataUrl);
|
||||
const byClassname = new Map(entries.map((e) => [e.classname, e]));
|
||||
sourceFurniDataMap.set(source.id, byClassname);
|
||||
} catch (error) {
|
||||
logServerError("clone-import.furnidata_prefetch_failed", error, {
|
||||
source: source.id,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Collect all missing items using pre-fetched data
|
||||
const allItems: Array<{
|
||||
classname: string;
|
||||
sourceName: string;
|
||||
sourceId: string;
|
||||
}> = [];
|
||||
|
||||
for (const source of sources) {
|
||||
const byClassname = sourceFurniDataMap.get(source.id);
|
||||
if (!byClassname) continue;
|
||||
|
||||
const classnames = [...byClassname.keys()];
|
||||
if (classnames.length === 0) continue;
|
||||
|
||||
const rows = await db
|
||||
.select({ item_name: ItemsBase.itemName })
|
||||
.from(ItemsBase)
|
||||
.where(inArray(ItemsBase.itemName, classnames));
|
||||
const have = new Set(rows.map((r) => r.item_name));
|
||||
|
||||
for (const classname of classnames) {
|
||||
if (!have.has(classname)) {
|
||||
allItems.push({
|
||||
classname,
|
||||
sourceName: source.name,
|
||||
sourceId: source.id,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (allItems.length === 0) {
|
||||
return new Response(
|
||||
sseEvent({
|
||||
type: "batch_complete",
|
||||
succeeded: 0,
|
||||
failed: 0,
|
||||
skipped: true,
|
||||
message: "All sources are up to date",
|
||||
}),
|
||||
{ headers: { "Content-Type": "text/event-stream" } },
|
||||
);
|
||||
}
|
||||
|
||||
if (allItems.length > 2000) {
|
||||
return new Response(
|
||||
sseEvent({
|
||||
type: "error",
|
||||
message: `Too many items to sync (${allItems.length}). Sync individual sources instead.`,
|
||||
}),
|
||||
{
|
||||
status: 400,
|
||||
headers: { "Content-Type": "text/event-stream" },
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// FurnitureData entries are appended once at the end (single write) rather
|
||||
// than once per item, removing the main serialization bottleneck.
|
||||
const deferredEntries: Array<{
|
||||
entry: Record<string, unknown>;
|
||||
itemType: string;
|
||||
}> = [];
|
||||
|
||||
// Source lookup cache — pre-fetched above, no need to re-fetch per item
|
||||
const sourceMap = new Map(sources.map((s) => [s.id, s]));
|
||||
|
||||
return runSseBatch({
|
||||
items: allItems,
|
||||
concurrency: 10,
|
||||
signal: request.signal,
|
||||
labelOf: (it) => `${it.sourceName}/${it.classname}`,
|
||||
worker: async (it, _index, report) => {
|
||||
const source = sourceMap.get(it.sourceId);
|
||||
if (!source) {
|
||||
return { ok: false, error: "source not found" };
|
||||
}
|
||||
|
||||
// Use pre-fetched furnidata instead of re-fetching
|
||||
const byClassname = sourceFurniDataMap.get(it.sourceId);
|
||||
if (!byClassname) {
|
||||
return {
|
||||
ok: false,
|
||||
error: "furnidata not available for this source",
|
||||
};
|
||||
}
|
||||
|
||||
const entry = byClassname.get(it.classname);
|
||||
if (!entry) {
|
||||
return {
|
||||
ok: false,
|
||||
error: "classname not found in source furnidata",
|
||||
};
|
||||
}
|
||||
|
||||
const result = await cloneSingleFurni({
|
||||
source,
|
||||
entry,
|
||||
onProgress: report,
|
||||
deferFurniData: true,
|
||||
});
|
||||
|
||||
if (result.ok) {
|
||||
if (result.furniDataEntry) {
|
||||
deferredEntries.push(result.furniDataEntry);
|
||||
}
|
||||
logAudit({
|
||||
userId: ctx.session.user.id,
|
||||
action: "auto_sync_furni",
|
||||
target: "FurniAsset",
|
||||
targetId: 0,
|
||||
after: {
|
||||
classname: it.classname,
|
||||
sourceId: it.sourceId,
|
||||
sourceName: it.sourceName,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
ok: result.ok,
|
||||
warnings: result.warnings,
|
||||
error: result.error,
|
||||
};
|
||||
},
|
||||
flush: async () => {
|
||||
if (deferredEntries.length === 0) return;
|
||||
try {
|
||||
await appendFurniEntriesBatch(deferredEntries);
|
||||
} catch (err) {
|
||||
for (const { entry, itemType } of deferredEntries) {
|
||||
try {
|
||||
await appendFurniEntry(entry, itemType);
|
||||
} catch {
|
||||
/* best effort */
|
||||
}
|
||||
}
|
||||
throw new Error(
|
||||
`FurnitureData batch write failed: ${(err as Error).message}`,
|
||||
);
|
||||
}
|
||||
},
|
||||
});
|
||||
},
|
||||
(request, ctx) => enqueueFurnitureSync(request, ctx.session.user.id, "clone"),
|
||||
);
|
||||
@@ -1,97 +1,8 @@
|
||||
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";
|
||||
|
||||
import { enqueueFurnitureSync } from "@/lib/services/furni-sync-queue";
|
||||
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
|
||||
},
|
||||
});
|
||||
},
|
||||
(request, ctx) =>
|
||||
enqueueFurnitureSync(request, ctx.session.user.id, "official"),
|
||||
);
|
||||
@@ -2,6 +2,7 @@ import { after } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { apiError, apiOk } from "@/lib/api";
|
||||
import { withAdmin } from "@/lib/api-handler";
|
||||
import { getRequestId } from "@/lib/foundation/request-context";
|
||||
import { validateClassnames } from "@/lib/furni/studio-inspection";
|
||||
import { PERMS } from "@/lib/permission-slugs";
|
||||
import { redis } from "@/lib/redis";
|
||||
@@ -92,6 +93,7 @@ export const POST = withAdmin(
|
||||
const job = await store.create({
|
||||
...body,
|
||||
userId: ctx.session.user.id,
|
||||
operationId: getRequestId(),
|
||||
createdAt,
|
||||
updatedAt: createdAt,
|
||||
state: "queued",
|
||||
|
||||
Reference in new issue
Block a user