Skip to content

Commit

Permalink
[Cases] Renaming comment type (#156086)
Browse files Browse the repository at this point in the history
This PR:

- Renames `CommentResponseRt` to `CommentRt`
- Create `CommentUI` for the UI based on the snake to camel case version
of `Comment`
- Introduces a `AttachmentTransformedAttributes` type that should be
used when returning `CommentAttributes` from the service layer
  • Loading branch information
jonathan-buttner authored May 3, 2023
1 parent a3fad62 commit b80b4c2
Show file tree
Hide file tree
Showing 27 changed files with 147 additions and 138 deletions.
4 changes: 2 additions & 2 deletions x-pack/plugins/cases/common/api/cases/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as rt from 'io-ts';

import { NumberFromString } from '../saved_object';
import { UserRt } from '../user';
import { CommentResponseRt } from './comment';
import { CommentRt } from './comment';
import { CasesStatusResponseRt, CaseStatusRt } from './status';
import { CaseConnectorRt } from '../connectors/connector';
import { CaseAssigneesRt } from './assignee';
Expand Down Expand Up @@ -258,7 +258,7 @@ export const CaseRt = rt.intersection([
version: rt.string,
}),
rt.partial({
comments: rt.array(CommentResponseRt),
comments: rt.array(CommentRt),
}),
]);

Expand Down
18 changes: 8 additions & 10 deletions x-pack/plugins/cases/common/api/cases/comment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export const CommentRequestRt = rt.union([
PersistableStateAttachmentRt,
]);

