refactor: topic and level model function
This commit is contained in:
parent
7c3893b010
commit
41af7d9737
|
|
@ -282,6 +282,10 @@ export const getLevelsByTopicId = async (req, res) => {
|
||||||
model: models.Level,
|
model: models.Level,
|
||||||
as: "level",
|
as: "level",
|
||||||
attributes: ["ID_LEVEL", "NAME_LEVEL"],
|
attributes: ["ID_LEVEL", "NAME_LEVEL"],
|
||||||
|
where: {
|
||||||
|
ID_TOPIC: idTopic,
|
||||||
|
IS_DELETED: 0,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
order: [["STUDENT_FINISH", "DESC"]],
|
order: [["STUDENT_FINISH", "DESC"]],
|
||||||
|
|
|
||||||
|
|
@ -303,11 +303,9 @@ export const getCompletedTopicsBySection = async (req, res) => {
|
||||||
const user = req.user;
|
const user = req.user;
|
||||||
const userId = user.ID;
|
const userId = user.ID;
|
||||||
|
|
||||||
const completedLevels = await models.StdLearning.findAll({
|
// Ambil semua pembelajaran user
|
||||||
where: {
|
const userLearnings = await models.StdLearning.findAll({
|
||||||
ID: userId,
|
where: { ID: userId },
|
||||||
IS_PASS: 1,
|
|
||||||
},
|
|
||||||
include: [
|
include: [
|
||||||
{
|
{
|
||||||
model: models.Level,
|
model: models.Level,
|
||||||
|
|
@ -335,17 +333,35 @@ export const getCompletedTopicsBySection = async (req, res) => {
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!completedLevels.length) {
|
if (!userLearnings.length) {
|
||||||
return response(200, null, "No completed topics found", res);
|
return response(200, null, "No topics found for this user", res);
|
||||||
}
|
}
|
||||||
|
|
||||||
const completedSections = {};
|
const completedSections = {};
|
||||||
|
|
||||||
for (const levelRecord of completedLevels) {
|
for (const learning of userLearnings) {
|
||||||
const level = levelRecord.level;
|
const { level } = learning;
|
||||||
const topic = level.levelTopic;
|
const { levelTopic: topic } = level;
|
||||||
const section = topic.topicSection;
|
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({
|
const level6 = await models.Level.findOne({
|
||||||
where: {
|
where: {
|
||||||
NAME_LEVEL: "Level 6",
|
NAME_LEVEL: "Level 6",
|
||||||
|
|
@ -353,40 +369,22 @@ export const getCompletedTopicsBySection = async (req, res) => {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (level6 && level.ID_LEVEL === level6.ID_LEVEL) {
|
// Jika Level 6 ditemukan dan IS_PASS bernilai 1, tambahkan COMPLETED_TOPICS
|
||||||
const totalTopicsInSection = await models.Topic.count({
|
if (level6 && level.ID_LEVEL === level6.ID_LEVEL && learning.IS_PASS === 1) {
|
||||||
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,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
completedSections[section.ID_SECTION].COMPLETED_TOPICS++;
|
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) {
|
response(200, result, "Completed topics by section fetched successfully", res);
|
||||||
return response(200, null, "No section with completed topics found", res);
|
|
||||||
}
|
|
||||||
|
|
||||||
response(
|
|
||||||
200,
|
|
||||||
result,
|
|
||||||
"Completed topics by section fetched successfully",
|
|
||||||
res
|
|
||||||
);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
response(500, null, "Internal Server Error", res);
|
response(500, null, "Internal Server Error", res);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user