From 33ba17707b72eda2890e6d94ef59824ece1a0a73 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Tue, 5 Sep 2023 19:32:22 +0800 Subject: [PATCH] fix setting labels --- packages/api/src/services/highlights.ts | 22 +++++++++------------- packages/api/src/services/labels.ts | 21 +++++++++++++++++++-- packages/api/src/services/library_item.ts | 6 +----- 3 files changed, 29 insertions(+), 20 deletions(-) diff --git a/packages/api/src/services/highlights.ts b/packages/api/src/services/highlights.ts index 14c1a00b0..301931605 100644 --- a/packages/api/src/services/highlights.ts +++ b/packages/api/src/services/highlights.ts @@ -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 & { 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( 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 diff --git a/packages/api/src/services/labels.ts b/packages/api/src/services/labels.ts index e8454c78b..c15d6f152 100644 --- a/packages/api/src/services/labels.ts +++ b/packages/api/src/services/labels.ts @@ -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 diff --git a/packages/api/src/services/library_item.ts b/packages/api/src/services/library_item.ts index b60713a16..84afb7e0a 100644 --- a/packages/api/src/services/library_item.ts +++ b/packages/api/src/services/library_item.ts @@ -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>( EntityType.PAGE, libraryItem,