From 700080f001dcbb3760ed0ed9d6054935398214fe Mon Sep 17 00:00:00 2001 From: Yared Yemane Date: Tue, 14 Apr 2026 08:45:45 -0700 Subject: [PATCH] fix duplicate subcategory error on quick path recreate Make human-language quick path creation idempotent by reusing existing subcategories/courses when names already exist, avoiding unique-constraint failures. Made-with: Cursor --- .../content-management/HumanLanguagePage.tsx | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/pages/content-management/HumanLanguagePage.tsx b/src/pages/content-management/HumanLanguagePage.tsx index 37307d8..8af1f12 100644 --- a/src/pages/content-management/HumanLanguagePage.tsx +++ b/src/pages/content-management/HumanLanguagePage.tsx @@ -694,6 +694,8 @@ export function HumanLanguagePage() { toast.error("Subcategory and course names are required") return } + const normalizedSubCategoryName = quickSubCategoryName.trim().toLowerCase() + const normalizedCourseName = quickCourseName.trim().toLowerCase() setQuickCreating(true) try { let effectiveCategoryId = categoryId @@ -706,15 +708,32 @@ export function HumanLanguagePage() { throw new Error("Missing human language category id") } - const createdSubCategory = await createCourseCategory({ - name: quickSubCategoryName.trim(), - parent_id: effectiveCategoryId, - }) - const subCategoryId = createdSubCategory.data?.data?.id + const existingSubCategory = subCategories.find( + (sub) => sub.sub_category_name.trim().toLowerCase() === normalizedSubCategoryName, + ) + const subCategoryId = + existingSubCategory?.sub_category_id ?? + ( + await createCourseCategory({ + name: quickSubCategoryName.trim(), + parent_id: effectiveCategoryId, + }) + ).data?.data?.id if (!subCategoryId) { throw new Error("Failed to create subcategory") } + const existingCourse = subCategories + .find((sub) => sub.sub_category_id === Number(subCategoryId)) + ?.courses.find((course) => course.course_name.trim().toLowerCase() === normalizedCourseName) + if (existingCourse) { + toast.success("Path already exists; reused existing subcategory/course") + setQuickSubCategoryName("") + setQuickCourseName("") + await loadHierarchy() + return + } + await createCourse({ category_id: effectiveCategoryId, sub_category_id: Number(subCategoryId),