Fix:Duplicate series and authors being added on matches and scans #2106

This commit is contained in:
advplyr 2023-09-17 15:29:39 -05:00
parent 0aae672e19
commit d18592eaeb
3 changed files with 16 additions and 14 deletions

View File

@ -1,4 +1,4 @@
const { DataTypes, Model, literal } = require('sequelize')
const { DataTypes, Model, where, fn, col } = require('sequelize')
const oldAuthor = require('../objects/entities/Author')
@ -114,14 +114,11 @@ class Author extends Model {
static async getOldByNameAndLibrary(authorName, libraryId) {
const author = (await this.findOne({
where: [
literal(`name = ':authorName' COLLATE NOCASE`),
where(fn('lower', col('name')), authorName.toLowerCase()),
{
libraryId
}
],
replacements: {
authorName
}
]
}))?.getOldAuthor()
return author
}

View File

@ -1,4 +1,4 @@
const { DataTypes, Model, literal } = require('sequelize')
const { DataTypes, Model, where, fn, col } = require('sequelize')
const oldSeries = require('../objects/entities/Series')
@ -105,14 +105,11 @@ class Series extends Model {
static async getOldByNameAndLibrary(seriesName, libraryId) {
const series = (await this.findOne({
where: [
literal(`name = ':seriesName' COLLATE NOCASE`),
where(fn('lower', col('name')), seriesName.toLowerCase()),
{
libraryId
}
],
replacements: {
seriesName
}
]
}))?.getOldSeries()
return series
}

View File

@ -553,13 +553,17 @@ class ApiRouter {
continue
}
if (mediaMetadata.authors[i].id?.startsWith('new')) {
mediaMetadata.authors[i].id = null
}
// Ensure the ID for the author exists
if (mediaMetadata.authors[i].id && !(await Database.checkAuthorExists(libraryId, mediaMetadata.authors[i].id))) {
Logger.warn(`[ApiRouter] Author id "${mediaMetadata.authors[i].id}" does not exist`)
mediaMetadata.authors[i].id = null
}
if (!mediaMetadata.authors[i].id || mediaMetadata.authors[i].id.startsWith('new')) {
if (!mediaMetadata.authors[i].id) {
let author = await Database.authorModel.getOldByNameAndLibrary(authorName, libraryId)
if (!author) {
author = new Author()
@ -590,13 +594,17 @@ class ApiRouter {
continue
}
if (mediaMetadata.series[i].id?.startsWith('new')) {
mediaMetadata.series[i].id = null
}
// Ensure the ID for the series exists
if (mediaMetadata.series[i].id && !(await Database.checkSeriesExists(libraryId, mediaMetadata.series[i].id))) {
Logger.warn(`[ApiRouter] Series id "${mediaMetadata.series[i].id}" does not exist`)
mediaMetadata.series[i].id = null
}
if (!mediaMetadata.series[i].id || mediaMetadata.series[i].id.startsWith('new')) {
if (!mediaMetadata.series[i].id) {
let seriesItem = await Database.seriesModel.getOldByNameAndLibrary(seriesName, libraryId)
if (!seriesItem) {
seriesItem = new Series()