From b6885b4e8919d0bf89753bb95b120eb0d2686627 Mon Sep 17 00:00:00 2001 From: elangptra Date: Thu, 19 Dec 2024 13:54:08 +0700 Subject: [PATCH] refactor: monitoring student function --- controllers/learningControllers/stdLearning.js | 18 +++++++++++++++--- .../monitoringControllers/monitoring.js | 1 + 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/controllers/learningControllers/stdLearning.js b/controllers/learningControllers/stdLearning.js index 79a2939..e735ba7 100644 --- a/controllers/learningControllers/stdLearning.js +++ b/controllers/learningControllers/stdLearning.js @@ -119,11 +119,23 @@ export const updateStdLearningById = async (req, res) => { ) { req.body.ID_STUDENT_LEARNING = id; - const existingMonitoring = await models.Monitoring.findOne({ - where: { ID_STUDENT_LEARNING: id }, + const existingMonitoringForTopic = await models.Monitoring.findOne({ + include: [ + { + model: models.StdLearning, + as: "stdLearningMonitoring", + include: [ + { + model: models.Level, + as: "level", + where: { ID_TOPIC: stdLearning.level.ID_TOPIC }, + }, + ], + }, + ], }); - if (!existingMonitoring) { + if (!existingMonitoringForTopic) { const newMonitoring = await createMonitoring(req); const { level, ...responseData } = stdLearning.toJSON(); diff --git a/controllers/monitoringControllers/monitoring.js b/controllers/monitoringControllers/monitoring.js index 46ffafc..657eb88 100644 --- a/controllers/monitoringControllers/monitoring.js +++ b/controllers/monitoringControllers/monitoring.js @@ -361,6 +361,7 @@ export const monitoringStudentProgressById = async (req, res) => { "STUDENT_START", "STUDENT_FINISH", ], + order: [["STUDENT_FINISH", "DESC"]], distinct: true, });