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
This commit is contained in:
parent
ea73323fce
commit
700080f001
|
|
@ -694,6 +694,8 @@ export function HumanLanguagePage() {
|
||||||
toast.error("Subcategory and course names are required")
|
toast.error("Subcategory and course names are required")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
const normalizedSubCategoryName = quickSubCategoryName.trim().toLowerCase()
|
||||||
|
const normalizedCourseName = quickCourseName.trim().toLowerCase()
|
||||||
setQuickCreating(true)
|
setQuickCreating(true)
|
||||||
try {
|
try {
|
||||||
let effectiveCategoryId = categoryId
|
let effectiveCategoryId = categoryId
|
||||||
|
|
@ -706,15 +708,32 @@ export function HumanLanguagePage() {
|
||||||
throw new Error("Missing human language category id")
|
throw new Error("Missing human language category id")
|
||||||
}
|
}
|
||||||
|
|
||||||
const createdSubCategory = await createCourseCategory({
|
const existingSubCategory = subCategories.find(
|
||||||
name: quickSubCategoryName.trim(),
|
(sub) => sub.sub_category_name.trim().toLowerCase() === normalizedSubCategoryName,
|
||||||
parent_id: effectiveCategoryId,
|
)
|
||||||
})
|
const subCategoryId =
|
||||||
const subCategoryId = createdSubCategory.data?.data?.id
|
existingSubCategory?.sub_category_id ??
|
||||||
|
(
|
||||||
|
await createCourseCategory({
|
||||||
|
name: quickSubCategoryName.trim(),
|
||||||
|
parent_id: effectiveCategoryId,
|
||||||
|
})
|
||||||
|
).data?.data?.id
|
||||||
if (!subCategoryId) {
|
if (!subCategoryId) {
|
||||||
throw new Error("Failed to create subcategory")
|
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({
|
await createCourse({
|
||||||
category_id: effectiveCategoryId,
|
category_id: effectiveCategoryId,
|
||||||
sub_category_id: Number(subCategoryId),
|
sub_category_id: Number(subCategoryId),
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user