From 5de038dafda294d13ca48861eda7d4405bbd7368 Mon Sep 17 00:00:00 2001 From: advplyr Date: Thu, 19 Nov 2026 17:39:34 -0600 Subject: [PATCH] Fix batch add collection endpoint to ignore duplicates, matching fix for #5716 --- server/controllers/CollectionController.js | 2 +- .../controllers/CollectionController.test.js | 41 ++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/server/controllers/CollectionController.js b/server/controllers/CollectionController.js index e0814babc..a8d912a8d 100644 --- a/server/controllers/CollectionController.js +++ b/server/controllers/CollectionController.js @@ -363,7 +363,7 @@ class CollectionController { let jsonExpanded = null if (hasUpdated) { - await Database.collectionBookModel.bulkCreate(collectionBooksToAdd) + await Database.collectionBookModel.bulkCreate(collectionBooksToAdd, { ignoreDuplicates: true }) jsonExpanded = await req.collection.getOldJsonExpanded(req.user) if (jsonExpanded) SocketAuthority.clientEmitter(req.user.id, 'collection_updated', jsonExpanded) diff --git a/test/server/controllers/CollectionController.test.js b/test/server/controllers/CollectionController.test.js index dcef3856c..f57bca673 100644 --- a/test/server/controllers/CollectionController.test.js +++ b/test/server/controllers/CollectionController.test.js @@ -6,7 +6,7 @@ const Database = require('../../../server/Database') const Logger = require('../../../server/Logger') const CollectionController = require('../../../server/controllers/CollectionController') -describe('CollectionController.addBook', () => { +describe('CollectionController', () => { /** @type {import('../../../server/models/Collection')} */ let collection /** @type {import('../../../server/models/LibraryItem')} */ @@ -71,4 +71,43 @@ describe('CollectionController.addBook', () => { const links = await Database.collectionBookModel.findAll({ where: { collectionId: collection.id, bookId: libraryItem.mediaId } }) expect(links).to.have.lengthOf(1) }) + + it('does not throw when a batch includes a book already in the collection', async () => { + const book = await Database.bookModel.create({ title: 'Second Book', audioFiles: [], tags: [], narrators: [], genres: [], chapters: [] }) + const secondItem = await Database.libraryItemModel.create({ + path: '/test-lib/book-2', + isFile: false, + libraryFiles: [], + mediaId: book.id, + mediaType: 'book', + libraryId: libraryItem.libraryId, + libraryFolderId: libraryItem.libraryFolderId + }) + sinon.stub(collection, 'getCollectionBooks').resolves([]) + sinon.stub(collection, 'getOldJsonExpanded').resolves(null) + + const res = { + statusCode: null, + body: null, + status(code) { + this.statusCode = code + return this + }, + send(body) { + this.body = body + return this + }, + json(body) { + this.body = body + return this + } + } + + await CollectionController.addBatch({ body: { books: [libraryItem.id, secondItem.id] }, collection, user: { id: 'user-id' } }, res) + + const existingLinks = await Database.collectionBookModel.findAll({ where: { collectionId: collection.id, bookId: libraryItem.mediaId } }) + expect(existingLinks).to.have.lengthOf(1) + const newLinks = await Database.collectionBookModel.findAll({ where: { collectionId: collection.id, bookId: secondItem.mediaId } }) + expect(newLinks).to.have.lengthOf(1) + }) })