import type { Readable } from 'stream' ; import type { CoreStart , KibanaRequest , SavedObject , SavedObjectsImportRetry , } from 'src/core/server' ; import { spaceIdToNamespace } from '../utils/namespace' ; import { createEmptyFailureResponse } from './lib/create_empty_failure_response' ; import { getIneligibleTypes } from './lib/get_ineligible_types' ; import { readStreamToCompletion } from './lib/read_stream_to_completion' ; import { createReadableStreamFromArray } from './lib/readable_stream_from_array' ; import { COPY_TO_SPACES_SAVED_OBJECTS_CLIENT_OPTS } from './lib/saved_objects_client_opts' ; import type { CopyOptions , CopyResponse , ResolveConflictsOptions } from './types' ; export function resolveCopySavedObjectsToSpacesConflictsFactory ( savedObjects , request ) { const { getTypeRegistry , getScopedClient , createExporter , createImporter } = savedObjects ; const savedObjectsClient = getScopedClient ( request , COPY_TO_SPACES_SAVED_OBJECTS_CLIENT_OPTS ) ; const savedObjectsExporter = createExporter ( savedObjectsClient ) ; const savedObjectsImporter = createImporter ( savedObjectsClient ) ; const exportRequestedObjects = async ( sourceSpaceId , options ) => { const objectStream = await savedObjectsExporter . exportByObjects ( { request , namespace : spaceIdToNamespace ( sourceSpaceId ) , includeReferencesDeep : options . includeReferences , excludeExportDetails : true , objects : options . objects , } ) ; return readStreamToCompletion < SavedObject > ( objectStream ) ; } ; const resolveConflictsForSpace = async ( spaceId , objectsStream , retries , createNewCopies ) => { try { const importResponse = await savedObjectsImporter . resolveImportErrors ( { namespace : spaceIdToNamespace ( spaceId ) , readStream : objectsStream , retries , createNewCopies , } ) ; return { success : importResponse . success , successCount : importResponse . successCount , successResults : importResponse . successResults , errors : importResponse . errors , } ; } catch ( error ) { return createEmptyFailureResponse ( [ error ] ) ; } } ; const resolveCopySavedObjectsToSpacesConflicts = async ( sourceSpaceId , options ) : Promise < CopyResponse > => { const response = { } ; const exportedSavedObjects = await exportRequestedObjects ( sourceSpaceId , { includeReferences : options . includeReferences , objects : options . objects , } ) ; const ineligibleTypes = getIneligibleTypes ( getTypeRegistry ( ) ) ; const filteredObjects = exportedSavedObjects . filter ( ( { type } ) => ! ineligibleTypes . includes ( type ) ) ; for ( const entry of Object . entries ( options . retries ) ) { const [ spaceId , entryRetries ] = entry ; const retries = entryRetries . map ( ( retry ) => ( { ... retry , replaceReferences : [ ] } ) ) ; response [ spaceId ] = await resolveConflictsForSpace ( spaceId , createReadableStreamFromArray ( filteredObjects ) , retries , options . createNewCopies ) ; } return response ; } ; return resolveCopySavedObjectsToSpacesConflicts ; }