50 lines
1.5 KiB
TypeScript
50 lines
1.5 KiB
TypeScript
import { requireDm } from "@/lib/session";
|
|
import { json, err, asResponse } from "@/lib/api";
|
|
import { applyPlan, parseAzgaar } from "@/lib/import";
|
|
import { prisma } from "@/lib/db";
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
export async function POST(req: Request) {
|
|
try {
|
|
await requireDm();
|
|
const url = new URL(req.url);
|
|
const action = url.searchParams.get("action") ?? "preview";
|
|
const b = (await req.json()) as Partial<{
|
|
input: string;
|
|
isPublic: boolean;
|
|
mapWidth: number;
|
|
mapHeight: number;
|
|
createLayer: boolean;
|
|
layerName: string;
|
|
layerImage: string; // optional data: URL
|
|
}>;
|
|
if (!b.input || typeof b.input !== "string")
|
|
return err("input (string) required");
|
|
const plan = parseAzgaar(b.input, {
|
|
isPublic: b.isPublic,
|
|
mapWidth: b.mapWidth,
|
|
mapHeight: b.mapHeight,
|
|
});
|
|
if (action === "apply") {
|
|
let layerId: string | null = null;
|
|
if (b.createLayer ?? plan.pins?.length) {
|
|
const layer = await prisma.mapLayer.create({
|
|
data: {
|
|
name: (b.layerName ?? plan.layerName ?? "Azgaar import").trim(),
|
|
imageData: b.layerImage ?? "",
|
|
width: b.mapWidth ?? 1000,
|
|
height: b.mapHeight ?? 1000,
|
|
},
|
|
});
|
|
layerId = layer.id;
|
|
}
|
|
const result = await applyPlan(plan, { layerId });
|
|
return json({ plan, result, layerId });
|
|
}
|
|
return json({ plan });
|
|
} catch (e) {
|
|
return asResponse(e);
|
|
}
|
|
}
|