export const CommentResponseRt = rt.intersection([
export const CommentRt = rt.intersection([
CommentAttributesRt,
rt.type({
id: rt.string,
Expand Down Expand Up @@ -233,8 +233,6 @@ export const CommentResponseTypePersistableStateRt = rt.intersection([
}),
]);

export const AllCommentsResponseRT = rt.array(CommentResponseRt);

export const CommentPatchRequestRt = rt.intersection([
/**
* Partial updates are not allowed.
Expand Down Expand Up @@ -266,14 +264,14 @@ export const CommentPatchAttributesRt = rt.intersection([
rt.partial(CommentAttributesBasicRt.props),
]);

export const CommentsResponseRt = rt.type({
comments: rt.array(CommentResponseRt),
export const CommentsFindResponseRt = rt.type({
comments: rt.array(CommentRt),
page: rt.number,
per_page: rt.number,
total: rt.number,
});

export const AllCommentsResponseRt = rt.array(CommentResponseRt);
export const CommentsRt = rt.array(CommentRt);

export const FindQueryParamsRt = rt.partial({
...SavedObjectFindOptionsRt.props,
Expand All @@ -286,7 +284,7 @@ export const BulkGetAttachmentsRequestRt = rt.type({
});

export const BulkGetAttachmentsResponseRt = rt.type({
attachments: AllCommentsResponseRt,
attachments: CommentsRt,
errors: rt.array(
rt.type({
error: rt.string,
Expand Down Expand Up @@ -314,7 +312,7 @@ export type CommentAttributesNoSO = rt.TypeOf<typeof CommentAttributesNoSORt>;
export type CommentAttributesWithoutRefs = rt.TypeOf<typeof CommentAttributesWithoutRefsRt>;
export type CommentRequest = rt.TypeOf<typeof CommentRequestRt>;
export type BulkCreateCommentRequest = rt.TypeOf<typeof BulkCreateCommentRequestRt>;
export type CommentResponse = rt.TypeOf<typeof CommentResponseRt>;
export type Comment = rt.TypeOf<typeof CommentRt>;
export type CommentResponseUserType = rt.TypeOf<typeof CommentResponseTypeUserRt>;
export type CommentResponseAlertsType = rt.TypeOf<typeof CommentResponseTypeAlertsRt>;
export type CommentResponseTypePersistableState = rt.TypeOf<
Expand All @@ -324,8 +322,8 @@ export type CommentResponseExternalReferenceType = rt.TypeOf<
typeof CommentResponseTypeExternalReferenceRt
>;
export type CommentResponseActionsType = rt.TypeOf<typeof CommentResponseTypeActionsRt>;
export type AllCommentsResponse = rt.TypeOf<typeof AllCommentsResponseRt>;
export type CommentsResponse = rt.TypeOf<typeof CommentsResponseRt>;
export type Comments = rt.TypeOf<typeof CommentsRt>;
export type CommentsFindResponse = rt.TypeOf<typeof CommentsFindResponseRt>;
export type CommentPatchRequest = rt.TypeOf<typeof CommentPatchRequestRt>;
export type CommentPatchAttributes = rt.TypeOf<typeof CommentPatchAttributesRt>;
export type CommentRequestUserType = rt.TypeOf<typeof ContextTypeUserRt>;
Expand Down
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 @@ -19,7 +19,7 @@ import type {
ActionConnector,
UserAction,
SingleCaseMetricsResponse,
CommentResponse,
Comment,
Case as CaseSnakeCase,
UserActionFindResponse,
FindTypeField as UserActionFindTypeField,
Expand Down Expand Up @@ -83,7 +83,7 @@ export type CaseViewRefreshPropInterface = null | {
refreshCase: () => Promise<void>;
};

export type Comment = SnakeToCamelCase<CommentResponse>;
export type CommentUI = SnakeToCamelCase<Comment>;
export type AlertComment = SnakeToCamelCase<CommentResponseAlertsType>;
export type ExternalReferenceComment = SnakeToCamelCase<CommentResponseExternalReferenceType>;
export type PersistableComment = SnakeToCamelCase<CommentResponseTypePersistableState>;
Expand All @@ -92,7 +92,7 @@ export type FindCaseUserActions = Omit<SnakeToCamelCase<UserActionFindResponse>,
userActions: UserActionUI[];
};
export type CaseUserActionsStats = SnakeToCamelCase<CaseUserActionStatsResponse>;
export type CaseUI = Omit<SnakeToCamelCase<CaseSnakeCase>, 'comments'> & { comments: Comment[] };
export type CaseUI = Omit<SnakeToCamelCase<CaseSnakeCase>, 'comments'> & { comments: CommentUI[] };
export type CasesUI = Omit<SnakeToCamelCase<CasesFindResponse>, 'cases'> & { cases: CaseUI[] };
export type CasesStatus = SnakeToCamelCase<CasesStatusResponse>;
export type CasesMetrics = SnakeToCamelCase<CasesMetricsResponse>;
Expand Down
14 changes: 7 additions & 7 deletions x-pack/plugins/cases/public/api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import type {
Case,
UserActions,
CommentRequest,
CommentResponse,
Comment,
CaseResolveResponse,
Cases,
} from '../../common/api';
import { isCommentUserAction } from '../../common/utils/user_actions';
import type { CasesUI, CaseUI, Comment, ResolvedCase } from '../containers/types';
import type { CasesUI, CaseUI, CommentUI, ResolvedCase } from '../containers/types';

export const convertArrayToCamelCase = (arrayOfSnakes: unknown[]): unknown[] =>
arrayOfSnakes.reduce((acc: unknown[], value) => {
Expand Down Expand Up @@ -65,11 +65,11 @@ export const convertCaseResolveToCamelCase = (res: CaseResolveResponse): Resolve
};
};

export const convertAttachmentsToCamelCase = (attachments: CommentResponse[]): Comment[] => {
export const convertAttachmentsToCamelCase = (attachments: Comment[]): CommentUI[] => {
return attachments.map((attachment) => convertAttachmentToCamelCase(attachment));
};

export const convertAttachmentToCamelCase = (attachment: CommentRequest): Comment => {
export const convertAttachmentToCamelCase = (attachment: CommentRequest): CommentUI => {
if (isCommentRequestTypeExternalReference(attachment)) {
return convertAttachmentToCamelExceptProperty(attachment, 'externalReferenceMetadata');
}
Expand All @@ -78,7 +78,7 @@ export const convertAttachmentToCamelCase = (attachment: CommentRequest): Commen
return convertAttachmentToCamelExceptProperty(attachment, 'persistableStateAttachmentState');
}

return convertToCamelCase<CommentRequest, Comment>(attachment);
return convertToCamelCase<CommentRequest, CommentUI>(attachment);
};

export const convertUserActionsToCamelCase = (userActions: UserActions) => {
Expand All @@ -102,15 +102,15 @@ export const convertUserActionsToCamelCase = (userActions: UserActions) => {
const convertAttachmentToCamelExceptProperty = (
attachment: CommentRequest,
key: string
): Comment => {
): CommentUI => {
const intactValue = get(attachment, key);
const attachmentWithoutIntactValue = omit(attachment, key);
const camelCaseAttachmentWithoutIntactValue = convertToCamelCase(attachmentWithoutIntactValue);

return {
...camelCaseAttachmentWithoutIntactValue,
[key]: intactValue,
} as Comment;
} as CommentUI;
};

export const convertAllCasesToCamel = (snakeCases: CasesFindResponse): CasesUI => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
*/

import { CommentType } from '../../../../common/api';
import type { Comment } from '../../../containers/types';
import type { CommentUI } from '../../../containers/types';

export const getManualAlertIds = (comments: Comment[]): string[] => {
const dedupeAlerts = comments.reduce((alertIds, comment: Comment) => {
export const getManualAlertIds = (comments: CommentUI[]): string[] => {
const dedupeAlerts = comments.reduce((alertIds, comment: CommentUI) => {
if (comment.type === CommentType.alert) {
const ids = Array.isArray(comment.alertId) ? comment.alertId : [comment.alertId];
ids.forEach((id) => alertIds.add(id));
Expand All @@ -20,8 +20,8 @@ export const getManualAlertIds = (comments: Comment[]): string[] => {
return Array.from(dedupeAlerts);
};

export const getRegistrationContextFromAlerts = (comments: Comment[]): string[] => {
const dedupeRegistrationContext = comments.reduce((registrationContexts, comment: Comment) => {
export const getRegistrationContextFromAlerts = (comments: CommentUI[]): string[] => {
const dedupeRegistrationContext = comments.reduce((registrationContexts, comment: CommentUI) => {
if (comment.type === CommentType.alert) {
const indices = Array.isArray(comment.index) ? comment.index : [comment.index];
indices.forEach((index) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { CommentUserAction } from '../../../../common/api';
import { Actions, CommentType } from '../../../../common/api';
import type { UserActionBuilder, UserActionBuilderArgs, UserActionResponse } from '../types';
import { createCommonUpdateUserActionBuilder } from '../common';
import type { Comment } from '../../../containers/types';
import type { CommentUI } from '../../../containers/types';
import * as i18n from './translations';
import { createUserAttachmentUserActionBuilder } from './user';
import { createAlertAttachmentUserActionBuilder } from './alert';
Expand Down Expand Up @@ -164,7 +164,7 @@ const getCreateCommentUserAction = ({
actionsNavigation,
}: {
userAction: UserActionResponse<CommentUserAction>;
comment: Comment;
comment: CommentUI;
} & Omit<
UserActionBuilderArgs,
'comments' | 'index' | 'handleOutlineComment' | 'currentUserProfile'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import type {
import { AttachmentActionType } from '../../../client/attachment_framework/types';
import { UserActionTimestamp } from '../timestamp';
import type { AttachmentTypeRegistry } from '../../../../common/registry';
import type { CommentResponse } from '../../../../common/api';
import type { Comment } from '../../../../common/api';
import type { UserActionBuilder, UserActionBuilderArgs } from '../types';
import type { SnakeToCamelCase } from '../../../../common/types';
import {
Expand Down Expand Up @@ -69,7 +69,7 @@ const getAttachmentRenderer = memoize(() => {
});

export const createRegisteredAttachmentUserActionBuilder = <
C extends CommentResponse,
C extends Comment,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
R extends AttachmentTypeRegistry<AttachmentType<any>>
>({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

import { CommentType } from '../../../common/api';
import { SECURITY_SOLUTION_OWNER } from '../../../common/constants';
import type { Comment } from '../../containers/types';
import type { CommentUI } from '../../containers/types';
import { isUserActionTypeSupported, getManualAlertIdsWithNoRuleId } from './helpers';

const comments: Comment[] = [
const comments: CommentUI[] = [
{
type: CommentType.alert,
alertId: 'alert-id-1',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
import { isEmpty } from 'lodash';

import { CommentType } from '../../../common/api';
import type { Comment } from '../../containers/types';
import type { CommentUI } from '../../containers/types';
import { SUPPORTED_ACTION_TYPES } from './constants';
import type { SupportedUserActionTypes } from './types';

export const isUserActionTypeSupported = (type: string): type is SupportedUserActionTypes =>
SUPPORTED_ACTION_TYPES.includes(type as SupportedUserActionTypes);

export const getManualAlertIdsWithNoRuleId = (comments: Comment[]): string[] => {
const dedupeAlerts = comments.reduce((alertIds, comment: Comment) => {
export const getManualAlertIdsWithNoRuleId = (comments: CommentUI[]): string[] => {
const dedupeAlerts = comments.reduce((alertIds, comment: CommentUI) => {
if (comment.type === CommentType.alert && isEmpty(comment.rule.id)) {
const ids = Array.isArray(comment.alertId) ? comment.alertId : [comment.alertId];
ids.forEach((id) => alertIds.add(id));
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 @@ -13,7 +13,7 @@ import type {
CaseUI,
CaseConnectors,
UserActionUI,
Comment,
CommentUI,
UseFetchAlertData,
CaseUserActionsStats,
} from '../../containers/types';
Expand Down Expand Up @@ -55,7 +55,7 @@ export interface UserActionBuilderArgs {
persistableStateAttachmentTypeRegistry: PersistableStateAttachmentTypeRegistry;
caseConnectors: CaseConnectors;
userAction: UserActionUI;
comments: Comment[];
comments: CommentUI[];
index: number;
commentRefs: React.MutableRefObject<
Record<string, AddCommentRefObject | UserActionMarkdownRefObject | null | undefined>
Expand Down
18 changes: 9 additions & 9 deletions x-pack/plugins/cases/public/containers/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
import type { FileJSON } from '@kbn/shared-ux-file-types';

import type { ActionLicense, CasesUI, CaseUI, CasesStatus, UserActionUI, Comment } from './types';
import type { ActionLicense, CasesUI, CaseUI, CasesStatus, UserActionUI, CommentUI } from './types';

import type {
ResolvedCase,
Expand All @@ -28,7 +28,7 @@ import type {
CasesStatusResponse,
UserAction,
UserActions,
CommentResponse,
Comment,
ActionCategory,
UserActionTypes,
UserActionWithResponse,
Expand Down Expand Up @@ -73,7 +73,7 @@ export const elasticUser = {

export const tags: string[] = ['coke', 'pepsi'];

export const basicComment: Comment = {
export const basicComment: CommentUI = {
comment: 'Solve this fast!',
type: CommentType.user,
id: basicCommentId,
Expand Down Expand Up @@ -125,7 +125,7 @@ export const alertCommentWithIndices: AlertComment = {
version: 'WzQ3LDFc',
};

export const hostIsolationComment = (overrides?: Record<string, unknown>): Comment => {
export const hostIsolationComment = (overrides?: Record<string, unknown>): CommentUI => {
return {
type: CommentType.actions,
comment: 'I just isolated the host!',
Expand All @@ -151,7 +151,7 @@ export const hostIsolationComment = (overrides?: Record<string, unknown>): Comme
};
};

export const hostReleaseComment: () => Comment = () => {
export const hostReleaseComment: () => CommentUI = () => {
return {
type: CommentType.actions,
comment: 'I just released the host!',
Expand Down Expand Up @@ -363,7 +363,7 @@ export const basicCasePost: CaseUI = {
updatedBy: null,
};

export const basicCommentPatch: Comment = {
export const basicCommentPatch: CommentUI = {
...basicComment,
updatedAt: basicUpdatedAt,
updatedBy: {
Expand Down Expand Up @@ -469,7 +469,7 @@ export const elasticUserSnake = {
email: '[email protected]',
};

export const basicCommentSnake: CommentResponse = {
export const basicCommentSnake: Comment = {
comment: 'Solve this fast!',
type: CommentType.user,
id: basicCommentId,
Expand All @@ -483,7 +483,7 @@ export const basicCommentSnake: CommentResponse = {
version: 'WzQ3LDFc',
};

export const externalReferenceAttachmentSnake: CommentResponse = {
export const externalReferenceAttachmentSnake: Comment = {
type: CommentType.externalReference,
id: 'external-reference-comment-id',
externalReferenceId: 'my-id',
Expand All @@ -500,7 +500,7 @@ export const externalReferenceAttachmentSnake: CommentResponse = {
version: 'WzQ3LDFc',
};

export const persistableStateAttachmentSnake: CommentResponse = {
export const persistableStateAttachmentSnake: Comment = {
type: CommentType.persistableState,
id: 'persistable-state-comment-id',
persistableStateAttachmentState: { test_foo: 'foo' },
Expand Down
12 changes: 6 additions & 6 deletions x-pack/plugins/cases/server/client/attachments/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

import type {
AlertResponse,
AllCommentsResponse,
Comments,
BulkGetAttachmentsResponse,
Case,
CommentResponse,
CommentsResponse,
Comment,
CommentsFindResponse,
} from '../../../common/api';
import type { CasesClient } from '../client';

Expand Down Expand Up @@ -60,19 +60,19 @@ export interface AttachmentsSubClient {
/**
* Retrieves all comments matching the search criteria.
*/
find(findArgs: FindArgs): Promise<CommentsResponse>;
find(findArgs: FindArgs): Promise<CommentsFindResponse>;
/**
* Retrieves all alerts attach to a case given a single case ID
*/
getAllAlertsAttachToCase(params: GetAllAlertsAttachToCase): Promise<AlertResponse>;
/**
* Gets all attachments for a single case.
*/
getAll(getAllArgs: GetAllArgs): Promise<AllCommentsResponse>;
getAll(getAllArgs: GetAllArgs): Promise<Comments>;
/**
* Retrieves a single attachment for a case.
*/
get(getArgs: GetArgs): Promise<CommentResponse>;
get(getArgs: GetArgs): Promise<Comment>;
/**
* Updates a specific attachment.
*
Expand Down
Loading

0 comments on commit b80b4c2

Please sign in to comment.