refactor: get student answer function

This commit is contained in:
elangptra 2024-10-31 10:04:01 +07:00
parent 21bcad6f79
commit ad2c9e74f4

View File

@ -105,6 +105,25 @@ export const getStudentAnswersByStdLearningId = async (req, res) => {
const stdLearning = await models.StdLearning.findByPk(id, { const stdLearning = await models.StdLearning.findByPk(id, {
include: [ 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, model: models.StdExercise,
as: "stdExercises", as: "stdExercises",
@ -159,13 +178,15 @@ export const getStudentAnswersByStdLearningId = async (req, res) => {
const stdLearningData = stdLearning.toJSON(); 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 sortedExercises = stdLearningData.stdExercises.sort((a, b) => {
const titleA = a.stdExerciseExercises.TITLE.toUpperCase(); const titleA = a.stdExerciseExercises.TITLE.toUpperCase();
const titleB = b.stdExerciseExercises.TITLE.toUpperCase(); const titleB = b.stdExerciseExercises.TITLE.toUpperCase();
if (titleA < titleB) return -1; return titleA.localeCompare(titleB);
if (titleA > titleB) return 1;
return 0;
}); });
const mappedExercises = sortedExercises.map((exercise) => { const mappedExercises = sortedExercises.map((exercise) => {
@ -200,7 +221,13 @@ export const getStudentAnswersByStdLearningId = async (req, res) => {
return response( return response(
200, 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, stdExercises: mappedExercises,
}, },
"Student learning exercises retrieved successfully", "Student learning exercises retrieved successfully",