fix: error when user has no category

This commit is contained in:
Sonny
2023-12-16 22:53:32 +01:00
parent 8b86fbfd7f
commit 2d8cf41a83

View File

@@ -25,7 +25,7 @@ async function createCategory({ req, res, user }) {
throw new Error('Category name already used');
}
const { id: lastCategoryId } = await prisma.category.findFirst({
const lastCategory = await prisma.category.findFirst({
where: {
authorId: user.id,
nextId: null,
@@ -39,14 +39,16 @@ async function createCategory({ req, res, user }) {
data: { name, authorId: user.id },
});
if (lastCategory) {
await prisma.category.update({
where: {
id: lastCategoryId,
id: lastCategory.id,
},
data: {
nextId: categoryCreated.id,
},
});
}
return res.status(200).send({
success: 'Category successfully created',