add: more things

Signed-off-by: Kenneth Obsequio <k80308392@gmail.com>
This commit is contained in:
2026-07-04 03:35:09 +08:00
parent a572c1e25f
commit 3c13bb9821
36 changed files with 896 additions and 928 deletions
+148
View File
@@ -9,6 +9,7 @@ const { syncObjectivesCreate, syncObjectivesUpdate } = require("../../utils/cour
const { syncJunction } = require("../../utils/courses/junction.util");
const { archiveOne, archiveMany } = require("../../utils/courses/archive.util");
const { restoreOne, restoreMany } = require("../../utils/courses/restore.util");
const { permanentDeleteOne, permanentDeleteMany } = require("../../utils/courses/permanentDelete.util");
const { getFieldValues } = require("../../utils/fieldValues.util");
const logActivity = require('../../utils/logActivity.util');
const UserNotification = require('../../models/notifications/user_notification.mdl');
@@ -313,6 +314,57 @@ exports.getCourseArchiveImpact = async (req, res) => {
}
};
exports.permanentlyDeleteCourse = async (req, res) => {
const t = await sequelize.transaction();
try {
const { courseId } = req.params;
const record = await permanentDeleteOne(Course, { course_id: courseId }, t);
if (record === null) return R.error(res, "Course not found.", 404);
if (record === false) return R.error(res, "Course must be archived before it can be permanently deleted.", 400);
await t.commit();
logActivity(req.user.user_id, 'permanently_delete_course', { entityType: 'course', entityId: Number(courseId) });
return R.success(res, "Course permanently deleted.");
} catch (err) {
await t.rollback();
console.error("[COURSE][PERMANENT DELETE]", err);
return R.error(res, "Could not permanently delete course.", 500);
}
};
exports.bulkPermanentlyDeleteCourses = async (req, res) => {
const t = await sequelize.transaction();
try {
const { ids = [] } = req.body;
if (!ids.length) return R.error(res, "No IDs provided.", 400);
const count = await permanentDeleteMany(Course, "course_id", ids, t);
await t.commit();
logActivity(req.user.user_id, 'bulk_permanently_delete_courses', { entityType: 'course', details: { ids, count } });
return R.success(res, `${count} course${count !== 1 ? "s" : ""} permanently deleted.`);
} catch (err) {
await t.rollback();
console.error("[COURSE][BULK PERMANENT DELETE]", err);
return R.error(res, "Could not permanently delete courses.", 500);
}
};
exports.getCoursePermanentDeleteImpact = async (req, res) => {
try {
const { courseId } = req.params;
const unitCount = await Unit.count({ where: { course_id: courseId }, paranoid: false });
const units = await Unit.findAll({ where: { course_id: courseId }, attributes: ["unit_id"], paranoid: false });
const lessonCount = units.length
? await Lesson.count({ where: { unit_id: units.map((u) => u.unit_id) }, paranoid: false })
: 0;
return R.success(res, "Impact retrieved.", { unitCount, lessonCount });
} catch (err) {
console.error("[COURSE][PERMANENT DELETE IMPACT]", err);
return R.error(res, "Could not retrieve impact.", 500);
}
};
// ══════════════════════════════════════════════════════════════════════════════
// COURSE PREREQUISITES
// ══════════════════════════════════════════════════════════════════════════════
@@ -600,6 +652,57 @@ exports.bulkRestoreUnits = async (req, res) => {
}
};
exports.permanentlyDeleteUnit = async (req, res) => {
const t = await sequelize.transaction();
try {
const { courseId, unitId } = req.params;
const record = await permanentDeleteOne(Unit, { unit_id: unitId, course_id: courseId }, t);
if (record === null) return R.error(res, "Unit not found.", 404);
if (record === false) return R.error(res, "Unit must be archived before it can be permanently deleted.", 400);
await t.commit();
logActivity(req.user.user_id, 'permanently_delete_unit', { entityType: 'unit', entityId: Number(unitId) });
return R.success(res, "Unit permanently deleted.");
} catch (err) {
await t.rollback();
console.error("[UNIT][PERMANENT DELETE]", err);
return R.error(res, "Could not permanently delete unit.", 500);
}
};
exports.bulkPermanentlyDeleteUnits = async (req, res) => {
const t = await sequelize.transaction();
try {
const { courseId } = req.params;
const { ids = [] } = req.body;
if (!ids.length) return R.error(res, "No IDs provided.", 400);
const units = await Unit.findAll({ where: { unit_id: ids, course_id: courseId }, paranoid: false });
const validIds = units.map((u) => u.unit_id);
const count = await permanentDeleteMany(Unit, "unit_id", validIds, t);
await t.commit();
logActivity(req.user.user_id, 'bulk_permanently_delete_units', { entityType: 'unit', details: { ids: validIds, count } });
return R.success(res, `${count} unit${count !== 1 ? "s" : ""} permanently deleted.`);
} catch (err) {
await t.rollback();
console.error("[UNIT][BULK PERMANENT DELETE]", err);
return R.error(res, "Could not permanently delete units.", 500);
}
};
exports.getUnitPermanentDeleteImpact = async (req, res) => {
try {
const { unitId } = req.params;
const lessonCount = await Lesson.count({ where: { unit_id: unitId }, paranoid: false });
return R.success(res, "Impact retrieved.", { lessonCount });
} catch (err) {
console.error("[UNIT][PERMANENT DELETE IMPACT]", err);
return R.error(res, "Could not retrieve impact.", 500);
}
};
// ══════════════════════════════════════════════════════════════════════════════
// LESSON
// ══════════════════════════════════════════════════════════════════════════════
@@ -859,6 +962,51 @@ exports.bulkRestoreLessons = async (req, res) => {
}
};
exports.permanentlyDeleteLesson = async (req, res) => {
const t = await sequelize.transaction();
try {
const { courseId, unitId, lessonId } = req.params;
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 permanentDeleteOne(Lesson, { lesson_id: lessonId, unit_id: unitId }, t);
if (record === null) return R.error(res, "Lesson not found.", 404);
if (record === false) return R.error(res, "Lesson must be archived before it can be permanently deleted.", 400);
await t.commit();
logActivity(req.user.user_id, 'permanently_delete_lesson', { entityType: 'lesson', entityId: Number(lessonId) });
return R.success(res, "Lesson permanently deleted.");
} catch (err) {
await t.rollback();
console.error("[LESSON][PERMANENT DELETE]", err);
return R.error(res, "Could not permanently delete lesson.", 500);
}
};
exports.bulkPermanentlyDeleteLessons = async (req, res) => {
const t = await sequelize.transaction();
try {
const { courseId, unitId } = req.params;
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 });
if (!unit) return R.error(res, "Unit not found.", 404);
const lessons = await Lesson.findAll({ where: { lesson_id: ids, unit_id: unitId }, paranoid: false });
const validIds = lessons.map((l) => l.lesson_id);
const count = await permanentDeleteMany(Lesson, "lesson_id", validIds, t);
await t.commit();
logActivity(req.user.user_id, 'bulk_permanently_delete_lessons', { entityType: 'lesson', details: { ids: validIds, count } });
return R.success(res, `${count} lesson${count !== 1 ? "s" : ""} permanently deleted.`);
} catch (err) {
await t.rollback();
console.error("[LESSON][BULK PERMANENT DELETE]", err);
return R.error(res, "Could not permanently delete lessons.", 500);
}
};
// ══════════════════════════════════════════════════════════════════════════════
// LESSON PAGE
// ══════════════════════════════════════════════════════════════════════════════