From 91c50eae4caac53c004bb5541eca37f4ab9dd94a Mon Sep 17 00:00:00 2001 From: "beetles-ai[bot]" <221859081+beetles-ai[bot]@users.noreply.github.com> Date: Mon, 13 Oct 2025 12:05:42 +0000 Subject: [PATCH] Patch for controllers/chapterController.js --- controllers/chapterController.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/controllers/chapterController.js b/controllers/chapterController.js index 5fa7727..2d0a8a2 100644 --- a/controllers/chapterController.js +++ b/controllers/chapterController.js @@ -31,12 +31,16 @@ export const createChapter = async (req, res) => { const newChapter = new Chapter({ name: chapterName, subjectName: name, standard }); - if (Array.isArray(topics)) { - for (const topicName of topics) { - const newTopic = new Topic({ name: topicName, subjectName: name, chapterName, standard }); - await newTopic.save(); - newChapter.topics.push(newTopic._id); - } +if (Array.isArray(topics)) { +for (const topicName of topics) { +let existingTopic = await Topic.findOne({ name: topicName, subjectName: name, chapterName, standard }); +if (!existingTopic) { +const newTopic = new Topic({ name: topicName, subjectName: name, chapterName, standard }); +await newTopic.save(); +existingTopic = newTopic; +} +newChapter.topics.push(existingTopic._id); +} } await newChapter.save();