import { collectSavedObjects } from './collect_saved_objects' ; import { createObjectsFilter } from './create_objects_filter' ; import { splitOverwrites } from './split_overwrites' ; import { SavedObjectsImportError , SavedObjectsImportResponse , SavedObjectsResolveImportErrorsOptions , SavedObjectsImportSuccess , } from './types' ; import { regenerateIds } from './regenerate_ids' ; import { validateReferences } from './validate_references' ; import { validateRetries } from './validate_retries' ; import { createSavedObjects } from './create_saved_objects' ; import { getImportIdMapForRetries } from './check_origin_conflicts' ; import { SavedObject } from '../types' ; import { checkConflicts } from './check_conflicts' ; /**
 * Resolve and return saved object import errors.
 * See the  SavedObjectsResolveImportErrorsOptions for more detailed informations.
 *
 *  public export async function resolveSavedObjectsImportErrors ( { readStream , objectLimit , retries , savedObjectsClient , typeRegistry , namespace , createNewCopies , } ) { validateRetries ( retries ) ; let successCount = 0 ; let errorAccumulator = [ ] ; let importIdMap = new Map ( ) ; const supportedTypes = typeRegistry . getImportableAndExportableTypes ( ) . map ( ( type ) => type . name ) ; const filter = createObjectsFilter ( retries ) ; const { errors : collectorErrors , collectedObjects : objectsToResolve } = await collectSavedObjects ( { readStream , objectLimit , filter , supportedTypes , } ) ; errorAccumulator = [ ... errorAccumulator , ... collectorErrors ] ; const retriesReferencesMap = new Map < string , { [ key ] : string ; } > ( ) ; for ( const retry of retries ) { const map = { } ; for ( const { type , from , to } of retry . replaceReferences ) { map [ `${ type }:${ from }` ] = to ; } retriesReferencesMap . set ( `${ retry . type }:${ retry . id }` , map ) ; } for ( const savedObject of objectsToResolve ) { const refMap = retriesReferencesMap . get ( `${ savedObject . type }:${ savedObject . id }` ) ; if ( ! refMap ) { continue ; } for ( const reference of savedObject . references || [ ] ) { if ( refMap [ `${ reference . type }:${ reference . id }` ] ) { reference . id = refMap [ `${ reference . type }:${ reference . id }` ] ; } } } const validateReferencesResult = await validateReferences ( objectsToResolve , savedObjectsClient , namespace , retries ) ; errorAccumulator = [ ... errorAccumulator , ... validateReferencesResult ] ; if ( createNewCopies ) { importIdMap = regenerateIds ( objectsToResolve ) ; } const checkConflictsParams = { objects : objectsToResolve , savedObjectsClient , namespace , retries , createNewCopies , } ; const checkConflictsResult = await checkConflicts ( checkConflictsParams ) ; errorAccumulator = [ ... errorAccumulator , ... checkConflictsResult . errors ] ; const getImportIdMapForRetriesParams = { objects : checkConflictsResult . filteredObjects , retries , createNewCopies , } ; const importIdMapForRetries = getImportIdMapForRetries ( getImportIdMapForRetriesParams ) ; importIdMap = new Map ( [ ... importIdMap , ... importIdMapForRetries , ... checkConflictsResult . importIdMap , ] ) ; let successResults = [ ] ; const accumulatedErrors = [ ... errorAccumulator ] ; const bulkCreateObjects = async ( objects , overwrite ? ) => { const createSavedObjectsParams = { objects , accumulatedErrors , savedObjectsClient , importIdMap , namespace , overwrite , } ; const { createdObjects , errors : bulkCreateErrors } = await createSavedObjects ( createSavedObjectsParams ) ; errorAccumulator = [ ... errorAccumulator , ... bulkCreateErrors ] ; successCount += createdObjects . length ; successResults = [ ... successResults , ... createdObjects . map ( ( { type , id , attributes : { title } , destinationId , originId } ) => { const meta = { title , icon : typeRegistry . getType ( type ) ?. management ?. icon } ; return { type , id , meta , ... ( overwrite && { overwrite } ) , ... ( destinationId && { destinationId } ) , ... ( destinationId && ! originId && ! createNewCopies && { createNewCopy : true } ) , } ; } ) , ] ; } ; const { objectsToOverwrite , objectsToNotOverwrite } = splitOverwrites ( objectsToResolve , retries ) ; await bulkCreateObjects ( objectsToOverwrite , true ) ; await bulkCreateObjects ( objectsToNotOverwrite ) ; const errorResults = errorAccumulator . map ( ( error ) => { const icon = typeRegistry . getType ( error . type ) ?. management ?. icon ; const attemptedOverwrite = retries . some ( ( { type , id , overwrite } ) => type === error . type && id === error . id && overwrite ) ; return { ... error , meta : { ... error . meta , icon } , ... ( attemptedOverwrite && { overwrite : true } ) , } ; } ) ; return { successCount , success : errorAccumulator . length === 0 , ... ( successResults . length && { successResults } ) , ... ( errorResults . length && { errors : errorResults } ) , } ; }