From f0b75b3108f78948a2dd5fc30662e440884d8529 Mon Sep 17 00:00:00 2001 From: Jonathan Buttner <56361221+jonathan-buttner@users.noreply.github.com> Date: Tue, 2 May 2023 16:02:36 -0400 Subject: [PATCH] [Cases] Renaming user action type (#156113) This PR creates a proper `UserAction` type and various attributes types to be used in the service layer of the backend. There shouldn't be any functionality changes here just type name changes. --- .../api/cases/user_actions/operations/find.ts | 4 +-- .../common/api/cases/user_actions/response.ts | 34 +++++++++--------- x-pack/plugins/cases/common/ui/types.ts | 6 ++-- x-pack/plugins/cases/public/api/utils.ts | 4 +-- .../public/components/user_actions/common.tsx | 8 +++-- .../public/components/user_actions/types.ts | 4 +-- .../use_user_actions_last_page.tsx | 4 +-- .../use_user_actions_pagination.tsx | 6 ++-- .../user_actions/user_actions_list.tsx | 4 +-- x-pack/plugins/cases/public/containers/api.ts | 4 +-- .../plugins/cases/public/containers/mock.ts | 35 ++++++++----------- .../plugins/cases/public/containers/utils.ts | 11 +++--- .../server/client/metrics/lifespan.test.ts | 8 ++--- .../cases/server/client/metrics/lifespan.ts | 6 ++-- .../client/metrics/test_utils/lifespan.ts | 4 +-- .../cases/server/client/typedoc_interfaces.ts | 4 +-- .../server/client/user_actions/connectors.ts | 12 +++---- .../cases/server/client/user_actions/utils.ts | 8 ++--- .../cases/server/common/types/user_actions.ts | 5 +++ .../services/user_actions/audit_logger.ts | 2 +- .../user_actions/builders/assignees.ts | 4 +-- .../builders/audit_logger_utils.ts | 2 +- .../services/user_actions/builders/tags.ts | 4 +-- .../server/services/user_actions/index.ts | 20 +++++------ .../user_actions/operations/create.ts | 4 +-- .../services/user_actions/operations/find.ts | 19 +++++----- .../services/user_actions/test_utils.ts | 4 +-- .../server/services/user_actions/transform.ts | 20 +++++------ .../server/services/user_actions/types.ts | 32 +++++++++++------ 29 files changed, 143 insertions(+), 139 deletions(-) diff --git a/x-pack/plugins/cases/common/api/cases/user_actions/operations/find.ts b/x-pack/plugins/cases/common/api/cases/user_actions/operations/find.ts index dc4524e2bf3e9..ce107c73010c1 100644 --- a/x-pack/plugins/cases/common/api/cases/user_actions/operations/find.ts +++ b/x-pack/plugins/cases/common/api/cases/user_actions/operations/find.ts @@ -6,7 +6,7 @@ */ import * as rt from 'io-ts'; -import { CaseUserActionsResponseRt } from '../response'; +import { UserActionsRt } from '../response'; import { ActionTypes } from '../common'; import { NumberFromString } from '../../../saved_object'; @@ -36,7 +36,7 @@ export const UserActionFindRequestRt = rt.partial({ export type UserActionFindRequest = rt.TypeOf; export const UserActionFindResponseRt = rt.type({ - userActions: CaseUserActionsResponseRt, + userActions: UserActionsRt, page: rt.number, perPage: rt.number, total: rt.number, diff --git a/x-pack/plugins/cases/common/api/cases/user_actions/response.ts b/x-pack/plugins/cases/common/api/cases/user_actions/response.ts index 8f0ef5517db4f..e4d1e681b1572 100644 --- a/x-pack/plugins/cases/common/api/cases/user_actions/response.ts +++ b/x-pack/plugins/cases/common/api/cases/user_actions/response.ts @@ -39,7 +39,7 @@ const CommonUserActionsRt = rt.union([ AssigneesUserActionRt, ]); -export const UserActionsRt = rt.union([ +const UserActionPayloadRt = rt.union([ CommonUserActionsRt, CreateCaseUserActionRt, ConnectorUserActionRt, @@ -55,7 +55,7 @@ const UserActionsWithoutConnectorIdRt = rt.union([ DeleteCaseUserActionRt, ]); -const CaseUserActionBasicRt = rt.intersection([UserActionsRt, UserActionCommonAttributesRt]); +const CaseUserActionBasicRt = rt.intersection([UserActionPayloadRt, UserActionCommonAttributesRt]); const CaseUserActionBasicWithoutConnectorIdRt = rt.intersection([ UserActionsWithoutConnectorIdRt, UserActionCommonAttributesRt, @@ -69,38 +69,38 @@ const CaseUserActionDeprecatedResponseRt = rt.intersection([ /** * This includes the comment_id but not the action_id or case_id */ -const CaseUserActionInjectedAttributesRt = rt.intersection([ - CaseUserActionBasicRt, - CaseUserActionInjectedIdsRt, -]); +const UserActionAttributes = rt.intersection([CaseUserActionBasicRt, CaseUserActionInjectedIdsRt]); -const CaseUserActionResponseRt = rt.intersection([ - CaseUserActionInjectedAttributesRt, +const UserActionRt = rt.intersection([ + UserActionAttributes, rt.type({ id: rt.string, version: rt.string, }), ]); -const CaseUserActionAttributesRt = CaseUserActionBasicRt; -export const CaseUserActionsResponseRt = rt.array(CaseUserActionResponseRt); +export const UserActionsRt = rt.array(UserActionRt); export const CaseUserActionsDeprecatedResponseRt = rt.array(CaseUserActionDeprecatedResponseRt); export const CaseUserActionStatsResponseRt = CaseUserActionStatsRt; -export type CaseUserActionAttributes = rt.TypeOf; export type CaseUserActionAttributesWithoutConnectorId = rt.TypeOf< typeof CaseUserActionBasicWithoutConnectorIdRt >; export type CaseUserActionStatsResponse = rt.TypeOf; -export type CaseUserActionsResponse = rt.TypeOf; -export type CaseUserActionResponse = rt.TypeOf; +export type UserActions = rt.TypeOf; +export type UserAction = rt.TypeOf; export type CaseUserActionsDeprecatedResponse = rt.TypeOf< typeof CaseUserActionsDeprecatedResponseRt >; export type CaseUserActionDeprecatedResponse = rt.TypeOf; -export type CaseUserActionInjectedAttributes = rt.TypeOf; +export type UserActionAttributes = rt.TypeOf; -export type UserAction = rt.TypeOf; +/** + * This defines the high level category for the user action. Whether the user add, removed, updated something + */ +export type ActionCategory = rt.TypeOf; +/** + * This defines the type of the user action, meaning what individual action was taken, for example changing the status, + * adding an assignee etc. + */ export type UserActionTypes = ActionTypeValues; - -export type CaseUserAction = rt.TypeOf; diff --git a/x-pack/plugins/cases/common/ui/types.ts b/x-pack/plugins/cases/common/ui/types.ts index c513f5a8e2337..c8a1e2172651f 100644 --- a/x-pack/plugins/cases/common/ui/types.ts +++ b/x-pack/plugins/cases/common/ui/types.ts @@ -17,7 +17,7 @@ import type { CaseStatuses, User, ActionConnector, - CaseUserActionResponse, + UserAction, SingleCaseMetricsResponse, CommentResponse, Case as CaseSnakeCase, @@ -87,9 +87,9 @@ export type Comment = SnakeToCamelCase; export type AlertComment = SnakeToCamelCase; export type ExternalReferenceComment = SnakeToCamelCase; export type PersistableComment = SnakeToCamelCase; -export type CaseUserActions = SnakeToCamelCase; +export type UserActionUI = SnakeToCamelCase; export type FindCaseUserActions = Omit, 'userActions'> & { - userActions: CaseUserActions[]; + userActions: UserActionUI[]; }; export type CaseUserActionsStats = SnakeToCamelCase; export type CaseUI = Omit, 'comments'> & { comments: Comment[] }; diff --git a/x-pack/plugins/cases/public/api/utils.ts b/x-pack/plugins/cases/public/api/utils.ts index d463fb4304249..4f2dde86dfec1 100644 --- a/x-pack/plugins/cases/public/api/utils.ts +++ b/x-pack/plugins/cases/public/api/utils.ts @@ -14,7 +14,7 @@ import { import type { CasesFindResponse, Case, - CaseUserActionsResponse, + UserActions, CommentRequest, CommentResponse, CaseResolveResponse, @@ -81,7 +81,7 @@ export const convertAttachmentToCamelCase = (attachment: CommentRequest): Commen return convertToCamelCase(attachment); }; -export const convertUserActionsToCamelCase = (userActions: CaseUserActionsResponse) => { +export const convertUserActionsToCamelCase = (userActions: UserActions) => { return userActions.map((userAction) => { if (isCommentUserAction(userAction)) { const userActionWithoutPayload = omit(userAction, 'payload.comment'); diff --git a/x-pack/plugins/cases/public/components/user_actions/common.tsx b/x-pack/plugins/cases/public/components/user_actions/common.tsx index 4d04f118bebe4..800831b4cc9b2 100644 --- a/x-pack/plugins/cases/public/components/user_actions/common.tsx +++ b/x-pack/plugins/cases/public/components/user_actions/common.tsx @@ -9,7 +9,7 @@ import React from 'react'; import type { EuiCommentProps } from '@elastic/eui'; import { EuiFlexGroup, EuiFlexItem } from '@elastic/eui'; -import type { ConnectorUserAction, UserAction } from '../../../common/api'; +import type { ConnectorUserAction, ActionCategory } from '../../../common/api'; import { Actions } from '../../../common/api'; import { UserActionTimestamp } from './timestamp'; import type { UserActionBuilder, UserActionBuilderArgs, UserActionResponse } from './types'; @@ -22,8 +22,10 @@ interface Props { handleOutlineComment: (id: string) => void; } -const showMoveToReference = (action: UserAction, commentId: string | null): commentId is string => - action === Actions.update && commentId != null; +const showMoveToReference = ( + action: ActionCategory, + commentId: string | null +): commentId is string => action === Actions.update && commentId != null; const CommentListActions: React.FC = React.memo(({ userAction, handleOutlineComment }) => ( diff --git a/x-pack/plugins/cases/public/components/user_actions/types.ts b/x-pack/plugins/cases/public/components/user_actions/types.ts index f8ff85e05bf18..4e2660734052f 100644 --- a/x-pack/plugins/cases/public/components/user_actions/types.ts +++ b/x-pack/plugins/cases/public/components/user_actions/types.ts @@ -12,7 +12,7 @@ import type { ActionTypes, UserActionWithResponse } from '../../../common/api'; import type { CaseUI, CaseConnectors, - CaseUserActions, + UserActionUI, Comment, UseFetchAlertData, CaseUserActionsStats, @@ -54,7 +54,7 @@ export interface UserActionBuilderArgs { externalReferenceAttachmentTypeRegistry: ExternalReferenceAttachmentTypeRegistry; persistableStateAttachmentTypeRegistry: PersistableStateAttachmentTypeRegistry; caseConnectors: CaseConnectors; - userAction: CaseUserActions; + userAction: UserActionUI; comments: Comment[]; index: number; commentRefs: React.MutableRefObject< diff --git a/x-pack/plugins/cases/public/components/user_actions/use_user_actions_last_page.tsx b/x-pack/plugins/cases/public/components/user_actions/use_user_actions_last_page.tsx index 800f1f94c4832..32fc45aa433ab 100644 --- a/x-pack/plugins/cases/public/components/user_actions/use_user_actions_last_page.tsx +++ b/x-pack/plugins/cases/public/components/user_actions/use_user_actions_last_page.tsx @@ -7,7 +7,7 @@ import { useMemo } from 'react'; -import type { CaseUserActions } from '../../containers/types'; +import type { UserActionUI } from '../../containers/types'; import { useFindCaseUserActions } from '../../containers/use_find_case_user_actions'; import type { UserActivityParams } from '../user_actions_activity_bar/types'; @@ -25,7 +25,7 @@ export const useLastPageUserActions = ({ const { data: lastPageUserActionsData, isLoading: isLoadingLastPageUserActions } = useFindCaseUserActions(caseId, { ...userActivityQueryParams, page: lastPage }, lastPage > 1); - const lastPageUserActions = useMemo(() => { + const lastPageUserActions = useMemo(() => { if (isLoadingLastPageUserActions || !lastPageUserActionsData) { return []; } diff --git a/x-pack/plugins/cases/public/components/user_actions/use_user_actions_pagination.tsx b/x-pack/plugins/cases/public/components/user_actions/use_user_actions_pagination.tsx index ec9b931af0ab2..bdaaa68de0157 100644 --- a/x-pack/plugins/cases/public/components/user_actions/use_user_actions_pagination.tsx +++ b/x-pack/plugins/cases/public/components/user_actions/use_user_actions_pagination.tsx @@ -8,7 +8,7 @@ import { useMemo } from 'react'; import { useInfiniteFindCaseUserActions } from '../../containers/use_infinite_find_case_user_actions'; -import type { CaseUserActions } from '../../containers/types'; +import type { UserActionUI } from '../../containers/types'; import type { UserActivityParams } from '../user_actions_activity_bar/types'; interface UserActionsPagination { @@ -32,12 +32,12 @@ export const useUserActionsPagination = ({ const showBottomList = lastPage > 1; - const infiniteCaseUserActions = useMemo(() => { + const infiniteCaseUserActions = useMemo(() => { if (!caseInfiniteUserActionsData?.pages?.length || isLoadingInfiniteUserActions) { return []; } - const userActionsData: CaseUserActions[] = []; + const userActionsData: UserActionUI[] = []; caseInfiniteUserActionsData.pages.forEach((page) => userActionsData.push(...page.userActions)); diff --git a/x-pack/plugins/cases/public/components/user_actions/user_actions_list.tsx b/x-pack/plugins/cases/public/components/user_actions/user_actions_list.tsx index 72d8371734548..1b4508291bfc7 100644 --- a/x-pack/plugins/cases/public/components/user_actions/user_actions_list.tsx +++ b/x-pack/plugins/cases/public/components/user_actions/user_actions_list.tsx @@ -11,7 +11,7 @@ import { EuiCommentList } from '@elastic/eui'; import React, { useMemo, useEffect, useState } from 'react'; import styled from 'styled-components'; -import type { CaseUserActions } from '../../containers/types'; +import type { UserActionUI } from '../../containers/types'; import type { UserActionBuilderArgs, UserActionTreeProps } from './types'; import { isUserActionTypeSupported } from './helpers'; import { useCasesContext } from '../cases_context/use_cases_context'; @@ -78,7 +78,7 @@ export type UserActionListProps = Omit< | 'statusActionButton' > & Pick & { - caseUserActions: CaseUserActions[]; + caseUserActions: UserActionUI[]; loadingAlertData: boolean; manualAlertsData: Record; bottomActions?: EuiCommentProps[]; diff --git a/x-pack/plugins/cases/public/containers/api.ts b/x-pack/plugins/cases/public/containers/api.ts index 2d9e17658d156..c804d1d34f60c 100644 --- a/x-pack/plugins/cases/public/containers/api.ts +++ b/x-pack/plugins/cases/public/containers/api.ts @@ -72,7 +72,7 @@ import type { CaseUI, SingleCaseMetrics, SingleCaseMetricsFeature, - CaseUserActions, + UserActionUI, } from './types'; import { @@ -186,7 +186,7 @@ export const findCaseUserActions = async ( ...response, userActions: convertUserActionsToCamelCase( decodeCaseUserActionsResponse(response.userActions) - ) as CaseUserActions[], + ) as UserActionUI[], }; }; diff --git a/x-pack/plugins/cases/public/containers/mock.ts b/x-pack/plugins/cases/public/containers/mock.ts index d2ff6a1c0de6f..d35d001d41bc4 100644 --- a/x-pack/plugins/cases/public/containers/mock.ts +++ b/x-pack/plugins/cases/public/containers/mock.ts @@ -6,14 +6,7 @@ */ import type { FileJSON } from '@kbn/shared-ux-file-types'; -import type { - ActionLicense, - CasesUI, - CaseUI, - CasesStatus, - CaseUserActions, - Comment, -} from './types'; +import type { ActionLicense, CasesUI, CaseUI, CasesStatus, UserActionUI, Comment } from './types'; import type { ResolvedCase, @@ -33,10 +26,10 @@ import type { CasesFindResponse, Cases, CasesStatusResponse, - CaseUserActionResponse, - CaseUserActionsResponse, - CommentResponse, UserAction, + UserActions, + CommentResponse, + ActionCategory, UserActionTypes, UserActionWithResponse, CommentUserAction, @@ -618,9 +611,9 @@ export const allCasesSnake: CasesFindResponse = { export const getUserAction = ( type: UserActionTypes, - action: UserAction, + action: ActionCategory, overrides?: Record -): CaseUserActions => { +): UserActionUI => { const commonProperties = { ...basicAction, id: `${type}-${action}`, @@ -745,27 +738,27 @@ export const getUserAction = ( return { ...commonProperties, ...overrides, - } as CaseUserActions; + } as UserActionUI; } }; export const getUserActionSnake = ( type: UserActionTypes, - action: UserAction, + action: ActionCategory, overrides?: Record -): CaseUserActionResponse => { +): UserAction => { return { ...covertToSnakeCase(getUserAction(type, action, overrides)), - } as unknown as CaseUserActionResponse; + } as unknown as UserAction; }; -export const caseUserActionsSnake: CaseUserActionsResponse = [ +export const caseUserActionsSnake: UserActions = [ getUserActionSnake('description', Actions.create), getUserActionSnake('comment', Actions.create), getUserActionSnake('description', Actions.update), ]; -export const caseUserActionsWithRegisteredAttachmentsSnake: CaseUserActionsResponse = [ +export const caseUserActionsWithRegisteredAttachmentsSnake: UserActions = [ getUserActionSnake('description', Actions.create), { created_at: basicCreatedAt, @@ -865,13 +858,13 @@ export const getHostIsolationUserAction = ( ...overrides, }); -export const caseUserActions: CaseUserActions[] = [ +export const caseUserActions: UserActionUI[] = [ getUserAction('description', Actions.create), getUserAction('comment', Actions.create), getUserAction('description', Actions.update), ]; -export const caseUserActionsWithRegisteredAttachments: CaseUserActions[] = [ +export const caseUserActionsWithRegisteredAttachments: UserActionUI[] = [ getUserAction('description', Actions.create), { createdAt: basicCreatedAt, diff --git a/x-pack/plugins/cases/public/containers/utils.ts b/x-pack/plugins/cases/public/containers/utils.ts index 66b540040415f..c1d89dc81595c 100644 --- a/x-pack/plugins/cases/public/containers/utils.ts +++ b/x-pack/plugins/cases/public/containers/utils.ts @@ -15,7 +15,7 @@ import { NO_ASSIGNEES_FILTERING_KEYWORD } from '../../common/constants'; import type { CasesConfigurationsResponse, CasesConfigureResponse, - CaseUserActionsResponse, + UserActions, CasePatchRequest, CaseResolveResponse, SingleCaseMetricsResponse, @@ -30,7 +30,7 @@ import { throwErrors, CaseConfigurationsResponseRt, CaseConfigureResponseRt, - CaseUserActionsResponseRt, + UserActionsRt, CommentType, CaseResolveResponseRt, SingleCaseMetricsResponseRt, @@ -80,11 +80,8 @@ export const decodeCaseConfigureResponse = (respCase?: CasesConfigureResponse) = fold(throwErrors(createToasterPlainError), identity) ); -export const decodeCaseUserActionsResponse = (respUserActions?: CaseUserActionsResponse) => - pipe( - CaseUserActionsResponseRt.decode(respUserActions), - fold(throwErrors(createToasterPlainError), identity) - ); +export const decodeCaseUserActionsResponse = (respUserActions?: UserActions) => + pipe(UserActionsRt.decode(respUserActions), fold(throwErrors(createToasterPlainError), identity)); export const decodeCaseUserActionStatsResponse = ( caseUserActionsStats: CaseUserActionStatsResponse diff --git a/x-pack/plugins/cases/server/client/metrics/lifespan.test.ts b/x-pack/plugins/cases/server/client/metrics/lifespan.test.ts index 808b6f334e15f..a36e9eb8a106b 100644 --- a/x-pack/plugins/cases/server/client/metrics/lifespan.test.ts +++ b/x-pack/plugins/cases/server/client/metrics/lifespan.test.ts @@ -6,7 +6,7 @@ */ import type { SavedObject } from '@kbn/core/server'; -import type { CaseUserActionInjectedAttributes } from '../../../common/api'; +import type { UserActionAttributes } from '../../../common/api'; import { CaseStatuses } from '../../../common/api'; import { getStatusInfo } from './lifespan'; import { createStatusChangeSavedObject } from './test_utils/lifespan'; @@ -120,7 +120,7 @@ describe('lifespan', () => { [ { attributes: { payload: { hello: 1, status: CaseStatuses.closed }, type: 'status' }, - } as unknown as SavedObject, + } as unknown as SavedObject, ], new Date(0) ); @@ -133,7 +133,7 @@ describe('lifespan', () => { [ { attributes: { payload: { status: CaseStatuses.closed }, type: 'awesome' }, - } as unknown as SavedObject, + } as unknown as SavedObject, ], new Date(0) ); @@ -149,7 +149,7 @@ describe('lifespan', () => { payload: { status: CaseStatuses.closed, created_at: 'blah' }, type: 'status', }, - } as unknown as SavedObject, + } as unknown as SavedObject, ], new Date(0) ); diff --git a/x-pack/plugins/cases/server/client/metrics/lifespan.ts b/x-pack/plugins/cases/server/client/metrics/lifespan.ts index a78e5397b7a13..7e564c99f7fbc 100644 --- a/x-pack/plugins/cases/server/client/metrics/lifespan.ts +++ b/x-pack/plugins/cases/server/client/metrics/lifespan.ts @@ -7,7 +7,7 @@ import type { SavedObject } from '@kbn/core/server'; import type { - CaseUserActionInjectedAttributes, + UserActionAttributes, SingleCaseMetricsResponse, StatusInfo, StatusUserAction, @@ -83,7 +83,7 @@ interface StatusCalculations { } export function getStatusInfo( - statusUserActions: Array>, + statusUserActions: Array>, caseOpenTimestamp: Date ): StatusInfo { const accStatusInfo = statusUserActions.reduce( @@ -138,7 +138,7 @@ export function getStatusInfo( } function isValidStatusChangeUserAction( - attributes: CaseUserActionInjectedAttributes, + attributes: UserActionAttributes, newStatusChangeTimestamp: Date ): attributes is UserActionWithResponse { return StatusUserActionRt.is(attributes) && isDateValid(newStatusChangeTimestamp); diff --git a/x-pack/plugins/cases/server/client/metrics/test_utils/lifespan.ts b/x-pack/plugins/cases/server/client/metrics/test_utils/lifespan.ts index 62593e39e7d5c..b54204003b8ee 100644 --- a/x-pack/plugins/cases/server/client/metrics/test_utils/lifespan.ts +++ b/x-pack/plugins/cases/server/client/metrics/test_utils/lifespan.ts @@ -6,12 +6,12 @@ */ import type { SavedObject } from '@kbn/core/server'; -import type { CaseStatuses, CaseUserActionInjectedAttributes } from '../../../../common/api'; +import type { CaseStatuses, UserActionAttributes } from '../../../../common/api'; export function createStatusChangeSavedObject( status: CaseStatuses, createdAt: Date -): SavedObject { +): SavedObject { return { references: [], id: '', diff --git a/x-pack/plugins/cases/server/client/typedoc_interfaces.ts b/x-pack/plugins/cases/server/client/typedoc_interfaces.ts index f67a1eff4631e..f0fe44d4d44da 100644 --- a/x-pack/plugins/cases/server/client/typedoc_interfaces.ts +++ b/x-pack/plugins/cases/server/client/typedoc_interfaces.ts @@ -25,7 +25,7 @@ import type { CasesFindResponse, CasesPatchRequest, Cases, - CaseUserActionsResponse, + UserActions, CommentsResponse, CasesBulkGetResponse, } from '../../common/api'; @@ -51,4 +51,4 @@ export interface ICasesConfigurePatch extends CasesConfigurePatch {} export interface ICommentsResponse extends CommentsResponse {} export interface IAllCommentsResponse extends AllCommentsResponse {} -export interface ICaseUserActionsResponse extends CaseUserActionsResponse {} +export interface ICaseUserActionsResponse extends UserActions {} diff --git a/x-pack/plugins/cases/server/client/user_actions/connectors.ts b/x-pack/plugins/cases/server/client/user_actions/connectors.ts index da949996d0fd7..79673389881d0 100644 --- a/x-pack/plugins/cases/server/client/user_actions/connectors.ts +++ b/x-pack/plugins/cases/server/client/user_actions/connectors.ts @@ -13,7 +13,7 @@ import type { SavedObject } from '@kbn/core-saved-objects-common/src/server_type import type { GetCaseConnectorsResponse, CaseConnector, - CaseUserActionInjectedAttributes, + UserActionAttributes, CaseExternalServiceBasic, GetCaseConnectorsPushDetails, } from '../../../common/api'; @@ -75,7 +75,7 @@ const checkConnectorsAuthorization = async ({ authorization, }: { connectors: CaseConnectorActivity[]; - latestUserAction?: SavedObject; + latestUserAction?: SavedObject; authorization: PublicMethodsOf; }) => { const entities: OwnerEntity[] = latestUserAction @@ -127,7 +127,7 @@ const getConnectorsInfo = async ({ }: { caseId: string; connectors: CaseConnectorActivity[]; - latestUserAction?: SavedObject; + latestUserAction?: SavedObject; actionsClient: PublicMethodsOf; userActionService: CaseUserActionService; logger: CasesClientArgs['logger']; @@ -224,7 +224,7 @@ const getPushDetails = (activity: CaseConnectorActivity[]) => { }; const getExternalServiceFromSavedObject = ( - savedObject: SavedObject | undefined + savedObject: SavedObject | undefined ): CaseExternalServiceBasic | undefined => { if (savedObject != null && isPushedUserAction(savedObject.attributes)) { return savedObject.attributes.payload.externalService; @@ -248,7 +248,7 @@ const isDateValid = (date: Date): boolean => { }; const getConnectorInfoFromSavedObject = ( - savedObject: SavedObject | undefined + savedObject: SavedObject | undefined ): CaseConnector | undefined => { if ( savedObject != null && @@ -268,7 +268,7 @@ const createConnectorInfoResult = ({ actionConnectors: ActionResult[]; connectors: CaseConnectorActivity[]; pushInfo: Map; - latestUserAction?: SavedObject; + latestUserAction?: SavedObject; }) => { const results: GetCaseConnectorsResponse = {}; const actionConnectorsMap = new Map( diff --git a/x-pack/plugins/cases/server/client/user_actions/utils.ts b/x-pack/plugins/cases/server/client/user_actions/utils.ts index 98df79b6e94ab..bc2deb2b2caf6 100644 --- a/x-pack/plugins/cases/server/client/user_actions/utils.ts +++ b/x-pack/plugins/cases/server/client/user_actions/utils.ts @@ -7,8 +7,8 @@ import type { SavedObjectsFindResponse } from '@kbn/core-saved-objects-api-server'; import type { - CaseUserActionInjectedAttributes, - CaseUserActionsResponse, + UserActionAttributes, + UserActions, CaseUserActionsDeprecatedResponse, CaseUserActionDeprecatedResponse, } from '../../../common/api'; @@ -20,6 +20,6 @@ export const extractAttributes = ( }; export const formatSavedObjects = ( - response: SavedObjectsFindResponse -): CaseUserActionsResponse => + response: SavedObjectsFindResponse +): UserActions => response.saved_objects.map((so) => ({ id: so.id, version: so.version ?? '', ...so.attributes })); diff --git a/x-pack/plugins/cases/server/common/types/user_actions.ts b/x-pack/plugins/cases/server/common/types/user_actions.ts index bc5366a2adaa8..14823f159a408 100644 --- a/x-pack/plugins/cases/server/common/types/user_actions.ts +++ b/x-pack/plugins/cases/server/common/types/user_actions.ts @@ -5,6 +5,8 @@ * 2.0. */ +import type { SavedObject } from '@kbn/core/server'; +import type { UserActionAttributes } from '../../../common/api'; import type { User } from './user'; interface UserActionCommonPersistedAttributes { @@ -18,3 +20,6 @@ export interface UserActionPersistedAttributes extends UserActionCommonPersisted type: string; payload: Record; } + +export type UserActionTransformedAttributes = UserActionAttributes; +export type UserActionSavedObjectTransformed = SavedObject; diff --git a/x-pack/plugins/cases/server/services/user_actions/audit_logger.ts b/x-pack/plugins/cases/server/services/user_actions/audit_logger.ts index af3599c11c67b..fc1d60684949d 100644 --- a/x-pack/plugins/cases/server/services/user_actions/audit_logger.ts +++ b/x-pack/plugins/cases/server/services/user_actions/audit_logger.ts @@ -8,7 +8,7 @@ import type { EcsEvent } from '@kbn/ecs'; import type { AuditLogger } from '@kbn/security-plugin/server'; import type { ArrayElement } from '@kbn/utility-types'; -import type { UserAction as Action } from '../../../common/api'; +import type { ActionCategory as Action } from '../../../common/api'; import type { EventDetails } from './types'; const actionsToEcsType: Record> = { diff --git a/x-pack/plugins/cases/server/services/user_actions/builders/assignees.ts b/x-pack/plugins/cases/server/services/user_actions/builders/assignees.ts index ffd695e62df37..9c21bf916758d 100644 --- a/x-pack/plugins/cases/server/services/user_actions/builders/assignees.ts +++ b/x-pack/plugins/cases/server/services/user_actions/builders/assignees.ts @@ -6,7 +6,7 @@ */ import { CASE_SAVED_OBJECT } from '../../../../common/constants'; -import type { UserAction } from '../../../../common/api'; +import type { ActionCategory } from '../../../../common/api'; import { ActionTypes, Actions } from '../../../../common/api'; import { UserActionBuilder } from '../abstract_builder'; import type { EventDetails, UserActionParameters, UserActionEvent } from '../types'; @@ -44,7 +44,7 @@ export class AssigneesUserActionBuilder extends UserActionBuilder { } } -const getVerbMessage = (action: UserAction, uids: string[]) => { +const getVerbMessage = (action: ActionCategory, uids: string[]) => { const uidText = `uids: [${uids}]`; switch (action) { diff --git a/x-pack/plugins/cases/server/services/user_actions/builders/audit_logger_utils.ts b/x-pack/plugins/cases/server/services/user_actions/builders/audit_logger_utils.ts index 4010c358ce601..396301a648e42 100644 --- a/x-pack/plugins/cases/server/services/user_actions/builders/audit_logger_utils.ts +++ b/x-pack/plugins/cases/server/services/user_actions/builders/audit_logger_utils.ts @@ -5,7 +5,7 @@ * 2.0. */ -import type { UserAction as Action } from '../../../../common/api'; +import type { ActionCategory as Action } from '../../../../common/api'; const actionsToVerbs: Record = { add: 'added', diff --git a/x-pack/plugins/cases/server/services/user_actions/builders/tags.ts b/x-pack/plugins/cases/server/services/user_actions/builders/tags.ts index 1a9066cbbf224..5dc4197185761 100644 --- a/x-pack/plugins/cases/server/services/user_actions/builders/tags.ts +++ b/x-pack/plugins/cases/server/services/user_actions/builders/tags.ts @@ -6,7 +6,7 @@ */ import { CASE_SAVED_OBJECT } from '../../../../common/constants'; -import type { UserAction } from '../../../../common/api'; +import type { ActionCategory } from '../../../../common/api'; import { ActionTypes, Actions } from '../../../../common/api'; import { UserActionBuilder } from '../abstract_builder'; import type { EventDetails, UserActionParameters, UserActionEvent } from '../types'; @@ -45,7 +45,7 @@ export class TagsUserActionBuilder extends UserActionBuilder { } } -const getPreposition = (action: UserAction): string => { +const getPreposition = (action: ActionCategory): string => { switch (action) { case Actions.add: return 'to'; diff --git a/x-pack/plugins/cases/server/services/user_actions/index.ts b/x-pack/plugins/cases/server/services/user_actions/index.ts index 167f0e123fb07..521cee600469b 100644 --- a/x-pack/plugins/cases/server/services/user_actions/index.ts +++ b/x-pack/plugins/cases/server/services/user_actions/index.ts @@ -5,14 +5,11 @@ * 2.0. */ -import type { SavedObject, SavedObjectsFindResponse, SavedObjectsRawDoc } from '@kbn/core/server'; +import type { SavedObjectsFindResponse, SavedObjectsRawDoc } from '@kbn/core/server'; import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; import type { KueryNode } from '@kbn/es-query'; -import type { - CaseUserActionDeprecatedResponse, - CaseUserActionInjectedAttributes, -} from '../../../common/api'; +import type { CaseUserActionDeprecatedResponse } from '../../../common/api'; import { ActionTypes } from '../../../common/api'; import { CASE_SAVED_OBJECT, @@ -38,7 +35,10 @@ import { defaultSortField } from '../../common/utils'; import { UserActionPersister } from './operations/create'; import { UserActionFinder } from './operations/find'; import { transformToExternalModel, legacyTransformFindResponseToExternalModel } from './transform'; -import type { UserActionPersistedAttributes } from '../../common/types/user_actions'; +import type { + UserActionPersistedAttributes, + UserActionSavedObjectTransformed, +} from '../../common/types/user_actions'; export class CaseUserActionService { private readonly _creator: UserActionPersister; @@ -222,7 +222,7 @@ export class CaseUserActionService { public async getMostRecentUserAction( caseId: string - ): Promise | undefined> { + ): Promise { try { this.context.log.debug( `Attempting to retrieve the most recent user action for case id: ${caseId}` @@ -327,7 +327,7 @@ export class CaseUserActionService { rawFieldsDoc = createCase.mostRecent.hits.hits[0]; } - let fieldsDoc: SavedObject | undefined; + let fieldsDoc: UserActionSavedObjectTransformed | undefined; if (rawFieldsDoc != null) { const doc = this.context.savedObjectsSerializer.rawToSavedObject( @@ -368,9 +368,7 @@ export class CaseUserActionService { } } - private getTopHitsDoc( - topHits: TopHits - ): SavedObject | undefined { + private getTopHitsDoc(topHits: TopHits): UserActionSavedObjectTransformed | undefined { if (topHits.hits.hits.length > 0) { const rawPushDoc = topHits.hits.hits[0]; diff --git a/x-pack/plugins/cases/server/services/user_actions/operations/create.ts b/x-pack/plugins/cases/server/services/user_actions/operations/create.ts index 25096baddf479..d4b5c0d283630 100644 --- a/x-pack/plugins/cases/server/services/user_actions/operations/create.ts +++ b/x-pack/plugins/cases/server/services/user_actions/operations/create.ts @@ -15,7 +15,7 @@ import type { ActionTypeValues, CaseAssignees, CaseUserProfile, - UserAction as Action, + ActionCategory, } from '../../../../common/api'; import { Actions, ActionTypes } from '../../../../common/api'; import { BuilderFactory } from '../builder_factory'; @@ -184,7 +184,7 @@ export class UserActionPersister { }: { commonArgs: CommonUserActionArgs; actionType: ActionType; - action: Action; + action: ActionCategory; createPayload: CreatePayloadFunction; modifiedItems?: Item[] | null; }) { diff --git a/x-pack/plugins/cases/server/services/user_actions/operations/find.ts b/x-pack/plugins/cases/server/services/user_actions/operations/find.ts index 5ce99d41a5ed2..c1b3a49ed6507 100644 --- a/x-pack/plugins/cases/server/services/user_actions/operations/find.ts +++ b/x-pack/plugins/cases/server/services/user_actions/operations/find.ts @@ -8,14 +8,9 @@ import type { KueryNode } from '@kbn/es-query'; import { fromKueryExpression } from '@kbn/es-query'; import type { SavedObjectsFindResponse } from '@kbn/core-saved-objects-api-server'; -import type { SavedObject } from '@kbn/core-saved-objects-server'; import { DEFAULT_PAGE, DEFAULT_PER_PAGE } from '../../../routes/api'; import { defaultSortField } from '../../../common/utils'; -import type { - ActionTypeValues, - FindTypeField, - CaseUserActionInjectedAttributes, -} from '../../../../common/api'; +import type { ActionTypeValues, FindTypeField } from '../../../../common/api'; import { Actions, ActionTypes, CommentType } from '../../../../common/api'; import { CASE_SAVED_OBJECT, @@ -26,7 +21,11 @@ import { import type { FindOptions, ServiceContext } from '../types'; import { transformFindResponseToExternalModel, transformToExternalModel } from '../transform'; import { buildFilter, combineFilters, NodeBuilderOperators } from '../../../client/utils'; -import type { UserActionPersistedAttributes } from '../../../common/types/user_actions'; +import type { + UserActionPersistedAttributes, + UserActionSavedObjectTransformed, + UserActionTransformedAttributes, +} from '../../../common/types/user_actions'; export class UserActionFinder { constructor(private readonly context: ServiceContext) {} @@ -38,7 +37,7 @@ export class UserActionFinder { page, perPage, filter, - }: FindOptions): Promise> { + }: FindOptions): Promise> { try { this.context.log.debug(`Attempting to find user actions for case id: ${caseId}`); @@ -168,7 +167,7 @@ export class UserActionFinder { }: { caseId: string; filter?: KueryNode; - }): Promise>> { + }): Promise { try { this.context.log.debug('Attempting to find status changes'); @@ -200,7 +199,7 @@ export class UserActionFinder { } ); - let userActions: Array> = []; + let userActions: UserActionSavedObjectTransformed[] = []; for await (const findResults of finder.find()) { userActions = userActions.concat( findResults.saved_objects.map((so) => diff --git a/x-pack/plugins/cases/server/services/user_actions/test_utils.ts b/x-pack/plugins/cases/server/services/user_actions/test_utils.ts index 49d47b13f8d04..b4dbb114fc2fe 100644 --- a/x-pack/plugins/cases/server/services/user_actions/test_utils.ts +++ b/x-pack/plugins/cases/server/services/user_actions/test_utils.ts @@ -21,7 +21,7 @@ import { import type { CaseUserActionAttributesWithoutConnectorId, ConnectorUserAction, - UserAction, + ActionCategory, } from '../../../common/api'; import { CaseSeverity, CaseStatuses, Actions } from '../../../common/api'; import { @@ -76,7 +76,7 @@ export const createUserActionSO = ({ type, references = [], }: { - action: UserAction; + action: ActionCategory; type?: string; payload?: Record; attributesOverrides?: Partial; diff --git a/x-pack/plugins/cases/server/services/user_actions/transform.ts b/x-pack/plugins/cases/server/services/user_actions/transform.ts index 10a6104e15807..fb0a17fc6a57b 100644 --- a/x-pack/plugins/cases/server/services/user_actions/transform.ts +++ b/x-pack/plugins/cases/server/services/user_actions/transform.ts @@ -14,11 +14,7 @@ import { isCreateCaseUserAction, isCommentUserAction, } from '../../../common/utils/user_actions'; -import type { - CaseUserActionAttributes, - CaseUserActionDeprecatedResponse, - CaseUserActionInjectedAttributes, -} from '../../../common/api'; +import type { CaseUserActionDeprecatedResponse, UserActionAttributes } from '../../../common/api'; import { NONE_CONNECTOR_ID } from '../../../common/api'; import { CASE_SAVED_OBJECT, CASE_COMMENT_SAVED_OBJECT } from '../../../common/constants'; import { @@ -33,12 +29,16 @@ import { isCommentRequestTypeExternalReferenceSO } from '../type_guards'; import type { PersistableStateAttachmentTypeRegistry } from '../../attachment_framework/persistable_state_registry'; import { injectPersistableReferencesToSO } from '../../attachment_framework/so_references'; import { findReferenceId } from '../../common/references'; -import type { UserActionPersistedAttributes } from '../../common/types/user_actions'; +import type { + UserActionPersistedAttributes, + UserActionSavedObjectTransformed, + UserActionTransformedAttributes, +} from '../../common/types/user_actions'; export function transformFindResponseToExternalModel( userActions: SavedObjectsFindResponse, persistableStateAttachmentTypeRegistry: PersistableStateAttachmentTypeRegistry -): SavedObjectsFindResponse { +): SavedObjectsFindResponse { return { ...userActions, saved_objects: userActions.saved_objects.map((so) => ({ @@ -51,7 +51,7 @@ export function transformFindResponseToExternalModel( export function transformToExternalModel( userAction: SavedObject, persistableStateAttachmentTypeRegistry: PersistableStateAttachmentTypeRegistry -): SavedObject { +): UserActionSavedObjectTransformed { const { references } = userAction; const commentId = @@ -64,7 +64,7 @@ export function transformToExternalModel( ...userAction.attributes, comment_id: commentId, payload, - } as CaseUserActionInjectedAttributes, + } as UserActionTransformedAttributes, }; } @@ -116,7 +116,7 @@ function legacyTransformToExternalModel( const addReferenceIdToPayload = ( userAction: SavedObject, persistableStateAttachmentTypeRegistry: PersistableStateAttachmentTypeRegistry -): CaseUserActionAttributes['payload'] => { +): UserActionAttributes['payload'] => { const connectorId = getConnectorIdFromReferences(userAction); const userActionAttributes = userAction.attributes; diff --git a/x-pack/plugins/cases/server/services/user_actions/types.ts b/x-pack/plugins/cases/server/services/user_actions/types.ts index f88bd2ba3fe15..addea6ddfa763 100644 --- a/x-pack/plugins/cases/server/services/user_actions/types.ts +++ b/x-pack/plugins/cases/server/services/user_actions/types.ts @@ -10,7 +10,6 @@ import type { SavedObjectsClientContract, Logger, ISavedObjectsSerializer, - SavedObject, SavedObjectsRawDoc, SavedObjectsUpdateResponse, } from '@kbn/core/server'; @@ -25,18 +24,20 @@ import type { CaseSeverity, CaseStatuses, CaseUserActionAttributesWithoutConnectorId, - CaseUserActionInjectedAttributes, CommentRequest, CommentUserAction, ConnectorUserAction, PushedUserAction, User, - UserAction, + ActionCategory, UserActionFindRequest, UserActionTypes, } from '../../../common/api'; import type { PersistableStateAttachmentTypeRegistry } from '../../attachment_framework/persistable_state_registry'; -import type { UserActionPersistedAttributes } from '../../common/types/user_actions'; +import type { + UserActionPersistedAttributes, + UserActionSavedObjectTransformed, +} from '../../common/types/user_actions'; import type { IndexRefresh } from '../types'; import type { CaseSavedObjectTransformed } from '../../common/types/case'; @@ -105,7 +106,16 @@ export interface CommonArguments { owner: string; attachmentId?: string; connectorId?: string; - action?: UserAction; + action?: ActionCategory; +} + +export interface Attributes { + action: ActionCategory; + created_at: string; + created_by: User; + owner: string; + type: UserActionTypes; + payload: Record; } export interface SavedObjectParameters { @@ -115,7 +125,7 @@ export interface SavedObjectParameters { export interface EventDetails { getMessage: (storedUserActionId?: string) => string; - action: UserAction; + action: ActionCategory; descriptiveAction: string; savedObjectId: string; savedObjectType: string; @@ -127,7 +137,7 @@ export interface UserActionEvent { } export type CommonBuilderArguments = CommonArguments & { - action: UserAction; + action: ActionCategory; type: UserActionTypes; value: unknown; valueKey: string; @@ -146,17 +156,17 @@ export interface ServiceContext { } export interface PushTimeFrameInfo { - mostRecent: SavedObject; - oldest: SavedObject; + mostRecent: UserActionSavedObjectTransformed; + oldest: UserActionSavedObjectTransformed; } export interface CaseConnectorActivity { connectorId: string; - fields: SavedObject; + fields: UserActionSavedObjectTransformed; push?: PushTimeFrameInfo; } -export type CaseConnectorFields = Map>; +export type CaseConnectorFields = Map; export interface PushInfo { date: Date;