mirror of
https://github.com/marcogll/TaxHacker_s23.git
synced 2026-01-13 13:25:18 +00:00
fix: show friendly error when duplicate category code exists (handle Prisma P2002) (#49)
This commit is contained in:
@@ -188,15 +188,26 @@ export async function addCategoryAction(userId: string, data: Prisma.CategoryCre
|
|||||||
return { success: false, error: validatedForm.error.message }
|
return { success: false, error: validatedForm.error.message }
|
||||||
}
|
}
|
||||||
|
|
||||||
const category = await createCategory(userId, {
|
const code = codeFromName(validatedForm.data.name)
|
||||||
code: codeFromName(validatedForm.data.name),
|
try {
|
||||||
name: validatedForm.data.name,
|
const category = await createCategory(userId, {
|
||||||
llm_prompt: validatedForm.data.llm_prompt,
|
code,
|
||||||
color: validatedForm.data.color || "",
|
name: validatedForm.data.name,
|
||||||
})
|
llm_prompt: validatedForm.data.llm_prompt,
|
||||||
revalidatePath("/settings/categories")
|
color: validatedForm.data.color || "",
|
||||||
|
})
|
||||||
|
revalidatePath("/settings/categories")
|
||||||
|
|
||||||
return { success: true, category }
|
return { success: true, category }
|
||||||
|
} catch (error: unknown) {
|
||||||
|
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === "P2002") {
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
error: `Category with the code "${code}" already exists. Try a different name.`,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return { success: false, error: "Failed to create category" }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function editCategoryAction(userId: string, code: string, data: Prisma.CategoryUpdateInput) {
|
export async function editCategoryAction(userId: string, code: string, data: Prisma.CategoryUpdateInput) {
|
||||||
|
|||||||
Reference in New Issue
Block a user