Fix labels not being updated in elastic

This commit is contained in:
Hongbo Wu 2022-05-19 12:08:50 +08:00
parent 18bf8ed7ac
commit b8225fc8c8
2 changed files with 71 additions and 4 deletions

View file

@ -136,7 +136,7 @@ export const deleteLabelInPages = async (
refresh: ctx.refresh,
})
if (body.updated === 0) return false
console.log('updated pages', body.updated)
await ctx.pubsub.entityDeleted(EntityType.LABEL, label, ctx.uid)
@ -146,3 +146,56 @@ export const deleteLabelInPages = async (
return false
}
}
export const updateLabelInPage = async (
label: Label,
ctx: PageContext
): Promise<boolean> => {
try {
const { body } = await client.updateByQuery({
index: INDEX_ALIAS,
body: {
script: {
source: `ctx._source.labels.removeIf(h -> h.name == params.label.name);
ctx._source.labels.add(params.label)`,
lang: 'painless',
params: {
label: label,
},
},
query: {
bool: {
filter: [
{
term: {
userId: ctx.uid,
},
},
{
nested: {
path: 'labels',
query: {
term: {
'labels.name': label.name,
},
},
},
},
],
},
},
},
refresh: ctx.refresh,
})
console.log('updated pages', body.updated)
await ctx.pubsub.entityUpdated<Label>(EntityType.LABEL, label, ctx.uid)
return true
} catch (e) {
console.error('failed to update label in elastic', e)
return false
}
}

View file

@ -29,7 +29,11 @@ import { getRepository, setClaims } from '../../entity/utils'
import { createPubSubClient } from '../../datalayer/pubsub'
import { AppDataSource } from '../../server'
import { getPageById } from '../../elastic/pages'
import { deleteLabelInPages, updateLabelsInPage } from '../../elastic/labels'
import {
deleteLabelInPages,
updateLabelInPage,
updateLabelsInPage,
} from '../../elastic/labels'
export const labelsResolver = authorized<LabelsSuccess, LabelsError>(
async (_obj, _params, { claims: { uid }, log }) => {
@ -266,7 +270,7 @@ export const updateLabelResolver = authorized<
UpdateLabelSuccess,
UpdateLabelError,
MutationUpdateLabelArgs
>(async (_, { input }, { claims: { uid }, log }) => {
>(async (_, { input }, { claims: { uid }, log, pubsub }) => {
log.info('updateLabelResolver')
try {
@ -308,7 +312,17 @@ export const updateLabelResolver = authorized<
if (!result.affected) {
log.error('failed to update')
return {
errorCodes: [UpdateLabelErrorCode.NotFound],
errorCodes: [UpdateLabelErrorCode.BadRequest],
}
}
const updated = await updateLabelInPage(label, {
pubsub,
uid,
})
if (!updated) {
return {
errorCodes: [UpdateLabelErrorCode.BadRequest],
}
}