fix setting labels

This commit is contained in:
Hongbo Wu
2023-09-05 19:32:22 +08:00
parent 0016b0a9a7
commit 33ba17707b
3 changed files with 29 additions and 20 deletions

View File

@ -3,7 +3,7 @@ import { DeepPartial } from 'typeorm'
import { Highlight } from '../entity/highlight'
import { homePageURL } from '../env'
import { createPubSubClient, EntityType } from '../pubsub'
import { authTrx, setClaims } from '../repository'
import { authTrx } from '../repository'
import { highlightRepository } from '../repository/highlight'
type HighlightEvent = DeepPartial<Highlight> & { pageId: string }
@ -24,8 +24,6 @@ export const createHighlight = async (
pubsub = createPubSubClient()
) => {
const newHighlight = await authTrx(async (tx) => {
await setClaims(tx, userId)
return tx
.withRepository(highlightRepository)
.createAndSave(highlight, libraryItemId, userId)
@ -71,18 +69,17 @@ export const updateHighlight = async (
pubsub = createPubSubClient()
) => {
const updatedHighlight = await authTrx(async (tx) => {
await tx.withRepository(highlightRepository).save({
const highlightRepo = tx.withRepository(highlightRepository)
await highlightRepo.save({
...highlight,
id: highlightId,
})
return tx.withRepository(highlightRepository).findById(highlightId)
return highlightRepo.findOneByOrFail({
id: highlightId,
})
})
if (!updatedHighlight) {
throw new Error(`Highlight ${highlightId} not found`)
}
await pubsub.entityUpdated<HighlightEvent>(
EntityType.HIGHLIGHT,
{ ...highlight, id: highlightId, pageId: updatedHighlight.libraryItem.id },
@ -95,10 +92,9 @@ export const updateHighlight = async (
export const deleteHighlightById = async (highlightId: string) => {
return authTrx(async (tx) => {
const highlightRepo = tx.withRepository(highlightRepository)
const highlight = await highlightRepo.findById(highlightId)
if (!highlight) {
throw new Error(`Highlight ${highlightId} not found`)
}
const highlight = await highlightRepo.findOneByOrFail({
id: highlightId,
})
await highlightRepo.delete(highlightId)
return highlight

View File

@ -55,9 +55,26 @@ export const saveLabelsInLibraryItem = async (
pubsub = createPubSubClient()
) => {
await authTrx(async (tx) => {
// delete existing labels
await tx
.withRepository(libraryItemRepository)
.save({ id: libraryItemId, labels })
.createQueryBuilder()
.delete()
.from('entity_labels')
.where('library_item_id = :id', { id: libraryItemId })
.execute()
// insert new labels
await tx
.createQueryBuilder()
.insert()
.into('entity_labels')
.values(
labels.map((label) => ({
library_item_id: libraryItemId,
label_id: label.id,
}))
)
.execute()
})
// create pubsub event

View File

@ -321,13 +321,9 @@ export const updateLibraryItem = async (
const itemRepo = tx.withRepository(libraryItemRepository)
await itemRepo.save({ id, ...libraryItem })
return itemRepo.findById(id)
return itemRepo.findOneByOrFail({ id })
})
if (!updatedLibraryItem) {
throw new Error(`Library item ${id} not found`)
}
await pubsub.entityUpdated<DeepPartial<LibraryItem>>(
EntityType.PAGE,
libraryItem,