diff --git a/packages/kbn-securitysolution-list-hooks/src/use_create_list_item/index.ts b/packages/kbn-securitysolution-list-hooks/src/use_create_list_item/index.ts index 289da386d8690..98939d93185d6 100644 --- a/packages/kbn-securitysolution-list-hooks/src/use_create_list_item/index.ts +++ b/packages/kbn-securitysolution-list-hooks/src/use_create_list_item/index.ts @@ -28,7 +28,7 @@ export const useCreateListItemMutation = ( const invalidateListItemQuery = useInvalidateListItemQuery(); return useMutation, CreateListMutationParams>( ({ listId, value, http }) => - createListItemWithOptionalSignal({ listId, value, http, refresh: 'true' }), + createListItemWithOptionalSignal({ listId, value, http, refresh: 'wait_for' }), { ...options, mutationKey: CREATE_LIST_ITEM_MUTATION_KEY, diff --git a/packages/kbn-securitysolution-list-hooks/src/use_delete_list_item/index.ts b/packages/kbn-securitysolution-list-hooks/src/use_delete_list_item/index.ts index 11cb95dfddd47..63107edda73a9 100644 --- a/packages/kbn-securitysolution-list-hooks/src/use_delete_list_item/index.ts +++ b/packages/kbn-securitysolution-list-hooks/src/use_delete_list_item/index.ts @@ -26,7 +26,7 @@ export const useDeleteListItemMutation = ( ) => { const invalidateListItemQuery = useInvalidateListItemQuery(); return useMutation, DeleteListMutationParams>( - ({ id, http }) => deleteListItemWithOptionalSignal({ id, http, refresh: 'true' }), + ({ id, http }) => deleteListItemWithOptionalSignal({ id, http }), { ...options, mutationKey: DELETE_LIST_ITEM_MUTATION_KEY, diff --git a/x-pack/plugins/lists/server/services/items/delete_list_item.test.ts b/x-pack/plugins/lists/server/services/items/delete_list_item.test.ts index a468eae0b8811..08ac751b8abd8 100644 --- a/x-pack/plugins/lists/server/services/items/delete_list_item.test.ts +++ b/x-pack/plugins/lists/server/services/items/delete_list_item.test.ts @@ -34,16 +34,26 @@ describe('delete_list_item', () => { test('Delete returns the same list item if a list item is returned from "getListItem"', async () => { const listItem = getListItemResponseMock(); - (getListItem as unknown as jest.Mock).mockResolvedValueOnce(listItem); + (getListItem as unknown as jest.Mock) + .mockResolvedValueOnce(listItem) + .mockResolvedValueOnce(null); const options = getDeleteListItemOptionsMock(); + (options.esClient.deleteByQuery as unknown as jest.Mock).mockResolvedValueOnce({ + deleted: true, + }); const deletedListItem = await deleteListItem(options); expect(deletedListItem).toEqual(listItem); }); test('Delete calls "deleteByQuery" if a list item is returned from "getListItem"', async () => { const listItem = getListItemResponseMock(); - (getListItem as unknown as jest.Mock).mockResolvedValueOnce(listItem); + (getListItem as unknown as jest.Mock) + .mockResolvedValueOnce(listItem) + .mockResolvedValueOnce(null); const options = getDeleteListItemOptionsMock(); + (options.esClient.deleteByQuery as unknown as jest.Mock).mockResolvedValueOnce({ + deleted: true, + }); await deleteListItem(options); const deleteByQuery = { index: LIST_ITEM_INDEX, diff --git a/x-pack/plugins/lists/server/services/items/delete_list_item.ts b/x-pack/plugins/lists/server/services/items/delete_list_item.ts index 751a56a543352..8314f24059c0b 100644 --- a/x-pack/plugins/lists/server/services/items/delete_list_item.ts +++ b/x-pack/plugins/lists/server/services/items/delete_list_item.ts @@ -8,6 +8,8 @@ import { ElasticsearchClient } from '@kbn/core/server'; import type { Id, ListItemSchema } from '@kbn/securitysolution-io-ts-list-types'; +import { waitUntilDocumentIndexed } from '../utils'; + import { getListItem } from '.'; export interface DeleteListItemOptions { @@ -27,7 +29,7 @@ export const deleteListItem = async ({ if (listItem == null) { return null; } else { - await esClient.deleteByQuery({ + const response = await esClient.deleteByQuery({ index: listItemIndex, query: { ids: { @@ -36,6 +38,21 @@ export const deleteListItem = async ({ }, refresh, }); + + if (response.deleted) { + const checkIfListItemDeleted = async (): Promise => { + const deletedListItem = await getListItem({ esClient, id, listItemIndex }); + if (deletedListItem !== null) { + throw Error( + 'List item was deleted, but the change was not propagated in the expected time interval.' + ); + } + }; + + await waitUntilDocumentIndexed(checkIfListItemDeleted); + } else { + throw Error('Deletion of List Item [item_id] from [item_index] was not successful'); + } } return listItem; };