mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-11-20 07:31:34 +01:00
Fix quick match crash when the series row already exists
This commit is contained in:
1 parent
cf1f162aba
commit
8af3448ac0
2 files changed
+43
-8
No files matched your search
@@ -311,14 +311,26 @@ class Scanner {
|
||||
} else {
|
||||
let seriesItem = await Database.seriesModel.getByNameAndLibrary(seriesMatchItem.series, libraryItem.libraryId)
|
||||
if (!seriesItem) {
|
||||
seriesItem = await Database.seriesModel.create({
|
||||
name: seriesMatchItem.series,
|
||||
nameIgnorePrefix: getTitleIgnorePrefix(seriesMatchItem.series),
|
||||
libraryId: libraryItem.libraryId
|
||||
})
|
||||
// Update filter data
|
||||
Database.addSeriesToFilterData(libraryItem.libraryId, seriesItem.name, seriesItem.id)
|
||||
SocketAuthority.emitter('series_added', seriesItem.toOldJSON())
|
||||
let seriesCreated = false
|
||||
try {
|
||||
seriesItem = await Database.seriesModel.create({
|
||||
name: seriesMatchItem.series,
|
||||
nameIgnorePrefix: getTitleIgnorePrefix(seriesMatchItem.series),
|
||||
libraryId: libraryItem.libraryId
|
||||
})
|
||||
seriesCreated = true
|
||||
} catch (error) {
|
||||
if (error.name !== 'SequelizeUniqueConstraintError') throw error
|
||||
seriesItem = await Database.seriesModel.findOne({
|
||||
where: { name: seriesMatchItem.series, libraryId: libraryItem.libraryId }
|
||||
})
|
||||
if (!seriesItem) throw error
|
||||
}
|
||||
if (seriesCreated) {
|
||||
// Update filter data
|
||||
Database.addSeriesToFilterData(libraryItem.libraryId, seriesItem.name, seriesItem.id)
|
||||
SocketAuthority.emitter('series_added', seriesItem.toOldJSON())
|
||||
}
|
||||
}
|
||||
let bookSeries
|
||||
try {
|
||||
|
||||
@@ -74,6 +74,29 @@ describe('Scanner.quickMatchBookBuildUpdatePayload', () => {
|
||||
expect(links).to.have.lengthOf(1)
|
||||
})
|
||||
|
||||
it('reuses an existing series when create hits the name unique constraint', async () => {
|
||||
const series = await Database.seriesModel.create({ name: 'Crescent City', libraryId: library.id })
|
||||
sinon.stub(Database.seriesModel, 'getByNameAndLibrary').resolves(null)
|
||||
|
||||
const result = await Scanner.quickMatchBookBuildUpdatePayload(
|
||||
apiRouterCtx,
|
||||
libraryItem,
|
||||
{
|
||||
series: [{ series: 'Crescent City', sequence: '3' }]
|
||||
},
|
||||
{}
|
||||
)
|
||||
|
||||
expect(result.hasSeriesUpdates).to.be.true
|
||||
expect(libraryItem.media.series).to.have.lengthOf(1)
|
||||
expect(libraryItem.media.series[0].id).to.equal(series.id)
|
||||
|
||||
const seriesRows = await Database.seriesModel.findAll({ where: { libraryId: library.id, name: 'Crescent City' } })
|
||||
expect(seriesRows).to.have.lengthOf(1)
|
||||
const links = await Database.bookSeriesModel.findAll({ where: { bookId: book.id, seriesId: series.id } })
|
||||
expect(links).to.have.lengthOf(1)
|
||||
})
|
||||
|
||||
it('does not throw when bookAuthor link already exists from a concurrent quick match', async () => {
|
||||
const author = await Database.authorModel.create({ name: 'Sarah Maas', libraryId: library.id })
|
||||
await Database.bookAuthorModel.create({ bookId: book.id, authorId: author.id })
|
||||
|
||||
Reference in new issue
Block a user