feat: create labels if not exist when setting labels in the highlight

This commit is contained in:
Hongbo Wu
2023-06-30 11:40:39 +08:00
parent 8d3cac989b
commit 13afb7febd
5 changed files with 60 additions and 26 deletions

View File

@ -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<boolean> => {
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<Label & { highlightId: string }>(
EntityType.LABEL,
{ highlightId, ...label },
ctx.uid
if (labelsToAdd) {
// publish labels to be added
await Promise.all(
labelsToAdd.map((label) =>
ctx.pubsub.entityCreated<Label & { highlightId: string }>(
EntityType.LABEL,
{ highlightId, ...label },
ctx.uid
)
)
}
)
}
return true

View File

@ -2481,7 +2481,8 @@ export enum SetLabelsErrorCode {
export type SetLabelsForHighlightInput = {
highlightId: Scalars['ID'];
labelIds: Array<Scalars['ID']>;
labelIds?: InputMaybe<Array<Scalars['ID']>>;
labels?: InputMaybe<Array<CreateLabelInput>>;
};
export type SetLabelsInput = {

View File

@ -1850,7 +1850,8 @@ enum SetLabelsErrorCode {
input SetLabelsForHighlightInput {
highlightId: ID!
labelIds: [ID!]!
labelIds: [ID!]
labels: [CreateLabelInput!]
}
input SetLabelsInput {

View File

@ -379,7 +379,14 @@ export const setLabelsForHighlightResolver = authorized<
>(async (_, { input }, { claims: { uid }, log, pubsub }) => {
log.info('setLabelsForHighlightResolver')
const { highlightId, labelIds } = input
const { highlightId, 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 })
@ -401,19 +408,39 @@ export const setLabelsForHighlightResolver = 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 = labelsSet.filter(
(label) =>
!highlight.labels?.some(
(highlightLabel) => highlightLabel.id === label.id
)
)
// set labels in the highlights
const updated = await setLabelsForHighlight(highlightId, labels, {
pubsub,
uid,
refresh: true,
})
const updated = await setLabelsForHighlight(
highlightId,
labelsSet,
ctx,
labelsToAdd
)
if (!updated) {
return {
errorCodes: [SetLabelsErrorCode.NotFound],
@ -431,7 +458,7 @@ export const setLabelsForHighlightResolver = authorized<
})
return {
labels,
labels: labelsSet,
}
} catch (error) {
log.error(error)

View File

@ -1837,7 +1837,8 @@ const schema = gql`
input SetLabelsForHighlightInput {
highlightId: ID!
labelIds: [ID!]!
labelIds: [ID!]
labels: [CreateLabelInput!]
}
union TypeaheadSearchResult = TypeaheadSearchSuccess | TypeaheadSearchError