From 8d3cac989b5f2d66221ce880f9562c66b875a193 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Fri, 30 Jun 2023 11:08:03 +0800 Subject: [PATCH 1/2] feat: create labels if not exist when setting labels in the page --- packages/api/src/generated/graphql.ts | 3 +- packages/api/src/generated/schema.graphql | 3 +- packages/api/src/resolvers/labels/index.ts | 44 +++++++++++++++------- packages/api/src/schema.ts | 3 +- 4 files changed, 37 insertions(+), 16 deletions(-) diff --git a/packages/api/src/generated/graphql.ts b/packages/api/src/generated/graphql.ts index 2c2af3081..c4e232e81 100644 --- a/packages/api/src/generated/graphql.ts +++ b/packages/api/src/generated/graphql.ts @@ -2485,7 +2485,8 @@ export type SetLabelsForHighlightInput = { }; export type SetLabelsInput = { - labelIds: Array; + labelIds?: InputMaybe>; + labels?: InputMaybe>; pageId: Scalars['ID']; }; diff --git a/packages/api/src/generated/schema.graphql b/packages/api/src/generated/schema.graphql index 7919e1273..3be069ee8 100644 --- a/packages/api/src/generated/schema.graphql +++ b/packages/api/src/generated/schema.graphql @@ -1854,7 +1854,8 @@ input SetLabelsForHighlightInput { } input SetLabelsInput { - labelIds: [ID!]! + labelIds: [ID!] + labels: [CreateLabelInput!] pageId: ID! } diff --git a/packages/api/src/resolvers/labels/index.ts b/packages/api/src/resolvers/labels/index.ts index 68699346f..e9fdacab2 100644 --- a/packages/api/src/resolvers/labels/index.ts +++ b/packages/api/src/resolvers/labels/index.ts @@ -41,6 +41,7 @@ import { import { AppDataSource } from '../../server' import { createLabel, + createLabels, getLabelByName, getLabelsByIds, } from '../../services/labels' @@ -210,7 +211,14 @@ export const setLabelsResolver = authorized< >(async (_, { input }, { claims: { uid }, log, pubsub }) => { log.info('setLabelsResolver') - const { pageId, labelIds } = input + const { pageId, labelIds, labels } = input + + if (!labelIds && !labels) { + log.info('labelIds or labels must be provided') + return { + errorCodes: [SetLabelsErrorCode.BadRequest], + } + } try { const user = await getRepository(User).findOneBy({ id: uid }) @@ -232,25 +240,35 @@ export const setLabelsResolver = authorized< } } - const labels = await getLabelsByIds(uid, labelIds) - if (labels.length !== labelIds.length) { - return { - errorCodes: [SetLabelsErrorCode.NotFound], + const ctx = { + uid, + pubsub, + refresh: true, + } + let labelsSet: Label[] = [] + + if (labels && labels.length > 0) { + // for new clients that send label names + // create labels if they don't exist + labelsSet = await createLabels(ctx, labels) + } else if (labelIds && labelIds.length > 0) { + // for old clients that send labelIds + labelsSet = await getLabelsByIds(uid, labelIds) + if (labelsSet.length !== labelIds.length) { + return { + errorCodes: [SetLabelsErrorCode.NotFound], + } } } // filter out labels that are already set - const labelsToAdd = labels.filter( + const labelsToAdd = labelsSet.filter( (label) => !page.labels?.some((pageLabel) => pageLabel.id === label.id) ) // update labels in the page const updated = await updateLabelsInPage( pageId, - labels, - { - pubsub, - uid, - refresh: true, - }, + labelsSet, + ctx, labelsToAdd ) if (!updated) { @@ -270,7 +288,7 @@ export const setLabelsResolver = authorized< }) return { - labels, + labels: labelsSet, } } catch (error) { log.error(error) diff --git a/packages/api/src/schema.ts b/packages/api/src/schema.ts index 9a7ec8071..2e600e0f1 100755 --- a/packages/api/src/schema.ts +++ b/packages/api/src/schema.ts @@ -1503,7 +1503,8 @@ const schema = gql` input SetLabelsInput { pageId: ID! - labelIds: [ID!]! + labelIds: [ID!] + labels: [CreateLabelInput!] } union SetLabelsResult = SetLabelsSuccess | SetLabelsError From 13afb7febd6df6ce378aed8e677f5d6d0ea1fa95 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Fri, 30 Jun 2023 11:40:39 +0800 Subject: [PATCH 2/2] feat: create labels if not exist when setting labels in the highlight --- packages/api/src/elastic/labels.ts | 26 ++++++----- packages/api/src/generated/graphql.ts | 3 +- packages/api/src/generated/schema.graphql | 3 +- packages/api/src/resolvers/labels/index.ts | 51 +++++++++++++++++----- packages/api/src/schema.ts | 3 +- 5 files changed, 60 insertions(+), 26 deletions(-) diff --git a/packages/api/src/elastic/labels.ts b/packages/api/src/elastic/labels.ts index 6117a2857..cafaf01e0 100644 --- a/packages/api/src/elastic/labels.ts +++ b/packages/api/src/elastic/labels.ts @@ -1,7 +1,7 @@ -import { Label, PageContext } from './types' -import { client, INDEX_ALIAS } from './index' -import { EntityType } from '../datalayer/pubsub' import { ResponseError } from '@elastic/elasticsearch/lib/errors' +import { EntityType } from '../datalayer/pubsub' +import { client, INDEX_ALIAS } from './index' +import { Label, PageContext } from './types' export const addLabelInPage = async ( pageId: string, @@ -273,7 +273,8 @@ export const updateLabel = async ( export const setLabelsForHighlight = async ( highlightId: string, labels: Label[], - ctx: PageContext + ctx: PageContext, + labelsToAdd?: Label[] ): Promise => { try { const { body } = await client.updateByQuery({ @@ -304,14 +305,17 @@ export const setLabelsForHighlight = async ( conflicts: 'proceed', // ignore conflicts }) - if (body.updated > 0) { - for (const label of labels) { - await ctx.pubsub.entityCreated