2022-04-24 18:41:47 -05:00
|
|
|
const Logger = require('../../Logger')
|
2023-07-04 18:14:44 -05:00
|
|
|
const uuidv4 = require("uuid").v4
|
2023-07-28 18:03:31 -05:00
|
|
|
const { checkNamesAreEqual, nameToLastFirst } = require('../../utils/parsers/parseNameString')
|
2022-03-09 19:23:17 -06:00
|
|
|
|
2022-03-08 19:31:44 -06:00
|
|
|
class Author {
|
|
|
|
constructor(author) {
|
|
|
|
this.id = null
|
|
|
|
this.asin = null
|
|
|
|
this.name = null
|
2022-03-13 06:42:43 -05:00
|
|
|
this.description = null
|
2022-03-08 19:31:44 -06:00
|
|
|
this.imagePath = null
|
|
|
|
this.addedAt = null
|
|
|
|
this.updatedAt = null
|
2023-07-08 09:57:32 -05:00
|
|
|
this.libraryId = null
|
2022-03-08 19:31:44 -06:00
|
|
|
|
|
|
|
if (author) {
|
|
|
|
this.construct(author)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
construct(author) {
|
|
|
|
this.id = author.id
|
|
|
|
this.asin = author.asin
|
2022-04-24 18:41:47 -05:00
|
|
|
this.name = author.name || ''
|
2022-03-13 06:42:43 -05:00
|
|
|
this.description = author.description || null
|
2022-03-08 19:31:44 -06:00
|
|
|
this.imagePath = author.imagePath
|
|
|
|
this.addedAt = author.addedAt
|
|
|
|
this.updatedAt = author.updatedAt
|
2023-07-08 09:57:32 -05:00
|
|
|
this.libraryId = author.libraryId
|
2022-03-08 19:31:44 -06:00
|
|
|
}
|
|
|
|
|
2023-07-28 18:03:31 -05:00
|
|
|
get lastFirst() {
|
|
|
|
if (!this.name) return ''
|
|
|
|
return nameToLastFirst(this.name)
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:31:44 -06:00
|
|
|
toJSON() {
|
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
asin: this.asin,
|
|
|
|
name: this.name,
|
2022-03-13 06:42:43 -05:00
|
|
|
description: this.description,
|
2022-03-08 19:31:44 -06:00
|
|
|
imagePath: this.imagePath,
|
|
|
|
addedAt: this.addedAt,
|
2023-07-08 09:57:32 -05:00
|
|
|
updatedAt: this.updatedAt,
|
|
|
|
libraryId: this.libraryId
|
2022-03-08 19:31:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-13 10:35:35 -05:00
|
|
|
toJSONExpanded(numBooks = 0) {
|
2022-12-26 16:08:53 -06:00
|
|
|
const json = this.toJSON()
|
2022-03-13 10:35:35 -05:00
|
|
|
json.numBooks = numBooks
|
|
|
|
return json
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:31:44 -06:00
|
|
|
toJSONMinimal() {
|
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
name: this.name
|
|
|
|
}
|
|
|
|
}
|
2022-03-09 19:23:17 -06:00
|
|
|
|
2023-07-08 09:57:32 -05:00
|
|
|
setData(data, libraryId) {
|
2023-07-04 18:14:44 -05:00
|
|
|
this.id = uuidv4()
|
2023-07-19 17:13:57 -05:00
|
|
|
if (!data.name) {
|
|
|
|
Logger.error(`[Author] setData: Setting author data without a name`, data)
|
|
|
|
}
|
|
|
|
this.name = data.name || ''
|
2022-03-13 06:42:43 -05:00
|
|
|
this.description = data.description || null
|
2022-03-09 19:23:17 -06:00
|
|
|
this.asin = data.asin || null
|
|
|
|
this.imagePath = data.imagePath || null
|
|
|
|
this.addedAt = Date.now()
|
|
|
|
this.updatedAt = Date.now()
|
2023-07-08 09:57:32 -05:00
|
|
|
this.libraryId = libraryId
|
2022-03-09 19:23:17 -06:00
|
|
|
}
|
2022-03-12 17:45:32 -06:00
|
|
|
|
2022-03-14 18:53:49 -05:00
|
|
|
update(payload) {
|
2022-12-26 16:08:53 -06:00
|
|
|
const json = this.toJSON()
|
2022-03-14 18:53:49 -05:00
|
|
|
delete json.id
|
|
|
|
delete json.addedAt
|
|
|
|
delete json.updatedAt
|
2022-12-26 16:08:53 -06:00
|
|
|
let hasUpdates = false
|
2022-03-14 18:53:49 -05:00
|
|
|
for (const key in json) {
|
|
|
|
if (payload[key] !== undefined && json[key] != payload[key]) {
|
|
|
|
this[key] = payload[key]
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hasUpdates
|
|
|
|
}
|
|
|
|
|
2022-03-12 17:45:32 -06:00
|
|
|
checkNameEquals(name) {
|
|
|
|
if (!name) return false
|
2022-04-24 18:41:47 -05:00
|
|
|
if (this.name === null) {
|
|
|
|
Logger.error(`[Author] Author name is null (${this.id})`)
|
|
|
|
return false
|
|
|
|
}
|
2022-09-18 16:58:20 -05:00
|
|
|
return checkNamesAreEqual(this.name, name)
|
2022-03-12 17:45:32 -06:00
|
|
|
}
|
2022-03-08 19:31:44 -06:00
|
|
|
}
|
|
|
|
module.exports = Author
|