diff --git a/x-pack/plugins/cases/public/components/all_cases/selector_modal/use_cases_add_to_existing_case_modal.tsx b/x-pack/plugins/cases/public/components/all_cases/selector_modal/use_cases_add_to_existing_case_modal.tsx index 301baba1d1ccd3..6d0da66a72cb71 100644 --- a/x-pack/plugins/cases/public/components/all_cases/selector_modal/use_cases_add_to_existing_case_modal.tsx +++ b/x-pack/plugins/cases/public/components/all_cases/selector_modal/use_cases_add_to_existing_case_modal.tsx @@ -64,6 +64,7 @@ export const useCasesAddToExistingCaseModal = (props: AddToExistingCaseModalProp getAttachments?: ({ theCase }: { theCase?: CaseUI }) => CaseAttachmentsWithoutOwner ) => { const attachments = getAttachments?.({ theCase }) ?? []; + // when the case is undefined in the modal // the user clicked "create new case" if (theCase === undefined) { diff --git a/x-pack/plugins/triggers_actions_ui/public/application/sections/alerts_table/hooks/use_bulk_actions.test.tsx b/x-pack/plugins/triggers_actions_ui/public/application/sections/alerts_table/hooks/use_bulk_actions.test.tsx index 635bc2ea1c77c4..158867ebb5a53c 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/sections/alerts_table/hooks/use_bulk_actions.test.tsx +++ b/x-pack/plugins/triggers_actions_ui/public/application/sections/alerts_table/hooks/use_bulk_actions.test.tsx @@ -43,9 +43,11 @@ describe('bulk action hooks', () => { const refresh = jest.fn(); const clearSelection = jest.fn(); const openNewCase = jest.fn(); + const openExistingCase = jest.fn().mockImplementation(({ getAttachments }) => { getAttachments({ theCase: { id: caseId } }); }); + mockCaseService.helpers.canUseCases = jest.fn().mockReturnValue({ create: true, read: true }); mockCaseService.ui.getCasesContext = jest.fn().mockReturnValue(() => 'Cases context'); @@ -124,6 +126,55 @@ describe('bulk action hooks', () => { expect(openExistingCase).toHaveBeenCalled(); }); + it('should open the flyout from the case modal', async () => { + openExistingCase.mockImplementationOnce(({ getAttachments }) => { + getAttachments({ theCase: undefined }); + }); + + const alerts = [ + { + _id: 'alert0', + _index: 'idx0', + data: [ + { + field: 'kibana.alert.case_ids', + value: [caseId], + }, + ], + ecs: { + _id: 'alert0', + _index: 'idx0', + }, + }, + { + _id: 'alert1', + _index: 'idx1', + data: [ + { + field: 'kibana.alert.case_ids', + value: ['test-case-2'], + }, + ], + ecs: { + _id: 'alert1', + _index: 'idx1', + }, + }, + ]; + + const { result } = renderHook( + () => useBulkAddToCaseActions({ casesConfig, refresh, clearSelection }), + { + wrapper: appMockRender.AppWrapper, + } + ); + + // @ts-expect-error: cases do not need all arguments + result.current[1].onClick(alerts); + + expect(mockCaseService.helpers.groupAlertsByRule).toHaveBeenCalledWith(alerts); + }); + it('should remove alerts that are already attached to the case', async () => { const { result } = renderHook( () => useBulkAddToCaseActions({ casesConfig, refresh, clearSelection }), diff --git a/x-pack/plugins/triggers_actions_ui/public/application/sections/alerts_table/hooks/use_bulk_actions.ts b/x-pack/plugins/triggers_actions_ui/public/application/sections/alerts_table/hooks/use_bulk_actions.ts index fdb2d886e0e071..59a9ad8d1e99fb 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/sections/alerts_table/hooks/use_bulk_actions.ts +++ b/x-pack/plugins/triggers_actions_ui/public/application/sections/alerts_table/hooks/use_bulk_actions.ts @@ -146,6 +146,10 @@ export const useBulkAddToCaseActions = ({ onClick: (alerts?: TimelineItem[]) => { selectCaseModal.open({ getAttachments: ({ theCase }) => { + if (theCase == null) { + return alerts ? casesService?.helpers.groupAlertsByRule(alerts) ?? [] : []; + } + return getCaseAttachments({ alerts, caseId: theCase.id, diff --git a/x-pack/plugins/triggers_actions_ui/public/application/sections/alerts_table/types.ts b/x-pack/plugins/triggers_actions_ui/public/application/sections/alerts_table/types.ts index 50317b045df85a..b480fdc64e6ad8 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/sections/alerts_table/types.ts +++ b/x-pack/plugins/triggers_actions_ui/public/application/sections/alerts_table/types.ts @@ -48,7 +48,7 @@ type UseCasesAddToExistingCaseModal = (props?: Record) => { open: ({ getAttachments, }: { - getAttachments: ({ theCase }: { theCase: { id: string } }) => any[]; + getAttachments: ({ theCase }: { theCase?: { id: string } }) => any[]; }) => void; close: () => void; };