@@ -10,11 +10,9 @@ import { UpdateDocumentByQueryResponse } from 'elasticsearch';
1010import { HttpStart } from '../../../../../../../src/core/public' ;
1111
1212import {
13- addExceptionListItem ,
14- updateExceptionListItem ,
1513 ExceptionListItemSchema ,
1614 CreateExceptionListItemSchema ,
17- UpdateExceptionListItemSchema ,
15+ useApi ,
1816} from '../../../lists_plugin_deps' ;
1917import { updateAlertStatus } from '../../../detections/containers/detection_engine/alerts/api' ;
2018import { getUpdateAlertsQuery } from '../../../detections/components/alerts_table/actions' ;
@@ -25,6 +23,7 @@ import {
2523import { getQueryFilter } from '../../../../common/detection_engine/get_query_filter' ;
2624import { Index } from '../../../../common/detection_engine/schemas/common/schemas' ;
2725import { formatExceptionItemForUpdate , prepareExceptionItemsForBulkClose } from './helpers' ;
26+ import { useKibana } from '../../lib/kibana' ;
2827
2928/**
3029 * Adds exception items to the list. Also optionally closes alerts.
@@ -66,11 +65,13 @@ export const useAddOrUpdateException = ({
6665 onError,
6766 onSuccess,
6867} : UseAddOrUpdateExceptionProps ) : ReturnUseAddOrUpdateException => {
68+ const { services } = useKibana ( ) ;
6969 const [ isLoading , setIsLoading ] = useState ( false ) ;
7070 const addOrUpdateExceptionRef = useRef < AddOrUpdateExceptionItemsFunc | null > ( null ) ;
71+ const { addExceptionListItem, updateExceptionListItem } = useApi ( services . http ) ;
7172 const addOrUpdateException = useCallback < AddOrUpdateExceptionItemsFunc > (
7273 async ( ruleId , exceptionItemsToAddOrUpdate , alertIdToClose , bulkCloseIndex ) => {
73- if ( addOrUpdateExceptionRef . current !== null ) {
74+ if ( addOrUpdateExceptionRef . current != null ) {
7475 addOrUpdateExceptionRef . current (
7576 ruleId ,
7677 exceptionItemsToAddOrUpdate ,
@@ -86,49 +87,33 @@ export const useAddOrUpdateException = ({
8687 let isSubscribed = true ;
8788 const abortCtrl = new AbortController ( ) ;
8889
89- const addOrUpdateItems = async (
90- exceptionItemsToAddOrUpdate : Array < ExceptionListItemSchema | CreateExceptionListItemSchema >
91- ) : Promise < void > => {
92- const toAdd : CreateExceptionListItemSchema [ ] = [ ] ;
93- const toUpdate : UpdateExceptionListItemSchema [ ] = [ ] ;
94- exceptionItemsToAddOrUpdate . forEach (
95- ( item : ExceptionListItemSchema | CreateExceptionListItemSchema ) => {
96- if ( 'id' in item && item . id !== undefined ) {
97- toUpdate . push ( formatExceptionItemForUpdate ( item ) ) ;
98- } else {
99- toAdd . push ( item ) ;
100- }
101- }
102- ) ;
103-
104- const promises : Array < Promise < ExceptionListItemSchema > > = [ ] ;
105- toAdd . forEach ( ( item : CreateExceptionListItemSchema ) => {
106- promises . push (
107- addExceptionListItem ( {
108- http,
109- listItem : item ,
110- signal : abortCtrl . signal ,
111- } )
112- ) ;
113- } ) ;
114- toUpdate . forEach ( ( item : UpdateExceptionListItemSchema ) => {
115- promises . push (
116- updateExceptionListItem ( {
117- http,
118- listItem : item ,
119- signal : abortCtrl . signal ,
120- } )
121- ) ;
122- } ) ;
123- await Promise . all ( promises ) ;
124- } ;
125-
126- const addOrUpdateExceptionItems : AddOrUpdateExceptionItemsFunc = async (
90+ const onUpdateExceptionItemsAndAlertStatus : AddOrUpdateExceptionItemsFunc = async (
12791 ruleId ,
12892 exceptionItemsToAddOrUpdate ,
12993 alertIdToClose ,
13094 bulkCloseIndex
13195 ) => {
96+ const addOrUpdateItems = async (
97+ exceptionListItems : Array < ExceptionListItemSchema | CreateExceptionListItemSchema >
98+ ) : Promise < void > => {
99+ await Promise . all (
100+ exceptionListItems . map (
101+ ( item : ExceptionListItemSchema | CreateExceptionListItemSchema ) => {
102+ if ( 'id' in item && item . id != null ) {
103+ const formattedExceptionItem = formatExceptionItemForUpdate ( item ) ;
104+ return updateExceptionListItem ( {
105+ listItem : formattedExceptionItem ,
106+ } ) ;
107+ } else {
108+ return addExceptionListItem ( {
109+ listItem : item ,
110+ } ) ;
111+ }
112+ }
113+ )
114+ ) ;
115+ } ;
116+
132117 try {
133118 setIsLoading ( true ) ;
134119 let alertIdResponse : UpdateDocumentByQueryResponse | undefined ;
@@ -170,7 +155,6 @@ export const useAddOrUpdateException = ({
170155 const updated = ( alertIdResponse ?. updated ?? 0 ) + ( bulkResponse ?. updated ?? 0 ) ;
171156 const conflicts =
172157 alertIdResponse ?. version_conflicts ?? 0 + ( bulkResponse ?. version_conflicts ?? 0 ) ;
173-
174158 if ( isSubscribed ) {
175159 setIsLoading ( false ) ;
176160 onSuccess ( updated , conflicts ) ;
@@ -187,12 +171,12 @@ export const useAddOrUpdateException = ({
187171 }
188172 } ;
189173
190- addOrUpdateExceptionRef . current = addOrUpdateExceptionItems ;
174+ addOrUpdateExceptionRef . current = onUpdateExceptionItemsAndAlertStatus ;
191175 return ( ) : void => {
192176 isSubscribed = false ;
193177 abortCtrl . abort ( ) ;
194178 } ;
195- } , [ http , onSuccess , onError ] ) ;
179+ } , [ http , onSuccess , onError , updateExceptionListItem , addExceptionListItem ] ) ;
196180
197181 return [ { isLoading } , addOrUpdateException ] ;
198182} ;
0 commit comments