From c29ef14656fabb1de46ba0bf82f4b2e997946bf5 Mon Sep 17 00:00:00 2001 From: Xavier Mouligneau <189600+XavierM@users.noreply.github.com> Date: Thu, 5 Mar 2020 23:29:55 -0500 Subject: [PATCH] [SIEM] [CASES] API with io-ts validation (#59265) * refactor to use io-ts, to be able to have ressource with sub, add total comments via comment_ids, be able to delete multiple cases/comments * fix test * adapt UI to refactor of the API * put it back the way it was * clean up to get cases * review I * review II - bring back url parameter * fix merge Co-authored-by: Elastic Machine --- .../ml/api/error_to_toaster.test.ts | 2 +- .../components/ml/api/error_to_toaster.ts | 2 +- .../siem/public/components/ml_popover/api.tsx | 2 +- .../siem/public/containers/case/api.ts | 102 ++++++++++-------- .../siem/public/containers/case/types.ts | 53 +-------- .../public/containers/case/use_get_case.tsx | 8 +- .../public/containers/case/use_get_cases.tsx | 59 +++++----- .../public/containers/case/use_get_tags.tsx | 25 ++--- .../public/containers/case/use_post_case.tsx | 80 +++++++------- .../containers/case/use_post_comment.tsx | 75 ++++++------- .../containers/case/use_update_case.tsx | 77 +++++++------ .../containers/case/use_update_comment.tsx | 78 +++++++++----- .../siem/public/containers/case/utils.ts | 31 +++++- .../detection_engine/rules/api.test.ts | 2 +- .../signals/errors_types/get_index_error.ts | 2 +- .../signals/errors_types/post_index_error.ts | 2 +- .../errors_types/privilege_user_error.ts | 2 +- .../plugins/siem/public/hooks/api/api.tsx | 2 +- .../ml => hooks}/api/throw_if_not_ok.test.ts | 2 +- .../ml => hooks}/api/throw_if_not_ok.ts | 8 +- .../case/components/add_comment/index.tsx | 34 +++--- .../case/components/add_comment/schema.tsx | 4 +- .../components/all_cases/__mock__/index.tsx | 10 +- .../case/components/all_cases/actions.tsx | 6 +- .../case/components/all_cases/columns.tsx | 4 +- .../case/components/all_cases/index.test.tsx | 2 +- .../components/case_view/__mock__/index.tsx | 8 +- .../case/components/case_view/index.test.tsx | 23 ++-- .../pages/case/components/case_view/index.tsx | 39 ++++--- .../pages/case/components/create/index.tsx | 32 +++--- .../pages/case/components/create/schema.tsx | 19 ++-- .../pages/case/components/tag_list/schema.tsx | 4 +- .../components/user_action_tree/index.tsx | 38 ++++--- .../server/lib/case/saved_object_mappings.ts | 81 -------------- .../plugins/siem/server/saved_objects.ts | 8 +- x-pack/plugins/case/common/api/cases/case.ts | 59 ++++++++++ .../plugins/case/common/api/cases/comment.ts | 56 ++++++++++ .../api/cases/index.ts} | 4 +- x-pack/plugins/case/common/api/index.ts | 9 ++ .../plugins/case/common/api/runtime_types.ts | 25 +++++ .../plugins/case/common/api/saved_object.ts | 34 ++++++ x-pack/plugins/case/common/api/user.ts | 12 +++ x-pack/plugins/case/kibana.json | 4 + x-pack/plugins/case/server/index.ts | 1 - x-pack/plugins/case/server/plugin.ts | 11 +- .../__fixtures__/create_mock_so_repository.ts | 56 ++++++++-- .../routes/api/__fixtures__/mock_router.ts | 2 +- .../api/__fixtures__/mock_saved_objects.ts | 60 ++++++++--- .../api/cases/comments/delete_all_comments.ts | 55 ++++++++++ .../comments}/delete_comment.test.ts | 37 ++++--- .../api/cases/comments/delete_comment.ts | 61 +++++++++++ .../api/cases/comments/find_comments.ts | 61 +++++++++++ .../comments/get_all_comment.ts} | 20 ++-- .../comments}/get_comment.test.ts | 45 +++++--- .../routes/api/cases/comments/get_comment.ts | 51 +++++++++ .../comments/patch_comment.test.ts} | 53 ++++++--- .../api/cases/comments/patch_comment.ts | 84 +++++++++++++++ .../comments}/post_comment.test.ts | 56 +++++++--- .../routes/api/cases/comments/post_comment.ts | 85 +++++++++++++++ .../delete_cases.test.ts} | 62 +++++++---- .../server/routes/api/cases/delete_cases.ts | 60 +++++++++++ .../get_all_cases.test.ts | 13 ++- .../routes/api/{ => cases}/get_all_cases.ts | 32 +++--- .../api/{__tests__ => cases}/get_case.test.ts | 57 ++++++---- .../server/routes/api/{ => cases}/get_case.ts | 35 +++--- .../patch_case.test.ts} | 66 +++++++----- .../server/routes/api/cases/patch_case.ts | 98 +++++++++++++++++ .../{__tests__ => cases}/post_case.test.ts | 27 +++-- .../case/server/routes/api/cases/post_case.ts | 48 +++++++++ .../routes/api/{ => cases/tags}/get_tags.ts | 4 +- .../case/server/routes/api/delete_case.ts | 56 ---------- .../case/server/routes/api/delete_comment.ts | 34 ------ .../case/server/routes/api/get_comment.ts | 33 ------ .../plugins/case/server/routes/api/index.ts | 43 ++++---- .../case/server/routes/api/post_case.ts | 40 ------- .../case/server/routes/api/post_comment.ts | 62 ----------- .../plugins/case/server/routes/api/types.ts | 76 +------------ .../case/server/routes/api/update_case.ts | 94 ---------------- .../case/server/routes/api/update_comment.ts | 67 ------------ .../plugins/case/server/routes/api/utils.ts | 102 ++++++++++-------- .../case/server/saved_object_types/cases.ts | 60 +++++++++++ .../server/saved_object_types/comments.ts | 48 +++++++++ .../case/server/saved_object_types/index.ts | 8 ++ x-pack/plugins/case/server/services/index.ts | 36 +++---- .../case/server/services/tags/read_tags.ts | 7 +- x-pack/tsconfig.json | 2 +- 86 files changed, 1889 insertions(+), 1248 deletions(-) rename x-pack/legacy/plugins/siem/public/{components/ml => hooks}/api/throw_if_not_ok.test.ts (99%) rename x-pack/legacy/plugins/siem/public/{components/ml => hooks}/api/throw_if_not_ok.ts (91%) delete mode 100644 x-pack/legacy/plugins/siem/server/lib/case/saved_object_mappings.ts create mode 100644 x-pack/plugins/case/common/api/cases/case.ts create mode 100644 x-pack/plugins/case/common/api/cases/comment.ts rename x-pack/plugins/case/{server/constants.ts => common/api/cases/index.ts} (67%) create mode 100644 x-pack/plugins/case/common/api/index.ts create mode 100644 x-pack/plugins/case/common/api/runtime_types.ts create mode 100644 x-pack/plugins/case/common/api/saved_object.ts create mode 100644 x-pack/plugins/case/common/api/user.ts create mode 100644 x-pack/plugins/case/server/routes/api/cases/comments/delete_all_comments.ts rename x-pack/plugins/case/server/routes/api/{__tests__ => cases/comments}/delete_comment.test.ts (61%) create mode 100644 x-pack/plugins/case/server/routes/api/cases/comments/delete_comment.ts create mode 100644 x-pack/plugins/case/server/routes/api/cases/comments/find_comments.ts rename x-pack/plugins/case/server/routes/api/{get_all_case_comments.ts => cases/comments/get_all_comment.ts} (51%) rename x-pack/plugins/case/server/routes/api/{__tests__ => cases/comments}/get_comment.test.ts (53%) create mode 100644 x-pack/plugins/case/server/routes/api/cases/comments/get_comment.ts rename x-pack/plugins/case/server/routes/api/{__tests__/update_comment.test.ts => cases/comments/patch_comment.test.ts} (64%) create mode 100644 x-pack/plugins/case/server/routes/api/cases/comments/patch_comment.ts rename x-pack/plugins/case/server/routes/api/{__tests__ => cases/comments}/post_comment.test.ts (66%) create mode 100644 x-pack/plugins/case/server/routes/api/cases/comments/post_comment.ts rename x-pack/plugins/case/server/routes/api/{__tests__/delete_case.test.ts => cases/delete_cases.test.ts} (60%) create mode 100644 x-pack/plugins/case/server/routes/api/cases/delete_cases.ts rename x-pack/plugins/case/server/routes/api/{__tests__ => cases}/get_all_cases.test.ts (84%) rename x-pack/plugins/case/server/routes/api/{ => cases}/get_all_cases.ts (52%) rename x-pack/plugins/case/server/routes/api/{__tests__ => cases}/get_case.test.ts (74%) rename x-pack/plugins/case/server/routes/api/{ => cases}/get_case.ts (58%) rename x-pack/plugins/case/server/routes/api/{__tests__/update_case.test.ts => cases/patch_case.test.ts} (69%) create mode 100644 x-pack/plugins/case/server/routes/api/cases/patch_case.ts rename x-pack/plugins/case/server/routes/api/{__tests__ => cases}/post_case.test.ts (82%) create mode 100644 x-pack/plugins/case/server/routes/api/cases/post_case.ts rename x-pack/plugins/case/server/routes/api/{ => cases/tags}/get_tags.ts (89%) delete mode 100644 x-pack/plugins/case/server/routes/api/delete_case.ts delete mode 100644 x-pack/plugins/case/server/routes/api/delete_comment.ts delete mode 100644 x-pack/plugins/case/server/routes/api/get_comment.ts delete mode 100644 x-pack/plugins/case/server/routes/api/post_case.ts delete mode 100644 x-pack/plugins/case/server/routes/api/post_comment.ts delete mode 100644 x-pack/plugins/case/server/routes/api/update_case.ts delete mode 100644 x-pack/plugins/case/server/routes/api/update_comment.ts create mode 100644 x-pack/plugins/case/server/saved_object_types/cases.ts create mode 100644 x-pack/plugins/case/server/saved_object_types/comments.ts create mode 100644 x-pack/plugins/case/server/saved_object_types/index.ts diff --git a/x-pack/legacy/plugins/siem/public/components/ml/api/error_to_toaster.test.ts b/x-pack/legacy/plugins/siem/public/components/ml/api/error_to_toaster.test.ts index 507d6cf98ed08..d4f38d817bd6b 100644 --- a/x-pack/legacy/plugins/siem/public/components/ml/api/error_to_toaster.test.ts +++ b/x-pack/legacy/plugins/siem/public/components/ml/api/error_to_toaster.test.ts @@ -5,7 +5,7 @@ */ import { isAnError, isToasterError, errorToToaster } from './error_to_toaster'; -import { ToasterErrors } from './throw_if_not_ok'; +import { ToasterErrors } from '../../../hooks/api/throw_if_not_ok'; describe('error_to_toaster', () => { let dispatchToaster = jest.fn(); diff --git a/x-pack/legacy/plugins/siem/public/components/ml/api/error_to_toaster.ts b/x-pack/legacy/plugins/siem/public/components/ml/api/error_to_toaster.ts index 779befaa0cd8e..b341016fff6ef 100644 --- a/x-pack/legacy/plugins/siem/public/components/ml/api/error_to_toaster.ts +++ b/x-pack/legacy/plugins/siem/public/components/ml/api/error_to_toaster.ts @@ -7,7 +7,7 @@ import { isError } from 'lodash/fp'; import uuid from 'uuid'; import { ActionToaster, AppToast } from '../../toasters'; -import { ToasterErrorsType, ToasterErrors } from './throw_if_not_ok'; +import { ToasterErrorsType, ToasterErrors } from '../../../hooks/api/throw_if_not_ok'; export type ErrorToToasterArgs = Partial & { error: unknown; diff --git a/x-pack/legacy/plugins/siem/public/components/ml_popover/api.tsx b/x-pack/legacy/plugins/siem/public/components/ml_popover/api.tsx index 120fd8c404ffd..1ab996f88515b 100644 --- a/x-pack/legacy/plugins/siem/public/components/ml_popover/api.tsx +++ b/x-pack/legacy/plugins/siem/public/components/ml_popover/api.tsx @@ -17,7 +17,7 @@ import { StartDatafeedResponse, StopDatafeedResponse, } from './types'; -import { throwIfErrorAttached, throwIfErrorAttachedToSetup } from '../ml/api/throw_if_not_ok'; +import { throwIfErrorAttached, throwIfErrorAttachedToSetup } from '../../hooks/api/throw_if_not_ok'; import { throwIfNotOk } from '../../hooks/api/api'; import { KibanaServices } from '../../lib/kibana'; diff --git a/x-pack/legacy/plugins/siem/public/containers/case/api.ts b/x-pack/legacy/plugins/siem/public/containers/case/api.ts index ff03a3799018c..81f8f83217e11 100644 --- a/x-pack/legacy/plugins/siem/public/containers/case/api.ts +++ b/x-pack/legacy/plugins/siem/public/containers/case/api.ts @@ -4,24 +4,29 @@ * you may not use this file except in compliance with the Elastic License. */ -import { KibanaServices } from '../../lib/kibana'; import { - AllCases, - Case, - CaseSnake, - Comment, - CommentSnake, - FetchCasesProps, - NewCase, - NewComment, - SortFieldCase, -} from './types'; + CaseResponse, + CasesResponse, + CaseRequest, + CommentRequest, + CommentResponse, +} from '../../../../../../plugins/case/common/api'; +import { KibanaServices } from '../../lib/kibana'; +import { AllCases, Case, Comment, FetchCasesProps, SortFieldCase } from './types'; import { throwIfNotOk } from '../../hooks/api/api'; import { CASES_URL } from './constants'; -import { convertToCamelCase, convertAllCasesToCamel } from './utils'; +import { + convertToCamelCase, + convertAllCasesToCamel, + decodeCaseResponse, + decodeCasesResponse, + decodeCommentResponse, +} from './utils'; + +const CaseSavedObjectType = 'cases'; export const getCase = async (caseId: string, includeComments: boolean = true): Promise => { - const response = await KibanaServices.get().http.fetch(`${CASES_URL}/${caseId}`, { + const response = await KibanaServices.get().http.fetch(`${CASES_URL}/${caseId}`, { method: 'GET', asResponse: true, query: { @@ -29,7 +34,16 @@ export const getCase = async (caseId: string, includeComments: boolean = true): }, }); await throwIfNotOk(response.response); - return convertToCamelCase(response.body!); + return convertToCamelCase(decodeCaseResponse(response.body)); +}; + +export const getTags = async (): Promise => { + const response = await KibanaServices.get().http.fetch(`${CASES_URL}/tags`, { + method: 'GET', + asResponse: true, + }); + await throwIfNotOk(response.response); + return response.body ?? []; }; export const getCases = async ({ @@ -45,70 +59,74 @@ export const getCases = async ({ sortOrder: 'desc', }, }: FetchCasesProps): Promise => { - const stateFilter = `case-workflow.attributes.state: ${filterOptions.state}`; + const stateFilter = `${CaseSavedObjectType}.attributes.state: ${filterOptions.state}`; const tags = [ - ...(filterOptions.tags?.reduce((acc, t) => [...acc, `case-workflow.attributes.tags: ${t}`], [ - stateFilter, - ]) ?? [stateFilter]), + ...(filterOptions.tags?.reduce( + (acc, t) => [...acc, `${CaseSavedObjectType}.attributes.tags: ${t}`], + [stateFilter] + ) ?? [stateFilter]), ]; const query = { ...queryParams, - filter: tags.join(' AND '), - search: filterOptions.search, + ...(tags.length > 0 ? { filter: tags.join(' AND ') } : {}), + ...(filterOptions.search.length > 0 ? { search: filterOptions.search } : {}), }; - const response = await KibanaServices.get().http.fetch(`${CASES_URL}`, { + const response = await KibanaServices.get().http.fetch(`${CASES_URL}/_find`, { method: 'GET', query, asResponse: true, }); await throwIfNotOk(response.response); - return convertAllCasesToCamel(response.body!); + return convertAllCasesToCamel(decodeCasesResponse(response.body)); }; -export const createCase = async (newCase: NewCase): Promise => { - const response = await KibanaServices.get().http.fetch(`${CASES_URL}`, { +export const postCase = async (newCase: CaseRequest): Promise => { + const response = await KibanaServices.get().http.fetch(`${CASES_URL}`, { method: 'POST', asResponse: true, body: JSON.stringify(newCase), }); await throwIfNotOk(response.response); - return convertToCamelCase(response.body!); + return convertToCamelCase(decodeCaseResponse(response.body)); }; -export const updateCaseProperty = async ( +export const patchCase = async ( caseId: string, - updatedCase: Partial, + updatedCase: Partial, version: string -): Promise> => { - const response = await KibanaServices.get().http.fetch(`${CASES_URL}/${caseId}`, { +): Promise => { + const response = await KibanaServices.get().http.fetch(`${CASES_URL}`, { method: 'PATCH', asResponse: true, - body: JSON.stringify({ case: updatedCase, version }), + body: JSON.stringify({ ...updatedCase, id: caseId, version }), }); await throwIfNotOk(response.response); - return convertToCamelCase, Partial>(response.body!); + return convertToCamelCase(decodeCaseResponse(response.body)); }; -export const createComment = async (newComment: NewComment, caseId: string): Promise => { - const response = await KibanaServices.get().http.fetch(`${CASES_URL}/${caseId}/comment`, { - method: 'POST', - asResponse: true, - body: JSON.stringify(newComment), - }); +export const postComment = async (newComment: CommentRequest, caseId: string): Promise => { + const response = await KibanaServices.get().http.fetch( + `${CASES_URL}/${caseId}/comments`, + { + method: 'POST', + asResponse: true, + body: JSON.stringify(newComment), + } + ); await throwIfNotOk(response.response); - return convertToCamelCase(response.body!); + return convertToCamelCase(decodeCommentResponse(response.body)); }; -export const updateComment = async ( +export const patchComment = async ( commentId: string, commentUpdate: string, version: string ): Promise> => { - const response = await KibanaServices.get().http.fetch(`${CASES_URL}/comment/${commentId}`, { + const response = await KibanaServices.get().http.fetch(`${CASES_URL}/comments`, { method: 'PATCH', asResponse: true, - body: JSON.stringify({ comment: commentUpdate, version }), + body: JSON.stringify({ comment: commentUpdate, id: commentId, version }), }); await throwIfNotOk(response.response); - return convertToCamelCase, Partial>(response.body!); + return convertToCamelCase(decodeCommentResponse(response.body)); }; diff --git a/x-pack/legacy/plugins/siem/public/containers/case/types.ts b/x-pack/legacy/plugins/siem/public/containers/case/types.ts index 9cc9f519f3a62..d479abdbd4489 100644 --- a/x-pack/legacy/plugins/siem/public/containers/case/types.ts +++ b/x-pack/legacy/plugins/siem/public/containers/case/types.ts @@ -4,31 +4,8 @@ * you may not use this file except in compliance with the Elastic License. */ -interface FormData { - isNew?: boolean; -} - -export interface NewCase extends FormData { - description: string; - tags: string[]; - title: string; -} - -export interface NewComment extends FormData { - comment: string; -} - -export interface CommentSnake { - comment_id: string; - created_at: string; - created_by: ElasticUserSnake; - comment: string; - updated_at: string; - version: string; -} - export interface Comment { - commentId: string; + id: string; createdAt: string; createdBy: ElasticUser; comment: string; @@ -36,21 +13,8 @@ export interface Comment { version: string; } -export interface CaseSnake { - case_id: string; - comments: CommentSnake[]; - created_at: string; - created_by: ElasticUserSnake; - description: string; - state: string; - tags: string[]; - title: string; - updated_at: string; - version: string; -} - export interface Case { - caseId: string; + id: string; comments: Comment[]; createdAt: string; createdBy: ElasticUser; @@ -75,29 +39,18 @@ export interface FilterOptions { tags: string[]; } -export interface AllCasesSnake { - cases: CaseSnake[]; - page: number; - per_page: number; - total: number; -} - export interface AllCases { cases: Case[]; page: number; perPage: number; total: number; } + export enum SortFieldCase { createdAt = 'createdAt', updatedAt = 'updatedAt', } -export interface ElasticUserSnake { - readonly username: string; - readonly full_name?: string | null; -} - export interface ElasticUser { readonly username: string; readonly fullName?: string | null; diff --git a/x-pack/legacy/plugins/siem/public/containers/case/use_get_case.tsx b/x-pack/legacy/plugins/siem/public/containers/case/use_get_case.tsx index ce71c26078db9..5f1dc96735d32 100644 --- a/x-pack/legacy/plugins/siem/public/containers/case/use_get_case.tsx +++ b/x-pack/legacy/plugins/siem/public/containers/case/use_get_case.tsx @@ -50,7 +50,7 @@ const dataFetchReducer = (state: CaseState, action: Action): CaseState => { } }; const initialData: Case = { - caseId: '', + id: '', createdAt: '', comments: [], createdBy: { @@ -83,7 +83,11 @@ export const useGetCase = (caseId: string): [CaseState] => { } } catch (error) { if (!didCancel) { - errorToToaster({ title: i18n.ERROR_TITLE, error, dispatchToaster }); + errorToToaster({ + title: i18n.ERROR_TITLE, + error: error.body && error.body.message ? new Error(error.body.message) : error, + dispatchToaster, + }); dispatch({ type: FETCH_FAILURE }); } } diff --git a/x-pack/legacy/plugins/siem/public/containers/case/use_get_cases.tsx b/x-pack/legacy/plugins/siem/public/containers/case/use_get_cases.tsx index e73b251477bf3..76e9b5c138269 100644 --- a/x-pack/legacy/plugins/siem/public/containers/case/use_get_cases.tsx +++ b/x-pack/legacy/plugins/siem/public/containers/case/use_get_cases.tsx @@ -4,15 +4,14 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Dispatch, SetStateAction, useCallback, useEffect, useReducer, useState } from 'react'; -import { isEqual } from 'lodash/fp'; +import { useCallback, useEffect, useReducer } from 'react'; import { DEFAULT_TABLE_ACTIVE_PAGE, DEFAULT_TABLE_LIMIT } from './constants'; import { AllCases, SortFieldCase, FilterOptions, QueryParams, Case } from './types'; import { errorToToaster } from '../../components/ml/api/error_to_toaster'; import { useStateToaster } from '../../components/toasters'; import * as i18n from './translations'; import { UpdateByKey } from './use_update_case'; -import { getCases, updateCaseProperty } from './api'; +import { getCases, patchCase } from './api'; export interface UseGetCasesState { caseCount: CaseCount; @@ -109,11 +108,11 @@ const initialData: AllCases = { total: 0, }; interface UseGetCases extends UseGetCasesState { - dispatchUpdateCaseProperty: Dispatch; - getCaseCount: Dispatch; - setFilters: Dispatch>; - setQueryParams: Dispatch>>; - setSelectedCases: Dispatch; + dispatchUpdateCaseProperty: ({ updateKey, updateValue, caseId, version }: UpdateCase) => void; + getCaseCount: (caseState: keyof CaseCount) => void; + setFilters: (filters: FilterOptions) => void; + setQueryParams: (queryParams: QueryParams) => void; + setSelectedCases: (mySelectedCases: Case[]) => void; } export const useGetCases = (): UseGetCases => { const [state, dispatch] = useReducer(dataFetchReducer, { @@ -138,33 +137,27 @@ export const useGetCases = (): UseGetCases => { selectedCases: [], }); const [, dispatchToaster] = useStateToaster(); - const [filterQuery, setFilters] = useState(state.filterOptions); - const [queryParams, setQueryParams] = useState>(state.queryParams); const setSelectedCases = useCallback((mySelectedCases: Case[]) => { dispatch({ type: 'UPDATE_TABLE_SELECTIONS', payload: mySelectedCases }); }, []); - useEffect(() => { - if (!isEqual(queryParams, state.queryParams)) { - dispatch({ type: 'UPDATE_QUERY_PARAMS', payload: queryParams }); - } - }, [queryParams, state.queryParams]); + const setQueryParams = useCallback((newQueryParams: QueryParams) => { + dispatch({ type: 'UPDATE_QUERY_PARAMS', payload: newQueryParams }); + }, []); - useEffect(() => { - if (!isEqual(filterQuery, state.filterOptions)) { - dispatch({ type: 'UPDATE_FILTER_OPTIONS', payload: filterQuery }); - } - }, [filterQuery, state.filterOptions]); + const setFilters = useCallback((newFilters: FilterOptions) => { + dispatch({ type: 'UPDATE_FILTER_OPTIONS', payload: newFilters }); + }, []); - const fetchCases = useCallback(() => { + const fetchCases = useCallback((filterOptions: FilterOptions, queryParams: QueryParams) => { let didCancel = false; const fetchData = async () => { dispatch({ type: 'FETCH_INIT', payload: 'cases' }); try { const response = await getCases({ - filterOptions: state.filterOptions, - queryParams: state.queryParams, + filterOptions, + queryParams, }); if (!didCancel) { dispatch({ @@ -174,7 +167,11 @@ export const useGetCases = (): UseGetCases => { } } catch (error) { if (!didCancel) { - errorToToaster({ title: i18n.ERROR_TITLE, error, dispatchToaster }); + errorToToaster({ + title: i18n.ERROR_TITLE, + error: error.body && error.body.message ? new Error(error.body.message) : error, + dispatchToaster, + }); dispatch({ type: 'FETCH_FAILURE', payload: 'cases' }); } } @@ -183,8 +180,12 @@ export const useGetCases = (): UseGetCases => { return () => { didCancel = true; }; - }, [state.queryParams, state.filterOptions]); - useEffect(() => fetchCases(), [state.queryParams, state.filterOptions]); + }, []); + + useEffect(() => fetchCases(state.filterOptions, state.queryParams), [ + state.queryParams, + state.filterOptions, + ]); const getCaseCount = useCallback((caseState: keyof CaseCount) => { let didCancel = false; @@ -219,14 +220,14 @@ export const useGetCases = (): UseGetCases => { const fetchData = async () => { dispatch({ type: 'FETCH_INIT', payload: 'caseUpdate' }); try { - await updateCaseProperty( + await patchCase( caseId, { [updateKey]: updateValue }, version ?? '' // saved object versions are typed as string | undefined, hope that's not true ); if (!didCancel) { dispatch({ type: 'FETCH_UPDATE_CASE_SUCCESS' }); - fetchCases(); + fetchCases(state.filterOptions, state.queryParams); getCaseCount('open'); getCaseCount('closed'); } @@ -242,7 +243,7 @@ export const useGetCases = (): UseGetCases => { didCancel = true; }; }, - [filterQuery, state.filterOptions] + [state.filterOptions, state.queryParams] ); return { diff --git a/x-pack/legacy/plugins/siem/public/containers/case/use_get_tags.tsx b/x-pack/legacy/plugins/siem/public/containers/case/use_get_tags.tsx index f796ae550c9ec..7d3e00a4f2be4 100644 --- a/x-pack/legacy/plugins/siem/public/containers/case/use_get_tags.tsx +++ b/x-pack/legacy/plugins/siem/public/containers/case/use_get_tags.tsx @@ -5,12 +5,12 @@ */ import { useEffect, useReducer } from 'react'; -import chrome from 'ui/chrome'; import { useStateToaster } from '../../components/toasters'; import { errorToToaster } from '../../components/ml/api/error_to_toaster'; -import * as i18n from './translations'; + +import { getTags } from './api'; import { FETCH_FAILURE, FETCH_INIT, FETCH_SUCCESS } from './constants'; -import { throwIfNotOk } from '../../hooks/api/api'; +import * as i18n from './translations'; interface TagsState { data: string[]; @@ -63,22 +63,17 @@ export const useGetTags = (): [TagsState] => { const fetchData = async () => { dispatch({ type: FETCH_INIT }); try { - const response = await fetch(`${chrome.getBasePath()}/api/cases/tags`, { - method: 'GET', - credentials: 'same-origin', - headers: { - 'content-type': 'application/json', - 'kbn-system-api': 'true', - }, - }); + const response = await getTags(); if (!didCancel) { - await throwIfNotOk(response); - const responseJson = await response.json(); - dispatch({ type: FETCH_SUCCESS, payload: responseJson }); + dispatch({ type: FETCH_SUCCESS, payload: response }); } } catch (error) { if (!didCancel) { - errorToToaster({ title: i18n.ERROR_TITLE, error, dispatchToaster }); + errorToToaster({ + title: i18n.ERROR_TITLE, + error: error.body && error.body.message ? new Error(error.body.message) : error, + dispatchToaster, + }); dispatch({ type: FETCH_FAILURE }); } } diff --git a/x-pack/legacy/plugins/siem/public/containers/case/use_post_case.tsx b/x-pack/legacy/plugins/siem/public/containers/case/use_post_case.tsx index 0fcc8a3a1abec..7497b30395155 100644 --- a/x-pack/legacy/plugins/siem/public/containers/case/use_post_case.tsx +++ b/x-pack/legacy/plugins/siem/public/containers/case/use_post_case.tsx @@ -4,24 +4,25 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Dispatch, SetStateAction, useEffect, useReducer, useState } from 'react'; +import { useReducer, useCallback } from 'react'; + +import { CaseRequest } from '../../../../../../plugins/case/common/api'; import { useStateToaster } from '../../components/toasters'; import { errorToToaster } from '../../components/ml/api/error_to_toaster'; + +import { postCase } from './api'; +import { FETCH_FAILURE, FETCH_INIT, FETCH_SUCCESS } from './constants'; import * as i18n from './translations'; -import { FETCH_FAILURE, FETCH_INIT, FETCH_SUCCESS, POST_NEW_CASE } from './constants'; -import { Case, NewCase } from './types'; -import { createCase } from './api'; -import { getTypedPayload } from './utils'; +import { Case } from './types'; interface NewCaseState { - data: NewCase; - newCase?: Case; + caseData: Case | null; isLoading: boolean; isError: boolean; } interface Action { type: string; - payload?: NewCase | Case; + payload?: Case; } const dataFetchReducer = (state: NewCaseState, action: Action): NewCaseState => { @@ -32,19 +33,12 @@ const dataFetchReducer = (state: NewCaseState, action: Action): NewCaseState => isLoading: true, isError: false, }; - case POST_NEW_CASE: - return { - ...state, - isLoading: false, - isError: false, - data: getTypedPayload(action.payload), - }; case FETCH_SUCCESS: return { ...state, isLoading: false, isError: false, - newCase: getTypedPayload(action.payload), + caseData: action.payload ?? null, }; case FETCH_FAILURE: return { @@ -56,41 +50,43 @@ const dataFetchReducer = (state: NewCaseState, action: Action): NewCaseState => throw new Error(); } }; -const initialData: NewCase = { - description: '', - isNew: false, - tags: [], - title: '', -}; -export const usePostCase = (): [NewCaseState, Dispatch>] => { +interface UsePostCase extends NewCaseState { + postCase: (data: CaseRequest) => void; +} +export const usePostCase = (): UsePostCase => { const [state, dispatch] = useReducer(dataFetchReducer, { isLoading: false, isError: false, - data: initialData, + caseData: null, }); - const [formData, setFormData] = useState(initialData); const [, dispatchToaster] = useStateToaster(); - useEffect(() => { - dispatch({ type: POST_NEW_CASE, payload: formData }); - }, [formData]); - - useEffect(() => { - const postCase = async () => { + const postMyCase = useCallback(async (data: CaseRequest) => { + let cancel = false; + try { dispatch({ type: FETCH_INIT }); - try { - const { isNew, ...dataWithoutIsNew } = state.data; - const response = await createCase(dataWithoutIsNew); - dispatch({ type: FETCH_SUCCESS, payload: response }); - } catch (error) { - errorToToaster({ title: i18n.ERROR_TITLE, error, dispatchToaster }); + const response = await postCase({ ...data, state: 'open' }); + if (!cancel) { + dispatch({ + type: FETCH_SUCCESS, + payload: response, + }); + } + } catch (error) { + if (!cancel) { + errorToToaster({ + title: i18n.ERROR_TITLE, + error: error.body && error.body.message ? new Error(error.body.message) : error, + dispatchToaster, + }); dispatch({ type: FETCH_FAILURE }); } - }; - if (state.data.isNew) { - postCase(); } - }, [state.data.isNew]); - return [state, setFormData]; + return () => { + cancel = true; + }; + }, []); + + return { ...state, postCase: postMyCase }; }; diff --git a/x-pack/legacy/plugins/siem/public/containers/case/use_post_comment.tsx b/x-pack/legacy/plugins/siem/public/containers/case/use_post_comment.tsx index d8abda25af286..63d24e2935c2a 100644 --- a/x-pack/legacy/plugins/siem/public/containers/case/use_post_comment.tsx +++ b/x-pack/legacy/plugins/siem/public/containers/case/use_post_comment.tsx @@ -4,25 +4,26 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Dispatch, SetStateAction, useEffect, useReducer, useState } from 'react'; +import { useReducer, useCallback } from 'react'; + +import { CommentRequest } from '../../../../../../plugins/case/common/api'; import { useStateToaster } from '../../components/toasters'; import { errorToToaster } from '../../components/ml/api/error_to_toaster'; + +import { postComment } from './api'; +import { FETCH_FAILURE, FETCH_INIT, FETCH_SUCCESS } from './constants'; import * as i18n from './translations'; -import { FETCH_FAILURE, FETCH_INIT, FETCH_SUCCESS, POST_NEW_COMMENT } from './constants'; -import { Comment, NewComment } from './types'; -import { createComment } from './api'; -import { getTypedPayload } from './utils'; +import { Comment } from './types'; interface NewCommentState { - data: NewComment; - newComment?: Comment; + commentData: Comment | null; isLoading: boolean; isError: boolean; caseId: string; } interface Action { type: string; - payload?: NewComment | Comment; + payload?: Comment; } const dataFetchReducer = (state: NewCommentState, action: Action): NewCommentState => { @@ -33,19 +34,12 @@ const dataFetchReducer = (state: NewCommentState, action: Action): NewCommentSta isLoading: true, isError: false, }; - case POST_NEW_COMMENT: - return { - ...state, - isLoading: false, - isError: false, - data: getTypedPayload(action.payload), - }; case FETCH_SUCCESS: return { ...state, isLoading: false, isError: false, - newComment: getTypedPayload(action.payload), + commentData: action.payload ?? null, }; case FETCH_FAILURE: return { @@ -57,41 +51,42 @@ const dataFetchReducer = (state: NewCommentState, action: Action): NewCommentSta throw new Error(); } }; -const initialData: NewComment = { - comment: '', -}; -export const usePostComment = ( - caseId: string -): [NewCommentState, Dispatch>] => { +interface UsePostComment extends NewCommentState { + postComment: (data: CommentRequest) => void; +} + +export const usePostComment = (caseId: string): UsePostComment => { const [state, dispatch] = useReducer(dataFetchReducer, { + commentData: null, isLoading: false, isError: false, caseId, - data: initialData, }); - const [formData, setFormData] = useState(initialData); const [, dispatchToaster] = useStateToaster(); - useEffect(() => { - dispatch({ type: POST_NEW_COMMENT, payload: formData }); - }, [formData]); - - useEffect(() => { - const postComment = async () => { + const postMyComment = useCallback(async (data: CommentRequest) => { + let cancel = false; + try { dispatch({ type: FETCH_INIT }); - try { - const { isNew, ...dataWithoutIsNew } = state.data; - const response = await createComment(dataWithoutIsNew, state.caseId); + const response = await postComment(data, state.caseId); + if (!cancel) { dispatch({ type: FETCH_SUCCESS, payload: response }); - } catch (error) { - errorToToaster({ title: i18n.ERROR_TITLE, error, dispatchToaster }); + } + } catch (error) { + if (!cancel) { + errorToToaster({ + title: i18n.ERROR_TITLE, + error: error.body && error.body.message ? new Error(error.body.message) : error, + dispatchToaster, + }); dispatch({ type: FETCH_FAILURE }); } - }; - if (state.data.isNew) { - postComment(); } - }, [state.data.isNew]); - return [state, setFormData]; + return () => { + cancel = true; + }; + }, []); + + return { ...state, postComment: postMyComment }; }; diff --git a/x-pack/legacy/plugins/siem/public/containers/case/use_update_case.tsx b/x-pack/legacy/plugins/siem/public/containers/case/use_update_case.tsx index f23be526fbeb7..21c8fb5dc7032 100644 --- a/x-pack/legacy/plugins/siem/public/containers/case/use_update_case.tsx +++ b/x-pack/legacy/plugins/siem/public/containers/case/use_update_case.tsx @@ -4,19 +4,22 @@ * you may not use this file except in compliance with the Elastic License. */ -import { useReducer } from 'react'; +import { useReducer, useCallback } from 'react'; + +import { CaseRequest } from '../../../../../../plugins/case/common/api'; import { useStateToaster } from '../../components/toasters'; import { errorToToaster } from '../../components/ml/api/error_to_toaster'; -import * as i18n from './translations'; + +import { patchCase } from './api'; import { FETCH_FAILURE, FETCH_INIT, FETCH_SUCCESS } from './constants'; +import * as i18n from './translations'; import { Case } from './types'; -import { updateCaseProperty } from './api'; import { getTypedPayload } from './utils'; -type UpdateKey = keyof Case; +type UpdateKey = keyof CaseRequest; interface NewCaseState { - data: Case; + caseData: Case; isLoading: boolean; isError: boolean; updateKey: UpdateKey | null; @@ -24,12 +27,12 @@ interface NewCaseState { export interface UpdateByKey { updateKey: UpdateKey; - updateValue: Case[UpdateKey]; + updateValue: CaseRequest[UpdateKey]; } interface Action { type: string; - payload?: Partial | UpdateKey; + payload?: Case | UpdateKey; } const dataFetchReducer = (state: NewCaseState, action: Action): NewCaseState => { @@ -47,10 +50,7 @@ const dataFetchReducer = (state: NewCaseState, action: Action): NewCaseState => ...state, isLoading: false, isError: false, - data: { - ...state.data, - ...getTypedPayload(action.payload), - }, + caseData: getTypedPayload(action.payload), updateKey: null, }; case FETCH_FAILURE: @@ -65,32 +65,47 @@ const dataFetchReducer = (state: NewCaseState, action: Action): NewCaseState => } }; -export const useUpdateCase = ( - caseId: string, - initialData: Case -): [NewCaseState, (updates: UpdateByKey) => void] => { +interface UseUpdateCase extends NewCaseState { + updateCaseProperty: (updates: UpdateByKey) => void; +} +export const useUpdateCase = (caseId: string, initialData: Case): UseUpdateCase => { const [state, dispatch] = useReducer(dataFetchReducer, { isLoading: false, isError: false, - data: initialData, + caseData: initialData, updateKey: null, }); const [, dispatchToaster] = useStateToaster(); - const dispatchUpdateCaseProperty = async ({ updateKey, updateValue }: UpdateByKey) => { - dispatch({ type: FETCH_INIT, payload: updateKey }); - try { - const response = await updateCaseProperty( - caseId, - { [updateKey]: updateValue }, - state.data.version ?? '' // saved object versions are typed as string | undefined, hope that's not true - ); - dispatch({ type: FETCH_SUCCESS, payload: response }); - } catch (error) { - errorToToaster({ title: i18n.ERROR_TITLE, error, dispatchToaster }); - dispatch({ type: FETCH_FAILURE }); - } - }; + const dispatchUpdateCaseProperty = useCallback( + async ({ updateKey, updateValue }: UpdateByKey) => { + let cancel = false; + try { + dispatch({ type: FETCH_INIT, payload: updateKey }); + const response = await patchCase( + caseId, + { [updateKey]: updateValue }, + state.caseData.version + ); + if (!cancel) { + dispatch({ type: FETCH_SUCCESS, payload: response }); + } + } catch (error) { + if (!cancel) { + errorToToaster({ + title: i18n.ERROR_TITLE, + error: error.body && error.body.message ? new Error(error.body.message) : error, + dispatchToaster, + }); + dispatch({ type: FETCH_FAILURE }); + } + } + return () => { + cancel = true; + }; + }, + [state] + ); - return [state, dispatchUpdateCaseProperty]; + return { ...state, updateCaseProperty: dispatchUpdateCaseProperty }; }; diff --git a/x-pack/legacy/plugins/siem/public/containers/case/use_update_comment.tsx b/x-pack/legacy/plugins/siem/public/containers/case/use_update_comment.tsx index bc8369117433a..d7649cb7d8fdb 100644 --- a/x-pack/legacy/plugins/siem/public/containers/case/use_update_comment.tsx +++ b/x-pack/legacy/plugins/siem/public/containers/case/use_update_comment.tsx @@ -4,17 +4,19 @@ * you may not use this file except in compliance with the Elastic License. */ -import { useReducer, useRef } from 'react'; +import { useReducer, useCallback } from 'react'; + import { useStateToaster } from '../../components/toasters'; import { errorToToaster } from '../../components/ml/api/error_to_toaster'; -import * as i18n from './translations'; + +import { patchComment } from './api'; import { FETCH_FAILURE, FETCH_INIT, FETCH_SUCCESS } from './constants'; +import * as i18n from './translations'; import { Comment } from './types'; -import { updateComment } from './api'; import { getTypedPayload } from './utils'; -interface CommetUpdateState { - data: Comment[]; +interface CommentUpdateState { + comments: Comment[]; isLoadingIds: string[]; isError: boolean; } @@ -29,7 +31,7 @@ interface Action { payload?: CommentUpdate | string; } -const dataFetchReducer = (state: CommetUpdateState, action: Action): CommetUpdateState => { +const dataFetchReducer = (state: CommentUpdateState, action: Action): CommentUpdateState => { switch (action.type) { case FETCH_INIT: return { @@ -40,15 +42,19 @@ const dataFetchReducer = (state: CommetUpdateState, action: Action): CommetUpdat case FETCH_SUCCESS: const updatePayload = getTypedPayload(action.payload); - const foundIndex = state.data.findIndex( - comment => comment.commentId === updatePayload.commentId + const foundIndex = state.comments.findIndex( + comment => comment.id === updatePayload.commentId ); - state.data[foundIndex] = { ...state.data[foundIndex], ...updatePayload.update }; + const newComments = state.comments; + if (foundIndex !== -1) { + newComments[foundIndex] = { ...state.comments[foundIndex], ...updatePayload.update }; + } + return { ...state, isLoadingIds: state.isLoadingIds.filter(id => updatePayload.commentId !== id), isError: false, - data: [...state.data], + comments: newComments, }; case FETCH_FAILURE: return { @@ -63,30 +69,46 @@ const dataFetchReducer = (state: CommetUpdateState, action: Action): CommetUpdat } }; -export const useUpdateComment = ( - comments: Comment[] -): [CommetUpdateState, (commentId: string, commentUpdate: string) => void] => { +interface UseUpdateComment extends CommentUpdateState { + updateComment: (commentId: string, commentUpdate: string) => void; +} + +export const useUpdateComment = (comments: Comment[]): UseUpdateComment => { const [state, dispatch] = useReducer(dataFetchReducer, { isLoadingIds: [], isError: false, - data: comments, + comments, }); - const dispatchUpdateComment = useRef<(commentId: string, commentUpdate: string) => void>(); const [, dispatchToaster] = useStateToaster(); - dispatchUpdateComment.current = async (commentId: string, commentUpdate: string) => { - dispatch({ type: FETCH_INIT, payload: commentId }); - try { - const currentComment = state.data.find(comment => comment.commentId === commentId) ?? { - version: '', + const dispatchUpdateComment = useCallback( + async (commentId: string, commentUpdate: string) => { + let cancel = false; + try { + dispatch({ type: FETCH_INIT, payload: commentId }); + const currentComment = state.comments.find(comment => comment.id === commentId) ?? { + version: '', + }; + const response = await patchComment(commentId, commentUpdate, currentComment.version); + if (!cancel) { + dispatch({ type: FETCH_SUCCESS, payload: { update: response, commentId } }); + } + } catch (error) { + if (!cancel) { + errorToToaster({ + title: i18n.ERROR_TITLE, + error: error.body && error.body.message ? new Error(error.body.message) : error, + dispatchToaster, + }); + dispatch({ type: FETCH_FAILURE, payload: commentId }); + } + } + return () => { + cancel = true; }; - const response = await updateComment(commentId, commentUpdate, currentComment.version); - dispatch({ type: FETCH_SUCCESS, payload: { update: response, commentId } }); - } catch (error) { - errorToToaster({ title: i18n.ERROR_TITLE, error, dispatchToaster }); - dispatch({ type: FETCH_FAILURE, payload: commentId }); - } - }; + }, + [state] + ); - return [state, dispatchUpdateComment.current]; + return { ...state, updateComment: dispatchUpdateComment }; }; diff --git a/x-pack/legacy/plugins/siem/public/containers/case/utils.ts b/x-pack/legacy/plugins/siem/public/containers/case/utils.ts index 14a3819bdfdad..a377c496fe726 100644 --- a/x-pack/legacy/plugins/siem/public/containers/case/utils.ts +++ b/x-pack/legacy/plugins/siem/public/containers/case/utils.ts @@ -5,7 +5,21 @@ */ import { camelCase, isArray, isObject, set } from 'lodash'; -import { AllCases, AllCasesSnake, Case, CaseSnake } from './types'; +import { fold } from 'fp-ts/lib/Either'; +import { identity } from 'fp-ts/lib/function'; +import { pipe } from 'fp-ts/lib/pipeable'; + +import { + CaseResponse, + CaseResponseRt, + CasesResponse, + CasesResponseRt, + throwErrors, + CommentResponse, + CommentResponseRt, +} from '../../../../../../plugins/case/common/api'; +import { ToasterErrors } from '../../hooks/api/throw_if_not_ok'; +import { AllCases, Case } from './types'; export const getTypedPayload = (a: unknown): T => a as T; @@ -32,9 +46,20 @@ export const convertToCamelCase = (snakeCase: T): U => return acc; }, {} as U); -export const convertAllCasesToCamel = (snakeCases: AllCasesSnake): AllCases => ({ - cases: snakeCases.cases.map(snakeCase => convertToCamelCase(snakeCase)), +export const convertAllCasesToCamel = (snakeCases: CasesResponse): AllCases => ({ + cases: snakeCases.cases.map(snakeCase => convertToCamelCase(snakeCase)), page: snakeCases.page, perPage: snakeCases.per_page, total: snakeCases.total, }); + +export const createToasterPlainError = (message: string) => new ToasterErrors([message]); + +export const decodeCaseResponse = (respCase?: CaseResponse) => + pipe(CaseResponseRt.decode(respCase), fold(throwErrors(createToasterPlainError), identity)); + +export const decodeCasesResponse = (respCases?: CasesResponse) => + pipe(CasesResponseRt.decode(respCases), fold(throwErrors(createToasterPlainError), identity)); + +export const decodeCommentResponse = (respComment?: CommentResponse) => + pipe(CommentResponseRt.decode(respComment), fold(throwErrors(createToasterPlainError), identity)); diff --git a/x-pack/legacy/plugins/siem/public/containers/detection_engine/rules/api.test.ts b/x-pack/legacy/plugins/siem/public/containers/detection_engine/rules/api.test.ts index b348678e789f8..05446577a0fa0 100644 --- a/x-pack/legacy/plugins/siem/public/containers/detection_engine/rules/api.test.ts +++ b/x-pack/legacy/plugins/siem/public/containers/detection_engine/rules/api.test.ts @@ -20,7 +20,7 @@ import { getPrePackagedRulesStatus, } from './api'; import { ruleMock, rulesMock } from './mock'; -import { ToasterErrors } from '../../../components/ml/api/throw_if_not_ok'; +import { ToasterErrors } from '../../../hooks/api/throw_if_not_ok'; const abortCtrl = new AbortController(); const mockKibanaServices = KibanaServices.get as jest.Mock; diff --git a/x-pack/legacy/plugins/siem/public/containers/detection_engine/signals/errors_types/get_index_error.ts b/x-pack/legacy/plugins/siem/public/containers/detection_engine/signals/errors_types/get_index_error.ts index 4f45b480772f2..79dae5b8acb87 100644 --- a/x-pack/legacy/plugins/siem/public/containers/detection_engine/signals/errors_types/get_index_error.ts +++ b/x-pack/legacy/plugins/siem/public/containers/detection_engine/signals/errors_types/get_index_error.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { MessageBody } from '../../../../components/ml/api/throw_if_not_ok'; +import { MessageBody } from '../../../../hooks/api/throw_if_not_ok'; export class SignalIndexError extends Error { message: string = ''; diff --git a/x-pack/legacy/plugins/siem/public/containers/detection_engine/signals/errors_types/post_index_error.ts b/x-pack/legacy/plugins/siem/public/containers/detection_engine/signals/errors_types/post_index_error.ts index d6d8cccfb4540..227699af71b42 100644 --- a/x-pack/legacy/plugins/siem/public/containers/detection_engine/signals/errors_types/post_index_error.ts +++ b/x-pack/legacy/plugins/siem/public/containers/detection_engine/signals/errors_types/post_index_error.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { MessageBody } from '../../../../components/ml/api/throw_if_not_ok'; +import { MessageBody } from '../../../../hooks/api/throw_if_not_ok'; export class PostSignalError extends Error { message: string = ''; diff --git a/x-pack/legacy/plugins/siem/public/containers/detection_engine/signals/errors_types/privilege_user_error.ts b/x-pack/legacy/plugins/siem/public/containers/detection_engine/signals/errors_types/privilege_user_error.ts index 5cd458a7fe9aa..19915e898bbeb 100644 --- a/x-pack/legacy/plugins/siem/public/containers/detection_engine/signals/errors_types/privilege_user_error.ts +++ b/x-pack/legacy/plugins/siem/public/containers/detection_engine/signals/errors_types/privilege_user_error.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { MessageBody } from '../../../../components/ml/api/throw_if_not_ok'; +import { MessageBody } from '../../../../hooks/api/throw_if_not_ok'; export class PrivilegeUserError extends Error { message: string = ''; diff --git a/x-pack/legacy/plugins/siem/public/hooks/api/api.tsx b/x-pack/legacy/plugins/siem/public/hooks/api/api.tsx index 69848c08fa3f8..1dfd6416531ee 100644 --- a/x-pack/legacy/plugins/siem/public/hooks/api/api.tsx +++ b/x-pack/legacy/plugins/siem/public/hooks/api/api.tsx @@ -6,7 +6,7 @@ import * as i18n from '../translations'; import { StartServices } from '../../plugin'; -import { parseJsonFromBody, ToasterErrors } from '../../components/ml/api/throw_if_not_ok'; +import { parseJsonFromBody, ToasterErrors } from './throw_if_not_ok'; import { IndexPatternSavedObject, IndexPatternSavedObjectAttributes } from '../types'; /** diff --git a/x-pack/legacy/plugins/siem/public/components/ml/api/throw_if_not_ok.test.ts b/x-pack/legacy/plugins/siem/public/hooks/api/throw_if_not_ok.test.ts similarity index 99% rename from x-pack/legacy/plugins/siem/public/components/ml/api/throw_if_not_ok.test.ts rename to x-pack/legacy/plugins/siem/public/hooks/api/throw_if_not_ok.test.ts index 9fd0010535203..bc0c765d6f2df 100644 --- a/x-pack/legacy/plugins/siem/public/components/ml/api/throw_if_not_ok.test.ts +++ b/x-pack/legacy/plugins/siem/public/hooks/api/throw_if_not_ok.test.ts @@ -14,7 +14,7 @@ import { ToasterErrors, tryParseResponse, } from './throw_if_not_ok'; -import { SetupMlResponse } from '../../ml_popover/types'; +import { SetupMlResponse } from '../../components/ml_popover/types'; describe('throw_if_not_ok', () => { afterEach(() => { diff --git a/x-pack/legacy/plugins/siem/public/components/ml/api/throw_if_not_ok.ts b/x-pack/legacy/plugins/siem/public/hooks/api/throw_if_not_ok.ts similarity index 91% rename from x-pack/legacy/plugins/siem/public/components/ml/api/throw_if_not_ok.ts rename to x-pack/legacy/plugins/siem/public/hooks/api/throw_if_not_ok.ts index 6ca843207a15e..7d70106b0e562 100644 --- a/x-pack/legacy/plugins/siem/public/components/ml/api/throw_if_not_ok.ts +++ b/x-pack/legacy/plugins/siem/public/hooks/api/throw_if_not_ok.ts @@ -6,11 +6,11 @@ import { has } from 'lodash/fp'; -import * as i18n from './translations'; -import { MlError } from '../types'; -import { SetupMlResponse } from '../../ml_popover/types'; +import * as i18n from '../../components/ml/api/translations'; +import { MlError } from '../../components/ml/types'; +import { SetupMlResponse } from '../../components/ml_popover/types'; -export { MessageBody, parseJsonFromBody } from '../../../utils/api'; +export { MessageBody, parseJsonFromBody } from '../../utils/api'; export interface MlStartJobError { error: MlError; diff --git a/x-pack/legacy/plugins/siem/public/pages/case/components/add_comment/index.tsx b/x-pack/legacy/plugins/siem/public/pages/case/components/add_comment/index.tsx index c8e0dafcf5742..16c6101b80d40 100644 --- a/x-pack/legacy/plugins/siem/public/pages/case/components/add_comment/index.tsx +++ b/x-pack/legacy/plugins/siem/public/pages/case/components/add_comment/index.tsx @@ -3,15 +3,17 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -import React, { useCallback } from 'react'; + import { EuiButton, EuiLoadingSpinner } from '@elastic/eui'; +import React, { useCallback } from 'react'; import styled from 'styled-components'; -import { Form, useForm, UseField } from '../../../../shared_imports'; -import { NewComment } from '../../../../containers/case/types'; + +import { CommentRequest } from '../../../../../../../../plugins/case/common/api'; import { usePostComment } from '../../../../containers/case/use_post_comment'; -import { schema } from './schema'; -import * as i18n from '../../translations'; import { MarkdownEditorForm } from '../../../../components/markdown_editor/form'; +import { Form, useForm, UseField } from '../../../../shared_imports'; +import * as i18n from '../../translations'; +import { schema } from './schema'; const MySpinner = styled(EuiLoadingSpinner)` position: absolute; @@ -19,24 +21,26 @@ const MySpinner = styled(EuiLoadingSpinner)` left: 50%; `; +const initialCommentValue: CommentRequest = { + comment: '', +}; + export const AddComment = React.memo<{ caseId: string; }>(({ caseId }) => { - const [{ data, isLoading, newComment }, setFormData] = usePostComment(caseId); - const { form } = useForm({ - defaultValue: data, + const { commentData, isLoading, postComment } = usePostComment(caseId); + const { form } = useForm({ + defaultValue: initialCommentValue, options: { stripEmptyFields: false }, schema, }); const onSubmit = useCallback(async () => { - const { isValid, data: newData } = await form.submit(); - if (isValid && newData.comment) { - setFormData({ ...newData, isNew: true } as NewComment); - } else if (isValid && data.comment) { - setFormData({ ...data, ...newData, isNew: true } as NewComment); + const { isValid, data } = await form.submit(); + if (isValid) { + await postComment(data); } - }, [form, data]); + }, [form]); return ( <> @@ -64,7 +68,7 @@ export const AddComment = React.memo<{ }} /> - {newComment && + {commentData != null && 'TO DO new comment got added but we didnt update the UI yet. Refresh the page to see your comment ;)'} ); diff --git a/x-pack/legacy/plugins/siem/public/pages/case/components/add_comment/schema.tsx b/x-pack/legacy/plugins/siem/public/pages/case/components/add_comment/schema.tsx index 5f30f59149d99..c61874a8dabfc 100644 --- a/x-pack/legacy/plugins/siem/public/pages/case/components/add_comment/schema.tsx +++ b/x-pack/legacy/plugins/siem/public/pages/case/components/add_comment/schema.tsx @@ -3,12 +3,14 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ + +import { CommentRequest } from '../../../../../../../../plugins/case/common/api'; import { FIELD_TYPES, fieldValidators, FormSchema } from '../../../../shared_imports'; import * as i18n from '../../translations'; const { emptyField } = fieldValidators; -export const schema: FormSchema = { +export const schema: FormSchema = { comment: { type: FIELD_TYPES.TEXTAREA, validations: [ diff --git a/x-pack/legacy/plugins/siem/public/pages/case/components/all_cases/__mock__/index.tsx b/x-pack/legacy/plugins/siem/public/pages/case/components/all_cases/__mock__/index.tsx index a054d685399bc..2e57e5f2f95d9 100644 --- a/x-pack/legacy/plugins/siem/public/pages/case/components/all_cases/__mock__/index.tsx +++ b/x-pack/legacy/plugins/siem/public/pages/case/components/all_cases/__mock__/index.tsx @@ -11,7 +11,7 @@ export const useGetCasesMockState: UseGetCasesState = { data: { cases: [ { - caseId: '3c4ddcc0-4e99-11ea-9290-35d05cb55c15', + id: '3c4ddcc0-4e99-11ea-9290-35d05cb55c15', createdAt: '2020-02-13T19:44:23.627Z', createdBy: { username: 'elastic' }, comments: [], @@ -23,7 +23,7 @@ export const useGetCasesMockState: UseGetCasesState = { version: 'WzQ3LDFd', }, { - caseId: '362a5c10-4e99-11ea-9290-35d05cb55c15', + id: '362a5c10-4e99-11ea-9290-35d05cb55c15', createdAt: '2020-02-13T19:44:13.328Z', createdBy: { username: 'elastic' }, comments: [], @@ -35,7 +35,7 @@ export const useGetCasesMockState: UseGetCasesState = { version: 'WzQ3LDFd', }, { - caseId: '34f8b9e0-4e99-11ea-9290-35d05cb55c15', + id: '34f8b9e0-4e99-11ea-9290-35d05cb55c15', createdAt: '2020-02-13T19:44:11.328Z', createdBy: { username: 'elastic' }, comments: [], @@ -47,7 +47,7 @@ export const useGetCasesMockState: UseGetCasesState = { version: 'WzQ3LDFd', }, { - caseId: '31890e90-4e99-11ea-9290-35d05cb55c15', + id: '31890e90-4e99-11ea-9290-35d05cb55c15', createdAt: '2020-02-13T19:44:05.563Z', createdBy: { username: 'elastic' }, comments: [], @@ -59,7 +59,7 @@ export const useGetCasesMockState: UseGetCasesState = { version: 'WzQ3LDFd', }, { - caseId: '2f5b3210-4e99-11ea-9290-35d05cb55c15', + id: '2f5b3210-4e99-11ea-9290-35d05cb55c15', createdAt: '2020-02-13T19:44:01.901Z', createdBy: { username: 'elastic' }, comments: [], diff --git a/x-pack/legacy/plugins/siem/public/pages/case/components/all_cases/actions.tsx b/x-pack/legacy/plugins/siem/public/pages/case/components/all_cases/actions.tsx index 5dad19b1e54d3..0ec09f2b57918 100644 --- a/x-pack/legacy/plugins/siem/public/pages/case/components/all_cases/actions.tsx +++ b/x-pack/legacy/plugins/siem/public/pages/case/components/all_cases/actions.tsx @@ -24,7 +24,7 @@ export const getActions = ({ icon: 'trash', name: i18n.DELETE, // eslint-disable-next-line no-console - onClick: ({ caseId }: Case) => console.log('TO DO Delete case', caseId), + onClick: ({ id }: Case) => console.log('TO DO Delete case', id), type: 'icon', 'data-test-subj': 'action-delete', }, @@ -37,7 +37,7 @@ export const getActions = ({ dispatchUpdate({ updateKey: 'state', updateValue: 'closed', - caseId: theCase.caseId, + caseId: theCase.id, version: theCase.version, }), type: 'icon', @@ -51,7 +51,7 @@ export const getActions = ({ dispatchUpdate({ updateKey: 'state', updateValue: 'open', - caseId: theCase.caseId, + caseId: theCase.id, version: theCase.version, }), type: 'icon', diff --git a/x-pack/legacy/plugins/siem/public/pages/case/components/all_cases/columns.tsx b/x-pack/legacy/plugins/siem/public/pages/case/components/all_cases/columns.tsx index 41a2bdf52d5a1..f6ed2694fdc40 100644 --- a/x-pack/legacy/plugins/siem/public/pages/case/components/all_cases/columns.tsx +++ b/x-pack/legacy/plugins/siem/public/pages/case/components/all_cases/columns.tsx @@ -42,9 +42,9 @@ export const getCasesColumns = ( { name: i18n.NAME, render: (theCase: Case) => { - if (theCase.caseId != null && theCase.title != null) { + if (theCase.id != null && theCase.title != null) { const caseDetailsLinkComponent = ( - {theCase.title} + {theCase.title} ); return theCase.state === 'open' ? ( caseDetailsLinkComponent diff --git a/x-pack/legacy/plugins/siem/public/pages/case/components/all_cases/index.test.tsx b/x-pack/legacy/plugins/siem/public/pages/case/components/all_cases/index.test.tsx index dd584f3f716b6..40a76c636954f 100644 --- a/x-pack/legacy/plugins/siem/public/pages/case/components/all_cases/index.test.tsx +++ b/x-pack/legacy/plugins/siem/public/pages/case/components/all_cases/index.test.tsx @@ -41,7 +41,7 @@ describe('AllCases', () => { .find(`a[data-test-subj="case-details-link"]`) .first() .prop('href') - ).toEqual(`#/link-to/case/${useGetCasesMockState.data.cases[0].caseId}`); + ).toEqual(`#/link-to/case/${useGetCasesMockState.data.cases[0].id}`); expect( wrapper .find(`a[data-test-subj="case-details-link"]`) diff --git a/x-pack/legacy/plugins/siem/public/pages/case/components/case_view/__mock__/index.tsx b/x-pack/legacy/plugins/siem/public/pages/case/components/case_view/__mock__/index.tsx index 89d321c6d106a..c2d3cae6774b0 100644 --- a/x-pack/legacy/plugins/siem/public/pages/case/components/case_view/__mock__/index.tsx +++ b/x-pack/legacy/plugins/siem/public/pages/case/components/case_view/__mock__/index.tsx @@ -10,11 +10,11 @@ import { Case } from '../../../../../containers/case/types'; export const caseProps: CaseProps = { caseId: '3c4ddcc0-4e99-11ea-9290-35d05cb55c15', initialData: { - caseId: '3c4ddcc0-4e99-11ea-9290-35d05cb55c15', + id: '3c4ddcc0-4e99-11ea-9290-35d05cb55c15', comments: [ { comment: 'Solve this fast!', - commentId: 'a357c6a0-5435-11ea-b427-fb51a1fcb7b8', + id: 'a357c6a0-5435-11ea-b427-fb51a1fcb7b8', createdAt: '2020-02-20T23:06:33.798Z', createdBy: { fullName: 'Steph Milovic', @@ -36,11 +36,11 @@ export const caseProps: CaseProps = { }; export const data: Case = { - caseId: '3c4ddcc0-4e99-11ea-9290-35d05cb55c15', + id: '3c4ddcc0-4e99-11ea-9290-35d05cb55c15', comments: [ { comment: 'Solve this fast!', - commentId: 'a357c6a0-5435-11ea-b427-fb51a1fcb7b8', + id: 'a357c6a0-5435-11ea-b427-fb51a1fcb7b8', createdAt: '2020-02-20T23:06:33.798Z', createdBy: { fullName: 'Steph Milovic', diff --git a/x-pack/legacy/plugins/siem/public/pages/case/components/case_view/index.test.tsx b/x-pack/legacy/plugins/siem/public/pages/case/components/case_view/index.test.tsx index 1539b3de5a0c1..e3bbfc0a83d71 100644 --- a/x-pack/legacy/plugins/siem/public/pages/case/components/case_view/index.test.tsx +++ b/x-pack/legacy/plugins/siem/public/pages/case/components/case_view/index.test.tsx @@ -12,16 +12,17 @@ import { caseProps, data } from './__mock__'; import { TestProviders } from '../../../../mock'; describe('CaseView ', () => { - const dispatchUpdateCaseProperty = jest.fn(); + const updateCaseProperty = jest.fn(); beforeEach(() => { jest.resetAllMocks(); - jest - .spyOn(apiHook, 'useUpdateCase') - .mockReturnValue([ - { data, isLoading: false, isError: false, updateKey: null }, - dispatchUpdateCaseProperty, - ]); + jest.spyOn(apiHook, 'useUpdateCase').mockReturnValue({ + caseData: data, + isLoading: false, + isError: false, + updateKey: null, + updateCaseProperty, + }); }); it('should render CaseComponent', () => { @@ -79,7 +80,7 @@ describe('CaseView ', () => { .find('input[data-test-subj="toggle-case-state"]') .simulate('change', { target: { value: false } }); - expect(dispatchUpdateCaseProperty).toBeCalledWith({ + expect(updateCaseProperty).toBeCalledWith({ updateKey: 'state', updateValue: 'closed', }); @@ -94,7 +95,7 @@ describe('CaseView ', () => { expect( wrapper .find( - `div[data-test-subj="user-action-${data.comments[0].commentId}-avatar"] [data-test-subj="user-action-avatar"]` + `div[data-test-subj="user-action-${data.comments[0].id}-avatar"] [data-test-subj="user-action-avatar"]` ) .first() .prop('name') @@ -103,7 +104,7 @@ describe('CaseView ', () => { expect( wrapper .find( - `div[data-test-subj="user-action-${data.comments[0].commentId}"] [data-test-subj="user-action-title"] strong` + `div[data-test-subj="user-action-${data.comments[0].id}"] [data-test-subj="user-action-title"] strong` ) .first() .text() @@ -112,7 +113,7 @@ describe('CaseView ', () => { expect( wrapper .find( - `div[data-test-subj="user-action-${data.comments[0].commentId}"] [data-test-subj="markdown"]` + `div[data-test-subj="user-action-${data.comments[0].id}"] [data-test-subj="markdown"]` ) .first() .prop('source') diff --git a/x-pack/legacy/plugins/siem/public/pages/case/components/case_view/index.tsx b/x-pack/legacy/plugins/siem/public/pages/case/components/case_view/index.tsx index 605f9e8fa1713..c917d27aebea3 100644 --- a/x-pack/legacy/plugins/siem/public/pages/case/components/case_view/index.tsx +++ b/x-pack/legacy/plugins/siem/public/pages/case/components/case_view/index.tsx @@ -60,10 +60,7 @@ export interface CaseProps { } export const CaseComponent = React.memo(({ caseId, initialData }) => { - const [{ data, isLoading, updateKey }, dispatchUpdateCaseProperty] = useUpdateCase( - caseId, - initialData - ); + const { caseData, isLoading, updateKey, updateCaseProperty } = useUpdateCase(caseId, initialData); const onUpdateField = useCallback( (newUpdateKey: keyof Case, updateValue: Case[keyof Case]) => { @@ -71,7 +68,7 @@ export const CaseComponent = React.memo(({ caseId, initialData }) => case 'title': const titleUpdate = getTypedPayload(updateValue); if (titleUpdate.length > 0) { - dispatchUpdateCaseProperty({ + updateCaseProperty({ updateKey: 'title', updateValue: titleUpdate, }); @@ -80,7 +77,7 @@ export const CaseComponent = React.memo(({ caseId, initialData }) => case 'description': const descriptionUpdate = getTypedPayload(updateValue); if (descriptionUpdate.length > 0) { - dispatchUpdateCaseProperty({ + updateCaseProperty({ updateKey: 'description', updateValue: descriptionUpdate, }); @@ -88,15 +85,15 @@ export const CaseComponent = React.memo(({ caseId, initialData }) => break; case 'tags': const tagsUpdate = getTypedPayload(updateValue); - dispatchUpdateCaseProperty({ + updateCaseProperty({ updateKey: 'tags', updateValue: tagsUpdate, }); break; case 'state': const stateUpdate = getTypedPayload(updateValue); - if (data.state !== updateValue) { - dispatchUpdateCaseProperty({ + if (caseData.state !== updateValue) { + updateCaseProperty({ updateKey: 'state', updateValue: stateUpdate, }); @@ -105,7 +102,7 @@ export const CaseComponent = React.memo(({ caseId, initialData }) => return null; } }, - [dispatchUpdateCaseProperty, data.state] + [updateCaseProperty, caseData.state] ); // TO DO refactor each of these const's into their own components @@ -146,11 +143,11 @@ export const CaseComponent = React.memo(({ caseId, initialData }) => titleNode={ } - title={data.title} + title={caseData.title} > @@ -160,10 +157,10 @@ export const CaseComponent = React.memo(({ caseId, initialData }) => {i18n.STATUS} - {data.state} + {caseData.state} @@ -172,7 +169,7 @@ export const CaseComponent = React.memo(({ caseId, initialData }) => @@ -184,10 +181,10 @@ export const CaseComponent = React.memo(({ caseId, initialData }) => @@ -204,7 +201,7 @@ export const CaseComponent = React.memo(({ caseId, initialData }) => @@ -213,11 +210,11 @@ export const CaseComponent = React.memo(({ caseId, initialData }) => diff --git a/x-pack/legacy/plugins/siem/public/pages/case/components/create/index.tsx b/x-pack/legacy/plugins/siem/public/pages/case/components/create/index.tsx index 65d7256fd6e20..840792f510fc0 100644 --- a/x-pack/legacy/plugins/siem/public/pages/case/components/create/index.tsx +++ b/x-pack/legacy/plugins/siem/public/pages/case/components/create/index.tsx @@ -14,8 +14,9 @@ import { } from '@elastic/eui'; import styled, { css } from 'styled-components'; import { Redirect } from 'react-router-dom'; + +import { CaseRequest } from '../../../../../../../../plugins/case/common/api'; import { Field, Form, getUseField, useForm, UseField } from '../../../../shared_imports'; -import { NewCase } from '../../../../containers/case/types'; import { usePostCase } from '../../../../containers/case/use_post_case'; import { schema } from './schema'; import * as i18n from '../../translations'; @@ -42,30 +43,37 @@ const MySpinner = styled(EuiLoadingSpinner)` z-index: 99; `; +const initialCaseValue: CaseRequest = { + description: '', + state: 'open', + tags: [], + title: '', +}; + export const Create = React.memo(() => { - const [{ data, isLoading, newCase }, setFormData] = usePostCase(); + const { caseData, isLoading, postCase } = usePostCase(); const [isCancel, setIsCancel] = useState(false); - const { form } = useForm({ - defaultValue: data, + const { form } = useForm({ + defaultValue: initialCaseValue, options: { stripEmptyFields: false }, schema, }); const onSubmit = useCallback(async () => { - const { isValid, data: newData } = await form.submit(); - if (isValid && newData.description) { - setFormData({ ...newData, isNew: true } as NewCase); - } else if (isValid && data.description) { - setFormData({ ...data, ...newData, isNew: true } as NewCase); + const { isValid, data } = await form.submit(); + if (isValid) { + await postCase(data); } - }, [form, data]); + }, [form]); - if (newCase && newCase.caseId) { - return ; + if (caseData != null && caseData.id) { + return ; } + if (isCancel) { return ; } + return ( {isLoading && } diff --git a/x-pack/legacy/plugins/siem/public/pages/case/components/create/schema.tsx b/x-pack/legacy/plugins/siem/public/pages/case/components/create/schema.tsx index c81a31f0d4f3f..91d3b77493b03 100644 --- a/x-pack/legacy/plugins/siem/public/pages/case/components/create/schema.tsx +++ b/x-pack/legacy/plugins/siem/public/pages/case/components/create/schema.tsx @@ -4,13 +4,21 @@ * you may not use this file except in compliance with the Elastic License. */ +import { CaseRequest } from '../../../../../../../../plugins/case/common/api'; import { FIELD_TYPES, fieldValidators, FormSchema } from '../../../../shared_imports'; -import { OptionalFieldLabel } from './optional_field_label'; import * as i18n from '../../translations'; +import { OptionalFieldLabel } from './optional_field_label'; const { emptyField } = fieldValidators; -export const schema: FormSchema = { +export const schemaTags = { + type: FIELD_TYPES.COMBO_BOX, + label: i18n.TAGS, + helpText: i18n.TAGS_HELP, + labelAppend: OptionalFieldLabel, +}; + +export const schema: FormSchema = { title: { type: FIELD_TYPES.TEXT, label: i18n.NAME, @@ -28,10 +36,5 @@ export const schema: FormSchema = { }, ], }, - tags: { - type: FIELD_TYPES.COMBO_BOX, - label: i18n.TAGS, - helpText: i18n.TAGS_HELP, - labelAppend: OptionalFieldLabel, - }, + tags: schemaTags, }; diff --git a/x-pack/legacy/plugins/siem/public/pages/case/components/tag_list/schema.tsx b/x-pack/legacy/plugins/siem/public/pages/case/components/tag_list/schema.tsx index 26a89408069fb..50ba114de528e 100644 --- a/x-pack/legacy/plugins/siem/public/pages/case/components/tag_list/schema.tsx +++ b/x-pack/legacy/plugins/siem/public/pages/case/components/tag_list/schema.tsx @@ -4,8 +4,8 @@ * you may not use this file except in compliance with the Elastic License. */ import { FormSchema } from '../../../../shared_imports'; -import { schema as createSchema } from '../create/schema'; +import { schemaTags } from '../create/schema'; export const schema: FormSchema = { - tags: createSchema.tags, + tags: schemaTags, }; diff --git a/x-pack/legacy/plugins/siem/public/pages/case/components/user_action_tree/index.tsx b/x-pack/legacy/plugins/siem/public/pages/case/components/user_action_tree/index.tsx index 63e0bbeb443c2..b68bfd73e50e9 100644 --- a/x-pack/legacy/plugins/siem/public/pages/case/components/user_action_tree/index.tsx +++ b/x-pack/legacy/plugins/siem/public/pages/case/components/user_action_tree/index.tsx @@ -23,10 +23,8 @@ const DescriptionId = 'description'; const NewId = 'newComent'; export const UserActionTree = React.memo( - ({ data, onUpdateField, isLoadingDescription }: UserActionTreeProps) => { - const [{ data: comments, isLoadingIds }, dispatchUpdateComment] = useUpdateComment( - data.comments - ); + ({ data: caseData, onUpdateField, isLoadingDescription }: UserActionTreeProps) => { + const { comments, isLoadingIds, updateComment } = useUpdateComment(caseData.comments); const [manageMarkdownEditIds, setManangeMardownEditIds] = useState([]); @@ -44,16 +42,16 @@ export const UserActionTree = React.memo( const handleSaveComment = useCallback( (id: string, content: string) => { handleManageMarkdownEditId(id); - dispatchUpdateComment(id, content); + updateComment(id, content); }, - [handleManageMarkdownEditId, dispatchUpdateComment] + [handleManageMarkdownEditId, updateComment] ); const MarkdownDescription = useMemo( () => ( { handleManageMarkdownEditId(DescriptionId); @@ -62,45 +60,45 @@ export const UserActionTree = React.memo( onChangeEditable={handleManageMarkdownEditId} /> ), - [data.description, handleManageMarkdownEditId, manageMarkdownEditIds, onUpdateField] + [caseData.description, handleManageMarkdownEditId, manageMarkdownEditIds, onUpdateField] ); - const MarkdownNewComment = useMemo(() => , [data.caseId]); + const MarkdownNewComment = useMemo(() => , [caseData.id]); return ( <> {comments.map(comment => ( } - onEdit={handleManageMarkdownEditId.bind(null, comment.commentId)} + onEdit={handleManageMarkdownEditId.bind(null, comment.id)} userName={comment.createdBy.username} /> ))} diff --git a/x-pack/legacy/plugins/siem/server/lib/case/saved_object_mappings.ts b/x-pack/legacy/plugins/siem/server/lib/case/saved_object_mappings.ts deleted file mode 100644 index 80cdb9e979a68..0000000000000 --- a/x-pack/legacy/plugins/siem/server/lib/case/saved_object_mappings.ts +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -/* eslint-disable @typescript-eslint/no-empty-interface */ -/* eslint-disable @typescript-eslint/camelcase */ -import { CaseAttributes, CommentAttributes } from '../../../../../../../x-pack/plugins/case/server'; -import { ElasticsearchMappingOf } from '../../utils/typed_elasticsearch_mappings'; - -// Temporary file to write mappings for case -// while Saved Object Mappings API is programmed for the NP -// See: https://github.com/elastic/kibana/issues/50309 - -export const caseSavedObjectType = 'case-workflow'; -export const caseCommentSavedObjectType = 'case-workflow-comment'; - -export const caseSavedObjectMappings: { - [caseSavedObjectType]: ElasticsearchMappingOf; -} = { - [caseSavedObjectType]: { - properties: { - created_at: { - type: 'date', - }, - description: { - type: 'text', - }, - title: { - type: 'keyword', - }, - created_by: { - properties: { - username: { - type: 'keyword', - }, - full_name: { - type: 'keyword', - }, - }, - }, - state: { - type: 'keyword', - }, - tags: { - type: 'keyword', - }, - updated_at: { - type: 'date', - }, - }, - }, -}; - -export const caseCommentSavedObjectMappings: { - [caseCommentSavedObjectType]: ElasticsearchMappingOf; -} = { - [caseCommentSavedObjectType]: { - properties: { - comment: { - type: 'text', - }, - created_at: { - type: 'date', - }, - created_by: { - properties: { - full_name: { - type: 'keyword', - }, - username: { - type: 'keyword', - }, - }, - }, - updated_at: { - type: 'date', - }, - }, - }, -}; diff --git a/x-pack/legacy/plugins/siem/server/saved_objects.ts b/x-pack/legacy/plugins/siem/server/saved_objects.ts index 58da333c7bc9a..76d8837883b8b 100644 --- a/x-pack/legacy/plugins/siem/server/saved_objects.ts +++ b/x-pack/legacy/plugins/siem/server/saved_objects.ts @@ -3,6 +3,7 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ + import { noteSavedObjectType, noteSavedObjectMappings } from './lib/note/saved_object_mappings'; import { pinnedEventSavedObjectType, @@ -16,10 +17,6 @@ import { ruleStatusSavedObjectMappings, ruleStatusSavedObjectType, } from './lib/detection_engine/rules/saved_object_mappings'; -import { - caseSavedObjectMappings, - caseCommentSavedObjectMappings, -} from './lib/case/saved_object_mappings'; export { noteSavedObjectType, @@ -31,8 +28,5 @@ export const savedObjectMappings = { ...timelineSavedObjectMappings, ...noteSavedObjectMappings, ...pinnedEventSavedObjectMappings, - // TODO: Remove once while Saved Object Mappings API is programmed for the NP See: https://github.com/elastic/kibana/issues/50309 - ...caseSavedObjectMappings, - ...caseCommentSavedObjectMappings, ...ruleStatusSavedObjectMappings, }; diff --git a/x-pack/plugins/case/common/api/cases/case.ts b/x-pack/plugins/case/common/api/cases/case.ts new file mode 100644 index 0000000000000..1bf39e6616480 --- /dev/null +++ b/x-pack/plugins/case/common/api/cases/case.ts @@ -0,0 +1,59 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import * as rt from 'io-ts'; + +import { CommentResponseRt } from './comment'; +import { UserRT } from '../user'; + +const CaseBasicRt = rt.type({ + description: rt.string, + state: rt.union([rt.literal('open'), rt.literal('closed')]), + tags: rt.array(rt.string), + title: rt.string, +}); + +export const CaseAttributesRt = rt.intersection([ + CaseBasicRt, + rt.type({ + comment_ids: rt.array(rt.string), + created_at: rt.string, + created_by: UserRT, + updated_at: rt.union([rt.string, rt.null]), + updated_by: rt.union([UserRT, rt.null]), + }), +]); + +export const CaseRequestRt = CaseBasicRt; + +export const CaseResponseRt = rt.intersection([ + CaseAttributesRt, + rt.type({ + id: rt.string, + version: rt.string, + }), + rt.partial({ + comments: rt.array(CommentResponseRt), + }), +]); + +export const CasesResponseRt = rt.type({ + cases: rt.array(CaseResponseRt), + page: rt.number, + per_page: rt.number, + total: rt.number, +}); + +export const CasePatchRequestRt = rt.intersection([ + rt.partial(CaseRequestRt.props), + rt.type({ id: rt.string, version: rt.string }), +]); + +export type CaseAttributes = rt.TypeOf; +export type CaseRequest = rt.TypeOf; +export type CaseResponse = rt.TypeOf; +export type CasesResponse = rt.TypeOf; +export type CasePatchRequest = rt.TypeOf; diff --git a/x-pack/plugins/case/common/api/cases/comment.ts b/x-pack/plugins/case/common/api/cases/comment.ts new file mode 100644 index 0000000000000..cebfa00425728 --- /dev/null +++ b/x-pack/plugins/case/common/api/cases/comment.ts @@ -0,0 +1,56 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import * as rt from 'io-ts'; + +import { UserRT } from '../user'; + +const CommentBasicRt = rt.type({ + comment: rt.string, +}); + +export const CommentAttributesRt = rt.intersection([ + CommentBasicRt, + rt.type({ + created_at: rt.string, + created_by: UserRT, + updated_at: rt.union([rt.string, rt.null]), + updated_by: rt.union([UserRT, rt.null]), + }), +]); + +export const CommentRequestRt = CommentBasicRt; + +export const CommentResponseRt = rt.intersection([ + CommentAttributesRt, + rt.type({ + id: rt.string, + version: rt.string, + }), +]); + +export const AllCommentsResponseRT = rt.array(CommentResponseRt); + +export const CommentPatchRequestRt = rt.intersection([ + rt.partial(CommentRequestRt.props), + rt.type({ id: rt.string, version: rt.string }), +]); + +export const CommentsResponseRt = rt.type({ + comments: rt.array(CommentResponseRt), + page: rt.number, + per_page: rt.number, + total: rt.number, +}); + +export const AllCommentsResponseRt = rt.array(CommentResponseRt); + +export type CommentAttributes = rt.TypeOf; +export type CommentRequest = rt.TypeOf; +export type CommentResponse = rt.TypeOf; +export type AllCommentsResponse = rt.TypeOf; +export type CommentsResponse = rt.TypeOf; +export type CommentPatchRequest = rt.TypeOf; diff --git a/x-pack/plugins/case/server/constants.ts b/x-pack/plugins/case/common/api/cases/index.ts similarity index 67% rename from x-pack/plugins/case/server/constants.ts rename to x-pack/plugins/case/common/api/cases/index.ts index 276dcd135254a..83e249e3257c4 100644 --- a/x-pack/plugins/case/server/constants.ts +++ b/x-pack/plugins/case/common/api/cases/index.ts @@ -4,5 +4,5 @@ * you may not use this file except in compliance with the Elastic License. */ -export const CASE_SAVED_OBJECT = 'case-workflow'; -export const CASE_COMMENT_SAVED_OBJECT = 'case-workflow-comment'; +export * from './case'; +export * from './comment'; diff --git a/x-pack/plugins/case/common/api/index.ts b/x-pack/plugins/case/common/api/index.ts new file mode 100644 index 0000000000000..3e94d91569ca5 --- /dev/null +++ b/x-pack/plugins/case/common/api/index.ts @@ -0,0 +1,9 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +export * from './cases'; +export * from './runtime_types'; +export * from './saved_object'; diff --git a/x-pack/plugins/case/common/api/runtime_types.ts b/x-pack/plugins/case/common/api/runtime_types.ts new file mode 100644 index 0000000000000..d5b858df38def --- /dev/null +++ b/x-pack/plugins/case/common/api/runtime_types.ts @@ -0,0 +1,25 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { fold } from 'fp-ts/lib/Either'; +import { identity } from 'fp-ts/lib/function'; +import { pipe } from 'fp-ts/lib/pipeable'; +import { Errors, Type } from 'io-ts'; +import { failure } from 'io-ts/lib/PathReporter'; + +type ErrorFactory = (message: string) => Error; + +export const createPlainError = (message: string) => new Error(message); + +export const throwErrors = (createError: ErrorFactory) => (errors: Errors) => { + throw createError(failure(errors).join('\n')); +}; + +export const decodeOrThrow = ( + runtimeType: Type, + createError: ErrorFactory = createPlainError +) => (inputValue: I) => + pipe(runtimeType.decode(inputValue), fold(throwErrors(createError), identity)); diff --git a/x-pack/plugins/case/common/api/saved_object.ts b/x-pack/plugins/case/common/api/saved_object.ts new file mode 100644 index 0000000000000..0da859649a34e --- /dev/null +++ b/x-pack/plugins/case/common/api/saved_object.ts @@ -0,0 +1,34 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import * as rt from 'io-ts'; + +import { either } from 'fp-ts/lib/Either'; + +const NumberFromString = new rt.Type( + 'NumberFromString', + rt.number.is, + (u, c) => + either.chain(rt.string.validate(u, c), s => { + const n = +s; + return isNaN(n) ? rt.failure(u, c, 'cannot parse to a number') : rt.success(n); + }), + String +); + +export const SavedObjectFindOptionsRt = rt.partial({ + defaultSearchOperator: rt.union([rt.literal('AND'), rt.literal('OR')]), + fields: rt.array(rt.string), + filter: rt.string, + page: NumberFromString, + perPage: NumberFromString, + search: rt.string, + searchFields: rt.array(rt.string), + sortField: rt.string, + sortOrder: rt.union([rt.literal('desc'), rt.literal('asc')]), +}); + +export type SavedObjectFindOptions = rt.TypeOf; diff --git a/x-pack/plugins/case/common/api/user.ts b/x-pack/plugins/case/common/api/user.ts new file mode 100644 index 0000000000000..bf5cde7af03f3 --- /dev/null +++ b/x-pack/plugins/case/common/api/user.ts @@ -0,0 +1,12 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import * as rt from 'io-ts'; + +export const UserRT = rt.type({ + full_name: rt.union([rt.undefined, rt.string, rt.null]), + username: rt.union([rt.string, rt.null]), +}); diff --git a/x-pack/plugins/case/kibana.json b/x-pack/plugins/case/kibana.json index 23e3cc789ad3b..4a0151546c8fb 100644 --- a/x-pack/plugins/case/kibana.json +++ b/x-pack/plugins/case/kibana.json @@ -3,6 +3,10 @@ "id": "case", "kibanaVersion": "kibana", "requiredPlugins": ["security"], + "optionalPlugins": [ + "spaces", + "security" + ], "server": true, "ui": false, "version": "8.0.0" diff --git a/x-pack/plugins/case/server/index.ts b/x-pack/plugins/case/server/index.ts index 990aef19b74f7..f924810baa912 100644 --- a/x-pack/plugins/case/server/index.ts +++ b/x-pack/plugins/case/server/index.ts @@ -7,7 +7,6 @@ import { PluginInitializerContext } from '../../../../src/core/server'; import { ConfigSchema } from './config'; import { CasePlugin } from './plugin'; -export { CaseAttributes, CommentAttributes } from './routes/api/types'; export const config = { schema: ConfigSchema }; export const plugin = (initializerContext: PluginInitializerContext) => diff --git a/x-pack/plugins/case/server/plugin.ts b/x-pack/plugins/case/server/plugin.ts index 5ca640f0b25c3..7ce3a61f03779 100644 --- a/x-pack/plugins/case/server/plugin.ts +++ b/x-pack/plugins/case/server/plugin.ts @@ -5,11 +5,15 @@ */ import { first, map } from 'rxjs/operators'; -import { CoreSetup, Logger, PluginInitializerContext } from 'kibana/server'; +import { Logger, PluginInitializerContext } from 'kibana/server'; +import { CoreSetup } from 'src/core/server'; + +import { SecurityPluginSetup } from '../../security/server'; + import { ConfigType } from './config'; import { initCaseApi } from './routes/api'; +import { caseSavedObjectType, caseCommentSavedObjectType } from './saved_object_types'; import { CaseService } from './services'; -import { SecurityPluginSetup } from '../../security/server'; function createConfig$(context: PluginInitializerContext) { return context.config.create().pipe(map(config => config)); @@ -35,6 +39,9 @@ export class CasePlugin { return; } + core.savedObjects.registerType(caseSavedObjectType); + core.savedObjects.registerType(caseCommentSavedObjectType); + const service = new CaseService(this.log); this.log.debug( diff --git a/x-pack/plugins/case/server/routes/api/__fixtures__/create_mock_so_repository.ts b/x-pack/plugins/case/server/routes/api/__fixtures__/create_mock_so_repository.ts index eb9afb27a749e..7c97adc1b31bf 100644 --- a/x-pack/plugins/case/server/routes/api/__fixtures__/create_mock_so_repository.ts +++ b/x-pack/plugins/case/server/routes/api/__fixtures__/create_mock_so_repository.ts @@ -5,12 +5,26 @@ */ import { SavedObjectsClientContract, SavedObjectsErrorHelpers } from 'src/core/server'; -import { CASE_COMMENT_SAVED_OBJECT } from '../../../constants'; -export const createMockSavedObjectsRepository = (savedObject: any[] = []) => { +import { CASE_COMMENT_SAVED_OBJECT, CASE_SAVED_OBJECT } from '../../../saved_object_types'; + +export const createMockSavedObjectsRepository = ({ + caseSavedObject = [], + caseCommentSavedObject = [], +}: { + caseSavedObject?: any[]; + caseCommentSavedObject?: any[]; +}) => { const mockSavedObjectsClientContract = ({ get: jest.fn((type, id) => { - const result = savedObject.filter(s => s.id === id); + if (type === CASE_COMMENT_SAVED_OBJECT) { + const result = caseCommentSavedObject.filter(s => s.id === id); + if (!result.length) { + throw SavedObjectsErrorHelpers.createGenericNotFoundError(type, id); + } + return result[0]; + } + const result = caseSavedObject.filter(s => s.id === id); if (!result.length) { throw SavedObjectsErrorHelpers.createGenericNotFoundError(type, id); } @@ -20,11 +34,20 @@ export const createMockSavedObjectsRepository = (savedObject: any[] = []) => { if (findArgs.hasReference && findArgs.hasReference.id === 'bad-guy') { throw SavedObjectsErrorHelpers.createBadRequestError('Error thrown for testing'); } + + if (findArgs.type === CASE_COMMENT_SAVED_OBJECT) { + return { + page: 1, + per_page: 5, + total: caseCommentSavedObject.length, + saved_objects: caseCommentSavedObject, + }; + } return { page: 1, per_page: 5, - total: savedObject.length, - saved_objects: savedObject, + total: caseSavedObject.length, + saved_objects: caseSavedObject, }; }), create: jest.fn((type, attributes, references) => { @@ -51,9 +74,16 @@ export const createMockSavedObjectsRepository = (savedObject: any[] = []) => { }; }), update: jest.fn((type, id, attributes) => { - if (!savedObject.find(s => s.id === id)) { - throw SavedObjectsErrorHelpers.createGenericNotFoundError(type, id); + if (type === CASE_COMMENT_SAVED_OBJECT) { + if (!caseCommentSavedObject.find(s => s.id === id)) { + throw SavedObjectsErrorHelpers.createGenericNotFoundError(type, id); + } + } else if (type === CASE_SAVED_OBJECT) { + if (!caseSavedObject.find(s => s.id === id)) { + throw SavedObjectsErrorHelpers.createGenericNotFoundError(type, id); + } } + return { id, type, @@ -63,13 +93,17 @@ export const createMockSavedObjectsRepository = (savedObject: any[] = []) => { }; }), delete: jest.fn((type: string, id: string) => { - const result = savedObject.filter(s => s.id === id); - if (!result.length) { - throw SavedObjectsErrorHelpers.createGenericNotFoundError(type, id); + let result = caseSavedObject.filter(s => s.id === id); + if (type === CASE_COMMENT_SAVED_OBJECT) { + result = caseCommentSavedObject.filter(s => s.id === id); } - if (type === 'case-workflow-comment' && id === 'bad-guy') { + if (type === CASE_COMMENT_SAVED_OBJECT && id === 'bad-guy') { throw SavedObjectsErrorHelpers.createBadRequestError('Error thrown for testing'); } + if (!result.length) { + throw SavedObjectsErrorHelpers.createGenericNotFoundError(type, id); + } + return {}; }), deleteByNamespace: jest.fn(), diff --git a/x-pack/plugins/case/server/routes/api/__fixtures__/mock_router.ts b/x-pack/plugins/case/server/routes/api/__fixtures__/mock_router.ts index ac9eddd6dd2cb..32348fecba1be 100644 --- a/x-pack/plugins/case/server/routes/api/__fixtures__/mock_router.ts +++ b/x-pack/plugins/case/server/routes/api/__fixtures__/mock_router.ts @@ -8,7 +8,7 @@ import { IRouter } from 'kibana/server'; import { loggingServiceMock, httpServiceMock } from '../../../../../../../src/core/server/mocks'; import { CaseService } from '../../../services'; import { authenticationMock } from '../__fixtures__'; -import { RouteDeps } from '../index'; +import { RouteDeps } from '../types'; export const createRoute = async ( api: (deps: RouteDeps) => void, diff --git a/x-pack/plugins/case/server/routes/api/__fixtures__/mock_saved_objects.ts b/x-pack/plugins/case/server/routes/api/__fixtures__/mock_saved_objects.ts index c7f6b6fad7d1a..3701e4f14e8b3 100644 --- a/x-pack/plugins/case/server/routes/api/__fixtures__/mock_saved_objects.ts +++ b/x-pack/plugins/case/server/routes/api/__fixtures__/mock_saved_objects.ts @@ -4,11 +4,15 @@ * you may not use this file except in compliance with the Elastic License. */ -export const mockCases = [ +import { SavedObject } from 'kibana/server'; +import { CaseAttributes, CommentAttributes } from '../../../../common/api'; + +export const mockCases: Array> = [ { - type: 'case-workflow', + type: 'cases', id: 'mock-id-1', attributes: { + comment_ids: ['mock-comment-1'], created_at: '2019-11-25T21:54:48.952Z', created_by: { full_name: 'elastic', @@ -19,15 +23,20 @@ export const mockCases = [ state: 'open', tags: ['defacement'], updated_at: '2019-11-25T21:54:48.952Z', + updated_by: { + full_name: 'elastic', + username: 'elastic', + }, }, references: [], updated_at: '2019-11-25T21:54:48.952Z', version: 'WzAsMV0=', }, { - type: 'case-workflow', + type: 'cases', id: 'mock-id-2', attributes: { + comment_ids: [], created_at: '2019-11-25T22:32:00.900Z', created_by: { full_name: 'elastic', @@ -38,15 +47,20 @@ export const mockCases = [ state: 'open', tags: ['Data Destruction'], updated_at: '2019-11-25T22:32:00.900Z', + updated_by: { + full_name: 'elastic', + username: 'elastic', + }, }, references: [], updated_at: '2019-11-25T22:32:00.900Z', version: 'WzQsMV0=', }, { - type: 'case-workflow', + type: 'cases', id: 'mock-id-3', attributes: { + comment_ids: [], created_at: '2019-11-25T22:32:17.947Z', created_by: { full_name: 'elastic', @@ -57,6 +71,10 @@ export const mockCases = [ state: 'open', tags: ['LOLBins'], updated_at: '2019-11-25T22:32:17.947Z', + updated_by: { + full_name: 'elastic', + username: 'elastic', + }, }, references: [], updated_at: '2019-11-25T22:32:17.947Z', @@ -73,9 +91,9 @@ export const mockCasesErrorTriggerData = [ }, ]; -export const mockCaseComments = [ +export const mockCaseComments: Array> = [ { - type: 'case-workflow-comment', + type: 'cases-comment', id: 'mock-comment-1', attributes: { comment: 'Wow, good luck catching that bad meanie!', @@ -85,11 +103,15 @@ export const mockCaseComments = [ username: 'elastic', }, updated_at: '2019-11-25T21:55:00.177Z', + updated_by: { + full_name: 'elastic', + username: 'elastic', + }, }, references: [ { - type: 'case-workflow', - name: 'associated-case-workflow', + type: 'cases', + name: 'associated-cases', id: 'mock-id-1', }, ], @@ -97,7 +119,7 @@ export const mockCaseComments = [ version: 'WzEsMV0=', }, { - type: 'case-workflow-comment', + type: 'cases-comment', id: 'mock-comment-2', attributes: { comment: 'Well I decided to update my comment. So what? Deal with it.', @@ -107,19 +129,24 @@ export const mockCaseComments = [ username: 'elastic', }, updated_at: '2019-11-25T21:55:14.633Z', + updated_by: { + full_name: 'elastic', + username: 'elastic', + }, }, references: [ { - type: 'case-workflow', - name: 'associated-case-workflow', + type: 'cases', + name: 'associated-cases', id: 'mock-id-1', }, ], updated_at: '2019-11-25T21:55:14.633Z', + version: 'WzMsMV0=', }, { - type: 'case-workflow-comment', + type: 'cases-comment', id: 'mock-comment-3', attributes: { comment: 'Wow, good luck catching that bad meanie!', @@ -129,15 +156,20 @@ export const mockCaseComments = [ username: 'elastic', }, updated_at: '2019-11-25T22:32:30.608Z', + updated_by: { + full_name: 'elastic', + username: 'elastic', + }, }, references: [ { - type: 'case-workflow', - name: 'associated-case-workflow', + type: 'cases', + name: 'associated-cases', id: 'mock-id-3', }, ], updated_at: '2019-11-25T22:32:30.608Z', + version: 'WzYsMV0=', }, ]; diff --git a/x-pack/plugins/case/server/routes/api/cases/comments/delete_all_comments.ts b/x-pack/plugins/case/server/routes/api/cases/comments/delete_all_comments.ts new file mode 100644 index 0000000000000..00d06bfdd2677 --- /dev/null +++ b/x-pack/plugins/case/server/routes/api/cases/comments/delete_all_comments.ts @@ -0,0 +1,55 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { schema } from '@kbn/config-schema'; +import { RouteDeps } from '../../types'; +import { wrapError } from '../../utils'; + +export function initDeleteAllCommentsApi({ caseService, router }: RouteDeps) { + router.delete( + { + path: '/api/cases/{case_id}/comments', + validate: { + params: schema.object({ + case_id: schema.string(), + }), + }, + }, + async (context, request, response) => { + try { + const client = context.core.savedObjects.client; + + const comments = await caseService.getAllCaseComments({ + client: context.core.savedObjects.client, + caseId: request.params.case_id, + }); + await Promise.all( + comments.saved_objects.map(comment => + caseService.deleteComment({ + client, + commentId: comment.id, + }) + ) + ); + + const updateCase = { + comment_ids: [], + }; + await caseService.patchCase({ + client: context.core.savedObjects.client, + caseId: request.params.case_id, + updatedAttributes: { + ...updateCase, + }, + }); + + return response.ok({ body: 'true' }); + } catch (error) { + return response.customError(wrapError(error)); + } + } + ); +} diff --git a/x-pack/plugins/case/server/routes/api/__tests__/delete_comment.test.ts b/x-pack/plugins/case/server/routes/api/cases/comments/delete_comment.test.ts similarity index 61% rename from x-pack/plugins/case/server/routes/api/__tests__/delete_comment.test.ts rename to x-pack/plugins/case/server/routes/api/cases/comments/delete_comment.test.ts index e50b3cbaa9c9a..8f05fbce391f8 100644 --- a/x-pack/plugins/case/server/routes/api/__tests__/delete_comment.test.ts +++ b/x-pack/plugins/case/server/routes/api/cases/comments/delete_comment.test.ts @@ -4,50 +4,61 @@ * you may not use this file except in compliance with the Elastic License. */ +import { kibanaResponseFactory, RequestHandler } from 'src/core/server'; +import { httpServerMock } from 'src/core/server/mocks'; + import { createMockSavedObjectsRepository, createRoute, createRouteContext, mockCases, - mockCasesErrorTriggerData, -} from '../__fixtures__'; -import { initDeleteCommentApi } from '../delete_comment'; -import { kibanaResponseFactory, RequestHandler } from 'src/core/server'; -import { httpServerMock } from 'src/core/server/mocks'; + mockCaseComments, +} from '../../__fixtures__'; +import { initDeleteCommentApi } from './delete_comment'; describe('DELETE comment', () => { let routeHandler: RequestHandler; beforeAll(async () => { routeHandler = await createRoute(initDeleteCommentApi, 'delete'); }); - it(`deletes the comment. responds with 204`, async () => { + it(`deletes the comment. responds with 200`, async () => { const request = httpServerMock.createKibanaRequest({ - path: '/api/cases/comments/{comment_id}', + path: '/api/cases/{case_id}/comments/{comment_id}', method: 'delete', params: { - comment_id: 'mock-id-1', + case_id: 'mock-id-1', + comment_id: 'mock-comment-1', }, }); - const theContext = createRouteContext(createMockSavedObjectsRepository(mockCases)); + const theContext = createRouteContext( + createMockSavedObjectsRepository({ + caseSavedObject: mockCases, + caseCommentSavedObject: mockCaseComments, + }) + ); const response = await routeHandler(theContext, request, kibanaResponseFactory); - expect(response.status).toEqual(204); + expect(response.status).toEqual(200); }); it(`returns an error when thrown from deleteComment service`, async () => { const request = httpServerMock.createKibanaRequest({ - path: '/api/cases/comments/{comment_id}', + path: '/api/cases/{case_id}/comments/{comment_id}', method: 'delete', params: { + case_id: 'mock-id-1', comment_id: 'bad-guy', }, }); const theContext = createRouteContext( - createMockSavedObjectsRepository(mockCasesErrorTriggerData) + createMockSavedObjectsRepository({ + caseSavedObject: mockCases, + caseCommentSavedObject: mockCaseComments, + }) ); const response = await routeHandler(theContext, request, kibanaResponseFactory); - expect(response.status).toEqual(400); + expect(response.status).toEqual(404); }); }); diff --git a/x-pack/plugins/case/server/routes/api/cases/comments/delete_comment.ts b/x-pack/plugins/case/server/routes/api/cases/comments/delete_comment.ts new file mode 100644 index 0000000000000..85c4701f82e1d --- /dev/null +++ b/x-pack/plugins/case/server/routes/api/cases/comments/delete_comment.ts @@ -0,0 +1,61 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import Boom from 'boom'; +import { schema } from '@kbn/config-schema'; +import { RouteDeps } from '../../types'; +import { wrapError } from '../../utils'; + +export function initDeleteCommentApi({ caseService, router }: RouteDeps) { + router.delete( + { + path: '/api/cases/{case_id}/comments/{comment_id}', + validate: { + params: schema.object({ + case_id: schema.string(), + comment_id: schema.string(), + }), + }, + }, + async (context, request, response) => { + try { + const client = context.core.savedObjects.client; + const myCase = await caseService.getCase({ + client: context.core.savedObjects.client, + caseId: request.params.case_id, + }); + + if (!myCase.attributes.comment_ids.includes(request.params.comment_id)) { + throw Boom.notFound( + `This comment ${request.params.comment_id} does not exist in ${myCase.attributes.title} (id: ${request.params.case_id}).` + ); + } + + await caseService.deleteComment({ + client, + commentId: request.params.comment_id, + }); + + const updateCase = { + comment_ids: myCase.attributes.comment_ids.filter( + cId => cId !== request.params.comment_id + ), + }; + await caseService.patchCase({ + client: context.core.savedObjects.client, + caseId: request.params.case_id, + updatedAttributes: { + ...updateCase, + }, + }); + + return response.ok({ body: 'true' }); + } catch (error) { + return response.customError(wrapError(error)); + } + } + ); +} diff --git a/x-pack/plugins/case/server/routes/api/cases/comments/find_comments.ts b/x-pack/plugins/case/server/routes/api/cases/comments/find_comments.ts new file mode 100644 index 0000000000000..dcf70d0d9819c --- /dev/null +++ b/x-pack/plugins/case/server/routes/api/cases/comments/find_comments.ts @@ -0,0 +1,61 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { schema } from '@kbn/config-schema'; +import Boom from 'boom'; + +import { pipe } from 'fp-ts/lib/pipeable'; +import { fold } from 'fp-ts/lib/Either'; +import { identity } from 'fp-ts/lib/function'; + +import { + CommentsResponseRt, + SavedObjectFindOptionsRt, + throwErrors, +} from '../../../../../common/api'; +import { RouteDeps } from '../../types'; +import { escapeHatch, transformComments, wrapError } from '../../utils'; + +export function initFindCaseCommentsApi({ caseService, router }: RouteDeps) { + router.get( + { + path: '/api/cases/{case_id}/comments/_find', + validate: { + params: schema.object({ + case_id: schema.string(), + }), + query: escapeHatch, + }, + }, + async (context, request, response) => { + try { + const query = pipe( + SavedObjectFindOptionsRt.decode(request.query), + fold(throwErrors(Boom.badRequest), identity) + ); + + const args = query + ? { + client: context.core.savedObjects.client, + caseId: request.params.case_id, + options: { + ...query, + sortField: 'created_at', + }, + } + : { + client: context.core.savedObjects.client, + caseId: request.params.case_id, + }; + + const theComments = await caseService.getAllCaseComments(args); + return response.ok({ body: CommentsResponseRt.encode(transformComments(theComments)) }); + } catch (error) { + return response.customError(wrapError(error)); + } + } + ); +} diff --git a/x-pack/plugins/case/server/routes/api/get_all_case_comments.ts b/x-pack/plugins/case/server/routes/api/cases/comments/get_all_comment.ts similarity index 51% rename from x-pack/plugins/case/server/routes/api/get_all_case_comments.ts rename to x-pack/plugins/case/server/routes/api/cases/comments/get_all_comment.ts index b74227fa8d983..65f2de7125236 100644 --- a/x-pack/plugins/case/server/routes/api/get_all_case_comments.ts +++ b/x-pack/plugins/case/server/routes/api/cases/comments/get_all_comment.ts @@ -5,26 +5,30 @@ */ import { schema } from '@kbn/config-schema'; -import { RouteDeps } from '.'; -import { formatAllComments, wrapError } from './utils'; -export function initGetAllCaseCommentsApi({ caseService, router }: RouteDeps) { +import { AllCommentsResponseRt } from '../../../../../common/api'; +import { RouteDeps } from '../../types'; +import { flattenCommentSavedObjects, wrapError } from '../../utils'; + +export function initGetAllCommentsApi({ caseService, router }: RouteDeps) { router.get( { - path: '/api/cases/{id}/comments', + path: '/api/cases/{case_id}/comments', validate: { params: schema.object({ - id: schema.string(), + case_id: schema.string(), }), }, }, async (context, request, response) => { try { - const theComments = await caseService.getAllCaseComments({ + const comments = await caseService.getAllCaseComments({ client: context.core.savedObjects.client, - caseId: request.params.id, + caseId: request.params.case_id, + }); + return response.ok({ + body: AllCommentsResponseRt.encode(flattenCommentSavedObjects(comments.saved_objects)), }); - return response.ok({ body: formatAllComments(theComments) }); } catch (error) { return response.customError(wrapError(error)); } diff --git a/x-pack/plugins/case/server/routes/api/__tests__/get_comment.test.ts b/x-pack/plugins/case/server/routes/api/cases/comments/get_comment.test.ts similarity index 53% rename from x-pack/plugins/case/server/routes/api/__tests__/get_comment.test.ts rename to x-pack/plugins/case/server/routes/api/cases/comments/get_comment.test.ts index 3add93acc641f..9c8d0e5254df0 100644 --- a/x-pack/plugins/case/server/routes/api/__tests__/get_comment.test.ts +++ b/x-pack/plugins/case/server/routes/api/cases/comments/get_comment.test.ts @@ -3,18 +3,18 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ +import { kibanaResponseFactory, RequestHandler } from 'src/core/server'; +import { httpServerMock } from 'src/core/server/mocks'; import { createMockSavedObjectsRepository, createRoute, createRouteContext, mockCaseComments, -} from '../__fixtures__'; -import { initGetCommentApi } from '../get_comment'; -import { kibanaResponseFactory, RequestHandler, SavedObject } from 'src/core/server'; -import { httpServerMock } from 'src/core/server/mocks'; -import { flattenCommentSavedObject } from '../utils'; -import { CommentAttributes } from '../types'; + mockCases, +} from '../../__fixtures__'; +import { flattenCommentSavedObject } from '../../utils'; +import { initGetCommentApi } from './get_comment'; describe('GET comment', () => { let routeHandler: RequestHandler; @@ -23,33 +23,44 @@ describe('GET comment', () => { }); it(`returns the comment`, async () => { const request = httpServerMock.createKibanaRequest({ - path: '/api/cases/comments/{id}', + path: '/api/cases/{case_id}/comments/{comment_id}', method: 'get', params: { - id: 'mock-comment-1', + case_id: 'mock-id-1', + comment_id: 'mock-comment-1', }, }); - const theContext = createRouteContext(createMockSavedObjectsRepository(mockCaseComments)); + const theContext = createRouteContext( + createMockSavedObjectsRepository({ + caseSavedObject: mockCases, + caseCommentSavedObject: mockCaseComments, + }) + ); const response = await routeHandler(theContext, request, kibanaResponseFactory); expect(response.status).toEqual(200); - expect(response.payload).toEqual( - flattenCommentSavedObject( - mockCaseComments.find(s => s.id === 'mock-comment-1') as SavedObject - ) - ); + const myPayload = mockCaseComments.find(s => s.id === 'mock-comment-1'); + expect(myPayload).not.toBeUndefined(); + if (myPayload != null) { + expect(response.payload).toEqual(flattenCommentSavedObject(myPayload)); + } }); it(`returns an error when getComment throws`, async () => { const request = httpServerMock.createKibanaRequest({ - path: '/api/cases/comments/{id}', + path: '/api/cases/{case_id}/comments/{comment_id}', method: 'get', params: { - id: 'not-real', + case_id: 'mock-id-1', + comment_id: 'not-real', }, }); - const theContext = createRouteContext(createMockSavedObjectsRepository(mockCaseComments)); + const theContext = createRouteContext( + createMockSavedObjectsRepository({ + caseCommentSavedObject: mockCaseComments, + }) + ); const response = await routeHandler(theContext, request, kibanaResponseFactory); expect(response.status).toEqual(404); diff --git a/x-pack/plugins/case/server/routes/api/cases/comments/get_comment.ts b/x-pack/plugins/case/server/routes/api/cases/comments/get_comment.ts new file mode 100644 index 0000000000000..06619abae8487 --- /dev/null +++ b/x-pack/plugins/case/server/routes/api/cases/comments/get_comment.ts @@ -0,0 +1,51 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { schema } from '@kbn/config-schema'; +import Boom from 'boom'; + +import { CommentResponseRt } from '../../../../../common/api'; +import { RouteDeps } from '../../types'; +import { flattenCommentSavedObject, wrapError } from '../../utils'; + +export function initGetCommentApi({ caseService, router }: RouteDeps) { + router.get( + { + path: '/api/cases/{case_id}/comments/{comment_id}', + validate: { + params: schema.object({ + case_id: schema.string(), + comment_id: schema.string(), + }), + }, + }, + async (context, request, response) => { + try { + const client = context.core.savedObjects.client; + const myCase = await caseService.getCase({ + client, + caseId: request.params.case_id, + }); + + if (!myCase.attributes.comment_ids.includes(request.params.comment_id)) { + throw Boom.notFound( + `This comment ${request.params.comment_id} does not exist in ${myCase.attributes.title} (id: ${request.params.case_id}).` + ); + } + + const comment = await caseService.getComment({ + client, + commentId: request.params.comment_id, + }); + return response.ok({ + body: CommentResponseRt.encode(flattenCommentSavedObject(comment)), + }); + } catch (error) { + return response.customError(wrapError(error)); + } + } + ); +} diff --git a/x-pack/plugins/case/server/routes/api/__tests__/update_comment.test.ts b/x-pack/plugins/case/server/routes/api/cases/comments/patch_comment.test.ts similarity index 64% rename from x-pack/plugins/case/server/routes/api/__tests__/update_comment.test.ts rename to x-pack/plugins/case/server/routes/api/cases/comments/patch_comment.test.ts index 6b4e3c194eb82..4e7e266f326a2 100644 --- a/x-pack/plugins/case/server/routes/api/__tests__/update_comment.test.ts +++ b/x-pack/plugins/case/server/routes/api/cases/comments/patch_comment.test.ts @@ -3,72 +3,93 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ +import { kibanaResponseFactory, RequestHandler } from 'src/core/server'; +import { httpServerMock } from 'src/core/server/mocks'; import { createMockSavedObjectsRepository, createRoute, createRouteContext, mockCaseComments, -} from '../__fixtures__'; -import { initUpdateCommentApi } from '../update_comment'; -import { kibanaResponseFactory, RequestHandler } from 'src/core/server'; -import { httpServerMock } from 'src/core/server/mocks'; + mockCases, +} from '../../__fixtures__'; +import { initPatchCommentApi } from './patch_comment'; -describe('UPDATE comment', () => { +describe('PATCH comment', () => { let routeHandler: RequestHandler; beforeAll(async () => { - routeHandler = await createRoute(initUpdateCommentApi, 'patch'); + routeHandler = await createRoute(initPatchCommentApi, 'patch'); }); - it(`Updates a comment`, async () => { + it(`Patch a comment`, async () => { const request = httpServerMock.createKibanaRequest({ - path: '/api/cases/comment/{id}', + path: '/api/cases/{case_id}/comments', method: 'patch', params: { - id: 'mock-comment-1', + case_id: 'mock-id-1', }, body: { comment: 'Update my comment', + id: 'mock-comment-1', version: 'WzEsMV0=', }, }); - const theContext = createRouteContext(createMockSavedObjectsRepository(mockCaseComments)); + const theContext = createRouteContext( + createMockSavedObjectsRepository({ + caseSavedObject: mockCases, + caseCommentSavedObject: mockCaseComments, + }) + ); const response = await routeHandler(theContext, request, kibanaResponseFactory); expect(response.status).toEqual(200); expect(response.payload.comment).toEqual('Update my comment'); }); + it(`Fails with 409 if version does not match`, async () => { const request = httpServerMock.createKibanaRequest({ - path: '/api/cases/comment/{id}', + path: '/api/cases/{case_id}/comments', method: 'patch', params: { - id: 'mock-comment-1', + case_id: 'mock-id-1', }, body: { + id: 'mock-comment-1', comment: 'Update my comment', version: 'badv=', }, }); - const theContext = createRouteContext(createMockSavedObjectsRepository(mockCaseComments)); + const theContext = createRouteContext( + createMockSavedObjectsRepository({ + caseSavedObject: mockCases, + caseCommentSavedObject: mockCaseComments, + }) + ); const response = await routeHandler(theContext, request, kibanaResponseFactory); expect(response.status).toEqual(409); }); it(`Returns an error if updateComment throws`, async () => { const request = httpServerMock.createKibanaRequest({ - path: '/api/cases/comment/{id}', + path: '/api/cases/{case_id}/comments', method: 'patch', params: { - id: 'mock-comment-does-not-exist', + case_id: 'mock-id-1', }, body: { comment: 'Update my comment', + id: 'mock-comment-does-not-exist', + version: 'WzEsMV0=', }, }); - const theContext = createRouteContext(createMockSavedObjectsRepository(mockCaseComments)); + const theContext = createRouteContext( + createMockSavedObjectsRepository({ + caseSavedObject: mockCases, + caseCommentSavedObject: mockCaseComments, + }) + ); const response = await routeHandler(theContext, request, kibanaResponseFactory); expect(response.status).toEqual(404); diff --git a/x-pack/plugins/case/server/routes/api/cases/comments/patch_comment.ts b/x-pack/plugins/case/server/routes/api/cases/comments/patch_comment.ts new file mode 100644 index 0000000000000..f1568f22c6c99 --- /dev/null +++ b/x-pack/plugins/case/server/routes/api/cases/comments/patch_comment.ts @@ -0,0 +1,84 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { schema } from '@kbn/config-schema'; +import Boom from 'boom'; +import { pipe } from 'fp-ts/lib/pipeable'; +import { fold } from 'fp-ts/lib/Either'; +import { identity } from 'fp-ts/lib/function'; + +import { CommentPatchRequestRt, CommentResponseRt, throwErrors } from '../../../../../common/api'; + +import { RouteDeps } from '../../types'; +import { escapeHatch, wrapError, flattenCommentSavedObject } from '../../utils'; + +export function initPatchCommentApi({ caseService, router }: RouteDeps) { + router.patch( + { + path: '/api/cases/{case_id}/comments', + validate: { + params: schema.object({ + case_id: schema.string(), + }), + body: escapeHatch, + }, + }, + async (context, request, response) => { + try { + const query = pipe( + CommentPatchRequestRt.decode(request.body), + fold(throwErrors(Boom.badRequest), identity) + ); + + const myCase = await caseService.getCase({ + client: context.core.savedObjects.client, + caseId: request.params.case_id, + }); + + if (!myCase.attributes.comment_ids.includes(query.id)) { + throw Boom.notFound( + `This comment ${query.id} does not exist in ${myCase.attributes.title} (id: ${request.params.case_id}).` + ); + } + + const myComment = await caseService.getComment({ + client: context.core.savedObjects.client, + commentId: query.id, + }); + + if (query.version !== myComment.version) { + throw Boom.conflict( + 'This case has been updated. Please refresh before saving additional updates.' + ); + } + + const updatedBy = await caseService.getUser({ request, response }); + const { full_name, username } = updatedBy; + const updatedComment = await caseService.patchComment({ + client: context.core.savedObjects.client, + commentId: query.id, + updatedAttributes: { + ...query, + updated_at: new Date().toISOString(), + updated_by: { full_name, username }, + }, + }); + + return response.ok({ + body: CommentResponseRt.encode( + flattenCommentSavedObject({ + ...updatedComment, + attributes: { ...myComment.attributes, ...updatedComment.attributes }, + references: myComment.references, + }) + ), + }); + } catch (error) { + return response.customError(wrapError(error)); + } + } + ); +} diff --git a/x-pack/plugins/case/server/routes/api/__tests__/post_comment.test.ts b/x-pack/plugins/case/server/routes/api/cases/comments/post_comment.test.ts similarity index 66% rename from x-pack/plugins/case/server/routes/api/__tests__/post_comment.test.ts rename to x-pack/plugins/case/server/routes/api/cases/comments/post_comment.test.ts index 653140af2a7cf..e51ec7c894d08 100644 --- a/x-pack/plugins/case/server/routes/api/__tests__/post_comment.test.ts +++ b/x-pack/plugins/case/server/routes/api/cases/comments/post_comment.test.ts @@ -4,15 +4,17 @@ * you may not use this file except in compliance with the Elastic License. */ +import { kibanaResponseFactory, RequestHandler } from 'src/core/server'; +import { httpServerMock } from 'src/core/server/mocks'; + import { createMockSavedObjectsRepository, createRoute, createRouteContext, mockCases, -} from '../__fixtures__'; -import { initPostCommentApi } from '../post_comment'; -import { kibanaResponseFactory, RequestHandler } from 'src/core/server'; -import { httpServerMock } from 'src/core/server/mocks'; + mockCaseComments, +} from '../../__fixtures__'; +import { initPostCommentApi } from './post_comment'; describe('POST comment', () => { let routeHandler: RequestHandler; @@ -21,35 +23,45 @@ describe('POST comment', () => { }); it(`Posts a new comment`, async () => { const request = httpServerMock.createKibanaRequest({ - path: '/api/cases/{id}/comment', + path: '/api/cases/{case_id}/comments', method: 'post', params: { - id: 'mock-id-1', + case_id: 'mock-id-1', }, body: { comment: 'Wow, good luck catching that bad meanie!', }, }); - const theContext = createRouteContext(createMockSavedObjectsRepository(mockCases)); + const theContext = createRouteContext( + createMockSavedObjectsRepository({ + caseSavedObject: mockCases, + caseCommentSavedObject: mockCaseComments, + }) + ); const response = await routeHandler(theContext, request, kibanaResponseFactory); expect(response.status).toEqual(200); - expect(response.payload.comment_id).toEqual('mock-comment'); + expect(response.payload.id).toEqual('mock-comment'); }); it(`Returns an error if the case does not exist`, async () => { const request = httpServerMock.createKibanaRequest({ - path: '/api/cases/{id}/comment', + path: '/api/cases/{case_id}/comments', method: 'post', params: { - id: 'this-is-not-real', + case_id: 'this-is-not-real', }, body: { comment: 'Wow, good luck catching that bad meanie!', }, }); - const theContext = createRouteContext(createMockSavedObjectsRepository(mockCases)); + const theContext = createRouteContext( + createMockSavedObjectsRepository({ + caseSavedObject: mockCases, + caseCommentSavedObject: mockCaseComments, + }) + ); const response = await routeHandler(theContext, request, kibanaResponseFactory); expect(response.status).toEqual(404); @@ -57,17 +69,22 @@ describe('POST comment', () => { }); it(`Returns an error if postNewCase throws`, async () => { const request = httpServerMock.createKibanaRequest({ - path: '/api/cases/{id}/comment', + path: '/api/cases/{case_id}/comments', method: 'post', params: { - id: 'mock-id-1', + case_id: 'mock-id-1', }, body: { comment: 'Throw an error', }, }); - const theContext = createRouteContext(createMockSavedObjectsRepository(mockCases)); + const theContext = createRouteContext( + createMockSavedObjectsRepository({ + caseSavedObject: mockCases, + caseCommentSavedObject: mockCaseComments, + }) + ); const response = await routeHandler(theContext, request, kibanaResponseFactory); expect(response.status).toEqual(400); @@ -77,17 +94,22 @@ describe('POST comment', () => { routeHandler = await createRoute(initPostCommentApi, 'post', true); const request = httpServerMock.createKibanaRequest({ - path: '/api/cases/{id}/comment', + path: '/api/cases/{case_id}/comments', method: 'post', params: { - id: 'mock-id-1', + case_id: 'mock-id-1', }, body: { comment: 'Wow, good luck catching that bad meanie!', }, }); - const theContext = createRouteContext(createMockSavedObjectsRepository(mockCases)); + const theContext = createRouteContext( + createMockSavedObjectsRepository({ + caseSavedObject: mockCases, + caseCommentSavedObject: mockCaseComments, + }) + ); const response = await routeHandler(theContext, request, kibanaResponseFactory); expect(response.status).toEqual(500); diff --git a/x-pack/plugins/case/server/routes/api/cases/comments/post_comment.ts b/x-pack/plugins/case/server/routes/api/cases/comments/post_comment.ts new file mode 100644 index 0000000000000..9e82a8ffaaec7 --- /dev/null +++ b/x-pack/plugins/case/server/routes/api/cases/comments/post_comment.ts @@ -0,0 +1,85 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { schema } from '@kbn/config-schema'; +import Boom from 'boom'; +import { pipe } from 'fp-ts/lib/pipeable'; +import { fold } from 'fp-ts/lib/Either'; +import { identity } from 'fp-ts/lib/function'; + +import { CommentRequestRt, CommentResponseRt, throwErrors } from '../../../../../common/api'; +import { CASE_SAVED_OBJECT } from '../../../../saved_object_types'; +import { + escapeHatch, + transformNewComment, + wrapError, + flattenCommentSavedObject, +} from '../../utils'; +import { RouteDeps } from '../../types'; + +export function initPostCommentApi({ caseService, router }: RouteDeps) { + router.post( + { + path: '/api/cases/{case_id}/comments', + validate: { + params: schema.object({ + case_id: schema.string(), + }), + body: escapeHatch, + }, + }, + async (context, request, response) => { + try { + const query = pipe( + CommentRequestRt.decode(request.body), + fold(throwErrors(Boom.badRequest), identity) + ); + + const myCase = await caseService.getCase({ + client: context.core.savedObjects.client, + caseId: request.params.case_id, + }); + + const createdBy = await caseService.getUser({ request, response }); + const createdDate = new Date().toISOString(); + + const newComment = await caseService.postNewComment({ + client: context.core.savedObjects.client, + attributes: transformNewComment({ + createdDate, + ...query, + ...createdBy, + }), + references: [ + { + type: CASE_SAVED_OBJECT, + name: `associated-${CASE_SAVED_OBJECT}`, + id: myCase.id, + }, + ], + }); + + const updateCase = { + comment_ids: [...myCase.attributes.comment_ids, newComment.id], + }; + + await caseService.patchCase({ + client: context.core.savedObjects.client, + caseId: request.params.case_id, + updatedAttributes: { + ...updateCase, + }, + }); + + return response.ok({ + body: CommentResponseRt.encode(flattenCommentSavedObject(newComment)), + }); + } catch (error) { + return response.customError(wrapError(error)); + } + } + ); +} diff --git a/x-pack/plugins/case/server/routes/api/__tests__/delete_case.test.ts b/x-pack/plugins/case/server/routes/api/cases/delete_cases.test.ts similarity index 60% rename from x-pack/plugins/case/server/routes/api/__tests__/delete_case.test.ts rename to x-pack/plugins/case/server/routes/api/cases/delete_cases.test.ts index 9ea42ba42406b..cee705694f21d 100644 --- a/x-pack/plugins/case/server/routes/api/__tests__/delete_case.test.ts +++ b/x-pack/plugins/case/server/routes/api/cases/delete_cases.test.ts @@ -4,61 +4,76 @@ * you may not use this file except in compliance with the Elastic License. */ +import { kibanaResponseFactory, RequestHandler } from 'src/core/server'; +import { httpServerMock } from 'src/core/server/mocks'; + import { createMockSavedObjectsRepository, createRoute, createRouteContext, mockCases, mockCasesErrorTriggerData, + mockCaseComments, } from '../__fixtures__'; -import { initDeleteCaseApi } from '../delete_case'; -import { kibanaResponseFactory, RequestHandler } from 'src/core/server'; -import { httpServerMock } from 'src/core/server/mocks'; +import { initDeleteCasesApi } from './delete_cases'; describe('DELETE case', () => { let routeHandler: RequestHandler; beforeAll(async () => { - routeHandler = await createRoute(initDeleteCaseApi, 'delete'); + routeHandler = await createRoute(initDeleteCasesApi, 'delete'); }); - it(`deletes the case. responds with 204`, async () => { + it(`deletes the case. responds with 200`, async () => { const request = httpServerMock.createKibanaRequest({ - path: '/api/cases/{id}', + path: '/api/cases', method: 'delete', - params: { - id: 'mock-id-1', + query: { + ids: ['mock-id-1'], }, }); - const theContext = createRouteContext(createMockSavedObjectsRepository(mockCases)); + const theContext = createRouteContext( + createMockSavedObjectsRepository({ + caseSavedObject: mockCases, + caseCommentSavedObject: mockCaseComments, + }) + ); const response = await routeHandler(theContext, request, kibanaResponseFactory); - expect(response.status).toEqual(204); + expect(response.status).toEqual(200); }); it(`returns an error when thrown from deleteCase service`, async () => { const request = httpServerMock.createKibanaRequest({ - path: '/api/cases/{id}', + path: '/api/cases', method: 'delete', - params: { - id: 'not-real', + query: { + ids: ['not-real'], }, }); - const theContext = createRouteContext(createMockSavedObjectsRepository(mockCases)); + const theContext = createRouteContext( + createMockSavedObjectsRepository({ + caseSavedObject: mockCases, + caseCommentSavedObject: mockCaseComments, + }) + ); const response = await routeHandler(theContext, request, kibanaResponseFactory); expect(response.status).toEqual(404); }); it(`returns an error when thrown from getAllCaseComments service`, async () => { const request = httpServerMock.createKibanaRequest({ - path: '/api/cases/{id}', + path: '/api/cases', method: 'delete', - params: { - id: 'bad-guy', + query: { + ids: ['bad-guy'], }, }); const theContext = createRouteContext( - createMockSavedObjectsRepository(mockCasesErrorTriggerData) + createMockSavedObjectsRepository({ + caseSavedObject: mockCasesErrorTriggerData, + caseCommentSavedObject: mockCaseComments, + }) ); const response = await routeHandler(theContext, request, kibanaResponseFactory); @@ -66,15 +81,18 @@ describe('DELETE case', () => { }); it(`returns an error when thrown from deleteComment service`, async () => { const request = httpServerMock.createKibanaRequest({ - path: '/api/cases/{id}', + path: '/api/cases', method: 'delete', - params: { - id: 'valid-id', + query: { + ids: ['valid-id'], }, }); const theContext = createRouteContext( - createMockSavedObjectsRepository(mockCasesErrorTriggerData) + createMockSavedObjectsRepository({ + caseSavedObject: mockCasesErrorTriggerData, + caseCommentSavedObject: mockCasesErrorTriggerData, + }) ); const response = await routeHandler(theContext, request, kibanaResponseFactory); diff --git a/x-pack/plugins/case/server/routes/api/cases/delete_cases.ts b/x-pack/plugins/case/server/routes/api/cases/delete_cases.ts new file mode 100644 index 0000000000000..559a477a83a6c --- /dev/null +++ b/x-pack/plugins/case/server/routes/api/cases/delete_cases.ts @@ -0,0 +1,60 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { schema } from '@kbn/config-schema'; +import { RouteDeps } from '../types'; +import { wrapError } from '../utils'; + +export function initDeleteCasesApi({ caseService, router }: RouteDeps) { + router.delete( + { + path: '/api/cases', + validate: { + query: schema.object({ + ids: schema.arrayOf(schema.string()), + }), + }, + }, + async (context, request, response) => { + try { + await Promise.all( + request.query.ids.map(id => + caseService.deleteCase({ + client: context.core.savedObjects.client, + caseId: id, + }) + ) + ); + const comments = await Promise.all( + request.query.ids.map(id => + caseService.getAllCaseComments({ + client: context.core.savedObjects.client, + caseId: id, + }) + ) + ); + + if (comments.some(c => c.saved_objects.length > 0)) { + await Promise.all( + comments.map(c => + Promise.all( + c.saved_objects.map(({ id }) => + caseService.deleteComment({ + client: context.core.savedObjects.client, + commentId: id, + }) + ) + ) + ) + ); + } + return response.ok({ body: 'true' }); + } catch (error) { + return response.customError(wrapError(error)); + } + } + ); +} diff --git a/x-pack/plugins/case/server/routes/api/__tests__/get_all_cases.test.ts b/x-pack/plugins/case/server/routes/api/cases/get_all_cases.test.ts similarity index 84% rename from x-pack/plugins/case/server/routes/api/__tests__/get_all_cases.test.ts rename to x-pack/plugins/case/server/routes/api/cases/get_all_cases.test.ts index 96c411a746d49..ec56c32f91745 100644 --- a/x-pack/plugins/case/server/routes/api/__tests__/get_all_cases.test.ts +++ b/x-pack/plugins/case/server/routes/api/cases/get_all_cases.test.ts @@ -4,15 +4,16 @@ * you may not use this file except in compliance with the Elastic License. */ +import { kibanaResponseFactory, RequestHandler } from 'src/core/server'; +import { httpServerMock } from 'src/core/server/mocks'; + import { createMockSavedObjectsRepository, createRoute, createRouteContext, mockCases, } from '../__fixtures__'; -import { initGetAllCasesApi } from '../get_all_cases'; -import { kibanaResponseFactory, RequestHandler } from 'src/core/server'; -import { httpServerMock } from 'src/core/server/mocks'; +import { initGetAllCasesApi } from './get_all_cases'; describe('GET all cases', () => { let routeHandler: RequestHandler; @@ -25,7 +26,11 @@ describe('GET all cases', () => { method: 'get', }); - const theContext = createRouteContext(createMockSavedObjectsRepository(mockCases)); + const theContext = createRouteContext( + createMockSavedObjectsRepository({ + caseSavedObject: mockCases, + }) + ); const response = await routeHandler(theContext, request, kibanaResponseFactory); expect(response.status).toEqual(200); diff --git a/x-pack/plugins/case/server/routes/api/get_all_cases.ts b/x-pack/plugins/case/server/routes/api/cases/get_all_cases.ts similarity index 52% rename from x-pack/plugins/case/server/routes/api/get_all_cases.ts rename to x-pack/plugins/case/server/routes/api/cases/get_all_cases.ts index ba26a07dc2394..96b8e8c110c01 100644 --- a/x-pack/plugins/case/server/routes/api/get_all_cases.ts +++ b/x-pack/plugins/case/server/routes/api/cases/get_all_cases.ts @@ -4,37 +4,45 @@ * you may not use this file except in compliance with the Elastic License. */ -import { schema } from '@kbn/config-schema'; -import { RouteDeps } from '.'; -import { formatAllCases, sortToSnake, wrapError } from './utils'; -import { SavedObjectsFindOptionsSchema } from './schema'; -import { AllCases } from './types'; +import Boom from 'boom'; + +import { pipe } from 'fp-ts/lib/pipeable'; +import { fold } from 'fp-ts/lib/Either'; +import { identity } from 'fp-ts/lib/function'; + +import { CasesResponseRt, SavedObjectFindOptionsRt, throwErrors } from '../../../../common/api'; +import { transformCases, sortToSnake, wrapError, escapeHatch } from '../utils'; +import { RouteDeps } from '../types'; export function initGetAllCasesApi({ caseService, router }: RouteDeps) { router.get( { - path: '/api/cases', + path: '/api/cases/_find', validate: { - query: schema.nullable(SavedObjectsFindOptionsSchema), + query: escapeHatch, }, }, async (context, request, response) => { try { - const args = request.query + const query = pipe( + SavedObjectFindOptionsRt.decode(request.query), + fold(throwErrors(Boom.badRequest), identity) + ); + + const args = query ? { client: context.core.savedObjects.client, options: { - ...request.query, - sortField: sortToSnake(request.query.sortField ?? ''), + ...query, + sortField: sortToSnake(query.sortField ?? ''), }, } : { client: context.core.savedObjects.client, }; const cases = await caseService.getAllCases(args); - const body: AllCases = formatAllCases(cases); return response.ok({ - body, + body: CasesResponseRt.encode(transformCases(cases)), }); } catch (error) { return response.customError(wrapError(error)); diff --git a/x-pack/plugins/case/server/routes/api/__tests__/get_case.test.ts b/x-pack/plugins/case/server/routes/api/cases/get_case.test.ts similarity index 74% rename from x-pack/plugins/case/server/routes/api/__tests__/get_case.test.ts rename to x-pack/plugins/case/server/routes/api/cases/get_case.test.ts index 60becf1228a0c..5912df2c40aa3 100644 --- a/x-pack/plugins/case/server/routes/api/__tests__/get_case.test.ts +++ b/x-pack/plugins/case/server/routes/api/cases/get_case.test.ts @@ -4,18 +4,20 @@ * you may not use this file except in compliance with the Elastic License. */ +import { kibanaResponseFactory, RequestHandler, SavedObject } from 'src/core/server'; +import { httpServerMock } from 'src/core/server/mocks'; + +import { CaseAttributes } from '../../../../common/api'; import { createMockSavedObjectsRepository, createRoute, createRouteContext, mockCases, mockCasesErrorTriggerData, + mockCaseComments, } from '../__fixtures__'; -import { initGetCaseApi } from '../get_case'; -import { kibanaResponseFactory, RequestHandler, SavedObject } from 'src/core/server'; -import { httpServerMock } from 'src/core/server/mocks'; import { flattenCaseSavedObject } from '../utils'; -import { CaseAttributes } from '../types'; +import { initGetCaseApi } from './get_case'; describe('GET case', () => { let routeHandler: RequestHandler; @@ -24,17 +26,21 @@ describe('GET case', () => { }); it(`returns the case with empty case comments when includeComments is false`, async () => { const request = httpServerMock.createKibanaRequest({ - path: '/api/cases/{id}', + path: '/api/cases/{case_id}', + method: 'get', params: { - id: 'mock-id-1', + case_id: 'mock-id-1', }, - method: 'get', query: { includeComments: false, }, }); - const theContext = createRouteContext(createMockSavedObjectsRepository(mockCases)); + const theContext = createRouteContext( + createMockSavedObjectsRepository({ + caseSavedObject: mockCases, + }) + ); const response = await routeHandler(theContext, request, kibanaResponseFactory); @@ -49,17 +55,21 @@ describe('GET case', () => { }); it(`returns an error when thrown from getCase`, async () => { const request = httpServerMock.createKibanaRequest({ - path: '/api/cases/{id}', + path: '/api/cases/{case_id}', + method: 'get', params: { - id: 'abcdefg', + case_id: 'abcdefg', }, - method: 'get', query: { includeComments: false, }, }); - const theContext = createRouteContext(createMockSavedObjectsRepository(mockCases)); + const theContext = createRouteContext( + createMockSavedObjectsRepository({ + caseSavedObject: mockCases, + }) + ); const response = await routeHandler(theContext, request, kibanaResponseFactory); @@ -68,17 +78,22 @@ describe('GET case', () => { }); it(`returns the case with case comments when includeComments is true`, async () => { const request = httpServerMock.createKibanaRequest({ - path: '/api/cases/{id}', + path: '/api/cases/{case_id}', + method: 'get', params: { - id: 'mock-id-1', + case_id: 'mock-id-1', }, - method: 'get', query: { includeComments: true, }, }); - const theContext = createRouteContext(createMockSavedObjectsRepository(mockCases)); + const theContext = createRouteContext( + createMockSavedObjectsRepository({ + caseSavedObject: mockCases, + caseCommentSavedObject: mockCaseComments, + }) + ); const response = await routeHandler(theContext, request, kibanaResponseFactory); @@ -87,18 +102,20 @@ describe('GET case', () => { }); it(`returns an error when thrown from getAllCaseComments`, async () => { const request = httpServerMock.createKibanaRequest({ - path: '/api/cases/{id}', + path: '/api/cases/{case_id}', + method: 'get', params: { - id: 'bad-guy', + case_id: 'bad-guy', }, - method: 'get', query: { includeComments: true, }, }); const theContext = createRouteContext( - createMockSavedObjectsRepository(mockCasesErrorTriggerData) + createMockSavedObjectsRepository({ + caseSavedObject: mockCasesErrorTriggerData, + }) ); const response = await routeHandler(theContext, request, kibanaResponseFactory); diff --git a/x-pack/plugins/case/server/routes/api/get_case.ts b/x-pack/plugins/case/server/routes/api/cases/get_case.ts similarity index 58% rename from x-pack/plugins/case/server/routes/api/get_case.ts rename to x-pack/plugins/case/server/routes/api/cases/get_case.ts index 2481197000beb..1415513bca346 100644 --- a/x-pack/plugins/case/server/routes/api/get_case.ts +++ b/x-pack/plugins/case/server/routes/api/cases/get_case.ts @@ -5,16 +5,18 @@ */ import { schema } from '@kbn/config-schema'; -import { RouteDeps } from '.'; -import { flattenCaseSavedObject, wrapError } from './utils'; + +import { CaseResponseRt } from '../../../../common/api'; +import { RouteDeps } from '../types'; +import { flattenCaseSavedObject, wrapError } from '../utils'; export function initGetCaseApi({ caseService, router }: RouteDeps) { router.get( { - path: '/api/cases/{id}', + path: '/api/cases/{case_id}', validate: { params: schema.object({ - id: schema.string(), + case_id: schema.string(), }), query: schema.object({ includeComments: schema.string({ defaultValue: 'true' }), @@ -22,26 +24,25 @@ export function initGetCaseApi({ caseService, router }: RouteDeps) { }, }, async (context, request, response) => { - let theCase; - const includeComments = JSON.parse(request.query.includeComments); try { - theCase = await caseService.getCase({ + const includeComments = JSON.parse(request.query.includeComments); + + const theCase = await caseService.getCase({ client: context.core.savedObjects.client, - caseId: request.params.id, + caseId: request.params.case_id, }); - } catch (error) { - return response.customError(wrapError(error)); - } - if (!includeComments) { - return response.ok({ body: flattenCaseSavedObject(theCase, []) }); - } - try { + + if (!includeComments) { + return response.ok({ body: CaseResponseRt.encode(flattenCaseSavedObject(theCase, [])) }); + } + const theComments = await caseService.getAllCaseComments({ client: context.core.savedObjects.client, - caseId: request.params.id, + caseId: request.params.case_id, }); + return response.ok({ - body: { ...flattenCaseSavedObject(theCase, theComments.saved_objects) }, + body: CaseResponseRt.encode(flattenCaseSavedObject(theCase, theComments.saved_objects)), }); } catch (error) { return response.customError(wrapError(error)); diff --git a/x-pack/plugins/case/server/routes/api/__tests__/update_case.test.ts b/x-pack/plugins/case/server/routes/api/cases/patch_case.test.ts similarity index 69% rename from x-pack/plugins/case/server/routes/api/__tests__/update_case.test.ts rename to x-pack/plugins/case/server/routes/api/cases/patch_case.test.ts index 25d5cafb4bb06..42fe9967ad0a0 100644 --- a/x-pack/plugins/case/server/routes/api/__tests__/update_case.test.ts +++ b/x-pack/plugins/case/server/routes/api/cases/patch_case.test.ts @@ -4,35 +4,39 @@ * you may not use this file except in compliance with the Elastic License. */ +import { kibanaResponseFactory, RequestHandler } from 'src/core/server'; +import { httpServerMock } from 'src/core/server/mocks'; + import { createMockSavedObjectsRepository, createRoute, createRouteContext, mockCases, + mockCaseComments, } from '../__fixtures__'; -import { initUpdateCaseApi } from '../update_case'; -import { kibanaResponseFactory, RequestHandler } from 'src/core/server'; -import { httpServerMock } from 'src/core/server/mocks'; +import { initPatchCaseApi } from './patch_case'; -describe('UPDATE case', () => { +describe('PATCH case', () => { let routeHandler: RequestHandler; beforeAll(async () => { - routeHandler = await createRoute(initUpdateCaseApi, 'patch'); + routeHandler = await createRoute(initPatchCaseApi, 'patch'); }); - it(`Updates a case`, async () => { + it(`Patch a case`, async () => { const request = httpServerMock.createKibanaRequest({ - path: '/api/cases/{id}', + path: '/api/cases', method: 'patch', - params: { - id: 'mock-id-1', - }, body: { - case: { state: 'closed' }, + id: 'mock-id-1', + state: 'closed', version: 'WzAsMV0=', }, }); - const theContext = createRouteContext(createMockSavedObjectsRepository(mockCases)); + const theContext = createRouteContext( + createMockSavedObjectsRepository({ + caseSavedObject: mockCases, + }) + ); const response = await routeHandler(theContext, request, kibanaResponseFactory); expect(response.status).toEqual(200); @@ -41,53 +45,61 @@ describe('UPDATE case', () => { }); it(`Fails with 409 if version does not match`, async () => { const request = httpServerMock.createKibanaRequest({ - path: '/api/cases/{id}', + path: '/api/cases', method: 'patch', - params: { - id: 'mock-id-1', - }, body: { + id: 'mock-id-1', case: { state: 'closed' }, version: 'badv=', }, }); - const theContext = createRouteContext(createMockSavedObjectsRepository(mockCases)); + const theContext = createRouteContext( + createMockSavedObjectsRepository({ + caseSavedObject: mockCases, + }) + ); const response = await routeHandler(theContext, request, kibanaResponseFactory); expect(response.status).toEqual(409); }); it(`Fails with 406 if updated field is unchanged`, async () => { const request = httpServerMock.createKibanaRequest({ - path: '/api/cases/{id}', + path: '/api/cases', method: 'patch', - params: { - id: 'mock-id-1', - }, body: { + id: 'mock-id-1', case: { state: 'open' }, version: 'WzAsMV0=', }, }); - const theContext = createRouteContext(createMockSavedObjectsRepository(mockCases)); + const theContext = createRouteContext( + createMockSavedObjectsRepository({ + caseSavedObject: mockCases, + caseCommentSavedObject: mockCaseComments, + }) + ); const response = await routeHandler(theContext, request, kibanaResponseFactory); expect(response.status).toEqual(406); }); it(`Returns an error if updateCase throws`, async () => { const request = httpServerMock.createKibanaRequest({ - path: '/api/cases/{id}', + path: '/api/cases', method: 'patch', - params: { - id: 'mock-id-does-not-exist', - }, body: { + id: 'mock-id-does-not-exist', state: 'closed', + version: 'WzAsMV0=', }, }); - const theContext = createRouteContext(createMockSavedObjectsRepository(mockCases)); + const theContext = createRouteContext( + createMockSavedObjectsRepository({ + caseSavedObject: mockCases, + }) + ); const response = await routeHandler(theContext, request, kibanaResponseFactory); expect(response.status).toEqual(404); diff --git a/x-pack/plugins/case/server/routes/api/cases/patch_case.ts b/x-pack/plugins/case/server/routes/api/cases/patch_case.ts new file mode 100644 index 0000000000000..eccede372c688 --- /dev/null +++ b/x-pack/plugins/case/server/routes/api/cases/patch_case.ts @@ -0,0 +1,98 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import Boom from 'boom'; +import { difference, get } from 'lodash'; +import { pipe } from 'fp-ts/lib/pipeable'; +import { fold } from 'fp-ts/lib/Either'; +import { identity } from 'fp-ts/lib/function'; + +import { + CaseAttributes, + CasePatchRequestRt, + throwErrors, + CaseResponseRt, +} from '../../../../common/api'; +import { escapeHatch, wrapError, flattenCaseSavedObject } from '../utils'; +import { RouteDeps } from '../types'; + +export function initPatchCaseApi({ caseService, router }: RouteDeps) { + router.patch( + { + path: '/api/cases', + validate: { + body: escapeHatch, + }, + }, + async (context, request, response) => { + try { + const query = pipe( + CasePatchRequestRt.decode(request.body), + fold(throwErrors(Boom.badRequest), identity) + ); + const myCase = await caseService.getCase({ + client: context.core.savedObjects.client, + caseId: query.id, + }); + + if (query.version !== myCase.version) { + throw Boom.conflict( + 'This case has been updated. Please refresh before saving additional updates.' + ); + } + const currentCase: CaseAttributes = myCase.attributes; + const updateCase: Partial = Object.entries(query).reduce( + (acc, [key, value]) => { + const currentValue = get(currentCase, key); + if ( + currentValue != null && + Array.isArray(value) && + Array.isArray(currentValue) && + difference(value, currentValue).length !== 0 + ) { + return { + ...acc, + [key]: value, + }; + } else if (currentValue != null && value !== currentValue) { + return { + ...acc, + [key]: value, + }; + } + return acc; + }, + {} + ); + if (Object.keys(updateCase).length > 0) { + const updatedBy = await caseService.getUser({ request, response }); + const { full_name, username } = updatedBy; + const updatedCase = await caseService.patchCase({ + client: context.core.savedObjects.client, + caseId: query.id, + updatedAttributes: { + ...updateCase, + updated_at: new Date().toISOString(), + updated_by: { full_name, username }, + }, + }); + return response.ok({ + body: CaseResponseRt.encode( + flattenCaseSavedObject({ + ...updatedCase, + attributes: { ...myCase.attributes, ...updatedCase.attributes }, + references: myCase.references, + }) + ), + }); + } + throw Boom.notAcceptable('All update fields are identical to current version.'); + } catch (error) { + return response.customError(wrapError(error)); + } + } + ); +} diff --git a/x-pack/plugins/case/server/routes/api/__tests__/post_case.test.ts b/x-pack/plugins/case/server/routes/api/cases/post_case.test.ts similarity index 82% rename from x-pack/plugins/case/server/routes/api/__tests__/post_case.test.ts rename to x-pack/plugins/case/server/routes/api/cases/post_case.test.ts index 32c7c5a015af0..0d14a659d2c42 100644 --- a/x-pack/plugins/case/server/routes/api/__tests__/post_case.test.ts +++ b/x-pack/plugins/case/server/routes/api/cases/post_case.test.ts @@ -4,15 +4,16 @@ * you may not use this file except in compliance with the Elastic License. */ +import { kibanaResponseFactory, RequestHandler } from 'src/core/server'; +import { httpServerMock } from 'src/core/server/mocks'; + import { createMockSavedObjectsRepository, createRoute, createRouteContext, mockCases, } from '../__fixtures__'; -import { initPostCaseApi } from '../post_case'; -import { kibanaResponseFactory, RequestHandler } from 'src/core/server'; -import { httpServerMock } from 'src/core/server/mocks'; +import { initPostCaseApi } from './post_case'; describe('POST cases', () => { let routeHandler: RequestHandler; @@ -31,11 +32,15 @@ describe('POST cases', () => { }, }); - const theContext = createRouteContext(createMockSavedObjectsRepository(mockCases)); + const theContext = createRouteContext( + createMockSavedObjectsRepository({ + caseSavedObject: mockCases, + }) + ); const response = await routeHandler(theContext, request, kibanaResponseFactory); expect(response.status).toEqual(200); - expect(response.payload.case_id).toEqual('mock-it'); + expect(response.payload.id).toEqual('mock-it'); expect(response.payload.created_by.username).toEqual('awesome'); }); it(`Returns an error if postNewCase throws`, async () => { @@ -50,7 +55,11 @@ describe('POST cases', () => { }, }); - const theContext = createRouteContext(createMockSavedObjectsRepository(mockCases)); + const theContext = createRouteContext( + createMockSavedObjectsRepository({ + caseSavedObject: mockCases, + }) + ); const response = await routeHandler(theContext, request, kibanaResponseFactory); expect(response.status).toEqual(400); @@ -70,7 +79,11 @@ describe('POST cases', () => { }, }); - const theContext = createRouteContext(createMockSavedObjectsRepository(mockCases)); + const theContext = createRouteContext( + createMockSavedObjectsRepository({ + caseSavedObject: mockCases, + }) + ); const response = await routeHandler(theContext, request, kibanaResponseFactory); expect(response.status).toEqual(500); diff --git a/x-pack/plugins/case/server/routes/api/cases/post_case.ts b/x-pack/plugins/case/server/routes/api/cases/post_case.ts new file mode 100644 index 0000000000000..9e854c3178e1e --- /dev/null +++ b/x-pack/plugins/case/server/routes/api/cases/post_case.ts @@ -0,0 +1,48 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import Boom from 'boom'; +import { pipe } from 'fp-ts/lib/pipeable'; +import { fold } from 'fp-ts/lib/Either'; +import { identity } from 'fp-ts/lib/function'; + +import { flattenCaseSavedObject, transformNewCase, wrapError, escapeHatch } from '../utils'; + +import { CaseRequestRt, throwErrors, CaseResponseRt } from '../../../../common/api'; +import { RouteDeps } from '../types'; + +export function initPostCaseApi({ caseService, router }: RouteDeps) { + router.post( + { + path: '/api/cases', + validate: { + body: escapeHatch, + }, + }, + async (context, request, response) => { + try { + const query = pipe( + CaseRequestRt.decode(request.body), + fold(throwErrors(Boom.badRequest), identity) + ); + + const createdBy = await caseService.getUser({ request, response }); + const createdDate = new Date().toISOString(); + const newCase = await caseService.postNewCase({ + client: context.core.savedObjects.client, + attributes: transformNewCase({ + createdDate, + newCase: query, + ...createdBy, + }), + }); + return response.ok({ body: CaseResponseRt.encode(flattenCaseSavedObject(newCase, [])) }); + } catch (error) { + return response.customError(wrapError(error)); + } + } + ); +} diff --git a/x-pack/plugins/case/server/routes/api/get_tags.ts b/x-pack/plugins/case/server/routes/api/cases/tags/get_tags.ts similarity index 89% rename from x-pack/plugins/case/server/routes/api/get_tags.ts rename to x-pack/plugins/case/server/routes/api/cases/tags/get_tags.ts index 1d714db4c0c28..b1a2f10dd6f95 100644 --- a/x-pack/plugins/case/server/routes/api/get_tags.ts +++ b/x-pack/plugins/case/server/routes/api/cases/tags/get_tags.ts @@ -4,8 +4,8 @@ * you may not use this file except in compliance with the Elastic License. */ -import { RouteDeps } from './index'; -import { wrapError } from './utils'; +import { RouteDeps } from '../../types'; +import { wrapError } from '../../utils'; export function initGetTagsApi({ caseService, router }: RouteDeps) { router.get( diff --git a/x-pack/plugins/case/server/routes/api/delete_case.ts b/x-pack/plugins/case/server/routes/api/delete_case.ts deleted file mode 100644 index a5ae72b8b46ff..0000000000000 --- a/x-pack/plugins/case/server/routes/api/delete_case.ts +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - -import { schema } from '@kbn/config-schema'; -import { RouteDeps } from '.'; -import { wrapError } from './utils'; - -export function initDeleteCaseApi({ caseService, router }: RouteDeps) { - router.delete( - { - path: '/api/cases/{id}', - validate: { - params: schema.object({ - id: schema.string(), - }), - }, - }, - async (context, request, response) => { - let allCaseComments; - try { - await caseService.deleteCase({ - client: context.core.savedObjects.client, - caseId: request.params.id, - }); - } catch (error) { - return response.customError(wrapError(error)); - } - try { - allCaseComments = await caseService.getAllCaseComments({ - client: context.core.savedObjects.client, - caseId: request.params.id, - }); - } catch (error) { - return response.customError(wrapError(error)); - } - try { - if (allCaseComments.saved_objects.length > 0) { - await Promise.all( - allCaseComments.saved_objects.map(({ id }) => - caseService.deleteComment({ - client: context.core.savedObjects.client, - commentId: id, - }) - ) - ); - } - return response.noContent(); - } catch (error) { - return response.customError(wrapError(error)); - } - } - ); -} diff --git a/x-pack/plugins/case/server/routes/api/delete_comment.ts b/x-pack/plugins/case/server/routes/api/delete_comment.ts deleted file mode 100644 index 4a540dd9fd69f..0000000000000 --- a/x-pack/plugins/case/server/routes/api/delete_comment.ts +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - -import { schema } from '@kbn/config-schema'; -import { RouteDeps } from '.'; -import { wrapError } from './utils'; - -export function initDeleteCommentApi({ caseService, router }: RouteDeps) { - router.delete( - { - path: '/api/cases/comments/{comment_id}', - validate: { - params: schema.object({ - comment_id: schema.string(), - }), - }, - }, - async (context, request, response) => { - const client = context.core.savedObjects.client; - try { - await caseService.deleteComment({ - client, - commentId: request.params.comment_id, - }); - return response.noContent(); - } catch (error) { - return response.customError(wrapError(error)); - } - } - ); -} diff --git a/x-pack/plugins/case/server/routes/api/get_comment.ts b/x-pack/plugins/case/server/routes/api/get_comment.ts deleted file mode 100644 index d892b4cfebc3b..0000000000000 --- a/x-pack/plugins/case/server/routes/api/get_comment.ts +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - -import { schema } from '@kbn/config-schema'; -import { RouteDeps } from '.'; -import { flattenCommentSavedObject, wrapError } from './utils'; - -export function initGetCommentApi({ caseService, router }: RouteDeps) { - router.get( - { - path: '/api/cases/comments/{id}', - validate: { - params: schema.object({ - id: schema.string(), - }), - }, - }, - async (context, request, response) => { - try { - const theComment = await caseService.getComment({ - client: context.core.savedObjects.client, - commentId: request.params.id, - }); - return response.ok({ body: flattenCommentSavedObject(theComment) }); - } catch (error) { - return response.customError(wrapError(error)); - } - } - ); -} diff --git a/x-pack/plugins/case/server/routes/api/index.ts b/x-pack/plugins/case/server/routes/api/index.ts index 32dfd6a78d1c2..f4dca6a64c8d2 100644 --- a/x-pack/plugins/case/server/routes/api/index.ts +++ b/x-pack/plugins/case/server/routes/api/index.ts @@ -4,35 +4,36 @@ * you may not use this file except in compliance with the Elastic License. */ -import { IRouter } from 'src/core/server'; -import { CaseServiceSetup } from '../../services'; -import { initDeleteCaseApi } from './delete_case'; -import { initDeleteCommentApi } from './delete_comment'; -import { initGetAllCaseCommentsApi } from './get_all_case_comments'; -import { initGetAllCasesApi } from './get_all_cases'; -import { initGetCaseApi } from './get_case'; -import { initGetCommentApi } from './get_comment'; -import { initGetTagsApi } from './get_tags'; -import { initPostCaseApi } from './post_case'; -import { initPostCommentApi } from './post_comment'; -import { initUpdateCaseApi } from './update_case'; -import { initUpdateCommentApi } from './update_comment'; +import { initDeleteCasesApi } from './cases/delete_cases'; +import { initGetAllCasesApi } from './cases/get_all_cases'; +import { initGetCaseApi } from './cases/get_case'; +import { initPatchCaseApi } from './cases/patch_case'; +import { initPostCaseApi } from './cases/post_case'; -export interface RouteDeps { - caseService: CaseServiceSetup; - router: IRouter; -} +import { initDeleteCommentApi } from './cases/comments/delete_comment'; +import { initDeleteAllCommentsApi } from './cases/comments/delete_all_comments'; +import { initFindCaseCommentsApi } from './cases/comments/find_comments'; +import { initGetAllCommentsApi } from './cases/comments/get_all_comment'; +import { initGetCommentApi } from './cases/comments/get_comment'; +import { initPatchCommentApi } from './cases/comments/patch_comment'; +import { initPostCommentApi } from './cases/comments/post_comment'; + +import { initGetTagsApi } from './cases/tags/get_tags'; + +import { RouteDeps } from './types'; export function initCaseApi(deps: RouteDeps) { - initDeleteCaseApi(deps); + initDeleteCasesApi(deps); initDeleteCommentApi(deps); - initGetAllCaseCommentsApi(deps); + initDeleteAllCommentsApi(deps); + initFindCaseCommentsApi(deps); initGetAllCasesApi(deps); initGetCaseApi(deps); initGetCommentApi(deps); + initGetAllCommentsApi(deps); initGetTagsApi(deps); initPostCaseApi(deps); initPostCommentApi(deps); - initUpdateCaseApi(deps); - initUpdateCommentApi(deps); + initPatchCaseApi(deps); + initPatchCommentApi(deps); } diff --git a/x-pack/plugins/case/server/routes/api/post_case.ts b/x-pack/plugins/case/server/routes/api/post_case.ts deleted file mode 100644 index 948bf02d5b3c1..0000000000000 --- a/x-pack/plugins/case/server/routes/api/post_case.ts +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - -import { flattenCaseSavedObject, formatNewCase, wrapError } from './utils'; -import { NewCaseSchema } from './schema'; -import { RouteDeps } from '.'; - -export function initPostCaseApi({ caseService, router }: RouteDeps) { - router.post( - { - path: '/api/cases', - validate: { - body: NewCaseSchema, - }, - }, - async (context, request, response) => { - let createdBy; - try { - createdBy = await caseService.getUser({ request, response }); - } catch (error) { - return response.customError(wrapError(error)); - } - - try { - const newCase = await caseService.postNewCase({ - client: context.core.savedObjects.client, - attributes: formatNewCase(request.body, { - ...createdBy, - }), - }); - return response.ok({ body: flattenCaseSavedObject(newCase, []) }); - } catch (error) { - return response.customError(wrapError(error)); - } - } - ); -} diff --git a/x-pack/plugins/case/server/routes/api/post_comment.ts b/x-pack/plugins/case/server/routes/api/post_comment.ts deleted file mode 100644 index f3f21becddfad..0000000000000 --- a/x-pack/plugins/case/server/routes/api/post_comment.ts +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - -import { schema } from '@kbn/config-schema'; -import { flattenCommentSavedObject, formatNewComment, wrapError } from './utils'; -import { NewCommentSchema } from './schema'; -import { RouteDeps } from '.'; -import { CASE_SAVED_OBJECT } from '../../constants'; - -export function initPostCommentApi({ caseService, router }: RouteDeps) { - router.post( - { - path: '/api/cases/{id}/comment', - validate: { - params: schema.object({ - id: schema.string(), - }), - body: NewCommentSchema, - }, - }, - async (context, request, response) => { - let createdBy; - let newComment; - try { - await caseService.getCase({ - client: context.core.savedObjects.client, - caseId: request.params.id, - }); - } catch (error) { - return response.customError(wrapError(error)); - } - try { - createdBy = await caseService.getUser({ request, response }); - } catch (error) { - return response.customError(wrapError(error)); - } - try { - newComment = await caseService.postNewComment({ - client: context.core.savedObjects.client, - attributes: formatNewComment({ - newComment: request.body, - ...createdBy, - }), - references: [ - { - type: CASE_SAVED_OBJECT, - name: `associated-${CASE_SAVED_OBJECT}`, - id: request.params.id, - }, - ], - }); - - return response.ok({ body: flattenCommentSavedObject(newComment) }); - } catch (error) { - return response.customError(wrapError(error)); - } - } - ); -} diff --git a/x-pack/plugins/case/server/routes/api/types.ts b/x-pack/plugins/case/server/routes/api/types.ts index 5f1c207bf9829..1252fd19cda02 100644 --- a/x-pack/plugins/case/server/routes/api/types.ts +++ b/x-pack/plugins/case/server/routes/api/types.ts @@ -3,74 +3,12 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ +import { IRouter } from 'src/core/server'; +import { CaseServiceSetup } from '../../services'; -import { TypeOf } from '@kbn/config-schema'; -import { - CommentSchema, - NewCaseSchema, - NewCommentSchema, - SavedObjectsFindOptionsSchema, - UpdatedCaseSchema, - UpdatedCommentSchema, - UserSchema, -} from './schema'; -import { SavedObjectAttributes } from '../../../../../../src/core/types'; - -export type NewCaseType = TypeOf; -export type CommentAttributes = TypeOf & SavedObjectAttributes; -export type NewCommentType = TypeOf; -export type SavedObjectsFindOptionsType = TypeOf; -export type UpdatedCaseTyped = TypeOf; -export type UpdatedCommentType = TypeOf; -export type UserType = TypeOf; - -export interface CaseAttributes extends NewCaseType, SavedObjectAttributes { - created_at: string; - created_by: UserType; - updated_at: string; -} - -export type FlattenedCaseSavedObject = CaseAttributes & { - case_id: string; - version: string; - comments: FlattenedCommentSavedObject[]; -}; - -export type FlattenedCasesSavedObject = Array< - CaseAttributes & { - case_id: string; - version: string; - // TO DO it is partial because we need to add it the commentCount - commentCount?: number; - } ->; - -export interface AllCases { - cases: FlattenedCasesSavedObject; - page: number; - per_page: number; - total: number; -} - -export type FlattenedCommentSavedObject = CommentAttributes & { - comment_id: string; - version: string; - // TO DO We might want to add the case_id where this comment is related too -}; - -export interface AllComments { - comments: FlattenedCommentSavedObject[]; - page: number; - per_page: number; - total: number; -} - -export interface UpdatedCaseType { - description?: UpdatedCaseTyped['description']; - state?: UpdatedCaseTyped['state']; - tags?: UpdatedCaseTyped['tags']; - title?: UpdatedCaseTyped['title']; - updated_at: string; +export interface RouteDeps { + caseService: CaseServiceSetup; + router: IRouter; } export enum SortFieldCase { @@ -78,7 +16,3 @@ export enum SortFieldCase { state = 'state', updatedAt = 'updated_at', } - -export type Writable = { - -readonly [K in keyof T]: T[K]; -}; diff --git a/x-pack/plugins/case/server/routes/api/update_case.ts b/x-pack/plugins/case/server/routes/api/update_case.ts deleted file mode 100644 index 1c1a56dfe9b3a..0000000000000 --- a/x-pack/plugins/case/server/routes/api/update_case.ts +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - -import { schema } from '@kbn/config-schema'; -import { SavedObject } from 'kibana/server'; -import Boom from 'boom'; -import { difference } from 'lodash'; -import { wrapError } from './utils'; -import { RouteDeps } from '.'; -import { UpdateCaseArguments } from './schema'; -import { CaseAttributes, UpdatedCaseTyped, Writable } from './types'; - -interface UpdateCase extends Writable { - [key: string]: any; -} - -export function initUpdateCaseApi({ caseService, router }: RouteDeps) { - router.patch( - { - path: '/api/cases/{id}', - validate: { - params: schema.object({ - id: schema.string(), - }), - body: UpdateCaseArguments, - }, - }, - async (context, request, response) => { - let theCase: SavedObject; - try { - theCase = await caseService.getCase({ - client: context.core.savedObjects.client, - caseId: request.params.id, - }); - } catch (error) { - return response.customError(wrapError(error)); - } - - if (request.body.version !== theCase.version) { - return response.customError( - wrapError( - Boom.conflict( - 'This case has been updated. Please refresh before saving additional updates.' - ) - ) - ); - } - const currentCase = theCase.attributes; - const updateCase: Partial = Object.entries(request.body.case).reduce( - (acc, [key, value]) => { - const currentValue = currentCase[key]; - if ( - Array.isArray(value) && - Array.isArray(currentValue) && - difference(value, currentValue).length !== 0 - ) { - return { - ...acc, - [key]: value, - }; - } else if (value !== currentCase[key]) { - return { - ...acc, - [key]: value, - }; - } - return acc; - }, - {} - ); - if (Object.keys(updateCase).length > 0) { - try { - const updatedCase = await caseService.updateCase({ - client: context.core.savedObjects.client, - caseId: request.params.id, - updatedAttributes: { - ...updateCase, - updated_at: new Date().toISOString(), - }, - }); - return response.ok({ body: { ...updatedCase.attributes, version: updatedCase.version } }); - } catch (error) { - return response.customError(wrapError(error)); - } - } - return response.customError( - wrapError(Boom.notAcceptable('All update fields are identical to current version.')) - ); - } - ); -} diff --git a/x-pack/plugins/case/server/routes/api/update_comment.ts b/x-pack/plugins/case/server/routes/api/update_comment.ts deleted file mode 100644 index 9f99253f76629..0000000000000 --- a/x-pack/plugins/case/server/routes/api/update_comment.ts +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - -import { schema } from '@kbn/config-schema'; -import { SavedObject } from 'kibana/server'; -import Boom from 'boom'; -import { wrapError } from './utils'; -import { UpdateCommentArguments } from './schema'; -import { RouteDeps } from '.'; -import { CommentAttributes } from './types'; - -export function initUpdateCommentApi({ caseService, router }: RouteDeps) { - router.patch( - { - path: '/api/cases/comment/{id}', - validate: { - params: schema.object({ - id: schema.string(), - }), - body: UpdateCommentArguments, - }, - }, - async (context, request, response) => { - let theComment: SavedObject; - try { - theComment = await caseService.getComment({ - client: context.core.savedObjects.client, - commentId: request.params.id, - }); - } catch (error) { - return response.customError(wrapError(error)); - } - if (request.body.version !== theComment.version) { - return response.customError( - wrapError( - Boom.conflict( - 'This comment has been updated. Please refresh before saving additional updates.' - ) - ) - ); - } - if (request.body.comment === theComment.attributes.comment) { - return response.customError( - wrapError(Boom.notAcceptable('Comment is identical to current version.')) - ); - } - try { - const updatedComment = await caseService.updateComment({ - client: context.core.savedObjects.client, - commentId: request.params.id, - updatedAttributes: { - comment: request.body.comment, - updated_at: new Date().toISOString(), - }, - }); - return response.ok({ - body: { ...updatedComment.attributes, version: updatedComment.version }, - }); - } catch (error) { - return response.customError(wrapError(error)); - } - } - ); -} diff --git a/x-pack/plugins/case/server/routes/api/utils.ts b/x-pack/plugins/case/server/routes/api/utils.ts index 32de41e1c01c5..920c53f404456 100644 --- a/x-pack/plugins/case/server/routes/api/utils.ts +++ b/x-pack/plugins/case/server/routes/api/utils.ts @@ -4,6 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ +import { schema } from '@kbn/config-schema'; import { boomify, isBoom } from 'boom'; import { CustomHttpResponseOptions, @@ -12,42 +13,53 @@ import { SavedObjectsFindResponse, } from 'kibana/server'; import { - AllComments, + CaseRequest, + CaseResponse, + CasesResponse, CaseAttributes, + CommentResponse, + CommentsResponse, CommentAttributes, - FlattenedCaseSavedObject, - FlattenedCommentSavedObject, - AllCases, - NewCaseType, - NewCommentType, - SortFieldCase, - UserType, -} from './types'; +} from '../../../common/api'; -export const formatNewCase = ( - newCase: NewCaseType, - { full_name, username }: { full_name?: string; username: string } -): CaseAttributes => ({ - created_at: new Date().toISOString(), +import { SortFieldCase } from './types'; + +export const transformNewCase = ({ + createdDate, + newCase, + full_name, + username, +}: { + createdDate: string; + newCase: CaseRequest; + full_name?: string | null; + username: string | null; +}): CaseAttributes => ({ + comment_ids: [], + created_at: createdDate, created_by: { full_name, username }, - updated_at: new Date().toISOString(), + updated_at: null, + updated_by: null, ...newCase, }); interface NewCommentArgs { - newComment: NewCommentType; - full_name?: UserType['full_name']; - username: UserType['username']; + comment: string; + createdDate: string; + full_name?: string | null; + username: string | null; } -export const formatNewComment = ({ - newComment, +export const transformNewComment = ({ + comment, + createdDate, full_name, username, }: NewCommentArgs): CommentAttributes => ({ - ...newComment, - created_at: new Date().toISOString(), + comment, + created_at: createdDate, created_by: { full_name, username }, - updated_at: new Date().toISOString(), + updated_at: null, + updated_by: null, }); export function wrapError(error: any): CustomHttpResponseOptions { @@ -59,7 +71,7 @@ export function wrapError(error: any): CustomHttpResponseOptions }; } -export const formatAllCases = (cases: SavedObjectsFindResponse): AllCases => ({ +export const transformCases = (cases: SavedObjectsFindResponse): CasesResponse => ({ page: cases.page, per_page: cases.per_page, total: cases.total, @@ -68,27 +80,24 @@ export const formatAllCases = (cases: SavedObjectsFindResponse): export const flattenCaseSavedObjects = ( savedObjects: SavedObjectsFindResponse['saved_objects'] -): FlattenedCaseSavedObject[] => - savedObjects.reduce( - (acc: FlattenedCaseSavedObject[], savedObject: SavedObject) => { - return [...acc, flattenCaseSavedObject(savedObject, [])]; - }, - [] - ); +): CaseResponse[] => + savedObjects.reduce((acc: CaseResponse[], savedObject: SavedObject) => { + return [...acc, flattenCaseSavedObject(savedObject, [])]; + }, []); export const flattenCaseSavedObject = ( savedObject: SavedObject, - comments: Array> -): FlattenedCaseSavedObject => ({ - case_id: savedObject.id, - version: savedObject.version ? savedObject.version : '0', + comments: Array> = [] +): CaseResponse => ({ + id: savedObject.id, + version: savedObject.version ?? '0', comments: flattenCommentSavedObjects(comments), ...savedObject.attributes, }); -export const formatAllComments = ( +export const transformComments = ( comments: SavedObjectsFindResponse -): AllComments => ({ +): CommentsResponse => ({ page: comments.page, per_page: comments.per_page, total: comments.total, @@ -97,19 +106,16 @@ export const formatAllComments = ( export const flattenCommentSavedObjects = ( savedObjects: SavedObjectsFindResponse['saved_objects'] -): FlattenedCommentSavedObject[] => - savedObjects.reduce( - (acc: FlattenedCommentSavedObject[], savedObject: SavedObject) => { - return [...acc, flattenCommentSavedObject(savedObject)]; - }, - [] - ); +): CommentResponse[] => + savedObjects.reduce((acc: CommentResponse[], savedObject: SavedObject) => { + return [...acc, flattenCommentSavedObject(savedObject)]; + }, []); export const flattenCommentSavedObject = ( savedObject: SavedObject -): FlattenedCommentSavedObject => ({ - comment_id: savedObject.id, - version: savedObject.version ? savedObject.version : '0', +): CommentResponse => ({ + id: savedObject.id, + version: savedObject.version ?? '0', ...savedObject.attributes, }); @@ -127,3 +133,5 @@ export const sortToSnake = (sortField: string): SortFieldCase => { return SortFieldCase.createdAt; } }; + +export const escapeHatch = schema.object({}, { allowUnknowns: true }); diff --git a/x-pack/plugins/case/server/saved_object_types/cases.ts b/x-pack/plugins/case/server/saved_object_types/cases.ts new file mode 100644 index 0000000000000..faed0a3100a42 --- /dev/null +++ b/x-pack/plugins/case/server/saved_object_types/cases.ts @@ -0,0 +1,60 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { SavedObjectsType } from 'src/core/server'; + +export const CASE_SAVED_OBJECT = 'cases'; + +export const caseSavedObjectType: SavedObjectsType = { + name: CASE_SAVED_OBJECT, + hidden: false, + namespaceAgnostic: false, + mappings: { + properties: { + comment_ids: { + type: 'keyword', + }, + created_at: { + type: 'date', + }, + created_by: { + properties: { + username: { + type: 'keyword', + }, + full_name: { + type: 'keyword', + }, + }, + }, + description: { + type: 'text', + }, + title: { + type: 'keyword', + }, + state: { + type: 'keyword', + }, + tags: { + type: 'keyword', + }, + updated_at: { + type: 'date', + }, + updated_by: { + properties: { + username: { + type: 'keyword', + }, + full_name: { + type: 'keyword', + }, + }, + }, + }, + }, +}; diff --git a/x-pack/plugins/case/server/saved_object_types/comments.ts b/x-pack/plugins/case/server/saved_object_types/comments.ts new file mode 100644 index 0000000000000..51c31421fec2f --- /dev/null +++ b/x-pack/plugins/case/server/saved_object_types/comments.ts @@ -0,0 +1,48 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { SavedObjectsType } from 'src/core/server'; + +export const CASE_COMMENT_SAVED_OBJECT = 'cases-comments'; + +export const caseCommentSavedObjectType: SavedObjectsType = { + name: CASE_COMMENT_SAVED_OBJECT, + hidden: false, + namespaceAgnostic: false, + mappings: { + properties: { + comment: { + type: 'text', + }, + created_at: { + type: 'date', + }, + created_by: { + properties: { + full_name: { + type: 'keyword', + }, + username: { + type: 'keyword', + }, + }, + }, + updated_at: { + type: 'date', + }, + updated_by: { + properties: { + username: { + type: 'keyword', + }, + full_name: { + type: 'keyword', + }, + }, + }, + }, + }, +}; diff --git a/x-pack/plugins/case/server/saved_object_types/index.ts b/x-pack/plugins/case/server/saved_object_types/index.ts new file mode 100644 index 0000000000000..1e29b9dd98ead --- /dev/null +++ b/x-pack/plugins/case/server/saved_object_types/index.ts @@ -0,0 +1,8 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +export { caseSavedObjectType, CASE_SAVED_OBJECT } from './cases'; +export { caseCommentSavedObjectType, CASE_COMMENT_SAVED_OBJECT } from './comments'; diff --git a/x-pack/plugins/case/server/services/index.ts b/x-pack/plugins/case/server/services/index.ts index e6416e268e30b..61b696d45d030 100644 --- a/x-pack/plugins/case/server/services/index.ts +++ b/x-pack/plugins/case/server/services/index.ts @@ -14,15 +14,10 @@ import { SavedObjectsUpdateResponse, SavedObjectReference, } from 'kibana/server'; -import { CASE_COMMENT_SAVED_OBJECT, CASE_SAVED_OBJECT } from '../constants'; -import { - CaseAttributes, - CommentAttributes, - SavedObjectsFindOptionsType, - UpdatedCaseType, - UpdatedCommentType, -} from '../routes/api/types'; + import { AuthenticatedUser, SecurityPluginSetup } from '../../../security/server'; +import { CaseAttributes, CommentAttributes, SavedObjectFindOptions } from '../../common/api'; +import { CASE_SAVED_OBJECT, CASE_COMMENT_SAVED_OBJECT } from '../saved_object_types'; import { readTags } from './tags/read_tags'; interface ClientArgs { @@ -33,8 +28,12 @@ interface GetCaseArgs extends ClientArgs { caseId: string; } +interface GetCommentsArgs extends GetCaseArgs { + options?: SavedObjectFindOptions; +} + interface GetCasesArgs extends ClientArgs { - options?: SavedObjectsFindOptionsType; + options?: SavedObjectFindOptions; } interface GetCommentArgs extends ClientArgs { commentId: string; @@ -47,13 +46,13 @@ interface PostCommentArgs extends ClientArgs { attributes: CommentAttributes; references: SavedObjectReference[]; } -interface UpdateCaseArgs extends ClientArgs { +interface PatchCaseArgs extends ClientArgs { caseId: string; - updatedAttributes: UpdatedCaseType; + updatedAttributes: Partial; } interface UpdateCommentArgs extends ClientArgs { commentId: string; - updatedAttributes: UpdatedCommentType; + updatedAttributes: Partial; } interface GetUserArgs { @@ -68,15 +67,15 @@ export interface CaseServiceSetup { deleteCase(args: GetCaseArgs): Promise<{}>; deleteComment(args: GetCommentArgs): Promise<{}>; getAllCases(args: GetCasesArgs): Promise>; - getAllCaseComments(args: GetCaseArgs): Promise>; + getAllCaseComments(args: GetCommentsArgs): Promise>; getCase(args: GetCaseArgs): Promise>; getComment(args: GetCommentArgs): Promise>; getTags(args: ClientArgs): Promise; getUser(args: GetUserArgs): Promise; postNewCase(args: PostCaseArgs): Promise>; postNewComment(args: PostCommentArgs): Promise>; - updateCase(args: UpdateCaseArgs): Promise>; - updateComment(args: UpdateCommentArgs): Promise>; + patchCase(args: PatchCaseArgs): Promise>; + patchComment(args: UpdateCommentArgs): Promise>; } export class CaseService { @@ -127,10 +126,11 @@ export class CaseService { throw error; } }, - getAllCaseComments: async ({ client, caseId }: GetCaseArgs) => { + getAllCaseComments: async ({ client, caseId, options }: GetCommentsArgs) => { try { this.log.debug(`Attempting to GET all comments for case ${caseId}`); return await client.find({ + ...options, type: CASE_COMMENT_SAVED_OBJECT, hasReference: { type: CASE_SAVED_OBJECT, id: caseId }, }); @@ -175,7 +175,7 @@ export class CaseService { throw error; } }, - updateCase: async ({ client, caseId, updatedAttributes }: UpdateCaseArgs) => { + patchCase: async ({ client, caseId, updatedAttributes }: PatchCaseArgs) => { try { this.log.debug(`Attempting to UPDATE case ${caseId}`); return await client.update(CASE_SAVED_OBJECT, caseId, { ...updatedAttributes }); @@ -184,7 +184,7 @@ export class CaseService { throw error; } }, - updateComment: async ({ client, commentId, updatedAttributes }: UpdateCommentArgs) => { + patchComment: async ({ client, commentId, updatedAttributes }: UpdateCommentArgs) => { try { this.log.debug(`Attempting to UPDATE comment ${commentId}`); return await client.update(CASE_COMMENT_SAVED_OBJECT, commentId, { diff --git a/x-pack/plugins/case/server/services/tags/read_tags.ts b/x-pack/plugins/case/server/services/tags/read_tags.ts index da5905fe4ea35..ddb79507b5fef 100644 --- a/x-pack/plugins/case/server/services/tags/read_tags.ts +++ b/x-pack/plugins/case/server/services/tags/read_tags.ts @@ -5,8 +5,9 @@ */ import { SavedObject, SavedObjectsClientContract } from 'kibana/server'; -import { CASE_SAVED_OBJECT } from '../../constants'; -import { CaseAttributes } from '../..'; + +import { CaseAttributes } from '../../../common/api'; +import { CASE_SAVED_OBJECT } from '../../saved_object_types'; const DEFAULT_PER_PAGE: number = 1000; @@ -23,7 +24,7 @@ export const convertTagsToSet = (tagObjects: Array>) return new Set(convertToTags(tagObjects)); }; -// Note: This is doing an in-memory aggregation of the tags by calling each of the alerting +// Note: This is doing an in-memory aggregation of the tags by calling each of the case // records in batches of this const setting and uses the fields to try to get the least // amount of data per record back. If saved objects at some point supports aggregations // then this should be replaced with a an aggregation call. diff --git a/x-pack/tsconfig.json b/x-pack/tsconfig.json index 31ef0bef18a85..a6c94ff74620e 100644 --- a/x-pack/tsconfig.json +++ b/x-pack/tsconfig.json @@ -43,4 +43,4 @@ "jest" ] } -} \ No newline at end of file +}