From 2dcab66af2809c9bcce21cbadbe7f8ebcce33015 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Wed, 8 Jun 2022 17:39:38 +0800 Subject: [PATCH] Update labels for both pages and highlights --- packages/api/src/elastic/labels.ts | 41 ++++++++++++++++------ packages/api/src/resolvers/labels/index.ts | 11 ++---- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/packages/api/src/elastic/labels.ts b/packages/api/src/elastic/labels.ts index 990c15449..58cd9ab83 100644 --- a/packages/api/src/elastic/labels.ts +++ b/packages/api/src/elastic/labels.ts @@ -95,7 +95,6 @@ export const updateLabelsInPage = async ( } export const deleteLabel = async ( - userId: string, label: string, ctx: PageContext ): Promise => { @@ -119,7 +118,7 @@ export const deleteLabel = async ( bool: { must: { term: { - userId, + userId: ctx.uid, }, }, should: [ @@ -167,7 +166,7 @@ export const deleteLabel = async ( } } -export const updateLabelInPage = async ( +export const updateLabel = async ( label: Label, ctx: PageContext ): Promise => { @@ -176,8 +175,14 @@ export const updateLabelInPage = async ( index: INDEX_ALIAS, body: { script: { - source: `ctx._source.labels.removeIf(l -> l.id == params.label.id); - ctx._source.labels.add(params.label)`, + source: `if (ctx._source.labels != null) { + ctx._source.labels.removeIf(l -> l.id == params.label.id); + ctx._source.labels.add(params.label) + } + if (ctx._source.highlights != null) { + ctx._source.highlights[0].labels.removeIf(l -> l.id == params.label.id); + ctx._source.highlights[0].labels.add(params.label) + }`, lang: 'painless', params: { label: label, @@ -185,12 +190,12 @@ export const updateLabelInPage = async ( }, query: { bool: { - filter: [ - { - term: { - userId: ctx.uid, - }, + must: { + term: { + userId: ctx.uid, }, + }, + should: [ { nested: { path: 'labels', @@ -201,7 +206,23 @@ export const updateLabelInPage = async ( }, }, }, + { + nested: { + path: 'highlights', + query: { + nested: { + path: 'highlights.labels', + query: { + term: { + 'highlights.labels.id': label.id, + }, + }, + }, + }, + }, + }, ], + minimum_should_match: 1, }, }, }, diff --git a/packages/api/src/resolvers/labels/index.ts b/packages/api/src/resolvers/labels/index.ts index f11c35c80..0a0bd1566 100644 --- a/packages/api/src/resolvers/labels/index.ts +++ b/packages/api/src/resolvers/labels/index.ts @@ -33,7 +33,7 @@ import { getPageById } from '../../elastic/pages' import { deleteLabel, setLabelsForHighlight, - updateLabelInPage, + updateLabel, updateLabelsInPage, } from '../../elastic/labels' import { getHighlightById } from '../../elastic/highlights' @@ -179,7 +179,7 @@ export const deleteLabelResolver = authorized< } // delete label in elastic pages and highlights - await deleteLabel(uid, labelId, { + await deleteLabel(labelId, { pubsub: createPubSubClient(), uid, }) @@ -322,15 +322,10 @@ export const updateLabelResolver = authorized< } } - const updated = await updateLabelInPage(label, { + await updateLabel(label, { pubsub, uid, }) - if (!updated) { - return { - errorCodes: [UpdateLabelErrorCode.BadRequest], - } - } return { label } } catch (error) {