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 });
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) {
console.error("[LESSON LIB][PAGE][DURATION]", durErr);
}
+14 -3
View File
@@ -31,6 +31,9 @@ function estimateBlockDuration(block) {
case "document":
return readingSecs(block.content?.body);
case "code":
return readingSecs(block.content?.code);
case "image":
return 60;
@@ -95,16 +98,24 @@ async function recomputeCourseDuration(courseId) {
* Recompute and persist duration_seconds up the chain:
* 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.
*
* `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 LessonPage = require("../models/courses/lesson_page.mdl");
const UnitLesson = require("../models/courses/unit_lessons.mdl");
const CourseUnit = require("../models/courses/course_units.mdl");
// 1. Lesson duration from blocks
const page = await LessonPage.findOne({ where: { lesson_id: lessonId } });
const lessonSecs = (page?.blocks ?? []).reduce(
let blocks = blocksOverride;
if (!blocks) {
const page = await LessonPage.findOne({ where: { lesson_id: lessonId } });
blocks = page?.blocks ?? [];
}
const lessonSecs = blocks.reduce(
(sum, block) => sum + estimateBlockDuration(block), 0
);