fix bulk action
This commit is contained in:
@ -1,5 +1,6 @@
|
|||||||
import { DeepPartial, SelectQueryBuilder } from 'typeorm'
|
import { DeepPartial, SelectQueryBuilder } from 'typeorm'
|
||||||
import { QueryDeepPartialEntity } from 'typeorm/query-builder/QueryPartialEntity'
|
import { QueryDeepPartialEntity } from 'typeorm/query-builder/QueryPartialEntity'
|
||||||
|
import { EntityLabel } from '../entity/entity_label'
|
||||||
import { Highlight } from '../entity/highlight'
|
import { Highlight } from '../entity/highlight'
|
||||||
import { Label } from '../entity/label'
|
import { Label } from '../entity/label'
|
||||||
import {
|
import {
|
||||||
@ -389,10 +390,12 @@ export const updateLibraryItems = async (
|
|||||||
) => {
|
) => {
|
||||||
// build the script
|
// build the script
|
||||||
let values: QueryDeepPartialEntity<LibraryItem> = {}
|
let values: QueryDeepPartialEntity<LibraryItem> = {}
|
||||||
|
let addLabels = false
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case BulkActionType.Archive:
|
case BulkActionType.Archive:
|
||||||
values = {
|
values = {
|
||||||
archivedAt: new Date(),
|
archivedAt: new Date(),
|
||||||
|
state: LibraryItemState.Archived,
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
case BulkActionType.Delete:
|
case BulkActionType.Delete:
|
||||||
@ -401,9 +404,7 @@ export const updateLibraryItems = async (
|
|||||||
}
|
}
|
||||||
break
|
break
|
||||||
case BulkActionType.AddLabels:
|
case BulkActionType.AddLabels:
|
||||||
values = {
|
addLabels = true
|
||||||
labels,
|
|
||||||
}
|
|
||||||
break
|
break
|
||||||
case BulkActionType.MarkAsRead:
|
case BulkActionType.MarkAsRead:
|
||||||
values = {
|
values = {
|
||||||
@ -422,6 +423,29 @@ export const updateLibraryItems = async (
|
|||||||
// build the where clause
|
// build the where clause
|
||||||
buildWhereClause(queryBuilder, args)
|
buildWhereClause(queryBuilder, args)
|
||||||
|
|
||||||
|
if (addLabels) {
|
||||||
|
if (!labels) {
|
||||||
|
throw new Error('Labels are required for this action')
|
||||||
|
}
|
||||||
|
|
||||||
|
const libraryItems = await queryBuilder.getMany()
|
||||||
|
// add labels in library items
|
||||||
|
const labelsToAdd = libraryItems.flatMap((libraryItem) =>
|
||||||
|
labels
|
||||||
|
.map((label) => ({
|
||||||
|
labelId: label.id,
|
||||||
|
libraryItemId: libraryItem.id,
|
||||||
|
}))
|
||||||
|
.filter((entityLabel) => {
|
||||||
|
const existingLabel = libraryItem.labels?.find(
|
||||||
|
(l) => l.id === entityLabel.labelId
|
||||||
|
)
|
||||||
|
return !existingLabel
|
||||||
|
})
|
||||||
|
)
|
||||||
|
return tx.getRepository(EntityLabel).save(labelsToAdd)
|
||||||
|
}
|
||||||
|
|
||||||
return queryBuilder.update(LibraryItem).set(values).execute()
|
return queryBuilder.update(LibraryItem).set(values).execute()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user