Skip to content

Commit

Permalink
Expanding API response fields
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-buttner committed Jan 11, 2023
1 parent cd86aad commit 2804c40
Show file tree
Hide file tree
Showing 9 changed files with 458 additions and 272 deletions.
6 changes: 5 additions & 1 deletion x-pack/plugins/cases/common/api/connectors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,11 @@ export const CaseConnectorRt = rt.intersection([
]);

export const GetCaseConnectorsResponseRt = rt.array(
rt.intersection([rt.type({ needsToBePushed: rt.boolean }), CaseConnectorRt])
rt.intersection([
rt.type({ needsToBePushed: rt.boolean, hasBeenPushed: rt.boolean }),
rt.partial(rt.type({ latestPushDate: rt.string }).props),
CaseConnectorRt,
])
);

export type CaseUserActionConnector = rt.TypeOf<typeof CaseUserActionConnectorRt>;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions x-pack/plugins/cases/server/client/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type UserActionsSubClientMock = jest.Mocked<UserActionsSubClient>;
const createUserActionsSubClientMock = (): UserActionsSubClientMock => {
return {
getAll: jest.fn(),
getConnectors: jest.fn(),
};
};

Expand Down
24 changes: 15 additions & 9 deletions x-pack/plugins/cases/server/client/user_actions/connectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export const getConnectors = async (
...enrichedConnector.connector,
name: enrichedConnector.name,
needsToBePushed: hasDataToPush(enrichedConnector),
latestPushDate: enrichedConnector.pushInfo?.pushDate.toISOString(),
hasBeenPushed: hasBeenPushed(enrichedConnector),
});
}

Expand Down Expand Up @@ -105,6 +107,11 @@ const checkConnectorsAuthorization = async ({
});
};

interface EnrichedPushInfo {
pushDate: Date;
connectorFieldsUsedInPush: CaseConnector;
}

