mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-09 07:03:25 +00:00
fix: error when user has no category
This commit is contained in:
@@ -25,7 +25,7 @@ async function createCategory({ req, res, user }) {
|
|||||||
throw new Error('Category name already used');
|
throw new Error('Category name already used');
|
||||||
}
|
}
|
||||||
|
|
||||||
const { id: lastCategoryId } = await prisma.category.findFirst({
|
const lastCategory = await prisma.category.findFirst({
|
||||||
where: {
|
where: {
|
||||||
authorId: user.id,
|
authorId: user.id,
|
||||||
nextId: null,
|
nextId: null,
|
||||||
@@ -39,14 +39,16 @@ async function createCategory({ req, res, user }) {
|
|||||||
data: { name, authorId: user.id },
|
data: { name, authorId: user.id },
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (lastCategory) {
|
||||||
await prisma.category.update({
|
await prisma.category.update({
|
||||||
where: {
|
where: {
|
||||||
id: lastCategoryId,
|
id: lastCategory.id,
|
||||||
},
|
},
|
||||||
data: {
|
data: {
|
||||||
nextId: categoryCreated.id,
|
nextId: categoryCreated.id,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return res.status(200).send({
|
return res.status(200).send({
|
||||||
success: 'Category successfully created',
|
success: 'Category successfully created',
|
||||||
|
|||||||
Reference in New Issue
Block a user