fix(lessons): cascade delete descendants in service (no FK on parent_id)

This commit is contained in:
2026-05-20 20:48:42 +02:00
parent 8af8ad54fa
commit fcad3d252e

View File

@@ -1,4 +1,4 @@
import { eq, isNull, sql } from 'drizzle-orm';
import { eq, inArray, isNull, sql } from 'drizzle-orm';
import type { Db } from '../db/client.js';
import { cards, lessons } from '../db/schema.js';
import { ApiError } from '../lib/errors.js';
@@ -60,8 +60,10 @@ export async function updateLesson(db: Db, id: number, input: LessonUpdateInput)
}
export async function deleteLesson(db: Db, id: number): Promise<void> {
const r = db.delete(lessons).where(eq(lessons.id, id)).run();
if (r.changes === 0) throw ApiError.notFound('Lesson');
const existing = db.select().from(lessons).where(eq(lessons.id, id)).get();
if (!existing) throw ApiError.notFound('Lesson');
const ids = await getDescendantLessonIds(db, id);
db.delete(lessons).where(inArray(lessons.id, ids)).run();
}
export async function moveLesson(db: Db, id: number, input: LessonMoveInput): Promise<Lesson> {