Skip to content

Commit

Permalink
Including owner when patching a comment
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-buttner committed Jun 22, 2021
1 parent d7f2325 commit cc7a06a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 26 deletions.
31 changes: 17 additions & 14 deletions x-pack/plugins/cases/public/containers/api.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -363,33 +363,36 @@ describe('Case Configuration API', () => {
});

test('check url, method, signal', async () => {
await patchComment(
basicCase.id,
basicCase.comments[0].id,
'updated comment',
basicCase.comments[0].version,
abortCtrl.signal
);
await patchComment({
caseId: basicCase.id,
commentId: basicCase.comments[0].id,
commentUpdate: 'updated comment',
version: basicCase.comments[0].version,
signal: abortCtrl.signal,
owner: SECURITY_SOLUTION_OWNER,
});
expect(fetchMock).toHaveBeenCalledWith(`${CASES_URL}/${basicCase.id}/comments`, {
method: 'PATCH',
body: JSON.stringify({
comment: 'updated comment',
type: CommentType.user,
id: basicCase.comments[0].id,
version: basicCase.comments[0].version,
owner: SECURITY_SOLUTION_OWNER,
}),
signal: abortCtrl.signal,
});
});

test('happy path', async () => {
const resp = await patchComment(
basicCase.id,
basicCase.comments[0].id,
'updated comment',
basicCase.comments[0].version,
abortCtrl.signal
);
const resp = await patchComment({
caseId: basicCase.id,
commentId: basicCase.comments[0].id,
commentUpdate: 'updated comment',
version: basicCase.comments[0].version,
signal: abortCtrl.signal,
owner: SECURITY_SOLUTION_OWNER,
});
expect(resp).toEqual(basicCase);
});
});
Expand Down
26 changes: 18 additions & 8 deletions x-pack/plugins/cases/public/containers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,21 +283,31 @@ export const postComment = async (
return convertToCamelCase<CaseResponse, Case>(decodeCaseResponse(response));
};

export const patchComment = async (
caseId: string,
commentId: string,
commentUpdate: string,
version: string,
signal: AbortSignal,
subCaseId?: string
): Promise<Case> => {
export const patchComment = async ({
caseId,
commentId,
commentUpdate,
version,
signal,
owner,
subCaseId,
}: {
caseId: string;
commentId: string;
commentUpdate: string;
version: string;
signal: AbortSignal;
owner: string;
subCaseId?: string;
}): Promise<Case> => {
const response = await KibanaServices.get().http.fetch<CaseResponse>(getCaseCommentsUrl(caseId), {
method: 'PATCH',
body: JSON.stringify({
comment: commentUpdate,
type: CommentType.user,
id: commentId,
version,
owner,
}),
...(subCaseId ? { query: { subCaseId } } : {}),
signal,
Expand Down
13 changes: 9 additions & 4 deletions x-pack/plugins/cases/public/containers/use_update_comment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { useReducer, useCallback, useRef, useEffect } from 'react';
import { useToasts } from '../common/lib/kibana';
import { useOwnerContext } from '../components/owner_context/use_owner_context';
import { patchComment } from './api';
import * as i18n from './translations';
import { Case } from './types';
Expand Down Expand Up @@ -72,6 +73,9 @@ export const useUpdateComment = (): UseUpdateComment => {
const toasts = useToasts();
const isCancelledRef = useRef(false);
const abortCtrlRef = useRef(new AbortController());
// this hook guarantees that there will be at least one value in the owner array, we'll
// just use the first entry just in case there are more than one entry
const owner = useOwnerContext()[0];

const dispatchUpdateComment = useCallback(
async ({
Expand All @@ -89,14 +93,15 @@ export const useUpdateComment = (): UseUpdateComment => {
abortCtrlRef.current = new AbortController();
dispatch({ type: 'FETCH_INIT', payload: commentId });

const response = await patchComment(
const response = await patchComment({
caseId,
commentId,
commentUpdate,
version,
abortCtrlRef.current.signal,
subCaseId
);
signal: abortCtrlRef.current.signal,
subCaseId,
owner,
});

if (!isCancelledRef.current) {
updateCase(response);
Expand Down

0 comments on commit cc7a06a

Please sign in to comment.