diff --git a/x-pack/plugins/cases/server/client/alerts/types.ts b/x-pack/plugins/cases/server/client/alerts/types.ts index d5e6c2e8252e9..ed2b6626427a9 100644 --- a/x-pack/plugins/cases/server/client/alerts/types.ts +++ b/x-pack/plugins/cases/server/client/alerts/types.ts @@ -39,6 +39,6 @@ export interface AlertGet { } export interface UpdateAlertCasesRequest { - alerts: Array<{ id: string; index: string }>; + alerts: AlertInfo[]; caseIds: string[]; } diff --git a/x-pack/plugins/cases/server/common/models/case_with_comments.ts b/x-pack/plugins/cases/server/common/models/case_with_comments.ts index 3a21fd3141ac6..cba3d461af45d 100644 --- a/x-pack/plugins/cases/server/common/models/case_with_comments.ts +++ b/x-pack/plugins/cases/server/common/models/case_with_comments.ts @@ -35,13 +35,12 @@ import { import type { CasesClientArgs } from '../../client'; import type { RefreshSetting } from '../../services/types'; import { createCaseError } from '../error'; -import type { CaseSavedObject } from '../types'; +import type { AlertInfo, CaseSavedObject } from '../types'; import { countAlertsForID, flattenCommentSavedObjects, transformNewComment, getOrUpdateLensReferences, - createAlertUpdateStatusRequest, isCommentRequestTypeAlert, getAlertInfoFromComments, } from '../utils'; @@ -312,31 +311,28 @@ export class CaseCommentModel { (attachment): attachment is CommentRequestAlertType => attachment.type === CommentType.alert ); - if (this.caseInfo.attributes.settings.syncAlerts) { - await this.updateAlertsStatus(alertAttachments); - } + const alerts = getAlertInfoFromComments(alertAttachments); + + if (alerts.length > 0) { + await this.params.services.alertsService.ensureAlertsAuthorized({ alerts }); + await this.updateAlertsSchemaWithCaseInfo(alerts); - if (alertAttachments.length > 0) { - await this.updateAlertsSchemaWithCaseInfo(alertAttachments); + if (this.caseInfo.attributes.settings.syncAlerts) { + await this.updateAlertsStatus(alerts); + } } } - private async updateAlertsStatus(alerts: CommentRequest[]) { - const alertsToUpdate = alerts - .map((alert) => - createAlertUpdateStatusRequest({ - comment: alert, - status: this.caseInfo.attributes.status, - }) - ) - .flat(); + private async updateAlertsStatus(alerts: AlertInfo[]) { + const alertsToUpdate = alerts.map((alert) => ({ + ...alert, + status: this.caseInfo.attributes.status, + })); await this.params.services.alertsService.updateAlertsStatus(alertsToUpdate); } - private async updateAlertsSchemaWithCaseInfo(alertAttachments: CommentRequestAlertType[]) { - const alerts = getAlertInfoFromComments(alertAttachments); - + private async updateAlertsSchemaWithCaseInfo(alerts: AlertInfo[]) { await this.params.services.alertsService.bulkUpdateCases({ alerts, caseIds: [this.caseInfo.id], diff --git a/x-pack/plugins/cases/server/services/alerts/index.ts b/x-pack/plugins/cases/server/services/alerts/index.ts index ad089857e2846..c79587aa57fb1 100644 --- a/x-pack/plugins/cases/server/services/alerts/index.ts +++ b/x-pack/plugins/cases/server/services/alerts/index.ts @@ -182,6 +182,10 @@ export class AlertService { ); } + private getNonEmptyAlerts(alerts: AlertInfo[]): AlertInfo[] { + return alerts.filter((alert) => !AlertService.isEmptyAlert(alert)); + } + public async getAlerts(alertsInfo: AlertInfo[]): Promise | undefined> { try { const docs = alertsInfo @@ -207,7 +211,7 @@ export class AlertService { public async bulkUpdateCases({ alerts, caseIds }: UpdateAlertCasesRequest): Promise { try { - const nonEmptyAlerts = alerts.filter((alert) => !AlertService.isEmptyAlert(alert)); + const nonEmptyAlerts = this.getNonEmptyAlerts(alerts); if (nonEmptyAlerts.length <= 0) { return; @@ -225,6 +229,26 @@ export class AlertService { }); } } + + public async ensureAlertsAuthorized({ alerts }: { alerts: AlertInfo[] }): Promise { + try { + const nonEmptyAlerts = this.getNonEmptyAlerts(alerts); + + if (nonEmptyAlerts.length <= 0) { + return; + } + + await this.alertsClient.ensureAllAlertsAuthorizedRead({ + alerts: nonEmptyAlerts, + }); + } catch (error) { + throw createCaseError({ + message: `Failed to authorize alerts: ${error}`, + error, + logger: this.logger, + }); + } + } } interface TranslatedUpdateAlertRequest {