Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Apr 18, 2023
1 parent 935ca81 commit 2364a3d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -915,10 +915,13 @@ export class AlertsClient {
const SCRIPT_PARAMS_ID = 'caseIds';

const painlessScript = `if (ctx._source['${ALERT_CASE_IDS}'] != null && ctx._source['${ALERT_CASE_IDS}'].length > 0 && params['${SCRIPT_PARAMS_ID}'] != null && params['${SCRIPT_PARAMS_ID}'].length > 0) {
for (int i=0; i < params['${SCRIPT_PARAMS_ID}'].length; i++) {
if (ctx._source['${ALERT_CASE_IDS}'].contains(params['${SCRIPT_PARAMS_ID}'][i])) {
int index = ctx._source['${ALERT_CASE_IDS}'].indexOf(params['${SCRIPT_PARAMS_ID}'][i]);
ctx._source['${ALERT_CASE_IDS}'].remove(index);
List storedCaseIds = ctx._source['${ALERT_CASE_IDS}'];
List caseIdsToRemove = params['${SCRIPT_PARAMS_ID}'];
for (int i=0; i < caseIdsToRemove.length; i++) {
if (storedCaseIds.contains(caseIdsToRemove[i])) {
int index = storedCaseIds.indexOf(caseIdsToRemove[i]);
storedCaseIds.remove(index);
}
}
}`;
Expand Down
40 changes: 26 additions & 14 deletions x-pack/test/cases_api_integration/common/lib/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,11 @@ export const createCaseAttachAlertAndDeleteAlert = async ({
});

const alertAfterDeletion = await getAlerts(alerts);

const caseIdsWithoutRemovedCase =
expectedHttpCode === 204
? updatedCases
.filter((theCase) => theCase.id !== caseToDelete.id)
.map((theCase) => theCase.id)
: updatedCases.map((theCase) => theCase.id);
const caseIdsWithoutRemovedCase = getCaseIdsWithoutRemovedCases({
expectedHttpCode,
updatedCases,
caseIdsToDelete: [caseToDelete.id],
});

for (const alert of alertAfterDeletion) {
expect(alert[ALERT_CASE_IDS]).eql(caseIdsWithoutRemovedCase);
Expand Down Expand Up @@ -174,13 +172,11 @@ export const createCaseAttachAlertAndDeleteCase = async ({
});

const alertAfterDeletion = await getAlerts(alerts);

const caseIdsWithoutRemovedCase =
expectedHttpCode === 204
? updatedCases
.filter((theCase) => !caseIdsToDelete.some((id) => theCase.id === id))
.map((theCase) => theCase.id)
: updatedCases.map((theCase) => theCase.id);
const caseIdsWithoutRemovedCase = getCaseIdsWithoutRemovedCases({
expectedHttpCode,
updatedCases,
caseIdsToDelete,
});

for (const alert of alertAfterDeletion) {
expect(alert[ALERT_CASE_IDS]).eql(caseIdsWithoutRemovedCase);
Expand Down Expand Up @@ -247,3 +243,19 @@ export const createCaseAndAttachAlert = async ({

return updatedCases;
};

export const getCaseIdsWithoutRemovedCases = ({
updatedCases,
caseIdsToDelete,
expectedHttpCode,
}: {
expectedHttpCode: number;
updatedCases: Array<{ id: string }>;
caseIdsToDelete: string[];
}) => {
return expectedHttpCode === 204
? updatedCases
.filter((theCase) => !caseIdsToDelete.some((id) => theCase.id === id))
.map((theCase) => theCase.id)
: updatedCases.map((theCase) => theCase.id);
};

0 comments on commit 2364a3d

Please sign in to comment.