import * as esKuery from '@kbn/es-query' ; import { getErrorMessage as getEsErrorMessage } from '../../../../elasticsearch' ; import type { ISavedObjectTypeRegistry } from '../../../saved_objects_type_registry' ; import type { IndexMapping } from '../../../mappings' ; import { LEGACY_URL_ALIAS_TYPE } from '../../../object_types' ; import type { RepositoryEsClient } from '../repository_es_client' ; import { getSearchDsl } from '../search_dsl' ; import { ALL_NAMESPACES_STRING , DEFAULT_NAMESPACE_STRING } from '../utils' ; internal export interface DeleteLegacyUrlAliasesParams { mappings : IndexMapping ; registry : ISavedObjectTypeRegistry ; client : RepositoryEsClient ; getIndexForType : ( type : string ) => string ; /** The object type. */ type : string ; /** The object ID. */ id : string ; /**
     * The namespaces to include or exclude when searching for legacy URL alias targets (depends on the `deleteBehavior` parameter).
     * Note that using `namespaces: [], deleteBehavior: 'exclusive'` will delete all aliases for this object in all spaces.
     */ namespaces : string [ ] ; /**
     * If this is equal to 'inclusive', all aliases with a `targetNamespace` in the `namespaces` array will be deleted.
     * If this is equal to 'exclusive', all aliases with a `targetNamespace` _not_ in the `namespaces` array will be deleted.
     */ deleteBehavior : 'inclusive' | 'exclusive' ; } internal export async function deleteLegacyUrlAliases ( params ) { const { mappings , registry , client , getIndexForType , type , id , namespaces , deleteBehavior } = params ; if ( namespaces . includes ( ALL_NAMESPACES_STRING ) ) { throwError ( type , id , '"namespaces" cannot include the * string' ) ; } const filteredNamespaces = namespaces . filter ( ( namespace ) => namespace !== DEFAULT_NAMESPACE_STRING ) ; if ( ! filteredNamespaces . length && deleteBehavior === 'inclusive' ) { return ; } const { buildNode } = esKuery . nodeTypes . function ; const match1 = buildNode ( 'is' , `${ LEGACY_URL_ALIAS_TYPE }.targetType` , type ) ; const match2 = buildNode ( 'is' , `${ LEGACY_URL_ALIAS_TYPE }.targetId` , id ) ; const kueryNode = buildNode ( 'and' , [ match1 , match2 ] ) ; try { await client . updateByQuery ( { index : getIndexForType ( LEGACY_URL_ALIAS_TYPE ) , refresh : false , body : { ... getSearchDsl ( mappings , registry , { type : LEGACY_URL_ALIAS_TYPE , kueryNode , } ) , script : { source : `
              if (params['namespaces'].indexOf(ctx._source['${ LEGACY_URL_ALIAS_TYPE }']['targetNamespace']) > -1) {
                ctx.op = params['matchTargetNamespaceOp'];
              } else {
                ctx.op = params['notMatchTargetNamespaceOp'];
              }
            ` , params : { namespaces : filteredNamespaces , matchTargetNamespaceOp : deleteBehavior === 'inclusive' ? 'delete' : 'noop' , notMatchTargetNamespaceOp : deleteBehavior === 'inclusive' ? 'noop' : 'delete' , } , lang : 'painless' , } , conflicts : 'proceed' , } , } , { ignore : [ 404 ] } ) ; } catch ( err ) { const errorMessage = getEsErrorMessage ( err ) ; throwError ( type , id , `${ errorMessage }` ) ; } } function throwError ( type , id , message ) { throw new Error ( `Failed to delete legacy URL aliases for ${ type }/${ id }: ${ message }` ) ; }