From 2285389b1258d1bd018d9457287a580d373d2e6e Mon Sep 17 00:00:00 2001 From: elangptra Date: Wed, 6 Nov 2024 08:04:51 +0700 Subject: [PATCH] refactor: get topic for admin and api route --- controllers/contentControllers/topic.js | 8 +++- controllers/usersControllers/user.js | 53 +++++++++++++++++++++++++ routes/index.js | 22 +++++----- 3 files changed, 71 insertions(+), 12 deletions(-) diff --git a/controllers/contentControllers/topic.js b/controllers/contentControllers/topic.js index e6878e9..97c61d0 100644 --- a/controllers/contentControllers/topic.js +++ b/controllers/contentControllers/topic.js @@ -105,10 +105,15 @@ export const getTopicForAdmin = async (req, res) => { [models.Op.like]: `%${search}%`, }, }, + { + DESCRIPTION_TOPIC: { + [models.Op.like]: `%${search}%`, + }, + } ], }), }, - attributes: ["ID_TOPIC", "NAME_TOPIC", "TIME_TOPIC"], + attributes: ["ID_TOPIC", "NAME_TOPIC", "DESCRIPTION_TOPIC", "TIME_TOPIC"], include: [ { model: models.Section, @@ -124,6 +129,7 @@ export const getTopicForAdmin = async (req, res) => { ID_TOPIC: topic.ID_TOPIC, NAME_SECTION: topic.topicSection.NAME_SECTION, NAME_TOPIC: topic.NAME_TOPIC, + DESCRIPTION_TOPIC: topic.DESCRIPTION_TOPIC, TIME_TOPIC: topic.TIME_TOPIC, })); diff --git a/controllers/usersControllers/user.js b/controllers/usersControllers/user.js index 666f27a..2cc313d 100644 --- a/controllers/usersControllers/user.js +++ b/controllers/usersControllers/user.js @@ -577,7 +577,60 @@ export const deleteUserById = async (req, res) => { return response(404, null, "User not found", res); } + const studentLearnings = await models.StdLearning.findAll({ + where: { ID: id }, + attributes: ["ID_STUDENT_LEARNING"], + transaction, + }); + + const studentLearningIds = studentLearnings.map( + (sl) => sl.ID_STUDENT_LEARNING + ); + + if (studentLearningIds.length > 0) { + await models.Monitoring.destroy({ + where: { + ID_STUDENT_LEARNING: studentLearningIds, + }, + transaction, + }); + + await models.StdExercise.destroy({ + where: { + ID_STUDENT_LEARNING: studentLearningIds, + }, + transaction, + }); + } + + await models.StdLearning.destroy({ + where: { ID: id }, + transaction, + }); + + await models.Report.destroy({ + where: { ID: id }, + transaction, + }); + if (user.ROLE === "teacher") { + const teacher = await models.Teacher.findOne({ + where: { ID: id }, + attributes: ["ID_GURU"], + transaction, + }); + + if (teacher) { + await models.Monitoring.update( + { ID_GURU: null }, + { + where: { ID_GURU: teacher.ID_GURU }, + transaction, + } + ); + + await teacher.destroy({ transaction }); + } await models.Teacher.destroy({ where: { ID: id }, transaction }); } else if (user.ROLE === "student") { await models.Student.destroy({ where: { ID: id }, transaction }); diff --git a/routes/index.js b/routes/index.js index dd3a9d4..c0ad7ee 100644 --- a/routes/index.js +++ b/routes/index.js @@ -12,16 +12,16 @@ import monitoring_routes from "./monitoring/monitoring.js"; import report_routes from "./user/report.js"; const route = express(); -route.use(user_routes); -route.use(auth_routes); -route.use(section_routes); -route.use(topic_routes); -route.use(level_routes); -route.use(exercise_routes); -route.use(stdLearning_routes); -route.use(stdExercise_routes); -route.use(class_routes); -route.use(monitoring_routes); -route.use(report_routes); +route.use("/api", user_routes); +route.use("/api", auth_routes); +route.use("/api", section_routes); +route.use("/api", topic_routes); +route.use("/api", level_routes); +route.use("/api", exercise_routes); +route.use("/api", stdLearning_routes); +route.use("/api", stdExercise_routes); +route.use("/api", class_routes); +route.use("/api", monitoring_routes); +route.use("/api", report_routes); export default route;