refactor: stdLearning and exercise model

This commit is contained in:
elangptra 2024-09-30 10:20:31 +07:00
parent d87be86627
commit dfe4ab12d0
6 changed files with 57 additions and 16 deletions

View File

@ -221,7 +221,7 @@ export const getExerciseByLevelId = async (req, res) => {
if (exerciseData.multipleChoices) { if (exerciseData.multipleChoices) {
exerciseData.multipleChoices = exerciseData.multipleChoices.map( exerciseData.multipleChoices = exerciseData.multipleChoices.map(
(choice) => { (choice) => {
const { ANSWER_KEY, ...rest } = choice.dataValues; // Exclude ANSWER_KEY const { ANSWER_KEY, ...rest } = choice.dataValues;
return rest; return rest;
} }
); );
@ -231,10 +231,7 @@ export const getExerciseByLevelId = async (req, res) => {
} else if (questionType === "MPQ") { } else if (questionType === "MPQ") {
if (exerciseData.matchingPairs) { if (exerciseData.matchingPairs) {
exerciseData.matchingPairs = exerciseData.matchingPairs.map( exerciseData.matchingPairs = exerciseData.matchingPairs.map(
(pair) => { (pair) => pair.dataValues
const { LEFT_PAIR, RIGHT_PAIR, ...rest } = pair.dataValues; // Exclude LEFT_PAIR, RIGHT_PAIR
return rest;
}
); );
} }
delete exerciseData.multipleChoices; delete exerciseData.multipleChoices;
@ -242,7 +239,7 @@ export const getExerciseByLevelId = async (req, res) => {
} else if (questionType === "TFQ") { } else if (questionType === "TFQ") {
if (exerciseData.trueFalse) { if (exerciseData.trueFalse) {
exerciseData.trueFalse = exerciseData.trueFalse.map((tf) => { exerciseData.trueFalse = exerciseData.trueFalse.map((tf) => {
const { IS_TRUE, ...rest } = tf.dataValues; // Exclude IS_TRUE const { IS_TRUE, ...rest } = tf.dataValues;
return rest; return rest;
}); });
} }

View File

@ -98,3 +98,7 @@ export const stdAnswerExercise = async (req, res, next) => {
); );
} }
}; };
export const getStudentAnswersByStdLearningId = async (req, res) => {
}

View File

@ -34,13 +34,18 @@ export const createStdLearning = async (req, res) => {
return response(401, null, "User not authenticated", res); return response(401, null, "User not authenticated", res);
} }
if (!ID_LEVEL) {
return response(400, null, "Level ID is required", res);
}
const ID = req.user.ID; const ID = req.user.ID;
try { try {
if (req.stdLearning) {
return response(
200,
req.stdLearning,
"Student Learning data found and reused",
res
);
}
const level = await models.Level.findByPk(ID_LEVEL); const level = await models.Level.findByPk(ID_LEVEL);
if (!level) { if (!level) {
return response(404, null, "Level not found", res); return response(404, null, "Level not found", res);
@ -214,7 +219,9 @@ export const learningScoreByStdLearningId = async (req, res) => {
? stdLearning.learningUser.students.NISN ? stdLearning.learningUser.students.NISN
: null, : null,
ID_LEVEL: stdLearning.level ? stdLearning.level.ID_LEVEL : null, ID_LEVEL: stdLearning.level ? stdLearning.level.ID_LEVEL : null,
CURRENT_LEVEL_NAME: stdLearning.level ? stdLearning.level.NAME_LEVEL : null, CURRENT_LEVEL_NAME: stdLearning.level
? stdLearning.level.NAME_LEVEL
: null,
SCORE: stdLearning.SCORE, SCORE: stdLearning.SCORE,
NEXT_LEARNING: stdLearning.NEXT_LEARNING, NEXT_LEARNING: stdLearning.NEXT_LEARNING,
NEXT_LEARNING_NAME: stdLearning.nextLevel.NAME_LEVEL, NEXT_LEARNING_NAME: stdLearning.nextLevel.NAME_LEVEL,

View File

@ -0,0 +1,35 @@
import models from "../models/index.js";
import response from "../response.js";
export const checkStdLearning = async (req, res, next) => {
const { ID_LEVEL } = req.body;
if (!req.user) {
return response(401, null, "User not authenticated", res);
}
if (!ID_LEVEL) {
return response(400, null, "Level ID is required", res);
}
const ID = req.user.ID;
try {
const existingStdLearning = await models.StdLearning.findOne({
where: {
ID: ID,
ID_LEVEL: ID_LEVEL,
},
});
if (existingStdLearning && existingStdLearning.SCORE === null) {
req.stdLearning = existingStdLearning;
return next();
}
return next();
} catch (error) {
console.log(error);
return response(500, null, "Internal Server Error", res);
}
};

View File

@ -15,10 +15,7 @@ const StudentModel = (DataTypes) => {
}, },
ID_CLASS: { ID_CLASS: {
type: DataTypes.UUID, type: DataTypes.UUID,
allowNull: false, allowNull: true,
validate: {
notEmpty: true,
},
references: { references: {
model: "class", model: "class",
key: "ID_CLASS", key: "ID_CLASS",

View File

@ -1,5 +1,6 @@
import express from "express"; import express from "express";
import { getStdLearnings, getStdLearningById, createStdLearning, learningScoreByStdLearningId, learningHistory, learningHistoryBySectionId, learningHistoryByTopicId, getLastCreatedStdLearningByLevelId } from "../../controllers/learningControllers/stdLearning.js"; import { getStdLearnings, getStdLearningById, createStdLearning, learningScoreByStdLearningId, learningHistory, learningHistoryBySectionId, learningHistoryByTopicId, getLastCreatedStdLearningByLevelId } from "../../controllers/learningControllers/stdLearning.js";
import { checkStdLearning } from "../../middlewares/checkStdLearning.js";
import { verifyLoginUser } from "../../middlewares/User/authUser.js"; import { verifyLoginUser } from "../../middlewares/User/authUser.js";
const router = express.Router(); const router = express.Router();
@ -18,6 +19,6 @@ router.get("/learningHistory/topic/:topicId", verifyLoginUser, learningHistoryBy
router.get("/stdLearning/level/:levelId", verifyLoginUser, getLastCreatedStdLearningByLevelId); router.get("/stdLearning/level/:levelId", verifyLoginUser, getLastCreatedStdLearningByLevelId);
router.post("/stdLearning", verifyLoginUser, createStdLearning); router.post("/stdLearning", verifyLoginUser, checkStdLearning, createStdLearning);
export default router export default router