mirror of
https://github.com/rgrgogu/new_starr.git
synced 2026-09-27 00:12:54 +08:00
Adjusted
This commit is contained in:
@@ -643,20 +643,17 @@ exports.updateLesson = async (req, res) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.deleteLesson = async (req, res) => {
|
exports.archiveLesson = async (req, res) => {
|
||||||
const t = await sequelize.transaction();
|
const t = await sequelize.transaction();
|
||||||
try {
|
try {
|
||||||
const { courseId, unitId, lessonId } = req.params;
|
const { courseId, unitId, lessonId } = req.params;
|
||||||
|
|
||||||
const lesson = await Lesson.findOne({
|
const record = await archiveOne(Lesson, {
|
||||||
where: { lesson_id: lessonId, unit_id: unitId, ...notDeleted },
|
where: { lesson_id: lessonId, unit_id: unitId, ...notDeleted },
|
||||||
include: [{ model: Unit, as: "unit", where: { course_id: courseId, ...notDeleted } }],
|
include: [{ model: Unit, as: "unit", where: { course_id: courseId, ...notDeleted } }],
|
||||||
});
|
}, req.user.user_id, t);
|
||||||
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 });
|
|
||||||
|
|
||||||
|
if (!record) return R.error(res, "Lesson not found.", 404);
|
||||||
await t.commit();
|
await t.commit();
|
||||||
return R.success(res, "Lesson archived.");
|
return R.success(res, "Lesson archived.");
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -670,7 +667,7 @@ exports.bulkArchiveLessons = async (req, res) => {
|
|||||||
const t = await sequelize.transaction();
|
const t = await sequelize.transaction();
|
||||||
try {
|
try {
|
||||||
const { courseId, unitId } = req.params;
|
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);
|
if (!ids.length) return R.error(res, "No IDs provided.", 400);
|
||||||
|
|
||||||
const lessons = await Lesson.findAll({
|
const lessons = await Lesson.findAll({
|
||||||
@@ -679,7 +676,7 @@ exports.bulkArchiveLessons = async (req, res) => {
|
|||||||
});
|
});
|
||||||
const validIds = lessons.map((l) => l.lesson_id);
|
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();
|
await t.commit();
|
||||||
return R.success(res, `${count} lesson${count !== 1 ? "s" : ""} archived.`);
|
return R.success(res, `${count} lesson${count !== 1 ? "s" : ""} archived.`);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -699,7 +696,7 @@ exports.getArchivedLessons = async (req, res) => {
|
|||||||
const result = await paginate(Lesson, req, {
|
const result = await paginate(Lesson, req, {
|
||||||
excludeAttributes: adminExclude,
|
excludeAttributes: adminExclude,
|
||||||
auditOptions: { mdl_Users, parentAlias: "Lesson" },
|
auditOptions: { mdl_Users, parentAlias: "Lesson" },
|
||||||
context: "list",
|
context: "archived",
|
||||||
findOptions: {
|
findOptions: {
|
||||||
where: { unit_id: unitId, ...onlyDeleted },
|
where: { unit_id: unitId, ...onlyDeleted },
|
||||||
paranoid: false,
|
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 });
|
const unit = await Unit.findOne({ where: { unit_id: unitId, course_id: courseId }, paranoid: false });
|
||||||
if (!unit) return R.error(res, "Unit not found.", 404);
|
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);
|
if (!record) return R.error(res, "Archived lesson not found.", 404);
|
||||||
await t.commit();
|
await t.commit();
|
||||||
return R.success(res, "Lesson restored.", { data: record });
|
return R.success(res, "Lesson restored.", { data: record });
|
||||||
@@ -756,7 +753,7 @@ exports.bulkRestoreLessons = async (req, res) => {
|
|||||||
const t = await sequelize.transaction();
|
const t = await sequelize.transaction();
|
||||||
try {
|
try {
|
||||||
const { courseId, unitId } = req.params;
|
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);
|
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 });
|
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 lessons = await Lesson.findAll({ where: { lesson_id: ids, unit_id: unitId, ...onlyDeleted }, paranoid: false });
|
||||||
const validIds = lessons.map((l) => l.lesson_id);
|
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();
|
await t.commit();
|
||||||
return R.success(res, `${count} lesson${count !== 1 ? "s" : ""} restored.`);
|
return R.success(res, `${count} lesson${count !== 1 ? "s" : ""} restored.`);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -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.patch("/:courseId/units/:unitId/lessons/:lessonId/restore", ctrl.restoreLesson);
|
||||||
router.get("/:courseId/units/:unitId/lessons/:lessonId", ctrl.getLesson);
|
router.get("/:courseId/units/:unitId/lessons/:lessonId", ctrl.getLesson);
|
||||||
router.put("/:courseId/units/:unitId/lessons/:lessonId", ctrl.updateLesson);
|
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
|
// LESSON PAGE
|
||||||
|
|||||||
@@ -3,8 +3,8 @@
|
|||||||
/**
|
/**
|
||||||
* Single archive — soft delete one record
|
* Single archive — soft delete one record
|
||||||
*/
|
*/
|
||||||
async function archiveOne(Model, where, deletedBy, transaction) {
|
async function archiveOne(Model, options, deletedBy, transaction) {
|
||||||
const record = await Model.findOne({ where });
|
const record = await Model.findOne(options);
|
||||||
if (!record) return null;
|
if (!record) return null;
|
||||||
await record.update({ deletedBy: deletedBy ?? null }, { transaction });
|
await record.update({ deletedBy: deletedBy ?? null }, { transaction });
|
||||||
await record.destroy({ transaction });
|
await record.destroy({ transaction });
|
||||||
|
|||||||
Reference in New Issue
Block a user