diff --git a/server/controllers/AuthorController.js b/server/controllers/AuthorController.js index 8c2e80aec..394b16d2b 100644 --- a/server/controllers/AuthorController.js +++ b/server/controllers/AuthorController.js @@ -1,5 +1,4 @@ const { Request, Response, NextFunction } = require('express') -const sequelize = require('sequelize') const fs = require('../libs/fsExtra') const { createNewSortInstance } = require('../libs/fastSort') @@ -113,18 +112,10 @@ class AuthorController { payload.lastFirst = Database.authorModel.getLastFirst(payload.name) } - // Check if author name matches another author in the same library and merge the authors + // Check if the new author name matches another author in the same library and merge the authors let existingAuthor = null if (authorNameUpdate) { - existingAuthor = await Database.authorModel.findOne({ - where: { - id: { - [sequelize.Op.not]: req.author.id - }, - name: payload.name, - libraryId: req.author.libraryId - } - }) + existingAuthor = await Database.authorModel.getByNameAndLibrary(payload.name, req.author.libraryId, req.author.id) } if (existingAuthor) { Logger.info(`[AuthorController] Merging author "${req.author.name}" with "${existingAuthor.name}"`) diff --git a/server/models/Author.js b/server/models/Author.js index 8ca45b63a..7c8958e9e 100644 --- a/server/models/Author.js +++ b/server/models/Author.js @@ -1,4 +1,4 @@ -const { DataTypes, Model } = require('sequelize') +const { DataTypes, Model, Op } = require('sequelize') const parseNameString = require('../utils/parsers/parseNameString') class Author extends Model { @@ -119,17 +119,24 @@ class Author extends Model { * Get author by name and libraryId. name case insensitive * @param {string} authorName * @param {string} libraryId + * @param {string} [excludeAuthorId] * @returns {Promise} */ - static async getByNameAndLibrary(authorName, libraryId) { + static async getByNameAndLibrary(authorName, libraryId, excludeAuthorId = null) { const searchName = this.normalizeSearchName(authorName) if (!searchName) return null - return this.findOne({ - where: { - searchName, - libraryId + + const where = { + searchName, + libraryId + } + if (excludeAuthorId) { + where.id = { + [Op.not]: excludeAuthorId } - }) + } + + return this.findOne({ where }) } /**