Skip to content

Commit

Permalink
[Cases] Renaming user action type (#156113)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jonathan-buttner authored May 2, 2023
1 parent de0498d commit f0b75b3
Show file tree
Hide file tree
Showing 29 changed files with 143 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -36,7 +36,7 @@ export const UserActionFindRequestRt = rt.partial({
export type UserActionFindRequest = rt.TypeOf<typeof UserActionFindRequestRt>;

export const UserActionFindResponseRt = rt.type({
userActions: CaseUserActionsResponseRt,
userActions: UserActionsRt,
page: rt.number,
perPage: rt.number,
total: rt.number,
Expand Down
34 changes: 17 additions & 17 deletions x-pack/plugins/cases/common/api/cases/user_actions/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const CommonUserActionsRt = rt.union([
AssigneesUserActionRt,
]);

export const UserActionsRt = rt.union([
const UserActionPayloadRt = rt.union([
CommonUserActionsRt,
CreateCaseUserActionRt,
ConnectorUserActionRt,
Expand All @@ -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,
Expand All @@ -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<typeof CaseUserActionAttributesRt>;
export type CaseUserActionAttributesWithoutConnectorId = rt.TypeOf<
typeof CaseUserActionBasicWithoutConnectorIdRt
>;
export type CaseUserActionStatsResponse = rt.TypeOf<typeof CaseUserActionStatsRt>;
export type CaseUserActionsResponse = rt.TypeOf<typeof CaseUserActionsResponseRt>;
export type CaseUserActionResponse = rt.TypeOf<typeof CaseUserActionResponseRt>;
export type UserActions = rt.TypeOf<typeof UserActionsRt>;
export type UserAction = rt.TypeOf<typeof UserActionRt>;
export type CaseUserActionsDeprecatedResponse = rt.TypeOf<
typeof CaseUserActionsDeprecatedResponseRt
>;
export type CaseUserActionDeprecatedResponse = rt.TypeOf<typeof CaseUserActionDeprecatedResponseRt>;
export type CaseUserActionInjectedAttributes = rt.TypeOf<typeof CaseUserActionInjectedAttributesRt>;
export type UserActionAttributes = rt.TypeOf<typeof UserActionAttributes>;

export type UserAction = rt.TypeOf<typeof ActionsRt>;
/**
* This defines the high level category for the user action. Whether the user add, removed, updated something
*/
export type ActionCategory = rt.TypeOf<typeof ActionsRt>;
/**
* 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<typeof CaseUserActionBasicRt>;
6 changes: 3 additions & 3 deletions x-pack/plugins/cases/common/ui/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type {
CaseStatuses,
User,
ActionConnector,
CaseUserActionResponse,
UserAction,
SingleCaseMetricsResponse,
CommentResponse,
Case as CaseSnakeCase,
Expand Down Expand Up @@ -87,9 +87,9 @@ export type Comment = SnakeToCamelCase<CommentResponse>;
export type AlertComment = SnakeToCamelCase<CommentResponseAlertsType>;
export type ExternalReferenceComment = SnakeToCamelCase<CommentResponseExternalReferenceType>;
export type PersistableComment = SnakeToCamelCase<CommentResponseTypePersistableState>;
export type CaseUserActions = SnakeToCamelCase<CaseUserActionResponse>;
export type UserActionUI = SnakeToCamelCase<UserAction>;
export type FindCaseUserActions = Omit<SnakeToCamelCase<UserActionFindResponse>, 'userActions'> & {
userActions: CaseUserActions[];
userActions: UserActionUI[];
};
export type CaseUserActionsStats = SnakeToCamelCase<CaseUserActionStatsResponse>;
export type CaseUI = Omit<SnakeToCamelCase<CaseSnakeCase>, 'comments'> & { comments: Comment[] };
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/cases/public/api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import type {
CasesFindResponse,
Case,
CaseUserActionsResponse,
UserActions,
CommentRequest,
CommentResponse,
CaseResolveResponse,
Expand Down Expand Up @@ -81,7 +81,7 @@ export const convertAttachmentToCamelCase = (attachment: CommentRequest): Commen
return convertToCamelCase<CommentRequest, Comment>(attachment);
};

export const convertUserActionsToCamelCase = (userActions: CaseUserActionsResponse) => {
export const convertUserActionsToCamelCase = (userActions: UserActions) => {
return userActions.map((userAction) => {
if (isCommentUserAction(userAction)) {
const userActionWithoutPayload = omit(userAction, 'payload.comment');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<Props> = React.memo(({ userAction, handleOutlineComment }) => (
<EuiFlexGroup responsive={false}>
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/cases/public/components/user_actions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { ActionTypes, UserActionWithResponse } from '../../../common/api';
import type {
CaseUI,
CaseConnectors,
CaseUserActions,
UserActionUI,
Comment,
UseFetchAlertData,
CaseUserActionsStats,
Expand Down Expand Up @@ -54,7 +54,7 @@ export interface UserActionBuilderArgs {
externalReferenceAttachmentTypeRegistry: ExternalReferenceAttachmentTypeRegistry;
persistableStateAttachmentTypeRegistry: PersistableStateAttachmentTypeRegistry;
caseConnectors: CaseConnectors;
userAction: CaseUserActions;
userAction: UserActionUI;
comments: Comment[];
index: number;
commentRefs: React.MutableRefObject<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -25,7 +25,7 @@ export const useLastPageUserActions = ({
const { data: lastPageUserActionsData, isLoading: isLoadingLastPageUserActions } =
useFindCaseUserActions(caseId, { ...userActivityQueryParams, page: lastPage }, lastPage > 1);

const lastPageUserActions = useMemo<CaseUserActions[]>(() => {
const lastPageUserActions = useMemo<UserActionUI[]>(() => {
if (isLoadingLastPageUserActions || !lastPageUserActionsData) {
return [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -32,12 +32,12 @@ export const useUserActionsPagination = ({

const showBottomList = lastPage > 1;

const infiniteCaseUserActions = useMemo<CaseUserActions[]>(() => {
const infiniteCaseUserActions = useMemo<UserActionUI[]>(() => {
if (!caseInfiniteUserActionsData?.pages?.length || isLoadingInfiniteUserActions) {
return [];
}

const userActionsData: CaseUserActions[] = [];
const userActionsData: UserActionUI[] = [];

caseInfiniteUserActionsData.pages.forEach((page) => userActionsData.push(...page.userActions));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -78,7 +78,7 @@ export type UserActionListProps = Omit<
| 'statusActionButton'
> &
Pick<UserActionBuilderArgs, 'commentRefs' | 'handleManageQuote'> & {
caseUserActions: CaseUserActions[];
caseUserActions: UserActionUI[];
loadingAlertData: boolean;
manualAlertsData: Record<string, unknown>;
bottomActions?: EuiCommentProps[];
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/cases/public/containers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ import type {
CaseUI,
SingleCaseMetrics,
SingleCaseMetricsFeature,
CaseUserActions,
UserActionUI,
} from './types';

import {
Expand Down Expand Up @@ -186,7 +186,7 @@ export const findCaseUserActions = async (
...response,
userActions: convertUserActionsToCamelCase(
decodeCaseUserActionsResponse(response.userActions)
) as CaseUserActions[],
) as UserActionUI[],
};
};

Expand Down
35 changes: 14 additions & 21 deletions x-pack/plugins/cases/public/containers/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -33,10 +26,10 @@ import type {
CasesFindResponse,
Cases,
CasesStatusResponse,
CaseUserActionResponse,
CaseUserActionsResponse,
CommentResponse,
UserAction,
UserActions,
CommentResponse,
ActionCategory,
UserActionTypes,
UserActionWithResponse,
CommentUserAction,
Expand Down Expand Up @@ -618,9 +611,9 @@ export const allCasesSnake: CasesFindResponse = {

export const getUserAction = (
type: UserActionTypes,
action: UserAction,
action: ActionCategory,
overrides?: Record<string, unknown>
): CaseUserActions => {
): UserActionUI => {
const commonProperties = {
...basicAction,
id: `${type}-${action}`,
Expand Down Expand Up @@ -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<string, unknown>
): 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,
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 4 additions & 7 deletions x-pack/plugins/cases/public/containers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { NO_ASSIGNEES_FILTERING_KEYWORD } from '../../common/constants';
import type {
CasesConfigurationsResponse,
CasesConfigureResponse,
CaseUserActionsResponse,
UserActions,
CasePatchRequest,
CaseResolveResponse,
SingleCaseMetricsResponse,
Expand All @@ -30,7 +30,7 @@ import {
throwErrors,
CaseConfigurationsResponseRt,
CaseConfigureResponseRt,
CaseUserActionsResponseRt,
UserActionsRt,
CommentType,
CaseResolveResponseRt,
SingleCaseMetricsResponseRt,
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit f0b75b3

Please sign in to comment.