mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
delete labels and highlights attached to the recreated item if the item was deleted before
This commit is contained in:
parent
abfa15e56f
commit
eb895b49bf
1 changed files with 42 additions and 0 deletions
|
|
@ -18,6 +18,7 @@ import { authTrx, getColumns, queryBuilderToRawSql } from '../repository'
|
|||
import { libraryItemRepository } from '../repository/library_item'
|
||||
import { Merge } from '../util'
|
||||
import { setRecentlySavedItemInRedis } from '../utils/helpers'
|
||||
import { logger } from '../utils/logger'
|
||||
import { parseSearchQuery } from '../utils/search'
|
||||
import { addLabelsToLibraryItem } from './labels'
|
||||
|
||||
|
|
@ -1184,3 +1185,44 @@ export const findLibraryItemIdsByLabelId = async (
|
|||
userId
|
||||
)
|
||||
}
|
||||
|
||||
export const recreateLibraryItem = async (
|
||||
id: string,
|
||||
userId: string,
|
||||
existingItemState: LibraryItemState,
|
||||
itemToSave: DeepPartial<LibraryItem>,
|
||||
pubsub = createPubSubClient()
|
||||
) => {
|
||||
// update the item except for id and slug
|
||||
const updatedItem = await updateLibraryItem(
|
||||
id,
|
||||
{
|
||||
...itemToSave,
|
||||
id: undefined,
|
||||
slug: undefined,
|
||||
} as QueryDeepPartialEntity<LibraryItem>,
|
||||
userId,
|
||||
pubsub
|
||||
)
|
||||
|
||||
try {
|
||||
// delete labels and highlights if the item was deleted
|
||||
if (existingItemState === LibraryItemState.Deleted) {
|
||||
logger.info('Deleting labels and highlights for item', { id })
|
||||
await authTrx(async (t) => {
|
||||
await t.getRepository(Highlight).delete({
|
||||
libraryItem: { id },
|
||||
})
|
||||
|
||||
await t.getRepository(EntityLabel).delete({
|
||||
libraryItemId: id,
|
||||
})
|
||||
})
|
||||
}
|
||||
} catch (error) {
|
||||
// continue to save the item even if we failed to delete labels and highlights
|
||||
logger.error('Failed to delete labels and highlights', error)
|
||||
}
|
||||
|
||||
return updatedItem
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue