refactor: topic and level model function

This commit is contained in:
elangptra 2024-10-16 11:13:24 +07:00
parent 7c3893b010
commit 41af7d9737
2 changed files with 40 additions and 38 deletions

View File

@ -282,6 +282,10 @@ export const getLevelsByTopicId = async (req, res) => {
model: models.Level,
as: "level",
attributes: ["ID_LEVEL", "NAME_LEVEL"],
where: {
ID_TOPIC: idTopic,
IS_DELETED: 0,
},
},
],
order: [["STUDENT_FINISH", "DESC"]],

View File

@ -303,11 +303,9 @@ export const getCompletedTopicsBySection = async (req, res) => {
const user = req.user;
const userId = user.ID;
const completedLevels = await models.StdLearning.findAll({
where: {
ID: userId,
IS_PASS: 1,
},
// Ambil semua pembelajaran user
const userLearnings = await models.StdLearning.findAll({
where: { ID: userId },
include: [
{
model: models.Level,
@ -335,17 +333,35 @@ export const getCompletedTopicsBySection = async (req, res) => {
],
});
if (!completedLevels.length) {
return response(200, null, "No completed topics found", res);
if (!userLearnings.length) {
return response(200, null, "No topics found for this user", res);
}
const completedSections = {};
for (const levelRecord of completedLevels) {
const level = levelRecord.level;
const topic = level.levelTopic;
const section = topic.topicSection;
for (const learning of userLearnings) {
const { level } = learning;
const { levelTopic: topic } = level;
const { topicSection: section } = topic;
// Ambil total topik untuk setiap section
const totalTopicsInSection = await models.Topic.count({
where: { ID_SECTION: section.ID_SECTION },
});
// Jika section belum ada di objek completedSections, tambahkan
if (!completedSections[section.ID_SECTION]) {
completedSections[section.ID_SECTION] = {
ID_SECTION: section.ID_SECTION,
NAME_SECTION: section.NAME_SECTION,
DESCRIPTION_SECTION: section.DESCRIPTION_SECTION,
THUMBNAIL: section.THUMBNAIL,
TOTAL_TOPICS: totalTopicsInSection,
COMPLETED_TOPICS: 0, // Awal dengan 0
};
}
// Cek apakah level saat ini adalah Level 6 untuk topik tersebut
const level6 = await models.Level.findOne({
where: {
NAME_LEVEL: "Level 6",
@ -353,40 +369,22 @@ export const getCompletedTopicsBySection = async (req, res) => {
},
});
if (level6 && level.ID_LEVEL === level6.ID_LEVEL) {
const totalTopicsInSection = await models.Topic.count({
where: { ID_SECTION: section.ID_SECTION },
});
if (!completedSections[section.ID_SECTION]) {
completedSections[section.ID_SECTION] = {
ID_SECTION: section.ID_SECTION,
NAME_SECTION: section.NAME_SECTION,
DESCRIPTION_SECTION: section.DESCRIPTION_SECTION,
THUMBNAIL: section.THUMBNAIL,
TOTAL_TOPICS: totalTopicsInSection,
COMPLETED_TOPICS: 0,
};
}
// Jika Level 6 ditemukan dan IS_PASS bernilai 1, tambahkan COMPLETED_TOPICS
if (level6 && level.ID_LEVEL === level6.ID_LEVEL && learning.IS_PASS === 1) {
completedSections[section.ID_SECTION].COMPLETED_TOPICS++;
}
}
const result = Object.values(completedSections);
// Siapkan hasil akhir dengan section yang selalu muncul, meski tidak ada COMPLETED_TOPICS
const result = Object.values(completedSections).map((section) => {
section.COMPLETED_TOPICS = section.COMPLETED_TOPICS || 0; // Tetap 0 jika tidak ada yang pass
return section;
});
if (!result.length) {
return response(200, null, "No section with completed topics found", res);
}
response(
200,
result,
"Completed topics by section fetched successfully",
res
);
response(200, result, "Completed topics by section fetched successfully", res);
} catch (error) {
console.error(error);
response(500, null, "Internal Server Error", res);
}
};