From 457c555df6b8ec7fbcc2a4f2020b4508dd82fc77 Mon Sep 17 00:00:00 2001 From: rgrgogu Date: Thu, 21 May 2026 13:27:31 +0800 Subject: [PATCH] Adjusted --- controllers/admin/courses.controller.js | 23 ++++++++++------------- routes/admin/courses.routes.js | 2 +- utils/courses/archive.util.js | 4 ++-- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/controllers/admin/courses.controller.js b/controllers/admin/courses.controller.js index b36ef2e..3694d7e 100644 --- a/controllers/admin/courses.controller.js +++ b/controllers/admin/courses.controller.js @@ -643,20 +643,17 @@ exports.updateLesson = async (req, res) => { } }; -exports.deleteLesson = async (req, res) => { +exports.archiveLesson = async (req, res) => { const t = await sequelize.transaction(); try { const { courseId, unitId, lessonId } = req.params; - const lesson = await Lesson.findOne({ + const record = await archiveOne(Lesson, { where: { lesson_id: lessonId, unit_id: unitId, ...notDeleted }, include: [{ model: Unit, as: "unit", where: { course_id: courseId, ...notDeleted } }], - }); - if (!lesson) return R.error(res, "Lesson not found.", 404); - - await lesson.update({ deletedBy: req.body.deletedBy ?? null }, { transaction: t }); - await lesson.destroy({ transaction: t }); + }, req.user.user_id, t); + if (!record) return R.error(res, "Lesson not found.", 404); await t.commit(); return R.success(res, "Lesson archived."); } catch (err) { @@ -670,7 +667,7 @@ exports.bulkArchiveLessons = async (req, res) => { const t = await sequelize.transaction(); try { const { courseId, unitId } = req.params; - const { ids = [], deletedBy } = req.body; + const { ids = [] } = req.body; if (!ids.length) return R.error(res, "No IDs provided.", 400); const lessons = await Lesson.findAll({ @@ -679,7 +676,7 @@ exports.bulkArchiveLessons = async (req, res) => { }); const validIds = lessons.map((l) => l.lesson_id); - const count = await archiveMany(Lesson, "lesson_id", validIds, deletedBy, t); + const count = await archiveMany(Lesson, "lesson_id", validIds, req.user.user_id, t); await t.commit(); return R.success(res, `${count} lesson${count !== 1 ? "s" : ""} archived.`); } catch (err) { @@ -699,7 +696,7 @@ exports.getArchivedLessons = async (req, res) => { const result = await paginate(Lesson, req, { excludeAttributes: adminExclude, auditOptions: { mdl_Users, parentAlias: "Lesson" }, - context: "list", + context: "archived", findOptions: { where: { unit_id: unitId, ...onlyDeleted }, paranoid: false, @@ -741,7 +738,7 @@ exports.restoreLesson = async (req, res) => { const unit = await Unit.findOne({ where: { unit_id: unitId, course_id: courseId }, paranoid: false }); if (!unit) return R.error(res, "Unit not found.", 404); - const record = await restoreOne(Lesson, { lesson_id: lessonId, unit_id: unitId, ...onlyDeleted }, req.body.restoredBy, t); + const record = await restoreOne(Lesson, { lesson_id: lessonId, unit_id: unitId, ...onlyDeleted }, req.user.user_id, t); if (!record) return R.error(res, "Archived lesson not found.", 404); await t.commit(); return R.success(res, "Lesson restored.", { data: record }); @@ -756,7 +753,7 @@ exports.bulkRestoreLessons = async (req, res) => { const t = await sequelize.transaction(); try { const { courseId, unitId } = req.params; - const { ids = [], restoredBy } = req.body; + const { ids = [] } = req.body; if (!ids.length) return R.error(res, "No IDs provided.", 400); const unit = await Unit.findOne({ where: { unit_id: unitId, course_id: courseId }, paranoid: false }); @@ -765,7 +762,7 @@ exports.bulkRestoreLessons = async (req, res) => { const lessons = await Lesson.findAll({ where: { lesson_id: ids, unit_id: unitId, ...onlyDeleted }, paranoid: false }); const validIds = lessons.map((l) => l.lesson_id); - const count = await restoreMany(Lesson, "lesson_id", validIds, restoredBy, t); + const count = await restoreMany(Lesson, "lesson_id", validIds, req.user.user_id, t); await t.commit(); return R.success(res, `${count} lesson${count !== 1 ? "s" : ""} restored.`); } catch (err) { diff --git a/routes/admin/courses.routes.js b/routes/admin/courses.routes.js index d286fed..47c1b86 100644 --- a/routes/admin/courses.routes.js +++ b/routes/admin/courses.routes.js @@ -125,7 +125,7 @@ router.get("/:courseId/units/:unitId/lessons/archives/:lessonId", ctrl.getArchiv router.patch("/:courseId/units/:unitId/lessons/:lessonId/restore", ctrl.restoreLesson); router.get("/:courseId/units/:unitId/lessons/:lessonId", ctrl.getLesson); router.put("/:courseId/units/:unitId/lessons/:lessonId", ctrl.updateLesson); -router.delete("/:courseId/units/:unitId/lessons/:lessonId", ctrl.deleteLesson); +router.delete("/:courseId/units/:unitId/lessons/:lessonId", ctrl.archiveLesson); // ══════════════════════════════════════════════════════════════════════════════ // LESSON PAGE diff --git a/utils/courses/archive.util.js b/utils/courses/archive.util.js index c357d2d..05c0607 100644 --- a/utils/courses/archive.util.js +++ b/utils/courses/archive.util.js @@ -3,8 +3,8 @@ /** * Single archive — soft delete one record */ -async function archiveOne(Model, where, deletedBy, transaction) { - const record = await Model.findOne({ where }); +async function archiveOne(Model, options, deletedBy, transaction) { + const record = await Model.findOne(options); if (!record) return null; await record.update({ deletedBy: deletedBy ?? null }, { transaction }); await record.destroy({ transaction });