This commit is contained in:
2026-07-20 06:38:33 +08:00
parent 36124cc4a1
commit 1290219077
2 changed files with 17 additions and 4 deletions
+3 -1
View File
@@ -444,7 +444,9 @@ exports.upsertLessonPage = async (req, res) => {
}, { returning: true }); }, { returning: true });
try { try {
await recomputeDurations(lessonId); // Pass the blocks we just wrote directly instead of re-reading the page —
// avoids depending on read-after-write visibility of the upsert we just did.
await recomputeDurations(lessonId, blocks);
} catch (durErr) { } catch (durErr) {
console.error("[LESSON LIB][PAGE][DURATION]", durErr); console.error("[LESSON LIB][PAGE][DURATION]", durErr);
} }
+13 -2
View File
@@ -31,6 +31,9 @@ function estimateBlockDuration(block) {
case "document": case "document":
return readingSecs(block.content?.body); return readingSecs(block.content?.body);
case "code":
return readingSecs(block.content?.code);
case "image": case "image":
return 60; return 60;
@@ -95,16 +98,24 @@ async function recomputeCourseDuration(courseId) {
* Recompute and persist duration_seconds up the chain: * Recompute and persist duration_seconds up the chain:
* blocks → lesson → every attached unit → every course those units are attached to. * blocks → lesson → every attached unit → every course those units are attached to.
* A lesson may live in many units, and a unit in many courses, so all parents refresh. * A lesson may live in many units, and a unit in many courses, so all parents refresh.
*
* `blocksOverride` lets a caller that just wrote the page (e.g. upsertLessonPage,
* which already has the upserted row in hand) skip the re-read — avoids relying on
* read-after-write visibility of the just-committed upsert.
*/ */
async function recomputeDurations(lessonId) { async function recomputeDurations(lessonId, blocksOverride = null) {
const Lesson = require("../models/courses/lessons.mdl"); const Lesson = require("../models/courses/lessons.mdl");
const LessonPage = require("../models/courses/lesson_page.mdl"); const LessonPage = require("../models/courses/lesson_page.mdl");
const UnitLesson = require("../models/courses/unit_lessons.mdl"); const UnitLesson = require("../models/courses/unit_lessons.mdl");
const CourseUnit = require("../models/courses/course_units.mdl"); const CourseUnit = require("../models/courses/course_units.mdl");
// 1. Lesson duration from blocks // 1. Lesson duration from blocks
let blocks = blocksOverride;
if (!blocks) {
const page = await LessonPage.findOne({ where: { lesson_id: lessonId } }); const page = await LessonPage.findOne({ where: { lesson_id: lessonId } });
const lessonSecs = (page?.blocks ?? []).reduce( blocks = page?.blocks ?? [];
}
const lessonSecs = blocks.reduce(
(sum, block) => sum + estimateBlockDuration(block), 0 (sum, block) => sum + estimateBlockDuration(block), 0
); );