interface EnrichedConnector {
connector: CaseConnector;
name: string;
Expand Down Expand Up @@ -171,7 +178,7 @@ const getPushInfo = async ({
return new Map();
}

const priorToPushFields = await userActionService.getConnectorFieldsBeforePushes(
const priorToPushFields = await userActionService.getConnectorFieldsUsedInPushes(
caseId,
pushRequest
);
Expand All @@ -184,7 +191,7 @@ const getPushInfo = async ({
if (connectorFields != null) {
enrichedPushInfo.set(request.connectorId, {
pushDate: request.date,
connectorFieldsBeforePush: connectorFields,
connectorFieldsUsedInPush: connectorFields,
});
}
}
Expand All @@ -208,11 +215,6 @@ const isDateValid = (date: Date): boolean => {
return !isNaN(date.getTime());
};

interface EnrichedPushInfo {
pushDate: Date;
connectorFieldsBeforePush: CaseConnector;
}

const getConnectorInfoFromSavedObject = (
savedObject: SavedObject<CaseUserActionResponse> | undefined
): CaseConnector | undefined => {
Expand All @@ -229,10 +231,14 @@ const hasDataToPush = (enrichedConnectorInfo: EnrichedConnector): boolean => {
return (
!isEqual(
enrichedConnectorInfo.connector,
enrichedConnectorInfo.pushInfo?.connectorFieldsBeforePush
enrichedConnectorInfo.pushInfo?.connectorFieldsUsedInPush
) ||
(enrichedConnectorInfo.pushInfo != null &&
enrichedConnectorInfo.latestUserActionDate != null &&
enrichedConnectorInfo.pushInfo.pushDate < enrichedConnectorInfo.latestUserActionDate)
enrichedConnectorInfo.latestUserActionDate > enrichedConnectorInfo.pushInfo.pushDate)
);
};

const hasBeenPushed = (enrichedConnectorInfo: EnrichedConnector): boolean => {
return enrichedConnectorInfo.pushInfo != null;
};
4 changes: 1 addition & 3 deletions x-pack/plugins/cases/server/client/user_actions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,4 @@ export interface UserActionGet {
caseId: string;
}

export interface GetConnectorsRequest {
caseId: string;
}
export type GetConnectorsRequest = UserActionGet;
2 changes: 1 addition & 1 deletion x-pack/plugins/cases/server/services/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ type FakeUserActionService = PublicMethodsOf<CaseUserActionService> & {
export const createUserActionServiceMock = (): CaseUserActionServiceMock => {
const service: FakeUserActionService = {
creator: createUserActionPersisterServiceMock(),
getConnectorFieldsBeforePushes: jest.fn(),
getConnectorFieldsUsedInPushes: jest.fn(),
getMostRecentUserAction: jest.fn(),
getCaseConnectorInformation: jest.fn(),
getAll: jest.fn(),
Expand Down
12 changes: 6 additions & 6 deletions x-pack/plugins/cases/server/services/user_actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class CaseUserActionService {
return this._creator;
}

public async getConnectorFieldsBeforePushes(
public async getConnectorFieldsUsedInPushes(
caseId: string,
pushes: PushInfo[]
): Promise<CaseConnectorFields> {
Expand All @@ -131,11 +131,11 @@ export class CaseUserActionService {
page: 1,
perPage: 1,
sortField: defaultSortField,
aggs: CaseUserActionService.buildConnectorFieldsBeforePushAggs(pushes),
aggs: CaseUserActionService.buildConnectorFieldsUsedInPushAggs(pushes),
filter: connectorsFilter,
});

return this.createCaseConnectorFieldsBeforePushes(response.aggregations);
return this.createCaseConnectorFieldsUsedInPushes(response.aggregations);
} catch (error) {
this.context.log.error(
`Error while retrieving the connector fields before the last push: ${caseId}: ${error}`
Expand All @@ -144,14 +144,14 @@ export class CaseUserActionService {
}
}

private static buildConnectorFieldsBeforePushAggs(
private static buildConnectorFieldsUsedInPushAggs(
pushes: PushInfo[]
): Record<string, estypes.AggregationsAggregationContainer> {
const filters: estypes.AggregationsBuckets<estypes.QueryDslQueryContainer> = {};

/**
* Group the user actions by the unique connector ids and bound the time range
* for that connector's push event
* for that connector's push event. We want to search for the fields before the push timestamp.
*/
for (const push of pushes) {
filters[push.connectorId] = {
Expand Down Expand Up @@ -231,7 +231,7 @@ export class CaseUserActionService {
};
}

private createCaseConnectorFieldsBeforePushes(
private createCaseConnectorFieldsUsedInPushes(
aggsResults?: ConnectorFieldsBeforePushAggsResult
): CaseConnectorFields {
const connectorFields: CaseConnectorFields = new Map();
Expand Down
70 changes: 36 additions & 34 deletions x-pack/test/cases_api_integration/common/lib/connectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,41 +238,43 @@ export const createCaseWithConnector = async ({
});

actionsRemover.add(auth?.space ?? 'default', connector.id, 'action', 'actions');
const configuration = await createConfiguration(
supertest,
{
...getConfigurationRequest({
id: connector.id,
name: connector.name,
type: connector.connector_type_id as ConnectorTypes,
}),
...configureReq,
},
200,
auth ?? undefined
);

const postedCase = await createCase(
supertest,
{
...createCaseReq,
connector: {
id: connector.id,
name: connector.name,
type: connector.connector_type_id,
fields: {
urgency: '2',
impact: '2',
severity: '2',
category: 'software',
subcategory: 'os',
},
} as CaseConnector,
},
200,
auth,
headers
);
const [configuration, postedCase] = await Promise.all([
createConfiguration(
supertest,
{
...getConfigurationRequest({
id: connector.id,
name: connector.name,
type: connector.connector_type_id as ConnectorTypes,
}),
...configureReq,
},
200,
auth ?? undefined
),
createCase(
supertest,
{
...createCaseReq,
connector: {
id: connector.id,
name: connector.name,
type: connector.connector_type_id,
fields: {
urgency: '2',
impact: '2',
severity: '2',
category: 'software',
subcategory: 'os',
},
} as CaseConnector,
},
200,
auth,
headers
),
]);

return { postedCase, connector, configuration };
};
Expand Down
Loading

0 comments on commit 2804c40

Please sign in to comment.