update bulk action to add labels

This commit is contained in:
Hongbo Wu 2024-01-31 13:56:56 +08:00
parent 5ed35bbeff
commit 43855247b8
2 changed files with 27 additions and 1 deletions

View file

@ -1,6 +1,7 @@
import { DeepPartial, FindOptionsWhere, In } from 'typeorm'
import { QueryDeepPartialEntity } from 'typeorm/query-builder/QueryPartialEntity'
import { EntityLabel, LabelSource } from '../entity/entity_label'
import { Highlight } from '../entity/highlight'
import { Label } from '../entity/label'
import { createPubSubClient, EntityType, PubsubClient } from '../pubsub'
import { authTrx } from '../repository'
@ -187,6 +188,20 @@ export const saveLabelsInHighlight = async (
{ highlightId, labels },
userId
)
const highlight = await authTrx(async (tx) =>
tx.getRepository(Highlight).findOne({
where: { id: highlightId },
relations: ['libraryItem'],
})
)
if (highlight) {
// update labels in library item
const jobs = await bulkEnqueueUpdateLabels([
{ libraryItemId: highlight.libraryItem.id, userId },
])
logger.info('update labels jobs enqueued', jobs)
}
}
export const findLabelsByIds = async (

View file

@ -17,6 +17,7 @@ import {
valuesToRawSql,
} from '../repository'
import { libraryItemRepository } from '../repository/library_item'
import { bulkEnqueueUpdateLabels } from '../utils/createTask'
import { setRecentlySavedItemInRedis, wordsCount } from '../utils/helpers'
import { logger } from '../utils/logger'
import { parseSearchQuery } from '../utils/search'
@ -1020,7 +1021,17 @@ export const batchUpdateLibraryItems = async (
return !existingLabel
})
)
return tx.getRepository(EntityLabel).save(labelsToAdd)
const labelsAdded = await tx.getRepository(EntityLabel).save(labelsToAdd)
const data = labelsAdded.map((label) => ({
libraryItemId: label.libraryItemId,
userId,
}))
// update labels in library item
const jobs = await bulkEnqueueUpdateLabels(data)
logger.info('update labels jobs enqueued', jobs)
return
}
// generate raw sql because postgres doesn't support prepared statements in DO blocks