39 lines
1.3 KiB
TypeScript
39 lines
1.3 KiB
TypeScript
import { db } from "../../../db/index";
|
|
import { topics, lessons } from "../../../db/schema";
|
|
import { eq, and } from "drizzle-orm";
|
|
import { generateLesson } from "../../../utils/generateLesson";
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const id = getRouterParam(event, "id")!;
|
|
|
|
const topic = await db.query.topics.findFirst({ where: eq(topics.id, id) });
|
|
if (!topic) throw createError({ statusCode: 404, message: "Topic not found" });
|
|
|
|
if (topic.status === "ready") {
|
|
return { status: "ready" };
|
|
}
|
|
|
|
if (topic.status === "generating") {
|
|
return { status: "generating" };
|
|
}
|
|
|
|
try {
|
|
await generateLesson(id);
|
|
|
|
// pre-generate the next topic silently
|
|
const currentTopic = await db.query.topics.findFirst({ where: eq(topics.id, id) });
|
|
const nextTopic = await db.query.topics.findFirst({
|
|
where: and(
|
|
eq(topics.courseId, currentTopic!.courseId),
|
|
eq(topics.order, currentTopic!.order + 1),
|
|
eq(topics.status, "pending")
|
|
)
|
|
});
|
|
if (nextTopic) generateLesson(nextTopic.id).catch((e) => console.error("[pre-gen]", e));
|
|
|
|
return { status: "ready" };
|
|
} catch (err: any) {
|
|
console.error(`[generate.post] topic ${id} failed: ${err?.message ?? err}`);
|
|
return { status: "error" };
|
|
}
|
|
});
|