From 41af7d97372acc0599d4dd386161b3f81bcfb709 Mon Sep 17 00:00:00 2001 From: elangptra Date: Wed, 16 Oct 2024 11:13:24 +0700 Subject: [PATCH] refactor: topic and level model function --- controllers/contentControllers/level.js | 4 ++ controllers/contentControllers/topic.js | 74 ++++++++++++------------- 2 files changed, 40 insertions(+), 38 deletions(-) diff --git a/controllers/contentControllers/level.js b/controllers/contentControllers/level.js index 88d1537..62d8097 100644 --- a/controllers/contentControllers/level.js +++ b/controllers/contentControllers/level.js @@ -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"]], diff --git a/controllers/contentControllers/topic.js b/controllers/contentControllers/topic.js index 99fb0c7..141966f 100644 --- a/controllers/contentControllers/topic.js +++ b/controllers/contentControllers/topic.js @@ -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); } }; +