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();
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user