refactor: topics model (get completed topics)
This commit is contained in:
parent
11bd20565f
commit
d87be86627
|
|
@ -140,7 +140,7 @@ export const deleteTopicById = async (req, res) => {
|
|||
}
|
||||
};
|
||||
|
||||
export const getCompletedTopics = async (req, res) => {
|
||||
export const getCompletedTopicsBySection = async (req, res) => {
|
||||
try {
|
||||
const user = req.user;
|
||||
const userId = user.ID;
|
||||
|
|
@ -158,10 +158,17 @@ export const getCompletedTopics = async (req, res) => {
|
|||
{
|
||||
model: models.Topic,
|
||||
as: "levelTopic",
|
||||
attributes: [
|
||||
"ID_TOPIC",
|
||||
"NAME_TOPIC",
|
||||
"DESCRIPTION_TOPIC",
|
||||
attributes: ["ID_TOPIC", "NAME_TOPIC", "DESCRIPTION_TOPIC"],
|
||||
include: [
|
||||
{
|
||||
model: models.Section,
|
||||
as: "topicSection",
|
||||
attributes: [
|
||||
"ID_SECTION",
|
||||
"NAME_SECTION",
|
||||
"DESCRIPTION_SECTION",
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
|
|
@ -173,35 +180,49 @@ export const getCompletedTopics = async (req, res) => {
|
|||
return response(404, null, "No completed topics found", res);
|
||||
}
|
||||
|
||||
const completedTopics = [];
|
||||
const completedSections = {};
|
||||
|
||||
for (const levelRecord of completedLevels) {
|
||||
const level = levelRecord.level;
|
||||
const topic = level.levelTopic;
|
||||
const section = topic.topicSection;
|
||||
|
||||
const level6 = await models.Level.findOne({
|
||||
where: {
|
||||
NAME_LEVEL: "Level 6",
|
||||
ID_TOPIC: level.ID_TOPIC,
|
||||
ID_TOPIC: topic.ID_TOPIC,
|
||||
},
|
||||
});
|
||||
|
||||
if (level6 && level.ID_LEVEL === level6.ID_LEVEL) {
|
||||
const topic = level.levelTopic;
|
||||
completedTopics.push({
|
||||
ID_TOPIC: topic.ID_TOPIC,
|
||||
NAME_TOPIC: topic.NAME_TOPIC,
|
||||
DESCRIPTION_TOPIC: topic.DESCRIPTION_TOPIC,
|
||||
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,
|
||||
TOTAL_TOPICS: totalTopicsInSection,
|
||||
COMPLETED_TOPICS: 0,
|
||||
};
|
||||
}
|
||||
|
||||
completedSections[section.ID_SECTION].COMPLETED_TOPICS++;
|
||||
}
|
||||
}
|
||||
|
||||
if (!completedTopics.length) {
|
||||
const result = Object.values(completedSections);
|
||||
|
||||
if (!result.length) {
|
||||
return response(404, null, "No completed topics for Level 6 found", res);
|
||||
}
|
||||
|
||||
response(
|
||||
200,
|
||||
completedTopics,
|
||||
"Completed topics fetched successfully",
|
||||
result,
|
||||
"Completed topics by section fetched successfully",
|
||||
res
|
||||
);
|
||||
} catch (error) {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import express from "express";
|
||||
import { getTopics, getTopicById, getTopicBySectionId, createTopic, updateTopicById, deleteTopicById, getCompletedTopics } from "../../controllers/contentControllers/topic.js";
|
||||
import { getTopics, getTopicById, getTopicBySectionId, createTopic, updateTopicById, deleteTopicById, getCompletedTopicsBySection } from "../../controllers/contentControllers/topic.js";
|
||||
import { verifyLoginUser, adminOnly } from "../../middlewares/User/authUser.js";
|
||||
|
||||
|
||||
|
|
@ -7,7 +7,7 @@ const router = express.Router();
|
|||
|
||||
router.get("/topic", verifyLoginUser, getTopics);
|
||||
|
||||
router.get("/topic/complete", verifyLoginUser, getCompletedTopics);
|
||||
router.get("/topic/complete", verifyLoginUser, getCompletedTopicsBySection);
|
||||
|
||||
router.get("/topic/section/:sectionId", verifyLoginUser, getTopicBySectionId);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user