import type { ExportExceptionDetails , IdOrUndefined , ListIdOrUndefined , NamespaceType , } from '@kbn/securitysolution-io-ts-list-types' ; import { transformDataToNdjson } from '@kbn/securitysolution-utils' ; import { SavedObjectsClientContract } from 'kibana/server' ; import { findExceptionListItem } from './find_exception_list_item' ; import { getExceptionList } from './get_exception_list' ; interface ExportExceptionListAndItemsOptions { id : IdOrUndefined ; listId : ListIdOrUndefined ; savedObjectsClient : SavedObjectsClientContract ; namespaceType : NamespaceType ; } export interface ExportExceptionListAndItemsReturn { exportData : string ; exportDetails : ExportExceptionDetails ; } export const exportExceptionListAndItems = async ( { id , listId , namespaceType , savedObjectsClient , } ) : Promise < ExportExceptionListAndItemsReturn | null > => { const exceptionList = await getExceptionList ( { id , listId , namespaceType , savedObjectsClient , } ) ; if ( exceptionList == null ) { return null ; } else { const listItems = await findExceptionListItem ( { filter : undefined , listId : exceptionList . list_id , namespaceType : exceptionList . namespace_type , page : 1 , perPage : 10000 , savedObjectsClient , sortField : 'exception-list.created_at' , sortOrder : 'desc' , } ) ; const exceptionItems = listItems ?. data ?? [ ] ; const { exportData } = getExport ( [ exceptionList , ... exceptionItems ] ) ; return { exportData : `${ exportData }` , exportDetails : { exported_exception_list_count : 1 , exported_exception_list_item_count : exceptionItems . length , missing_exception_list_item_count : 0 , missing_exception_list_items : [ ] , missing_exception_lists : [ ] , missing_exception_lists_count : 0 , } , } ; } } ; export const getExport = ( data ) : { exportData : string ; } => { const ndjson = transformDataToNdjson ( data ) ; return { exportData : ndjson } ; } ;