Skip to content

Commit

Permalink
Normalize response
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Jun 16, 2021
1 parent d57be95 commit 17c04df
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
5 changes: 3 additions & 2 deletions x-pack/plugins/cases/common/api/cases/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
import * as rt from 'io-ts';

const AlertRt = rt.type({
alertId: rt.array(rt.string),
index: rt.array(rt.string),
id: rt.string,
index: rt.string,
attached_at: rt.string,
});

export const AlertResponseRt = rt.array(AlertRt);
Expand Down
30 changes: 21 additions & 9 deletions x-pack/plugins/cases/server/client/cases/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,34 @@ import {
CasesByAlertIDRequest,
CasesByAlertIDRequestRt,
AlertResponse,
AttributesTypeAlerts,
} from '../../../common/api';
import { countAlertsForID, flattenCaseSavedObject } from '../../common';
import { countAlertsForID, flattenCaseSavedObject, getIDsAndIndicesAsArrays } from '../../common';
import { createCaseError } from '../../common/error';
import { ENABLE_CASE_CONNECTOR } from '../../../common/constants';
import { CasesClient, CasesClientArgs } from '..';
import { Operations } from '../../authorization';
import { combineAuthorizedAndOwnerFilter } from '../utils';
import { CasesService } from '../../services';

const normalizeAlertResponse = (alerts: Array<SavedObject<AttributesTypeAlerts>>): AlertResponse =>
alerts.reduce((acc: AlertResponse, alert) => {
const { ids, indices } = getIDsAndIndicesAsArrays(alert.attributes);

if (ids.length !== indices.length) {
return acc;
}

return [
...acc,
...ids.map((id, index) => ({
id,
index: indices[index],
attached_at: alert.attributes.created_at,
})),
];
}, []);

export interface GetAllAlertsAttachToCase {
caseId: string;
}
Expand Down Expand Up @@ -73,14 +92,7 @@ export const getAllAlertsAttachToCase = async (
}))
);

return alerts.map((alert) => ({
alertId: Array.isArray(alert.attributes.alertId)
? alert.attributes.alertId
: [alert.attributes.alertId],
index: Array.isArray(alert.attributes.index)
? alert.attributes.index
: [alert.attributes.index],
}));
return normalizeAlertResponse(alerts);
};

/**
Expand Down
9 changes: 2 additions & 7 deletions x-pack/plugins/cases/server/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,17 +271,12 @@ const getAndValidateAlertInfoFromComment = (comment: CommentRequest): AlertInfo[
/**
* Builds an AlertInfo object accumulating the alert IDs and indices for the passed in alerts.
*/
export const getAlertInfoFromComments = (comments: CommentRequest[] | undefined): AlertInfo[] => {
if (comments === undefined) {
return [];
}

return comments.reduce((acc: AlertInfo[], comment) => {
export const getAlertInfoFromComments = (comments: CommentRequest[] = []): AlertInfo[] =>
comments.reduce((acc: AlertInfo[], comment) => {
const alertInfo = getAndValidateAlertInfoFromComment(comment);
acc.push(...alertInfo);
return acc;
}, []);
};

type NewCommentArgs = CommentRequest & {
associationType: AssociationType;
Expand Down

0 comments on commit 17c04df

Please sign in to comment.