From de5a659fccefe26beab750f4bbdc0307b27db140 Mon Sep 17 00:00:00 2001 From: Jonathan Buttner Date: Thu, 27 Apr 2023 15:52:51 -0400 Subject: [PATCH 1/5] Renaming comment type --- x-pack/plugins/cases/common/api/cases/case.ts | 4 ++-- .../cases/common/api/cases/comment/index.ts | 14 +++++++------- x-pack/plugins/cases/common/ui/types.ts | 6 +++--- x-pack/plugins/cases/public/api/utils.ts | 14 +++++++------- .../components/case_view/components/helpers.ts | 10 +++++----- .../user_actions/comment/comment.tsx | 4 ++-- .../comment/registered_attachments.tsx | 4 ++-- .../components/user_actions/helpers.test.ts | 4 ++-- .../public/components/user_actions/helpers.ts | 6 +++--- .../public/components/user_actions/types.ts | 4 ++-- x-pack/plugins/cases/public/containers/mock.ts | 18 +++++++++--------- .../cases/server/client/attachments/client.ts | 8 ++++---- .../cases/server/client/attachments/get.ts | 14 +++++++------- .../plugins/cases/server/client/cases/mock.ts | 16 ++++++++-------- .../plugins/cases/server/client/cases/utils.ts | 4 ++-- .../cases/server/client/typedoc_interfaces.ts | 4 ++-- x-pack/plugins/cases/server/common/utils.ts | 12 ++++++------ .../common/lib/api/attachments.ts | 8 ++++---- .../common/lib/api/omit.ts | 8 ++++---- .../cases_api_integration/common/lib/mock.ts | 4 ++-- .../tests/common/cases/import_export.ts | 4 ++-- .../tests/common/comments/find_comments.ts | 6 +++--- 22 files changed, 88 insertions(+), 88 deletions(-) diff --git a/x-pack/plugins/cases/common/api/cases/case.ts b/x-pack/plugins/cases/common/api/cases/case.ts index 4eaf633fb98c7..648e29ec3d76a 100644 --- a/x-pack/plugins/cases/common/api/cases/case.ts +++ b/x-pack/plugins/cases/common/api/cases/case.ts @@ -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'; @@ -258,7 +258,7 @@ export const CaseResponseRt = rt.intersection([ version: rt.string, }), rt.partial({ - comments: rt.array(CommentResponseRt), + comments: rt.array(CommentRt), }), ]); diff --git a/x-pack/plugins/cases/common/api/cases/comment/index.ts b/x-pack/plugins/cases/common/api/cases/comment/index.ts index 7729673410a71..3bc9f69e84ba2 100644 --- a/x-pack/plugins/cases/common/api/cases/comment/index.ts +++ b/x-pack/plugins/cases/common/api/cases/comment/index.ts @@ -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, @@ -233,7 +233,7 @@ export const CommentResponseTypePersistableStateRt = rt.intersection([ }), ]); -export const AllCommentsResponseRT = rt.array(CommentResponseRt); +export const AllCommentsResponseRT = rt.array(CommentRt); export const CommentPatchRequestRt = rt.intersection([ /** @@ -266,14 +266,14 @@ export const CommentPatchAttributesRt = rt.intersection([ rt.partial(CommentAttributesBasicRt.props), ]); -export const CommentsResponseRt = rt.type({ - comments: rt.array(CommentResponseRt), +export const CommentsRt = rt.type({ + comments: rt.array(CommentRt), page: rt.number, per_page: rt.number, total: rt.number, }); -export const AllCommentsResponseRt = rt.array(CommentResponseRt); +export const AllCommentsResponseRt = rt.array(CommentRt); export const FindQueryParamsRt = rt.partial({ ...SavedObjectFindOptionsRt.props, @@ -314,7 +314,7 @@ export type CommentAttributesNoSO = rt.TypeOf; export type CommentAttributesWithoutRefs = rt.TypeOf; export type CommentRequest = rt.TypeOf; export type BulkCreateCommentRequest = rt.TypeOf; -export type CommentResponse = rt.TypeOf; +export type Comment = rt.TypeOf; export type CommentResponseUserType = rt.TypeOf; export type CommentResponseAlertsType = rt.TypeOf; export type CommentResponseTypePersistableState = rt.TypeOf< @@ -325,7 +325,7 @@ export type CommentResponseExternalReferenceType = rt.TypeOf< >; export type CommentResponseActionsType = rt.TypeOf; export type AllCommentsResponse = rt.TypeOf; -export type CommentsResponse = rt.TypeOf; +export type Comments = rt.TypeOf; export type CommentPatchRequest = rt.TypeOf; export type CommentPatchAttributes = rt.TypeOf; export type CommentRequestUserType = rt.TypeOf; diff --git a/x-pack/plugins/cases/common/ui/types.ts b/x-pack/plugins/cases/common/ui/types.ts index e9bfcd0682f5b..76e6f2adeb546 100644 --- a/x-pack/plugins/cases/common/ui/types.ts +++ b/x-pack/plugins/cases/common/ui/types.ts @@ -19,7 +19,7 @@ import type { ActionConnector, CaseUserActionResponse, SingleCaseMetricsResponse, - CommentResponse, + Comment, CaseResponse, UserActionFindResponse, FindTypeField as UserActionFindTypeField, @@ -83,7 +83,7 @@ export type CaseViewRefreshPropInterface = null | { refreshCase: () => Promise; }; -export type Comment = SnakeToCamelCase; +export type CommentUI = SnakeToCamelCase; export type AlertComment = SnakeToCamelCase; export type ExternalReferenceComment = SnakeToCamelCase; export type PersistableComment = SnakeToCamelCase; @@ -92,7 +92,7 @@ export type FindCaseUserActions = Omit, userActions: CaseUserActions[]; }; export type CaseUserActionsStats = SnakeToCamelCase; -export type Case = Omit, 'comments'> & { comments: Comment[] }; +export type Case = Omit, 'comments'> & { comments: CommentUI[] }; export type Cases = Omit, 'cases'> & { cases: Case[] }; export type CasesStatus = SnakeToCamelCase; export type CasesMetrics = SnakeToCamelCase; diff --git a/x-pack/plugins/cases/public/api/utils.ts b/x-pack/plugins/cases/public/api/utils.ts index f7ce3bf0817d2..fd5d28139a4b4 100644 --- a/x-pack/plugins/cases/public/api/utils.ts +++ b/x-pack/plugins/cases/public/api/utils.ts @@ -16,12 +16,12 @@ import type { CaseResponse, CaseUserActionsResponse, CommentRequest, - CommentResponse, + Comment, CaseResolveResponse, CasesResponse, } from '../../common/api'; import { isCommentUserAction } from '../../common/utils/user_actions'; -import type { Cases, Case, Comment, ResolvedCase } from '../containers/types'; +import type { Cases, Case, CommentUI, ResolvedCase } from '../containers/types'; export const convertArrayToCamelCase = (arrayOfSnakes: unknown[]): unknown[] => arrayOfSnakes.reduce((acc: unknown[], value) => { @@ -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'); } @@ -78,7 +78,7 @@ export const convertAttachmentToCamelCase = (attachment: CommentRequest): Commen return convertAttachmentToCamelExceptProperty(attachment, 'persistableStateAttachmentState'); } - return convertToCamelCase(attachment); + return convertToCamelCase(attachment); }; export const convertUserActionsToCamelCase = (userActions: CaseUserActionsResponse) => { @@ -102,7 +102,7 @@ export const convertUserActionsToCamelCase = (userActions: CaseUserActionsRespon const convertAttachmentToCamelExceptProperty = ( attachment: CommentRequest, key: string -): Comment => { +): CommentUI => { const intactValue = get(attachment, key); const attachmentWithoutIntactValue = omit(attachment, key); const camelCaseAttachmentWithoutIntactValue = convertToCamelCase(attachmentWithoutIntactValue); @@ -110,7 +110,7 @@ const convertAttachmentToCamelExceptProperty = ( return { ...camelCaseAttachmentWithoutIntactValue, [key]: intactValue, - } as Comment; + } as CommentUI; }; export const convertAllCasesToCamel = (snakeCases: CasesFindResponse): Cases => ({ diff --git a/x-pack/plugins/cases/public/components/case_view/components/helpers.ts b/x-pack/plugins/cases/public/components/case_view/components/helpers.ts index 0fb247dda5282..b3ddc403cc661 100644 --- a/x-pack/plugins/cases/public/components/case_view/components/helpers.ts +++ b/x-pack/plugins/cases/public/components/case_view/components/helpers.ts @@ -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)); @@ -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) => { diff --git a/x-pack/plugins/cases/public/components/user_actions/comment/comment.tsx b/x-pack/plugins/cases/public/components/user_actions/comment/comment.tsx index 434a95cc69b1c..b4fbc12ea48e4 100644 --- a/x-pack/plugins/cases/public/components/user_actions/comment/comment.tsx +++ b/x-pack/plugins/cases/public/components/user_actions/comment/comment.tsx @@ -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'; @@ -164,7 +164,7 @@ const getCreateCommentUserAction = ({ actionsNavigation, }: { userAction: UserActionResponse; - comment: Comment; + comment: CommentUI; } & Omit< UserActionBuilderArgs, 'comments' | 'index' | 'handleOutlineComment' | 'currentUserProfile' diff --git a/x-pack/plugins/cases/public/components/user_actions/comment/registered_attachments.tsx b/x-pack/plugins/cases/public/components/user_actions/comment/registered_attachments.tsx index 9142a71ed7184..9f79e966a0833 100644 --- a/x-pack/plugins/cases/public/components/user_actions/comment/registered_attachments.tsx +++ b/x-pack/plugins/cases/public/components/user_actions/comment/registered_attachments.tsx @@ -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 { @@ -69,7 +69,7 @@ const getAttachmentRenderer = memoize((attachmentViewObject: AttachmentViewObjec }); export const createRegisteredAttachmentUserActionBuilder = < - C extends CommentResponse, + C extends Comment, // eslint-disable-next-line @typescript-eslint/no-explicit-any R extends AttachmentTypeRegistry> >({ diff --git a/x-pack/plugins/cases/public/components/user_actions/helpers.test.ts b/x-pack/plugins/cases/public/components/user_actions/helpers.test.ts index bd1873a591bee..0e84b9463694b 100644 --- a/x-pack/plugins/cases/public/components/user_actions/helpers.test.ts +++ b/x-pack/plugins/cases/public/components/user_actions/helpers.test.ts @@ -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', diff --git a/x-pack/plugins/cases/public/components/user_actions/helpers.ts b/x-pack/plugins/cases/public/components/user_actions/helpers.ts index fe9e5b2bb5cff..a3bd67bd6e655 100644 --- a/x-pack/plugins/cases/public/components/user_actions/helpers.ts +++ b/x-pack/plugins/cases/public/components/user_actions/helpers.ts @@ -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)); 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 5146a97aba65b..4396aeea50f02 100644 --- a/x-pack/plugins/cases/public/components/user_actions/types.ts +++ b/x-pack/plugins/cases/public/components/user_actions/types.ts @@ -13,7 +13,7 @@ import type { Case, CaseConnectors, CaseUserActions, - Comment, + CommentUI, UseFetchAlertData, CaseUserActionsStats, } from '../../containers/types'; @@ -55,7 +55,7 @@ export interface UserActionBuilderArgs { persistableStateAttachmentTypeRegistry: PersistableStateAttachmentTypeRegistry; caseConnectors: CaseConnectors; userAction: CaseUserActions; - comments: Comment[]; + comments: CommentUI[]; index: number; commentRefs: React.MutableRefObject< Record diff --git a/x-pack/plugins/cases/public/containers/mock.ts b/x-pack/plugins/cases/public/containers/mock.ts index a4192513d7bec..37f1cbd5744cf 100644 --- a/x-pack/plugins/cases/public/containers/mock.ts +++ b/x-pack/plugins/cases/public/containers/mock.ts @@ -6,7 +6,7 @@ */ import type { FileJSON } from '@kbn/shared-ux-file-types'; -import type { ActionLicense, Cases, Case, CasesStatus, CaseUserActions, Comment } from './types'; +import type { ActionLicense, Cases, Case, CasesStatus, CaseUserActions, CommentUI } from './types'; import type { ResolvedCase, @@ -28,7 +28,7 @@ import type { CasesStatusResponse, CaseUserActionResponse, CaseUserActionsResponse, - CommentResponse, + Comment, UserAction, UserActionTypes, UserActionWithResponse, @@ -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, @@ -125,7 +125,7 @@ export const alertCommentWithIndices: AlertComment = { version: 'WzQ3LDFc', }; -export const hostIsolationComment = (overrides?: Record): Comment => { +export const hostIsolationComment = (overrides?: Record): CommentUI => { return { type: CommentType.actions, comment: 'I just isolated the host!', @@ -151,7 +151,7 @@ export const hostIsolationComment = (overrides?: Record): Comme }; }; -export const hostReleaseComment: () => Comment = () => { +export const hostReleaseComment: () => CommentUI = () => { return { type: CommentType.actions, comment: 'I just released the host!', @@ -363,7 +363,7 @@ export const basicCasePost: Case = { updatedBy: null, }; -export const basicCommentPatch: Comment = { +export const basicCommentPatch: CommentUI = { ...basicComment, updatedAt: basicUpdatedAt, updatedBy: { @@ -469,7 +469,7 @@ export const elasticUserSnake = { email: 'leslie.knope@elastic.co', }; -export const basicCommentSnake: CommentResponse = { +export const basicCommentSnake: Comment = { comment: 'Solve this fast!', type: CommentType.user, id: basicCommentId, @@ -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', @@ -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' }, diff --git a/x-pack/plugins/cases/server/client/attachments/client.ts b/x-pack/plugins/cases/server/client/attachments/client.ts index 5b204a21929c2..b45806ee9f58a 100644 --- a/x-pack/plugins/cases/server/client/attachments/client.ts +++ b/x-pack/plugins/cases/server/client/attachments/client.ts @@ -10,8 +10,8 @@ import type { AllCommentsResponse, BulkGetAttachmentsResponse, CaseResponse, - CommentResponse, - CommentsResponse, + Comment, + Comments, } from '../../../common/api'; import type { CasesClient } from '../client'; @@ -60,7 +60,7 @@ export interface AttachmentsSubClient { /** * Retrieves all comments matching the search criteria. */ - find(findArgs: FindArgs): Promise; + find(findArgs: FindArgs): Promise; /** * Retrieves all alerts attach to a case given a single case ID */ @@ -72,7 +72,7 @@ export interface AttachmentsSubClient { /** * Retrieves a single attachment for a case. */ - get(getArgs: GetArgs): Promise; + get(getArgs: GetArgs): Promise; /** * Updates a specific attachment. * diff --git a/x-pack/plugins/cases/server/client/attachments/get.ts b/x-pack/plugins/cases/server/client/attachments/get.ts index 91160e857c5cf..dc885f37848ea 100644 --- a/x-pack/plugins/cases/server/client/attachments/get.ts +++ b/x-pack/plugins/cases/server/client/attachments/get.ts @@ -10,10 +10,10 @@ import type { AlertResponse, AllCommentsResponse, AttributesTypeAlerts, - CommentResponse, - CommentsResponse, + Comment, + Comments, } from '../../../common/api'; -import { AllCommentsResponseRt, CommentResponseRt, CommentsResponseRt } from '../../../common/api'; +import { AllCommentsResponseRt, CommentRt, CommentsRt } from '../../../common/api'; import { defaultSortField, transformComments, @@ -100,7 +100,7 @@ export const getAllAlertsAttachToCase = async ( export async function find( { caseID, queryParams }: FindArgs, clientArgs: CasesClientArgs -): Promise { +): Promise { const { unsecuredSavedObjectsClient, services: { caseService }, @@ -159,7 +159,7 @@ export async function find( })) ); - return CommentsResponseRt.encode(transformComments(theComments)); + return CommentsRt.encode(transformComments(theComments)); } catch (error) { throw createCaseError({ message: `Failed to find comments case id: ${caseID}: ${error}`, @@ -175,7 +175,7 @@ export async function find( export async function get( { attachmentID, caseID }: GetArgs, clientArgs: CasesClientArgs -): Promise { +): Promise { const { services: { attachmentService }, logger, @@ -192,7 +192,7 @@ export async function get( operation: Operations.getComment, }); - return CommentResponseRt.encode(flattenCommentSavedObject(comment)); + return CommentRt.encode(flattenCommentSavedObject(comment)); } catch (error) { throw createCaseError({ message: `Failed to get comment case id: ${caseID} attachment id: ${attachmentID}: ${error}`, diff --git a/x-pack/plugins/cases/server/client/cases/mock.ts b/x-pack/plugins/cases/server/client/cases/mock.ts index 48ece730f3ead..527c517e4a79e 100644 --- a/x-pack/plugins/cases/server/client/cases/mock.ts +++ b/x-pack/plugins/cases/server/client/cases/mock.ts @@ -6,7 +6,7 @@ */ import type { - CommentResponse, + Comment, CommentResponseAlertsType, ConnectorMappingsAttributes, CaseUserActionsDeprecatedResponse, @@ -31,7 +31,7 @@ const entity = { updatedBy: null, }; -export const comment: CommentResponse = { +export const comment: Comment = { id: 'comment-user-1', comment: 'Wow, good luck catching that bad meanie!', type: CommentType.user as const, @@ -53,7 +53,7 @@ export const comment: CommentResponse = { version: 'WzEsMV0=', }; -export const isolateCommentActions: CommentResponse = { +export const isolateCommentActions: Comment = { id: 'mock-action-comment-1', comment: 'Isolating this for investigation', type: CommentType.actions as const, @@ -84,7 +84,7 @@ export const isolateCommentActions: CommentResponse = { version: 'WzEsMV0=', }; -export const releaseCommentActions: CommentResponse = { +export const releaseCommentActions: Comment = { id: 'mock-action-comment-2', comment: 'Releasing this for investigation', type: CommentType.actions as const, @@ -115,7 +115,7 @@ export const releaseCommentActions: CommentResponse = { version: 'WzEsMV0=', }; -export const isolateCommentActionsMultipleTargets: CommentResponse = { +export const isolateCommentActionsMultipleTargets: Comment = { id: 'mock-action-comment-3', comment: 'Isolating this for investigation', type: CommentType.actions as const, @@ -150,7 +150,7 @@ export const isolateCommentActionsMultipleTargets: CommentResponse = { version: 'WzEsMV0=', }; -export const commentAlert: CommentResponse = { +export const commentAlert: Comment = { id: 'comment-alert-1', alertId: 'alert-id-1', index: 'alert-index-1', @@ -186,7 +186,7 @@ export const commentAlertMultipleIds: CommentResponseAlertsType = { owner: SECURITY_SOLUTION_OWNER, }; -export const commentExternalReference: CommentResponse = { +export const commentExternalReference: Comment = { id: 'comment-external-reference-1', type: CommentType.externalReference as const, externalReferenceId: 'my-id', @@ -213,7 +213,7 @@ export const commentExternalReference: CommentResponse = { version: 'WzEsMV0=', }; -export const commentPersistableState: CommentResponse = { +export const commentPersistableState: Comment = { id: 'comment-persistable-state-1', type: CommentType.persistableState, persistableStateAttachmentTypeId: '.test', diff --git a/x-pack/plugins/cases/server/client/cases/utils.ts b/x-pack/plugins/cases/server/client/cases/utils.ts index 1288c2ecc48d7..069695587474c 100644 --- a/x-pack/plugins/cases/server/client/cases/utils.ts +++ b/x-pack/plugins/cases/server/client/cases/utils.ts @@ -16,7 +16,7 @@ import type { ActionConnector, CaseFullExternalService, CaseResponse, - CommentResponse, + Comment, User, CaseAttributes, CaseAssignees, @@ -77,7 +77,7 @@ export const getLatestPushInfo = ( return null; }; -const getCommentContent = (comment: CommentResponse): string => { +const getCommentContent = (comment: Comment): string => { if (comment.type === CommentType.user) { return comment.comment; } else if (comment.type === CommentType.alert) { diff --git a/x-pack/plugins/cases/server/client/typedoc_interfaces.ts b/x-pack/plugins/cases/server/client/typedoc_interfaces.ts index 7234efdf5d355..a1aa86c8046a0 100644 --- a/x-pack/plugins/cases/server/client/typedoc_interfaces.ts +++ b/x-pack/plugins/cases/server/client/typedoc_interfaces.ts @@ -26,7 +26,7 @@ import type { CasesPatchRequest, CasesResponse, CaseUserActionsResponse, - CommentsResponse, + Comments, CasesBulkGetResponse, } from '../../common/api'; @@ -48,7 +48,7 @@ export interface ICasesConfigureResponse extends CasesConfigureResponse {} export interface ICasesConfigureRequest extends CasesConfigureRequest {} export interface ICasesConfigurePatch extends CasesConfigurePatch {} -export interface ICommentsResponse extends CommentsResponse {} +export interface ICommentsResponse extends Comments {} export interface IAllCommentsResponse extends AllCommentsResponse {} export interface ICaseUserActionsResponse extends CaseUserActionsResponse {} diff --git a/x-pack/plugins/cases/server/common/utils.ts b/x-pack/plugins/cases/server/common/utils.ts index 8949e01aceca7..47a546ab9374a 100644 --- a/x-pack/plugins/cases/server/common/utils.ts +++ b/x-pack/plugins/cases/server/common/utils.ts @@ -37,8 +37,8 @@ import type { CommentRequestAlertType, CommentRequestExternalReferenceSOType, CommentRequestUserType, - CommentResponse, - CommentsResponse, + Comment, + Comments, User, } from '../../common/api'; import { @@ -135,7 +135,7 @@ export const flattenCaseSavedObject = ({ export const transformComments = ( comments: SavedObjectsFindResponse -): CommentsResponse => ({ +): Comments => ({ page: comments.page, per_page: comments.per_page, total: comments.total, @@ -144,14 +144,14 @@ export const transformComments = ( export const flattenCommentSavedObjects = ( savedObjects: Array> -): CommentResponse[] => - savedObjects.reduce((acc: CommentResponse[], savedObject: SavedObject) => { +): Comment[] => + savedObjects.reduce((acc: Comment[], savedObject: SavedObject) => { return [...acc, flattenCommentSavedObject(savedObject)]; }, []); export const flattenCommentSavedObject = ( savedObject: SavedObject -): CommentResponse => ({ +): Comment => ({ id: savedObject.id, version: savedObject.version ?? '0', ...savedObject.attributes, diff --git a/x-pack/test/cases_api_integration/common/lib/api/attachments.ts b/x-pack/test/cases_api_integration/common/lib/api/attachments.ts index f9e8f2658db81..0ead00d99ad10 100644 --- a/x-pack/test/cases_api_integration/common/lib/api/attachments.ts +++ b/x-pack/test/cases_api_integration/common/lib/api/attachments.ts @@ -14,8 +14,8 @@ import { CaseResponse, CommentPatchRequest, CommentRequest, - CommentResponse, - CommentsResponse, + Comment, + Comments, CommentType, getCaseFindAttachmentsUrl, getCasesDeleteFileAttachmentsUrl, @@ -224,7 +224,7 @@ export const getComment = async ({ commentId: string; expectedHttpCode?: number; auth?: { user: User; space: string | null }; -}): Promise => { +}): Promise => { const { body: comment } = await supertest .get(`${getSpaceUrlPrefix(auth.space)}${CASES_URL}/${caseId}/comments/${commentId}`) .auth(auth.user.username, auth.user.password) @@ -295,7 +295,7 @@ export const findAttachments = async ({ query?: Record; expectedHttpCode?: number; auth?: { user: User; space: string | null }; -}): Promise => { +}): Promise => { const { body } = await supertest .get(`${getSpaceUrlPrefix(auth.space)}${getCaseFindAttachmentsUrl(caseId)}`) .set('kbn-xsrf', 'true') diff --git a/x-pack/test/cases_api_integration/common/lib/api/omit.ts b/x-pack/test/cases_api_integration/common/lib/api/omit.ts index 0b069f409157b..b543ebff837fb 100644 --- a/x-pack/test/cases_api_integration/common/lib/api/omit.ts +++ b/x-pack/test/cases_api_integration/common/lib/api/omit.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { CaseResponse, CommentResponse } from '@kbn/cases-plugin/common/api'; +import { CaseResponse, Comment } from '@kbn/cases-plugin/common/api'; import { omit } from 'lodash'; interface CommonSavedObjectAttributes { @@ -43,9 +43,9 @@ export const removeServerGeneratedPropertiesFromCase = ( }; export const removeServerGeneratedPropertiesFromComments = ( - comments: CommentResponse[] | undefined -): Array> | undefined => { + comments: Comment[] | undefined +): Array> | undefined => { return comments?.map((comment) => { - return removeServerGeneratedPropertiesFromSavedObject(comment, []); + return removeServerGeneratedPropertiesFromSavedObject(comment, []); }); }; diff --git a/x-pack/test/cases_api_integration/common/lib/mock.ts b/x-pack/test/cases_api_integration/common/lib/mock.ts index 1ea6c1b1e1359..eb3ebb805af9b 100644 --- a/x-pack/test/cases_api_integration/common/lib/mock.ts +++ b/x-pack/test/cases_api_integration/common/lib/mock.ts @@ -9,7 +9,7 @@ import { CasePostRequest, CaseResponse, CasesFindResponse, - CommentResponse, + Comment, ConnectorTypes, CommentRequestUserType, CommentRequestAlertType, @@ -185,7 +185,7 @@ export const commentsResp = ({ comments, }: { comments: CommentRequestWithID[]; -}): Array> => { +}): Array> => { return comments.map(({ comment, id }) => { const baseFields = { id, diff --git a/x-pack/test/cases_api_integration/security_and_spaces/tests/common/cases/import_export.ts b/x-pack/test/cases_api_integration/security_and_spaces/tests/common/cases/import_export.ts index 1a3dd08e369d0..1f21ca98c08d0 100644 --- a/x-pack/test/cases_api_integration/security_and_spaces/tests/common/cases/import_export.ts +++ b/x-pack/test/cases_api_integration/security_and_spaces/tests/common/cases/import_export.ts @@ -17,7 +17,7 @@ import { } from '@kbn/cases-plugin/common/constants'; import { AttributesTypeUser, - CommentsResponse, + Comments, CaseAttributes, CasePostRequest, PushedUserAction, @@ -99,7 +99,7 @@ export default ({ getService }: FtrProviderContext): void => { expect(findResponse.cases[0].title).to.eql('A case to export'); expect(findResponse.cases[0].description).to.eql('a description'); - const { body: commentsResponse }: { body: CommentsResponse } = await supertestService + const { body: commentsResponse }: { body: Comments } = await supertestService .get(`${CASES_URL}/${findResponse.cases[0].id}/comments/_find`) .send() .expect(200); diff --git a/x-pack/test/cases_api_integration/security_and_spaces/tests/common/comments/find_comments.ts b/x-pack/test/cases_api_integration/security_and_spaces/tests/common/comments/find_comments.ts index 7132972fb5b96..6ae412aded18e 100644 --- a/x-pack/test/cases_api_integration/security_and_spaces/tests/common/comments/find_comments.ts +++ b/x-pack/test/cases_api_integration/security_and_spaces/tests/common/comments/find_comments.ts @@ -8,7 +8,7 @@ import expect from '@kbn/expect'; import { CASES_URL } from '@kbn/cases-plugin/common/constants'; -import { CommentsResponse } from '@kbn/cases-plugin/common/api'; +import { Comments } from '@kbn/cases-plugin/common/api'; import { FtrProviderContext } from '../../../../common/ftr_provider_context'; import { getPostCaseRequest, @@ -221,7 +221,7 @@ export default ({ getService }: FtrProviderContext): void => { caseID: obsCase.id, }, ]) { - const { body: caseComments }: { body: CommentsResponse } = await supertestWithoutAuth + const { body: caseComments }: { body: Comments } = await supertestWithoutAuth .get(`${getSpaceUrlPrefix(space1)}${CASES_URL}/${scenario.caseID}/comments/_find`) .auth(scenario.user.username, scenario.user.password) .expect(200); @@ -279,7 +279,7 @@ export default ({ getService }: FtrProviderContext): void => { caseId: obsCase.id, }); - const { body: res }: { body: CommentsResponse } = await supertestWithoutAuth + const { body: res }: { body: Comments } = await supertestWithoutAuth .get( `${getSpaceUrlPrefix('space1')}${CASES_URL}/${ obsCase.id From d271d41816d1549b0db8cbeba721d8893143c04e Mon Sep 17 00:00:00 2001 From: Jonathan Buttner Date: Mon, 1 May 2023 15:05:03 -0400 Subject: [PATCH 2/5] Using transformed attributes type --- .../cases/server/common/types/attachments.ts | 3 +++ .../server/services/attachments/index.ts | 18 +++++++++++------- .../services/attachments/operations/get.ts | 19 +++++++++---------- .../cases/server/services/cases/index.ts | 7 ++++--- .../cases/server/services/so_references.ts | 17 +++++++++-------- 5 files changed, 36 insertions(+), 28 deletions(-) diff --git a/x-pack/plugins/cases/server/common/types/attachments.ts b/x-pack/plugins/cases/server/common/types/attachments.ts index 55b9990c5fb12..02c84925ef531 100644 --- a/x-pack/plugins/cases/server/common/types/attachments.ts +++ b/x-pack/plugins/cases/server/common/types/attachments.ts @@ -6,6 +6,7 @@ */ import type { JsonValue } from '@kbn/utility-types'; +import type { CommentAttributes } from '../../../common/api'; import type { User } from './user'; interface AttachmentCommonPersistedAttributes { @@ -46,3 +47,5 @@ export interface AttachmentRequestAttributes { export type AttachmentPersistedAttributes = AttachmentRequestAttributes & AttachmentCommonPersistedAttributes; + +export type AttachmentTransformedAttributes = CommentAttributes; diff --git a/x-pack/plugins/cases/server/services/attachments/index.ts b/x-pack/plugins/cases/server/services/attachments/index.ts index e76b93ce0e5b9..2e8788a4021ba 100644 --- a/x-pack/plugins/cases/server/services/attachments/index.ts +++ b/x-pack/plugins/cases/server/services/attachments/index.ts @@ -14,7 +14,6 @@ import type { } from '@kbn/core/server'; import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; -import type { CommentAttributes as AttachmentAttributes } from '../../../common/api'; import { CommentType } from '../../../common/api'; import { CASE_COMMENT_SAVED_OBJECT, CASE_SAVED_OBJECT } from '../../../common/constants'; import { buildFilter, combineFilters } from '../../client/utils'; @@ -38,7 +37,10 @@ import type { UpdateAttachmentArgs, } from './types'; import { AttachmentGetter } from './operations/get'; -import type { AttachmentPersistedAttributes } from '../../common/types/attachments'; +import type { + AttachmentPersistedAttributes, + AttachmentTransformedAttributes, +} from '../../common/types/attachments'; export class AttachmentService { private readonly _getter: AttachmentGetter; @@ -155,7 +157,7 @@ export class AttachmentService { references, id, refresh, - }: CreateAttachmentArgs): Promise> { + }: CreateAttachmentArgs): Promise> { try { this.context.log.debug(`Attempting to POST a new comment`); @@ -190,7 +192,7 @@ export class AttachmentService { public async bulkCreate({ attachments, refresh, - }: BulkCreateAttachments): Promise> { + }: BulkCreateAttachments): Promise> { try { this.context.log.debug(`Attempting to bulk create attachments`); const res = @@ -231,7 +233,7 @@ export class AttachmentService { attachmentId, updatedAttributes, options, - }: UpdateAttachmentArgs): Promise> { + }: UpdateAttachmentArgs): Promise> { try { this.context.log.debug(`Attempting to UPDATE comment ${attachmentId}`); @@ -278,7 +280,9 @@ export class AttachmentService { public async bulkUpdate({ comments, refresh, - }: BulkUpdateAttachmentArgs): Promise> { + }: BulkUpdateAttachmentArgs): Promise< + SavedObjectsBulkUpdateResponse + > { try { this.context.log.debug( `Attempting to UPDATE comments ${comments.map((c) => c.attachmentId).join(', ')}` @@ -336,7 +340,7 @@ export class AttachmentService { options, }: { options?: SavedObjectFindOptionsKueryNode; - }): Promise> { + }): Promise> { try { this.context.log.debug(`Attempting to find comments`); const res = diff --git a/x-pack/plugins/cases/server/services/attachments/operations/get.ts b/x-pack/plugins/cases/server/services/attachments/operations/get.ts index 4aa5c65ebdcc1..bac3e5fd40731 100644 --- a/x-pack/plugins/cases/server/services/attachments/operations/get.ts +++ b/x-pack/plugins/cases/server/services/attachments/operations/get.ts @@ -8,7 +8,10 @@ import type { SavedObject } from '@kbn/core/server'; import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; import { FILE_SO_TYPE } from '@kbn/files-plugin/common'; -import type { AttachmentPersistedAttributes } from '../../../common/types/attachments'; +import type { + AttachmentPersistedAttributes, + AttachmentTransformedAttributes, +} from '../../../common/types/attachments'; import { CASE_COMMENT_SAVED_OBJECT, CASE_SAVED_OBJECT, @@ -16,11 +19,7 @@ import { MAX_DOCS_PER_PAGE, } from '../../../../common/constants'; import { buildFilter, combineFilters } from '../../../client/utils'; -import type { - AttachmentTotals, - AttributesTypeAlerts, - CommentAttributes as AttachmentAttributes, -} from '../../../../common/api'; +import type { AttachmentTotals, AttributesTypeAlerts } from '../../../../common/api'; import { CommentType } from '../../../../common/api'; import type { AlertIdsAggsResult, @@ -42,7 +41,7 @@ export class AttachmentGetter { public async bulkGet( attachmentIds: string[] - ): Promise> { + ): Promise> { try { this.context.log.debug( `Attempting to retrieve attachments with ids: ${attachmentIds.join()}` @@ -192,7 +191,7 @@ export class AttachmentGetter { public async get({ attachmentId, - }: GetAttachmentArgs): Promise> { + }: GetAttachmentArgs): Promise> { try { this.context.log.debug(`Attempting to GET attachment ${attachmentId}`); const res = await this.context.unsecuredSavedObjectsClient.get( @@ -302,7 +301,7 @@ export class AttachmentGetter { }: { caseId: string; fileIds: string[]; - }): Promise>> { + }): Promise>> { try { this.context.log.debug('Attempting to find file attachments'); @@ -331,7 +330,7 @@ export class AttachmentGetter { } ); - const foundAttachments: Array> = []; + const foundAttachments: Array> = []; for await (const attachmentSavedObjects of finder.find()) { foundAttachments.push( diff --git a/x-pack/plugins/cases/server/services/cases/index.ts b/x-pack/plugins/cases/server/services/cases/index.ts index 93c6d610d5539..42fd0f03a25e2 100644 --- a/x-pack/plugins/cases/server/services/cases/index.ts +++ b/x-pack/plugins/cases/server/services/cases/index.ts @@ -27,7 +27,7 @@ import { CASE_SAVED_OBJECT, MAX_DOCS_PER_PAGE, } from '../../../common/constants'; -import type { Case, CommentAttributes, User, CaseStatuses } from '../../../common/api'; +import type { Case, User, CaseStatuses } from '../../../common/api'; import { caseStatuses } from '../../../common/api'; import type { SavedObjectFindOptionsKueryNode } from '../../common/types'; import { defaultSortField, flattenCaseSavedObject } from '../../common/utils'; @@ -66,6 +66,7 @@ import type { PatchCaseArgs, PatchCasesArgs, } from './types'; +import type { AttachmentTransformedAttributes } from '../../common/types/attachments'; export class CasesService { private readonly log: Logger; @@ -343,7 +344,7 @@ export class CasesService { private async getAllComments({ id, options, - }: FindCommentsArgs): Promise> { + }: FindCommentsArgs): Promise> { try { this.log.debug(`Attempting to GET all comments internal for id ${JSON.stringify(id)}`); if (options?.page !== undefined || options?.perPage !== undefined) { @@ -378,7 +379,7 @@ export class CasesService { public async getAllCaseComments({ id, options, - }: FindCaseCommentsArgs): Promise> { + }: FindCaseCommentsArgs): Promise> { try { const refs = this.asArray(id).map((caseID) => ({ type: CASE_SAVED_OBJECT, id: caseID })); if (refs.length <= 0) { diff --git a/x-pack/plugins/cases/server/services/so_references.ts b/x-pack/plugins/cases/server/services/so_references.ts index 8a3cc747c752a..f3d9261d2d375 100644 --- a/x-pack/plugins/cases/server/services/so_references.ts +++ b/x-pack/plugins/cases/server/services/so_references.ts @@ -12,8 +12,8 @@ import type { } from '@kbn/core/server'; import { isEqual, uniqWith } from 'lodash'; import type { - CommentAttributesNoSO, CommentAttributes, + CommentAttributesNoSO, CommentPatchAttributes, } from '../../common/api'; import type { PersistableStateAttachmentTypeRegistry } from '../attachment_framework/persistable_state_registry'; @@ -25,6 +25,7 @@ import { EXTERNAL_REFERENCE_REF_NAME } from '../common/constants'; import type { AttachmentPersistedAttributes, AttachmentRequestAttributes, + AttachmentTransformedAttributes, } from '../common/types/attachments'; import { isCommentRequestTypeExternalReferenceSO } from './type_guards'; import type { PartialField } from '../types'; @@ -56,11 +57,11 @@ type OptionalAttributes = PartialField, 'attributes'>; export const injectAttachmentAttributesAndHandleErrors = ( savedObject: OptionalAttributes, persistableStateAttachmentTypeRegistry: PersistableStateAttachmentTypeRegistry -): OptionalAttributes => { +): OptionalAttributes => { if (!hasAttributes(savedObject)) { // we don't actually have an attributes field here so the type doesn't matter, this cast is to get the types to stop // complaining though - return savedObject as OptionalAttributes; + return savedObject as OptionalAttributes; } return injectAttachmentSOAttributesFromRefs(savedObject, persistableStateAttachmentTypeRegistry); @@ -73,9 +74,9 @@ const hasAttributes = (savedObject: OptionalAttributes): savedObject is Sa export const injectAttachmentSOAttributesFromRefs = ( savedObject: SavedObject, persistableStateAttachmentTypeRegistry: PersistableStateAttachmentTypeRegistry -): SavedObject => { +): SavedObject => { const soExtractor = getAttachmentSOExtractor(savedObject.attributes); - const so = soExtractor.populateFieldsFromReferences(savedObject); + const so = soExtractor.populateFieldsFromReferences(savedObject); const injectedAttributes = injectPersistableReferencesToSO(so.attributes, so.references, { persistableStateAttachmentTypeRegistry, }); @@ -87,9 +88,9 @@ export const injectAttachmentSOAttributesFromRefsForPatch = ( updatedAttributes: CommentPatchAttributes, savedObject: SavedObjectsUpdateResponse, persistableStateAttachmentTypeRegistry: PersistableStateAttachmentTypeRegistry -): SavedObjectsUpdateResponse => { +): SavedObjectsUpdateResponse => { const soExtractor = getAttachmentSOExtractor(savedObject.attributes); - const so = soExtractor.populateFieldsFromReferencesForPatch({ + const so = soExtractor.populateFieldsFromReferencesForPatch({ dataBeforeRequest: updatedAttributes, dataReturnedFromRequest: savedObject, }); @@ -105,7 +106,7 @@ export const injectAttachmentSOAttributesFromRefsForPatch = ( return { ...so, attributes: { ...so.attributes, ...injectedAttributes }, - } as SavedObjectsUpdateResponse; + } as SavedObjectsUpdateResponse; }; interface ExtractionResults { From f340554b59fa5274587ef06a3ebda334fa54962c Mon Sep 17 00:00:00 2001 From: Jonathan Buttner Date: Tue, 2 May 2023 13:40:39 -0400 Subject: [PATCH 3/5] Addressing feedback --- .../cases/common/api/cases/comment/index.ts | 10 ++++------ .../cases/server/client/attachments/client.ts | 8 ++++---- .../plugins/cases/server/client/attachments/get.ts | 14 +++++++------- .../cases/server/client/typedoc_interfaces.ts | 8 ++++---- .../cases/server/common/types/attachments.ts | 2 ++ x-pack/plugins/cases/server/common/utils.ts | 4 ++-- .../cases/server/services/attachments/index.ts | 4 ++-- .../server/services/attachments/operations/get.ts | 9 ++++----- .../plugins/cases/server/services/so_references.ts | 3 ++- 9 files changed, 31 insertions(+), 31 deletions(-) diff --git a/x-pack/plugins/cases/common/api/cases/comment/index.ts b/x-pack/plugins/cases/common/api/cases/comment/index.ts index 3bc9f69e84ba2..b6b35e7cc73b5 100644 --- a/x-pack/plugins/cases/common/api/cases/comment/index.ts +++ b/x-pack/plugins/cases/common/api/cases/comment/index.ts @@ -233,8 +233,6 @@ export const CommentResponseTypePersistableStateRt = rt.intersection([ }), ]); -export const AllCommentsResponseRT = rt.array(CommentRt); - export const CommentPatchRequestRt = rt.intersection([ /** * Partial updates are not allowed. @@ -266,14 +264,14 @@ export const CommentPatchAttributesRt = rt.intersection([ rt.partial(CommentAttributesBasicRt.props), ]); -export const CommentsRt = rt.type({ +export const CommentsFindResponseRt = rt.type({ comments: rt.array(CommentRt), page: rt.number, per_page: rt.number, total: rt.number, }); -export const AllCommentsResponseRt = rt.array(CommentRt); +export const CommentsRt = rt.array(CommentRt); export const FindQueryParamsRt = rt.partial({ ...SavedObjectFindOptionsRt.props, @@ -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, @@ -324,8 +322,8 @@ export type CommentResponseExternalReferenceType = rt.TypeOf< typeof CommentResponseTypeExternalReferenceRt >; export type CommentResponseActionsType = rt.TypeOf; -export type AllCommentsResponse = rt.TypeOf; export type Comments = rt.TypeOf; +export type CommentsFindResponse = rt.TypeOf; export type CommentPatchRequest = rt.TypeOf; export type CommentPatchAttributes = rt.TypeOf; export type CommentRequestUserType = rt.TypeOf; diff --git a/x-pack/plugins/cases/server/client/attachments/client.ts b/x-pack/plugins/cases/server/client/attachments/client.ts index ae4abe2d667dc..987e3c17575e3 100644 --- a/x-pack/plugins/cases/server/client/attachments/client.ts +++ b/x-pack/plugins/cases/server/client/attachments/client.ts @@ -7,11 +7,11 @@ import type { AlertResponse, - AllCommentsResponse, + Comments, BulkGetAttachmentsResponse, Case, Comment, - Comments, + CommentsFindResponse, } from '../../../common/api'; import type { CasesClient } from '../client'; @@ -60,7 +60,7 @@ export interface AttachmentsSubClient { /** * Retrieves all comments matching the search criteria. */ - find(findArgs: FindArgs): Promise; + find(findArgs: FindArgs): Promise; /** * Retrieves all alerts attach to a case given a single case ID */ @@ -68,7 +68,7 @@ export interface AttachmentsSubClient { /** * Gets all attachments for a single case. */ - getAll(getAllArgs: GetAllArgs): Promise; + getAll(getAllArgs: GetAllArgs): Promise; /** * Retrieves a single attachment for a case. */ diff --git a/x-pack/plugins/cases/server/client/attachments/get.ts b/x-pack/plugins/cases/server/client/attachments/get.ts index dc885f37848ea..79832e98c8016 100644 --- a/x-pack/plugins/cases/server/client/attachments/get.ts +++ b/x-pack/plugins/cases/server/client/attachments/get.ts @@ -8,12 +8,12 @@ import type { SavedObject } from '@kbn/core/server'; import type { AlertResponse, - AllCommentsResponse, + Comments, AttributesTypeAlerts, Comment, - Comments, + CommentsFindResponse, } from '../../../common/api'; -import { AllCommentsResponseRt, CommentRt, CommentsRt } from '../../../common/api'; +import { CommentsRt, CommentRt, CommentsFindResponseRt } from '../../../common/api'; import { defaultSortField, transformComments, @@ -100,7 +100,7 @@ export const getAllAlertsAttachToCase = async ( export async function find( { caseID, queryParams }: FindArgs, clientArgs: CasesClientArgs -): Promise { +): Promise { const { unsecuredSavedObjectsClient, services: { caseService }, @@ -159,7 +159,7 @@ export async function find( })) ); - return CommentsRt.encode(transformComments(theComments)); + return CommentsFindResponseRt.encode(transformComments(theComments)); } catch (error) { throw createCaseError({ message: `Failed to find comments case id: ${caseID}: ${error}`, @@ -208,7 +208,7 @@ export async function get( export async function getAll( { caseID }: GetAllArgs, clientArgs: CasesClientArgs -): Promise { +): Promise { const { services: { caseService }, logger, @@ -232,7 +232,7 @@ export async function getAll( comments.saved_objects.map((comment) => ({ id: comment.id, owner: comment.attributes.owner })) ); - return AllCommentsResponseRt.encode(flattenCommentSavedObjects(comments.saved_objects)); + return CommentsRt.encode(flattenCommentSavedObjects(comments.saved_objects)); } catch (error) { throw createCaseError({ message: `Failed to get all comments case id: ${caseID}: ${error}`, diff --git a/x-pack/plugins/cases/server/client/typedoc_interfaces.ts b/x-pack/plugins/cases/server/client/typedoc_interfaces.ts index 22359af185384..59dc5d9eb25d8 100644 --- a/x-pack/plugins/cases/server/client/typedoc_interfaces.ts +++ b/x-pack/plugins/cases/server/client/typedoc_interfaces.ts @@ -14,7 +14,7 @@ /* eslint-disable @typescript-eslint/no-empty-interface */ import type { - AllCommentsResponse, + Comments, CasePostRequest, CaseResolveResponse, Case, @@ -26,7 +26,7 @@ import type { CasesPatchRequest, Cases, CaseUserActionsResponse, - Comments, + CommentsFindResponse, CasesBulkGetResponse, } from '../../common/api'; @@ -48,7 +48,7 @@ export interface ICasesConfigureResponse extends CasesConfigureResponse {} export interface ICasesConfigureRequest extends CasesConfigureRequest {} export interface ICasesConfigurePatch extends CasesConfigurePatch {} -export interface ICommentsResponse extends Comments {} -export interface IAllCommentsResponse extends AllCommentsResponse {} +export interface ICommentsResponse extends CommentsFindResponse {} +export interface IAllCommentsResponse extends Comments {} export interface ICaseUserActionsResponse extends CaseUserActionsResponse {} diff --git a/x-pack/plugins/cases/server/common/types/attachments.ts b/x-pack/plugins/cases/server/common/types/attachments.ts index 02c84925ef531..c48f250999a97 100644 --- a/x-pack/plugins/cases/server/common/types/attachments.ts +++ b/x-pack/plugins/cases/server/common/types/attachments.ts @@ -5,6 +5,7 @@ * 2.0. */ +import type { SavedObject } from '@kbn/core/server'; import type { JsonValue } from '@kbn/utility-types'; import type { CommentAttributes } from '../../../common/api'; import type { User } from './user'; @@ -49,3 +50,4 @@ export type AttachmentPersistedAttributes = AttachmentRequestAttributes & AttachmentCommonPersistedAttributes; export type AttachmentTransformedAttributes = CommentAttributes; +export type AttachmentSavedObjectTransformed = SavedObject; diff --git a/x-pack/plugins/cases/server/common/utils.ts b/x-pack/plugins/cases/server/common/utils.ts index 988c38a9c0795..75ca980af639b 100644 --- a/x-pack/plugins/cases/server/common/utils.ts +++ b/x-pack/plugins/cases/server/common/utils.ts @@ -36,7 +36,7 @@ import type { CommentRequestAlertType, CommentRequestUserType, Comment, - Comments, + CommentsFindResponse, User, } from '../../common/api'; import { @@ -133,7 +133,7 @@ export const flattenCaseSavedObject = ({ export const transformComments = ( comments: SavedObjectsFindResponse -): Comments => ({ +): CommentsFindResponse => ({ page: comments.page, per_page: comments.per_page, total: comments.total, diff --git a/x-pack/plugins/cases/server/services/attachments/index.ts b/x-pack/plugins/cases/server/services/attachments/index.ts index 2e8788a4021ba..7fead24a65162 100644 --- a/x-pack/plugins/cases/server/services/attachments/index.ts +++ b/x-pack/plugins/cases/server/services/attachments/index.ts @@ -6,7 +6,6 @@ */ import type { - SavedObject, SavedObjectsBulkResponse, SavedObjectsBulkUpdateResponse, SavedObjectsFindResponse, @@ -40,6 +39,7 @@ import { AttachmentGetter } from './operations/get'; import type { AttachmentPersistedAttributes, AttachmentTransformedAttributes, + AttachmentSavedObjectTransformed, } from '../../common/types/attachments'; export class AttachmentService { @@ -157,7 +157,7 @@ export class AttachmentService { references, id, refresh, - }: CreateAttachmentArgs): Promise> { + }: CreateAttachmentArgs): Promise { try { this.context.log.debug(`Attempting to POST a new comment`); diff --git a/x-pack/plugins/cases/server/services/attachments/operations/get.ts b/x-pack/plugins/cases/server/services/attachments/operations/get.ts index bac3e5fd40731..c5ba13633ab43 100644 --- a/x-pack/plugins/cases/server/services/attachments/operations/get.ts +++ b/x-pack/plugins/cases/server/services/attachments/operations/get.ts @@ -11,6 +11,7 @@ import { FILE_SO_TYPE } from '@kbn/files-plugin/common'; import type { AttachmentPersistedAttributes, AttachmentTransformedAttributes, + AttachmentSavedObjectTransformed, } from '../../../common/types/attachments'; import { CASE_COMMENT_SAVED_OBJECT, @@ -189,9 +190,7 @@ export class AttachmentGetter { } } - public async get({ - attachmentId, - }: GetAttachmentArgs): Promise> { + public async get({ attachmentId }: GetAttachmentArgs): Promise { try { this.context.log.debug(`Attempting to GET attachment ${attachmentId}`); const res = await this.context.unsecuredSavedObjectsClient.get( @@ -301,7 +300,7 @@ export class AttachmentGetter { }: { caseId: string; fileIds: string[]; - }): Promise>> { + }): Promise { try { this.context.log.debug('Attempting to find file attachments'); @@ -330,7 +329,7 @@ export class AttachmentGetter { } ); - const foundAttachments: Array> = []; + const foundAttachments: AttachmentSavedObjectTransformed[] = []; for await (const attachmentSavedObjects of finder.find()) { foundAttachments.push( diff --git a/x-pack/plugins/cases/server/services/so_references.ts b/x-pack/plugins/cases/server/services/so_references.ts index f3d9261d2d375..24896b7601a5c 100644 --- a/x-pack/plugins/cases/server/services/so_references.ts +++ b/x-pack/plugins/cases/server/services/so_references.ts @@ -26,6 +26,7 @@ import type { AttachmentPersistedAttributes, AttachmentRequestAttributes, AttachmentTransformedAttributes, + AttachmentSavedObjectTransformed, } from '../common/types/attachments'; import { isCommentRequestTypeExternalReferenceSO } from './type_guards'; import type { PartialField } from '../types'; @@ -74,7 +75,7 @@ const hasAttributes = (savedObject: OptionalAttributes): savedObject is Sa export const injectAttachmentSOAttributesFromRefs = ( savedObject: SavedObject, persistableStateAttachmentTypeRegistry: PersistableStateAttachmentTypeRegistry -): SavedObject => { +): AttachmentSavedObjectTransformed => { const soExtractor = getAttachmentSOExtractor(savedObject.attributes); const so = soExtractor.populateFieldsFromReferences(savedObject); const injectedAttributes = injectPersistableReferencesToSO(so.attributes, so.references, { From 82e6df2015927ea6e23c44461078279159d0ab7a Mon Sep 17 00:00:00 2001 From: Jonathan Buttner Date: Tue, 2 May 2023 14:13:38 -0400 Subject: [PATCH 4/5] Fixing type errors --- .../cases_api_integration/common/lib/api/attachments.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/x-pack/test/cases_api_integration/common/lib/api/attachments.ts b/x-pack/test/cases_api_integration/common/lib/api/attachments.ts index b5d423309aec8..15e3901dcf970 100644 --- a/x-pack/test/cases_api_integration/common/lib/api/attachments.ts +++ b/x-pack/test/cases_api_integration/common/lib/api/attachments.ts @@ -8,14 +8,14 @@ import type SuperTest from 'supertest'; import { CASES_INTERNAL_URL, CASES_URL } from '@kbn/cases-plugin/common/constants'; import { - AllCommentsResponse, + Comments, BulkCreateCommentRequest, BulkGetAttachmentsResponse, Case, CommentPatchRequest, CommentRequest, Comment, - Comments, + CommentsFindResponse, CommentType, getCaseFindAttachmentsUrl, getCasesDeleteFileAttachmentsUrl, @@ -203,7 +203,7 @@ export const getAllComments = async ({ caseId: string; auth?: { user: User; space: string | null }; expectedHttpCode?: number; -}): Promise => { +}): Promise => { const { body: comments } = await supertest .get(`${getSpaceUrlPrefix(auth.space)}${CASES_URL}/${caseId}/comments`) .auth(auth.user.username, auth.user.password) @@ -295,7 +295,7 @@ export const findAttachments = async ({ query?: Record; expectedHttpCode?: number; auth?: { user: User; space: string | null }; -}): Promise => { +}): Promise => { const { body } = await supertest .get(`${getSpaceUrlPrefix(auth.space)}${getCaseFindAttachmentsUrl(caseId)}`) .set('kbn-xsrf', 'true') From 9d018cfa9f9688a93091c4fafd11ba5f09c0945e Mon Sep 17 00:00:00 2001 From: Jonathan Buttner Date: Tue, 2 May 2023 14:46:22 -0400 Subject: [PATCH 5/5] Fixing comments type errors --- .../tests/common/cases/import_export.ts | 11 +++++------ .../tests/common/comments/find_comments.ts | 14 ++++++++------ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/x-pack/test/cases_api_integration/security_and_spaces/tests/common/cases/import_export.ts b/x-pack/test/cases_api_integration/security_and_spaces/tests/common/cases/import_export.ts index 4f96aed515793..8d656de080556 100644 --- a/x-pack/test/cases_api_integration/security_and_spaces/tests/common/cases/import_export.ts +++ b/x-pack/test/cases_api_integration/security_and_spaces/tests/common/cases/import_export.ts @@ -10,14 +10,12 @@ import { join } from 'path'; import { SavedObject } from '@kbn/core/server'; import supertest from 'supertest'; import { - CASES_URL, CASE_SAVED_OBJECT, CASE_USER_ACTION_SAVED_OBJECT, CASE_COMMENT_SAVED_OBJECT, } from '@kbn/cases-plugin/common/constants'; import { AttributesTypeUser, - Comments, CaseAttributes, CasePostRequest, PushedUserAction, @@ -39,6 +37,7 @@ import { createComment, findCases, getCaseUserActions, + findAttachments, } from '../../../../common/lib/api'; import { getPostCaseRequest, postCommentUserReq } from '../../../../common/lib/mock'; import { FtrProviderContext } from '../../../../common/ftr_provider_context'; @@ -102,10 +101,10 @@ export default ({ getService }: FtrProviderContext): void => { expect(findResponse.cases[0].title).to.eql('A case to export'); expect(findResponse.cases[0].description).to.eql('a description'); - const { body: commentsResponse }: { body: Comments } = await supertestService - .get(`${CASES_URL}/${findResponse.cases[0].id}/comments/_find`) - .send() - .expect(200); + const commentsResponse = await findAttachments({ + supertest: supertestService, + caseId: findResponse.cases[0].id, + }); const comment = commentsResponse.comments[0] as unknown as AttributesTypeUser; expect(comment.comment).to.eql('A comment for my case'); diff --git a/x-pack/test/cases_api_integration/security_and_spaces/tests/common/comments/find_comments.ts b/x-pack/test/cases_api_integration/security_and_spaces/tests/common/comments/find_comments.ts index 6ae412aded18e..e7dac5f4c3fae 100644 --- a/x-pack/test/cases_api_integration/security_and_spaces/tests/common/comments/find_comments.ts +++ b/x-pack/test/cases_api_integration/security_and_spaces/tests/common/comments/find_comments.ts @@ -8,7 +8,7 @@ import expect from '@kbn/expect'; import { CASES_URL } from '@kbn/cases-plugin/common/constants'; -import { Comments } from '@kbn/cases-plugin/common/api'; +import { CommentsFindResponse } from '@kbn/cases-plugin/common/api'; import { FtrProviderContext } from '../../../../common/ftr_provider_context'; import { getPostCaseRequest, @@ -26,6 +26,7 @@ import { getSpaceUrlPrefix, createCase, superUserSpace1Auth, + findAttachments, } from '../../../../common/lib/api'; import { @@ -221,10 +222,11 @@ export default ({ getService }: FtrProviderContext): void => { caseID: obsCase.id, }, ]) { - const { body: caseComments }: { body: Comments } = await supertestWithoutAuth - .get(`${getSpaceUrlPrefix(space1)}${CASES_URL}/${scenario.caseID}/comments/_find`) - .auth(scenario.user.username, scenario.user.password) - .expect(200); + const caseComments = await findAttachments({ + supertest: supertestWithoutAuth, + caseId: scenario.caseID, + auth: { user: scenario.user, space: space1 }, + }); ensureSavedObjectIsAuthorized( caseComments.comments, @@ -279,7 +281,7 @@ export default ({ getService }: FtrProviderContext): void => { caseId: obsCase.id, }); - const { body: res }: { body: Comments } = await supertestWithoutAuth + const { body: res }: { body: CommentsFindResponse } = await supertestWithoutAuth .get( `${getSpaceUrlPrefix('space1')}${CASES_URL}/${ obsCase.id