diff --git a/x-pack/plugins/cases/server/client/cases/utils.test.ts b/x-pack/plugins/cases/server/client/cases/utils.test.ts index bfd5d1279420be..d0e0066d482d58 100644 --- a/x-pack/plugins/cases/server/client/cases/utils.test.ts +++ b/x-pack/plugins/cases/server/client/cases/utils.test.ts @@ -523,8 +523,7 @@ describe('utils', () => { }, ], }, - // Remove second push - userActions: userActions.filter((item, index) => index !== 4), + userActions, connector, mappings: [ ...mappings, @@ -551,7 +550,7 @@ describe('utils', () => { ]); }); - it('it removes alerts correctly', async () => { + it('it filters out the alerts from the comments correctly', async () => { const res = await createIncident({ actionsClient: actionsMock, theCase: { @@ -582,6 +581,32 @@ describe('utils', () => { ]); }); + it('does not add the alerts count comment if all alerts have been pushed', async () => { + const res = await createIncident({ + actionsClient: actionsMock, + theCase: { + ...theCase, + comments: [ + { ...commentObj, id: 'comment-user-1', pushed_at: '2019-11-25T21:55:00.177Z' }, + { ...commentGeneratedAlert, pushed_at: '2019-11-25T21:55:00.177Z' }, + ], + }, + userActions, + connector, + mappings, + alerts: [], + casesConnectors, + }); + + expect(res.comments).toEqual([ + { + comment: + 'Wow, good luck catching that bad meanie! (added at 2019-11-25T21:55:00.177Z by elastic)', + commentId: 'comment-user-1', + }, + ]); + }); + it('updates an existing incident', async () => { const existingIncidentData = { priority: null, @@ -644,22 +669,6 @@ describe('utils', () => { }); }); - it('throws error if connector is not supported', async () => { - expect.assertions(2); - createIncident({ - actionsClient: actionsMock, - theCase, - userActions, - connector: { ...connector, actionTypeId: 'not-supported' }, - mappings, - alerts: [], - casesConnectors, - }).catch((e) => { - expect(e).not.toBeNull(); - expect(e).toEqual(new Error('Invalid external service')); - }); - }); - describe('getLatestPushInfo', () => { it('it returns the latest push information correctly', async () => { const res = getLatestPushInfo('456', userActions); diff --git a/x-pack/plugins/cases/server/client/cases/utils.ts b/x-pack/plugins/cases/server/client/cases/utils.ts index f5a10d705e095d..f70df8e39c271f 100644 --- a/x-pack/plugins/cases/server/client/cases/utils.ts +++ b/x-pack/plugins/cases/server/client/cases/utils.ts @@ -81,13 +81,53 @@ const getCommentContent = (comment: CommentResponse): string => { return ''; }; -const countAlerts = (comments: CaseResponse['comments']): number => - comments?.reduce((total, comment) => { - if (comment.type === CommentType.alert || comment.type === CommentType.generatedAlert) { - return total + (Array.isArray(comment.alertId) ? comment.alertId.length : 1); - } - return total; - }, 0) ?? 0; +interface CountAlertsInfo { + totalComments: number; + pushed: number; + totalAlerts: number; +} + +const getAlertsInfo = ( + comments: CaseResponse['comments'] +): { totalAlerts: number; hasUnpushedAlertComments: boolean } => { + const countingInfo = { totalComments: 0, pushed: 0, totalAlerts: 0 }; + + const res = + comments?.reduce(({ totalComments, pushed, totalAlerts }, comment) => { + if (comment.type === CommentType.alert || comment.type === CommentType.generatedAlert) { + return { + totalComments: totalComments + 1, + pushed: comment.pushed_at != null ? pushed + 1 : pushed, + totalAlerts: totalAlerts + (Array.isArray(comment.alertId) ? comment.alertId.length : 1), + }; + } + return { totalComments, pushed, totalAlerts }; + }, countingInfo) ?? countingInfo; + + return { + totalAlerts: res.totalAlerts, + hasUnpushedAlertComments: res.totalComments > res.pushed, + }; +}; + +const addAlertMessage = ( + caseId: string, + caseComments: CaseResponse['comments'], + comments: ExternalServiceComment[] +): ExternalServiceComment[] => { + const { totalAlerts, hasUnpushedAlertComments } = getAlertsInfo(caseComments); + + const newComments = [...comments]; + + if (hasUnpushedAlertComments) { + newComments.push({ + comment: `Elastic Alerts attached to the case: ${totalAlerts}`, + commentId: `${caseId}-total-alerts`, + }); + } + + return newComments; +}; export const createIncident = async ({ actionsClient, @@ -164,8 +204,6 @@ export const createIncident = async ({ comment.type === CommentType.user && commentsIdsToBeUpdated.has(comment.id) ); - const totalAlerts = countAlerts(caseComments); - let comments: ExternalServiceComment[] = []; if (commentsToBeUpdated && Array.isArray(commentsToBeUpdated) && commentsToBeUpdated.length > 0) { @@ -175,12 +213,7 @@ export const createIncident = async ({ } } - if (totalAlerts > 0) { - comments.push({ - comment: `Elastic Alerts attached to the case: ${totalAlerts}`, - commentId: `${theCase.id}-total-alerts`, - }); - } + comments = addAlertMessage(theCase.id, caseComments, comments); return { incident, comments }; };