mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-11-20 07:31:34 +01:00
Fix batch add collection endpoint to ignore duplicates, matching fix for #5716
This commit is contained in:
1 parent
a5540d07c6
commit
5de038dafd
2 files changed
+41
-2
No files matched your search
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user