missing nextOrderIndex for course added

This commit is contained in:
2026-07-20 09:25:16 +08:00
parent 72a792d585
commit b401c1b53a
2 changed files with 28 additions and 2 deletions
+27 -2
View File
@@ -65,6 +65,11 @@ async function getUnitLessonLink(unitId, lessonId, transaction) {
exports.getCourses = async (req, res) => { exports.getCourses = async (req, res) => {
try { try {
// Default to catalog order (same field the client-facing course list sorts
// by) so Move Up/Down is reflected on refetch; an explicit column sort from
// the table header still takes priority.
if (!req.query.sort) req.query.sort = JSON.stringify([{ id: "order_index", desc: false }]);
const result = await paginate(Course, req, { const result = await paginate(Course, req, {
excludeAttributes: courseExclude, excludeAttributes: courseExclude,
computedAttributes: [ computedAttributes: [
@@ -145,7 +150,7 @@ exports.createCourse = async (req, res) => {
const course = await Course.create({ const course = await Course.create({
title, title,
description: description ?? null, description: description ?? null,
order_index: order_index ?? 0, order_index: order_index ?? await nextOrderIndex(Course, {}, t),
course_code: course_code ?? null, course_code: course_code ?? null,
level: level ?? null, level: level ?? null,
subscription: subscription ?? "free", subscription: subscription ?? "free",
@@ -260,7 +265,7 @@ exports.createCourseFull = async (req, res) => {
const course = await Course.create({ const course = await Course.create({
title, title,
description: description ?? null, description: description ?? null,
order_index: order_index ?? 0, order_index: order_index ?? await nextOrderIndex(Course, {}, t),
course_code: course_code ?? null, course_code: course_code ?? null,
level: level ?? null, level: level ?? null,
subscription: subscription ?? "free", subscription: subscription ?? "free",
@@ -443,6 +448,26 @@ exports.updateCourse = async (req, res) => {
} }
}; };
exports.reorderCourses = async (req, res) => {
const t = await sequelize.transaction();
try {
const { course_ids = [] } = req.body;
if (!course_ids.length) return R.error(res, "course_ids is required.", 400);
await Promise.all(course_ids.map((id, i) =>
Course.update({ order_index: i }, { where: { course_id: id, ...notDeleted }, transaction: t })
));
await t.commit();
logActivity(req.user?.user_id, "reorder_courses", { entityType: "course", details: { course_ids } });
return R.success(res, "Course order updated.");
} catch (err) {
await t.rollback();
console.error("[COURSE][REORDER]", err);
return R.error(res, "Could not reorder courses.", 500);
}
};
exports.archiveCourse = async (req, res) => { exports.archiveCourse = async (req, res) => {
const t = await sequelize.transaction(); const t = await sequelize.transaction();
try { try {
+1
View File
@@ -28,6 +28,7 @@ router.get("/by-subscription", ctrl.getCoursesBySubscription);
router.get("/units-flat", ctrl.getUnitsFlat); router.get("/units-flat", ctrl.getUnitsFlat);
router.get("/lessons-flat", ctrl.getLessonsFlat); router.get("/lessons-flat", ctrl.getLessonsFlat);
router.get("/quizzes-flat", ctrl.getQuizzesFlat); router.get("/quizzes-flat", ctrl.getQuizzesFlat);
router.put("/order", ctrl.reorderCourses); // persist top-level course catalog ordering { course_ids }
// ── then :courseId ──────────────────────────────────────────────────────────── // ── then :courseId ────────────────────────────────────────────────────────────
router.get("/archives/:courseId", ctrl.getArchivedCourse); router.get("/archives/:courseId", ctrl.getArchivedCourse);