import { ExceptionListItemSchema , ImportExceptionListItemSchemaDecoded , NamespaceType , } from '@kbn/securitysolution-io-ts-list-types' ; import { getSavedObjectTypes } from '@kbn/securitysolution-list-utils' ; import { SavedObjectsClientContract , SavedObjectsFindResponse } from 'kibana/server' ; import { ExceptionListSoSchema } from '../../../../schemas/saved_objects' ; import { getExceptionListsItemFilter } from '../../find_exception_list_items' ; import { CHUNK_PARSED_OBJECT_SIZE } from '../../import_exception_list_and_items' ; import { transformSavedObjectsToFoundExceptionListItem } from '..' ; param objects { array } param savedObjectsClient { object } returns { string } export const getItemsFilter = ( { objects , namespaceType , } ) : string => { return `${ getSavedObjectTypes ( { namespaceType : [ namespaceType ] , } ) [ 0 ] }.attributes.item_id:(${ objects . map ( ( item ) => item . item_id ) . join ( ' OR ' ) })` ; } ; param agnosticListItems { array } param nonAgnosticListItems { array } param savedObjectsClient { object } returns { object } export const findAllListItemTypes = async ( agnosticListItems , nonAgnosticListItems , savedObjectsClient ) : Promise < SavedObjectsFindResponse < ExceptionListSoSchema > | null > => { const agnosticFilter = getItemsFilter ( { namespaceType : 'agnostic' , objects : agnosticListItems , } ) ; const nonAgnosticFilter = getItemsFilter ( { namespaceType : 'single' , objects : nonAgnosticListItems , } ) ; const savedObjectType = getSavedObjectTypes ( { namespaceType : [ 'single' ] } ) ; const savedObjectTypeAgnostic = getSavedObjectTypes ( { namespaceType : [ 'agnostic' ] } ) ; if ( ! agnosticListItems . length && ! nonAgnosticListItems . length ) { return null ; } else if ( agnosticListItems . length && ! nonAgnosticListItems . length ) { return savedObjectsClient . find < ExceptionListSoSchema > ( { filter : getExceptionListsItemFilter ( { filter : [ agnosticFilter ] , listId : agnosticListItems . map ( ( { list_id : listId } ) => listId ) , savedObjectType : agnosticListItems . map ( ( { namespace_type : namespaceType } ) => getSavedObjectTypes ( { namespaceType : [ namespaceType ] } ) [ 0 ] ) , } ) , page : undefined , perPage : CHUNK_PARSED_OBJECT_SIZE , sortField : undefined , sortOrder : undefined , type : savedObjectTypeAgnostic , } ) ; } else if ( ! agnosticListItems . length && nonAgnosticListItems . length ) { return savedObjectsClient . find < ExceptionListSoSchema > ( { filter : getExceptionListsItemFilter ( { filter : [ nonAgnosticFilter ] , listId : nonAgnosticListItems . map ( ( { list_id : listId } ) => listId ) , savedObjectType : nonAgnosticListItems . map ( ( { namespace_type : namespaceType } ) => getSavedObjectTypes ( { namespaceType : [ namespaceType ] } ) [ 0 ] ) , } ) , page : undefined , perPage : CHUNK_PARSED_OBJECT_SIZE , sortField : undefined , sortOrder : undefined , type : savedObjectType , } ) ; } else { const items = [ ... nonAgnosticListItems , ... agnosticListItems ] ; return savedObjectsClient . find < ExceptionListSoSchema > ( { filter : getExceptionListsItemFilter ( { filter : [ nonAgnosticFilter , agnosticFilter ] , listId : items . map ( ( { list_id : listId } ) => listId ) , savedObjectType : items . map ( ( { namespace_type : namespaceType } ) => getSavedObjectTypes ( { namespaceType : [ namespaceType ] } ) [ 0 ] ) , } ) , page : undefined , perPage : CHUNK_PARSED_OBJECT_SIZE , sortField : undefined , sortOrder : undefined , type : [ ... savedObjectType , ... savedObjectTypeAgnostic ] , } ) ; } } ; param agnosticListItems { array } param nonAgnosticListItems { array } param savedObjectsClient { object } returns { object } export const getAllListItemTypes = async ( agnosticListItems , nonAgnosticListItems , savedObjectsClient ) : Promise < Record < string , ExceptionListItemSchema > > => { const foundItemsResponse = await findAllListItemTypes ( agnosticListItems , nonAgnosticListItems , savedObjectsClient ) ; if ( foundItemsResponse == null ) { return { } ; } const transformedResponse = transformSavedObjectsToFoundExceptionListItem ( { savedObjectsFindResponse : foundItemsResponse , } ) ; return transformedResponse . data . reduce ( ( acc , item ) => ( { ... acc , [ item . item_id ] : item , } ) , { } ) ; } ;