import { ImportExceptionListItemSchemaDecoded , NamespaceType , } from '@kbn/securitysolution-io-ts-list-types' ; import { SavedObjectsClientContract } from 'kibana/server' ; import { ImportDataResponse , ImportResponse } from '../../import_exception_list_and_items' ; import { getAllListItemTypes } from './find_all_exception_list_item_types' ; import { getAllListTypes } from './find_all_exception_list_types' ; import { sortExceptionItemsToUpdateOrCreate } from './sort_exception_items_to_create_update' ; import { bulkCreateImportedItems } from './bulk_create_imported_items' ; import { bulkUpdateImportedItems } from './bulk_update_imported_items' ; import { sortItemsImportsByNamespace } from './sort_import_by_namespace' ; import { sortImportResponses } from './sort_import_responses' ; param savedObjectsClient param itemsChunks param isOverwrite param user returns { Object } export const importExceptionListItems = async ( { itemsChunks , isOverwrite , savedObjectsClient , user , } ) : Promise < ImportDataResponse > => { let importExceptionListItemsResponse = [ ] ; for await ( const itemsChunk of itemsChunks ) { const [ agnosticListItems , nonAgnosticListItems ] = sortItemsImportsByNamespace ( itemsChunk ) ; const mapList = ( list ) : { listId : string ; namespaceType : NamespaceType ; } => ( { listId : list . list_id , namespaceType : list . namespace_type , } ) ; const foundLists = await getAllListTypes ( agnosticListItems . map ( mapList ) , nonAgnosticListItems . map ( mapList ) , savedObjectsClient ) ; const foundItems = await getAllListItemTypes ( agnosticListItems , nonAgnosticListItems , savedObjectsClient ) ; const { errors , itemsToCreate , itemsToUpdate } = sortExceptionItemsToUpdateOrCreate ( { existingItems : foundItems , existingLists : foundLists , isOverwrite , items : itemsChunk , user , } ) ; const bulkCreateResponse = await bulkCreateImportedItems ( { itemsToCreate , savedObjectsClient , } ) ; const bulkUpdateResponse = await bulkUpdateImportedItems ( { itemsToUpdate , savedObjectsClient , } ) ; importExceptionListItemsResponse = [ ... importExceptionListItemsResponse , ... bulkCreateResponse , ... bulkUpdateResponse , ... errors , ] ; } return sortImportResponses ( importExceptionListItemsResponse ) ; } ;