fix: edit category not working when updating name

This commit is contained in:
Sonny
2023-12-21 13:49:15 +01:00
committed by Sonny
parent 5a28917429
commit 115ff3e2f8
2 changed files with 14 additions and 3 deletions

View File

@@ -25,10 +25,9 @@ async function editCategory({ req, res, user }) {
}
const isCategoryNameAlreadyUsed = await getUserCategoryByName(user, name);
if (isCategoryNameAlreadyUsed.id !== cid) {
if (isCategoryNameAlreadyUsed && isCategoryNameAlreadyUsed?.id !== cid) {
throw new Error('Category name already used');
}
if (category.id === nextId) {
throw new Error('Category nextId cannot be equal to current category ID');
}
@@ -81,6 +80,7 @@ async function editCategory({ req, res, user }) {
id: category.id,
},
data: {
name,
nextId,
},
}),
@@ -96,6 +96,17 @@ async function editCategory({ req, res, user }) {
}),
].filter((a) => a !== null && a !== undefined),
);
} else {
await prisma.category.update({
where: {
authorId: userId,
id: category.id,
},
data: {
name,
nextId: category.nextId,
},
});
}
return res.send({

View File

@@ -40,7 +40,7 @@ export default function PageEditCategory({
makeRequest({
url: `${PATHS.API.CATEGORY}/${category.id}`,
method: 'PUT',
body: { name },
body: { name, nextId: category.nextId },
})
.then((data) =>
router.push(`${PATHS.HOME}?categoryId=${data?.categoryId}`),