diff --git a/controllers/admin/lessons.controller.js b/controllers/admin/lessons.controller.js index 3d59634..da3082c 100644 --- a/controllers/admin/lessons.controller.js +++ b/controllers/admin/lessons.controller.js @@ -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); } diff --git a/utils/duration.util.js b/utils/duration.util.js index e5614e3..fdad7f7 100644 --- a/utils/duration.util.js +++ b/utils/duration.util.js @@ -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 );