mirror of
https://github.com/rgrgogu/new_starr.git
synced 2026-09-27 00:12:54 +08:00
+22
-14
@@ -59,9 +59,10 @@ async function recomputeUnitDuration(unitId) {
|
||||
const Unit = require("../models/courses/units.mdl");
|
||||
|
||||
const [unitResult] = await Lesson.sequelize.query(`
|
||||
SELECT COALESCE(SUM(duration_seconds), 0) AS total
|
||||
FROM lessons
|
||||
WHERE unit_id = :unitId AND "deletedAt" IS NULL
|
||||
SELECT COALESCE(SUM(l.duration_seconds), 0) AS total
|
||||
FROM lessons l
|
||||
JOIN unit_lessons ul ON ul.lesson_id = l.lesson_id
|
||||
WHERE ul.unit_id = :unitId AND l."deletedAt" IS NULL
|
||||
`, { replacements: { unitId }, type: Lesson.sequelize.QueryTypes.SELECT });
|
||||
|
||||
await Unit.update({ duration_seconds: unitResult.total }, { where: { unit_id: unitId } });
|
||||
@@ -77,9 +78,10 @@ async function recomputeCourseDuration(courseId) {
|
||||
const { Course } = require("../models/courses/courses.mdl");
|
||||
|
||||
const [courseResult] = await Unit.sequelize.query(`
|
||||
SELECT COALESCE(SUM(duration_seconds), 0) AS total
|
||||
FROM units
|
||||
WHERE course_id = :courseId AND "deletedAt" IS NULL
|
||||
SELECT COALESCE(SUM(u.duration_seconds), 0) AS total
|
||||
FROM units u
|
||||
JOIN course_units cu ON cu.unit_id = u.unit_id
|
||||
WHERE cu.course_id = :courseId AND u."deletedAt" IS NULL
|
||||
`, { replacements: { courseId }, type: Unit.sequelize.QueryTypes.SELECT });
|
||||
|
||||
await Course.update({ duration_seconds: courseResult.total }, { where: { course_id: courseId } });
|
||||
@@ -87,12 +89,14 @@ async function recomputeCourseDuration(courseId) {
|
||||
|
||||
/**
|
||||
* Recompute and persist duration_seconds up the chain:
|
||||
* blocks → lesson → unit → course
|
||||
* 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.
|
||||
*/
|
||||
async function recomputeDurations(lessonId) {
|
||||
const Lesson = require("../models/courses/lessons.mdl");
|
||||
const Unit = require("../models/courses/units.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 } });
|
||||
@@ -104,13 +108,17 @@ async function recomputeDurations(lessonId) {
|
||||
const safeLessonSecs = isNaN(lessonSecs) ? 0 : lessonSecs;
|
||||
await Lesson.update({ duration_seconds: safeLessonSecs }, { where: { lesson_id: lessonId } });
|
||||
|
||||
// 2. Unit duration — sum of its lessons
|
||||
const lesson = await Lesson.findOne({ where: { lesson_id: lessonId } });
|
||||
await recomputeUnitDuration(lesson.unit_id);
|
||||
// 2. Every unit that contains this lesson
|
||||
const unitLinks = await UnitLesson.findAll({ where: { lesson_id: lessonId }, attributes: ["unit_id"] });
|
||||
const unitIds = [...new Set(unitLinks.map((l) => String(l.unit_id)))];
|
||||
for (const unitId of unitIds) await recomputeUnitDuration(unitId);
|
||||
|
||||
// 3. Course duration — sum of its units
|
||||
const unit = await Unit.findOne({ where: { unit_id: lesson.unit_id } });
|
||||
await recomputeCourseDuration(unit.course_id);
|
||||
// 3. Every course that contains those units
|
||||
if (unitIds.length) {
|
||||
const courseLinks = await CourseUnit.findAll({ where: { unit_id: unitIds }, attributes: ["course_id"] });
|
||||
const courseIds = [...new Set(courseLinks.map((l) => String(l.course_id)))];
|
||||
for (const courseId of courseIds) await recomputeCourseDuration(courseId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user