Remove req.oldLibraryItem usage

This commit is contained in:
advplyr 2025-01-02 15:54:10 -06:00
parent eb853d9f09
commit 5cd14108f9
2 changed files with 20 additions and 22 deletions

View File

@ -27,7 +27,6 @@ const ShareManager = require('../managers/ShareManager')
* *
* @typedef RequestEntityObject * @typedef RequestEntityObject
* @property {import('../models/LibraryItem')} libraryItem * @property {import('../models/LibraryItem')} libraryItem
* @property {Object} oldLibraryItem - To be removed
* *
* @typedef {RequestWithUser & RequestEntityObject} LibraryItemControllerRequest * @typedef {RequestWithUser & RequestEntityObject} LibraryItemControllerRequest
* *
@ -97,13 +96,14 @@ class LibraryItemController {
await CacheManager.purgeCoverCache(req.libraryItem.id) await CacheManager.purgeCoverCache(req.libraryItem.id)
} }
const hasUpdates = req.oldLibraryItem.update(req.body) const oldLibraryItem = Database.libraryItemModel.getOldLibraryItem(req.libraryItem)
const hasUpdates = oldLibraryItem.update(req.body)
if (hasUpdates) { if (hasUpdates) {
Logger.debug(`[LibraryItemController] Updated now saving`) Logger.debug(`[LibraryItemController] Updated now saving`)
await Database.updateLibraryItem(req.oldLibraryItem) await Database.updateLibraryItem(oldLibraryItem)
SocketAuthority.emitter('item_updated', req.oldLibraryItem.toJSONExpanded()) SocketAuthority.emitter('item_updated', oldLibraryItem.toJSONExpanded())
} }
res.json(req.oldLibraryItem.toJSON()) res.json(oldLibraryItem.toJSON())
} }
/** /**
@ -234,30 +234,32 @@ class LibraryItemController {
} }
} }
const oldLibraryItem = Database.libraryItemModel.getOldLibraryItem(req.libraryItem)
// Book specific - Get all series being removed from this item // Book specific - Get all series being removed from this item
let seriesRemoved = [] let seriesRemoved = []
if (req.libraryItem.isBook && mediaPayload.metadata?.series) { if (req.libraryItem.isBook && mediaPayload.metadata?.series) {
const seriesIdsInUpdate = mediaPayload.metadata.series?.map((se) => se.id) || [] const seriesIdsInUpdate = mediaPayload.metadata.series?.map((se) => se.id) || []
seriesRemoved = req.oldLibraryItem.media.metadata.series.filter((se) => !seriesIdsInUpdate.includes(se.id)) seriesRemoved = oldLibraryItem.media.metadata.series.filter((se) => !seriesIdsInUpdate.includes(se.id))
} }
let authorsRemoved = [] let authorsRemoved = []
if (req.libraryItem.isBook && mediaPayload.metadata?.authors) { if (req.libraryItem.isBook && mediaPayload.metadata?.authors) {
const authorIdsInUpdate = mediaPayload.metadata.authors.map((au) => au.id) const authorIdsInUpdate = mediaPayload.metadata.authors.map((au) => au.id)
authorsRemoved = req.oldLibraryItem.media.metadata.authors.filter((au) => !authorIdsInUpdate.includes(au.id)) authorsRemoved = oldLibraryItem.media.metadata.authors.filter((au) => !authorIdsInUpdate.includes(au.id))
} }
const hasUpdates = req.oldLibraryItem.media.update(mediaPayload) || mediaPayload.url const hasUpdates = oldLibraryItem.media.update(mediaPayload) || mediaPayload.url
if (hasUpdates) { if (hasUpdates) {
req.oldLibraryItem.updatedAt = Date.now() oldLibraryItem.updatedAt = Date.now()
if (isPodcastAutoDownloadUpdated) { if (isPodcastAutoDownloadUpdated) {
this.cronManager.checkUpdatePodcastCron(req.oldLibraryItem) this.cronManager.checkUpdatePodcastCron(oldLibraryItem)
} }
Logger.debug(`[LibraryItemController] Updated library item media ${req.oldLibraryItem.media.metadata.title}`) Logger.debug(`[LibraryItemController] Updated library item media ${oldLibraryItem.media.metadata.title}`)
await Database.updateLibraryItem(req.oldLibraryItem) await Database.updateLibraryItem(oldLibraryItem)
SocketAuthority.emitter('item_updated', req.oldLibraryItem.toJSONExpanded()) SocketAuthority.emitter('item_updated', oldLibraryItem.toJSONExpanded())
if (authorsRemoved.length) { if (authorsRemoved.length) {
// Check remove empty authors // Check remove empty authors
@ -272,7 +274,7 @@ class LibraryItemController {
} }
res.json({ res.json({
updated: hasUpdates, updated: hasUpdates,
libraryItem: req.oldLibraryItem libraryItem: oldLibraryItem
}) })
} }
@ -527,7 +529,8 @@ class LibraryItemController {
options.overrideDetails = !!reqBody.overrideDetails options.overrideDetails = !!reqBody.overrideDetails
} }
var matchResult = await Scanner.quickMatchLibraryItem(this, req.oldLibraryItem, options) const oldLibraryItem = Database.libraryItemModel.getOldLibraryItem(req.libraryItem)
var matchResult = await Scanner.quickMatchLibraryItem(this, oldLibraryItem, options)
res.json(matchResult) res.json(matchResult)
} }
@ -1144,7 +1147,6 @@ class LibraryItemController {
*/ */
async middleware(req, res, next) { async middleware(req, res, next) {
req.libraryItem = await Database.libraryItemModel.getExpandedById(req.params.id) req.libraryItem = await Database.libraryItemModel.getExpandedById(req.params.id)
req.oldLibraryItem = Database.libraryItemModel.getOldLibraryItem(req.libraryItem)
if (!req.libraryItem?.media) return res.sendStatus(404) if (!req.libraryItem?.media) return res.sendStatus(404)
// Check user can access this library item // Check user can access this library item

View File

@ -83,12 +83,10 @@ describe('LibraryItemController', () => {
it('should remove authors and series with no books on library item delete', async () => { it('should remove authors and series with no books on library item delete', async () => {
const libraryItem = await Database.libraryItemModel.getExpandedById(libraryItem1Id) const libraryItem = await Database.libraryItemModel.getExpandedById(libraryItem1Id)
const oldLibraryItem = Database.libraryItemModel.getOldLibraryItem(libraryItem)
const fakeReq = { const fakeReq = {
query: {}, query: {},
libraryItem, libraryItem
oldLibraryItem
} }
const fakeRes = { const fakeRes = {
sendStatus: sinon.spy() sendStatus: sinon.spy()
@ -159,7 +157,6 @@ describe('LibraryItemController', () => {
it('should remove authors and series with no books on library item update media', async () => { it('should remove authors and series with no books on library item update media', async () => {
const libraryItem = await Database.libraryItemModel.getExpandedById(libraryItem1Id) const libraryItem = await Database.libraryItemModel.getExpandedById(libraryItem1Id)
const oldLibraryItem = Database.libraryItemModel.getOldLibraryItem(libraryItem)
// Update library item 1 remove all authors and series // Update library item 1 remove all authors and series
const fakeReq = { const fakeReq = {
@ -170,8 +167,7 @@ describe('LibraryItemController', () => {
series: [] series: []
} }
}, },
libraryItem, libraryItem
oldLibraryItem
} }
const fakeRes = { const fakeRes = {
json: sinon.spy() json: sinon.spy()