-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5350d7d
commit 57fb9e5
Showing
8 changed files
with
507 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
240 changes: 240 additions & 0 deletions
240
x-pack/plugins/cases/server/client/user_actions/connectors.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,240 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { isEqual } from 'lodash'; | ||
|
||
import type { SavedObject } from '@kbn/core/server'; | ||
import type { PublicMethodsOf } from '@kbn/utility-types'; | ||
import type { ActionsClient } from '@kbn/actions-plugin/server'; | ||
import type { | ||
CaseUserActionResponse, | ||
GetCaseConnectorsResponse, | ||
CaseConnector, | ||
} from '../../../common/api'; | ||
import { GetCaseConnectorsResponseRt } from '../../../common/api'; | ||
import { isConnectorUserAction, isCreateCaseUserAction } from '../../../common/utils/user_actions'; | ||
import { createCaseError } from '../../common/error'; | ||
import type { CasesClientArgs } from '..'; | ||
import type { Authorization, OwnerEntity } from '../../authorization'; | ||
import { Operations } from '../../authorization'; | ||
import type { GetConnectorsRequest } from './types'; | ||
import type { CaseConnectorActivity, PushInfo } from '../../services/user_actions/types'; | ||
import type { CaseUserActionService } from '../../services'; | ||
|
||
export const getConnectors = async ( | ||
{ caseId }: GetConnectorsRequest, | ||
clientArgs: CasesClientArgs | ||
): Promise<GetCaseConnectorsResponse> => { | ||
const { | ||
services: { userActionService }, | ||
logger, | ||
authorization, | ||
actionsClient, | ||
} = clientArgs; | ||
|
||
try { | ||
const [connectors, latestUserAction] = await Promise.all([ | ||
userActionService.getCaseConnectorInformation(caseId), | ||
userActionService.getMostRecentUserAction(caseId), | ||
]); | ||
|
||
await checkConnectorsAuthorization({ authorization, connectors, latestUserAction }); | ||
|
||
const enrichedConnectors = await enrichConnectors({ | ||
caseId, | ||
actionsClient, | ||
connectors, | ||
latestUserAction, | ||
userActionService, | ||
}); | ||
|
||
const results: GetCaseConnectorsResponse = []; | ||
|
||
for (const enrichedConnector of enrichedConnectors) { | ||
results.push({ | ||
...enrichedConnector.connector, | ||
name: enrichedConnector.name, | ||
needsToBePushed: hasDataToPush(enrichedConnector), | ||
}); | ||
} | ||
|
||
console.log('connectors response', JSON.stringify(results, null, 2)); | ||
|
||
return GetCaseConnectorsResponseRt.encode(results); | ||
} catch (error) { | ||
throw createCaseError({ | ||
message: `Failed to retrieve the case connectors case id: ${caseId}: ${error}`, | ||
error, | ||
logger, | ||
}); | ||
} | ||
}; | ||
|
||
const checkConnectorsAuthorization = async ({ | ||
connectors, | ||
latestUserAction, | ||
authorization, | ||
}: { | ||
connectors: CaseConnectorActivity[]; | ||
latestUserAction?: SavedObject<CaseUserActionResponse>; | ||
authorization: PublicMethodsOf<Authorization>; | ||
}) => { | ||
const entities: OwnerEntity[] = latestUserAction | ||
? [{ owner: latestUserAction.attributes.owner, id: latestUserAction.id }] | ||
: []; | ||
|
||
for (const connector of connectors) { | ||
entities.push({ | ||
owner: connector.fields.attributes.owner, | ||
id: connector.connectorId, | ||
}); | ||
|
||
if (connector.push) { | ||
entities.push({ | ||
owner: connector.push.attributes.owner, | ||
id: connector.connectorId, | ||
}); | ||
} | ||
} | ||
|
||
await authorization.ensureAuthorized({ | ||
entities, | ||
operation: Operations.getUserActions, | ||
}); | ||
}; | ||
|
||
interface EnrichedConnector { | ||
connector: CaseConnector; | ||
name: string; | ||
pushInfo?: EnrichedPushInfo; | ||
latestUserActionDate?: Date; | ||
} | ||
|
||
const enrichConnectors = async ({ | ||
caseId, | ||
connectors, | ||
latestUserAction, | ||
actionsClient, | ||
userActionService, | ||
}: { | ||
caseId: string; | ||
connectors: CaseConnectorActivity[]; | ||
latestUserAction?: SavedObject<CaseUserActionResponse>; | ||
actionsClient: PublicMethodsOf<ActionsClient>; | ||
userActionService: CaseUserActionService; | ||
}): Promise<EnrichedConnector[]> => { | ||
const pushInfo = await getPushInfo({ caseId, activity: connectors, userActionService }); | ||
|
||
const enrichedConnectors: EnrichedConnector[] = []; | ||
|
||
for (const aggregationConnector of connectors) { | ||
const connectorDetails = await actionsClient.get({ id: aggregationConnector.connectorId }); | ||
const connector = getConnectorInfoFromSavedObject(aggregationConnector.fields); | ||
|
||
const latestUserActionCreatedAt = getDate(latestUserAction?.attributes.created_at); | ||
|
||
if (connector != null) { | ||
enrichedConnectors.push({ | ||
connector, | ||
name: connectorDetails.name, | ||
pushInfo: pushInfo.get(aggregationConnector.connectorId), | ||
latestUserActionDate: latestUserActionCreatedAt, | ||
}); | ||
} | ||
} | ||
|
||
return enrichedConnectors; | ||
}; | ||
|
||
const getPushInfo = async ({ | ||
caseId, | ||
activity, | ||
userActionService, | ||
}: { | ||
caseId: string; | ||
activity: CaseConnectorActivity[]; | ||
userActionService: CaseUserActionService; | ||
}): Promise<Map<string, EnrichedPushInfo>> => { | ||
const pushRequest: PushInfo[] = []; | ||
|
||
for (const connectorInfo of activity) { | ||
const pushCreatedAt = getDate(connectorInfo.push?.attributes.created_at); | ||
|
||
if (connectorInfo.push != null && pushCreatedAt != null) { | ||
pushRequest.push({ connectorId: connectorInfo.connectorId, date: pushCreatedAt }); | ||
} | ||
} | ||
|
||
if (pushRequest.length <= 0) { | ||
return new Map(); | ||
} | ||
|
||
const priorToPushFields = await userActionService.getConnectorFieldsBeforePushes( | ||
caseId, | ||
pushRequest | ||
); | ||
|
||
const enrichedPushInfo = new Map<string, EnrichedPushInfo>(); | ||
for (const request of pushRequest) { | ||
const connectorFieldsSO = priorToPushFields.get(request.connectorId); | ||
const connectorFields = getConnectorInfoFromSavedObject(connectorFieldsSO); | ||
|
||
if (connectorFields != null) { | ||
enrichedPushInfo.set(request.connectorId, { | ||
pushDate: request.date, | ||
connectorFieldsBeforePush: connectorFields, | ||
}); | ||
} | ||
} | ||
|
||
return enrichedPushInfo; | ||
}; | ||
|
||
const getDate = (timestamp: string | undefined): Date | undefined => { | ||
if (timestamp == null) { | ||
return; | ||
} | ||
|
||
const date = new Date(timestamp); | ||
|
||
if (isDateValid(date)) { | ||
return date; | ||
} | ||
}; | ||
|
||
const isDateValid = (date: Date): boolean => { | ||
return !isNaN(date.getTime()); | ||
}; | ||
|
||
interface EnrichedPushInfo { | ||
pushDate: Date; | ||
connectorFieldsBeforePush: CaseConnector; | ||
} | ||
|
||
const getConnectorInfoFromSavedObject = ( | ||
savedObject: SavedObject<CaseUserActionResponse> | undefined | ||
): CaseConnector | undefined => { | ||
if ( | ||
savedObject != null && | ||
(isConnectorUserAction(savedObject.attributes) || | ||
isCreateCaseUserAction(savedObject.attributes)) | ||
) { | ||
return savedObject.attributes.payload.connector; | ||
} | ||
}; | ||
|
||
const hasDataToPush = (enrichedConnectorInfo: EnrichedConnector): boolean => { | ||
return ( | ||
!isEqual( | ||
enrichedConnectorInfo.connector, | ||
enrichedConnectorInfo.pushInfo?.connectorFieldsBeforePush | ||
) || | ||
(enrichedConnectorInfo.pushInfo != null && | ||
enrichedConnectorInfo.latestUserActionDate != null && | ||
enrichedConnectorInfo.pushInfo.pushDate < enrichedConnectorInfo.latestUserActionDate) | ||
); | ||
}; |
Oops, something went wrong.