From ad2c9e74f45e20d336a533b4815c4dcc79f675c6 Mon Sep 17 00:00:00 2001 From: elangptra Date: Thu, 31 Oct 2024 10:04:01 +0700 Subject: [PATCH] refactor: get student answer function --- .../learningControllers/stdExercise.js | 35 ++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/controllers/learningControllers/stdExercise.js b/controllers/learningControllers/stdExercise.js index b856649..7a12d27 100644 --- a/controllers/learningControllers/stdExercise.js +++ b/controllers/learningControllers/stdExercise.js @@ -105,6 +105,25 @@ export const getStudentAnswersByStdLearningId = async (req, res) => { const stdLearning = await models.StdLearning.findByPk(id, { include: [ + { + model: models.Level, + as: "level", + attributes: ["ID_LEVEL", "NAME_LEVEL"], + include: [ + { + model: models.Topic, + as: "levelTopic", + attributes: ["ID_TOPIC", "NAME_TOPIC"], + include: [ + { + model: models.Section, + as: "topicSection", + attributes: ["ID_SECTION", "NAME_SECTION"], + }, + ], + }, + ], + }, { model: models.StdExercise, as: "stdExercises", @@ -159,13 +178,15 @@ export const getStudentAnswersByStdLearningId = async (req, res) => { const stdLearningData = stdLearning.toJSON(); + const { NAME_SECTION } = stdLearningData.level.levelTopic.topicSection; + const { NAME_TOPIC } = stdLearningData.level.levelTopic; + const { NAME_LEVEL } = stdLearningData.level; + const sortedExercises = stdLearningData.stdExercises.sort((a, b) => { const titleA = a.stdExerciseExercises.TITLE.toUpperCase(); const titleB = b.stdExerciseExercises.TITLE.toUpperCase(); - if (titleA < titleB) return -1; - if (titleA > titleB) return 1; - return 0; + return titleA.localeCompare(titleB); }); const mappedExercises = sortedExercises.map((exercise) => { @@ -200,7 +221,13 @@ export const getStudentAnswersByStdLearningId = async (req, res) => { return response( 200, { - ...stdLearningData, + ID_STUDENT_LEARNING: stdLearningData.ID_STUDENT_LEARNING, + ID_LEVEL: stdLearningData.ID_LEVEL, + NAME_SECTION, + NAME_TOPIC, + NAME_LEVEL, + SCORE: stdLearningData.SCORE, + IS_PASS: stdLearningData.IS_PASS, stdExercises: mappedExercises, }, "Student learning exercises retrieved successfully",