Fix collection add crash when the book is already in the collection

This commit is contained in:
mikiher committed 2026-11-10 08:19:33 +02:00
1 parent 83dce682e8
commit e9ff70e522
2 files changed
+85 -6

No files matched your search

+11 -6
View File
@@ -247,12 +247,17 @@ class CollectionController {
return res.status(400).send('Book already in collection')
}
// Create collectionBook record
await Database.collectionBookModel.create({
collectionId: req.collection.id,
bookId: libraryItem.mediaId,
order: collectionBooks.length + 1
})
// Create collectionBook record. A second add can pass the check above and hit the unique pair.
try {
await Database.collectionBookModel.create({
collectionId: req.collection.id,
bookId: libraryItem.mediaId,
order: collectionBooks.length + 1
})
} catch (error) {
if (error.name !== 'SequelizeUniqueConstraintError') throw error
return res.status(400).send('Book already in collection')
}
const jsonExpanded = await req.collection.getOldJsonExpanded(req.user)
if (jsonExpanded) SocketAuthority.clientEmitter(req.user.id, 'collection_updated', jsonExpanded)
res.json(jsonExpanded)