Remove old Author object & fix issue deleting empty authors

This commit is contained in:
advplyr 2024-08-31 13:27:48 -05:00
parent acc4bdbc5e
commit ba742563c2
13 changed files with 227 additions and 314 deletions

View file

@ -1,7 +1,5 @@
const { DataTypes, Model, where, fn, col } = require('sequelize')
const oldAuthor = require('../objects/entities/Author')
class Author extends Model {
constructor(values, options) {
super(values, options)
@ -26,69 +24,6 @@ class Author extends Model {
this.createdAt
}
getOldAuthor() {
return new oldAuthor({
id: this.id,
asin: this.asin,
name: this.name,
description: this.description,
imagePath: this.imagePath,
libraryId: this.libraryId,
addedAt: this.createdAt.valueOf(),
updatedAt: this.updatedAt.valueOf()
})
}
static updateFromOld(oldAuthor) {
const author = this.getFromOld(oldAuthor)
return this.update(author, {
where: {
id: author.id
}
})
}
static createFromOld(oldAuthor) {
const author = this.getFromOld(oldAuthor)
return this.create(author)
}
static createBulkFromOld(oldAuthors) {
const authors = oldAuthors.map(this.getFromOld)
return this.bulkCreate(authors)
}
static getFromOld(oldAuthor) {
return {
id: oldAuthor.id,
name: oldAuthor.name,
lastFirst: oldAuthor.lastFirst,
asin: oldAuthor.asin,
description: oldAuthor.description,
imagePath: oldAuthor.imagePath,
libraryId: oldAuthor.libraryId
}
}
static removeById(authorId) {
return this.destroy({
where: {
id: authorId
}
})
}
/**
* Get oldAuthor by id
* @param {string} authorId
* @returns {Promise<oldAuthor>}
*/
static async getOldById(authorId) {
const author = await this.findByPk(authorId)
if (!author) return null
return author.getOldAuthor()
}
/**
* Check if author exists
* @param {string} authorId
@ -99,25 +34,22 @@ class Author extends Model {
}
/**
* Get old author by name and libraryId. name case insensitive
* Get author by name and libraryId. name case insensitive
* TODO: Look for authors ignoring punctuation
*
* @param {string} authorName
* @param {string} libraryId
* @returns {Promise<oldAuthor>}
* @returns {Promise<Author>}
*/
static async getOldByNameAndLibrary(authorName, libraryId) {
const author = (
await this.findOne({
where: [
where(fn('lower', col('name')), authorName.toLowerCase()),
{
libraryId
}
]
})
)?.getOldAuthor()
return author
static async getByNameAndLibrary(authorName, libraryId) {
return this.findOne({
where: [
where(fn('lower', col('name')), authorName.toLowerCase()),
{
libraryId
}
]
})
}
/**
@ -213,5 +145,36 @@ class Author extends Model {
})
Author.belongsTo(library)
}
toOldJSON() {
return {
id: this.id,
asin: this.asin,
name: this.name,
description: this.description,
imagePath: this.imagePath,
libraryId: this.libraryId,
addedAt: this.createdAt.valueOf(),
updatedAt: this.updatedAt.valueOf()
}
}
/**
*
* @param {number} numBooks
* @returns
*/
toOldJSONExpanded(numBooks = 0) {
const oldJson = this.toOldJSON()
oldJson.numBooks = numBooks
return oldJson
}
toJSONMinimal() {
return {
id: this.id,
name: this.name
}
}
}
module.exports = Author