missing nextOrderIndex for course added
This commit is contained in:
@@ -65,6 +65,11 @@ async function getUnitLessonLink(unitId, lessonId, transaction) {
|
||||
|
||||
exports.getCourses = async (req, res) => {
|
||||
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, {
|
||||
excludeAttributes: courseExclude,
|
||||
computedAttributes: [
|
||||
@@ -145,7 +150,7 @@ exports.createCourse = async (req, res) => {
|
||||
const course = await Course.create({
|
||||
title,
|
||||
description: description ?? null,
|
||||
order_index: order_index ?? 0,
|
||||
order_index: order_index ?? await nextOrderIndex(Course, {}, t),
|
||||
course_code: course_code ?? null,
|
||||
level: level ?? null,
|
||||
subscription: subscription ?? "free",
|
||||
@@ -260,7 +265,7 @@ exports.createCourseFull = async (req, res) => {
|
||||
const course = await Course.create({
|
||||
title,
|
||||
description: description ?? null,
|
||||
order_index: order_index ?? 0,
|
||||
order_index: order_index ?? await nextOrderIndex(Course, {}, t),
|
||||
course_code: course_code ?? null,
|
||||
level: level ?? null,
|
||||
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) => {
|
||||
const t = await sequelize.transaction();
|
||||
try {
|
||||
|
||||
@@ -28,6 +28,7 @@ router.get("/by-subscription", ctrl.getCoursesBySubscription);
|
||||
router.get("/units-flat", ctrl.getUnitsFlat);
|
||||
router.get("/lessons-flat", ctrl.getLessonsFlat);
|
||||
router.get("/quizzes-flat", ctrl.getQuizzesFlat);
|
||||
router.put("/order", ctrl.reorderCourses); // persist top-level course catalog ordering { course_ids }
|
||||
|
||||
// ── then :courseId ────────────────────────────────────────────────────────────
|
||||
router.get("/archives/:courseId", ctrl.getArchivedCourse);
|
||||
|
||||
Reference in New Issue
Block a user