import type { ListId , NamespaceType } from '@kbn/securitysolution-io-ts-list-types' ; import { getSavedObjectType } from '@kbn/securitysolution-list-utils' ; import { asyncForEach } from '@kbn/std' ; import { SavedObjectsClientContract } from '../../../../../../src/core/server' ; import { findExceptionListItem } from './find_exception_list_item' ; const PER_PAGE = 100 ; interface DeleteExceptionListItemByListOptions { listId : ListId ; namespaceType : NamespaceType ; savedObjectsClient : SavedObjectsClientContract ; } export const deleteExceptionListItemByList = async ( { listId , savedObjectsClient , namespaceType , } ) : Promise < void > => { const ids = await getExceptionListItemIds ( { listId , namespaceType , savedObjectsClient } ) ; await deleteFoundExceptionListItems ( { ids , namespaceType , savedObjectsClient } ) ; } ; export const getExceptionListItemIds = async ( { listId , savedObjectsClient , namespaceType , } ) : Promise < string [ ] > => { let page = 1 ; let ids = [ ] ; let foundExceptionListItems = await findExceptionListItem ( { filter : undefined , listId , namespaceType , page , perPage : PER_PAGE , savedObjectsClient , sortField : 'tie_breaker_id' , sortOrder : 'desc' , } ) ; while ( foundExceptionListItems != null && foundExceptionListItems . data . length > 0 ) { ids = [ ... ids , ... foundExceptionListItems . data . map ( ( exceptionListItem ) => exceptionListItem . id ) , ] ; page += 1 ; foundExceptionListItems = await findExceptionListItem ( { filter : undefined , listId , namespaceType , page , perPage : PER_PAGE , savedObjectsClient , sortField : 'tie_breaker_id' , sortOrder : 'desc' , } ) ; } return ids ; } ; /**
 * NOTE: This is slow and terrible as we are deleting everything one at a time.
 * TODO: Replace this with a bulk call or a delete by query would be more useful
 */ export const deleteFoundExceptionListItems = async ( { ids , savedObjectsClient , namespaceType , } ) : Promise < void > => { const savedObjectType = getSavedObjectType ( { namespaceType } ) ; await asyncForEach ( ids , async ( id ) => { try { await savedObjectsClient . delete ( savedObjectType , id ) ; } catch ( err ) { } } ) ; } ;