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