Skip to content

Commit

Permalink
[Cases] Move remaining HTTP functionality to client (#96507)
Browse files Browse the repository at this point in the history
* Moving deletes and find for attachments

* Moving rest of comment apis

* Migrating configuration routes to client

* Finished moving routes, starting utils refactor

* Refactoring utilites and fixing integration tests

* Addressing PR feedback

* Fixing mocks and types

* Fixing integration tests

* Renaming status_stats

* Fixing test type errors

* Adding plugins to kibana.json

* Adding cases to required plugin
  • Loading branch information
jonathan-buttner authored Apr 14, 2021
1 parent 34f2d86 commit 613e859
Show file tree
Hide file tree
Showing 66 changed files with 2,715 additions and 2,328 deletions.
9 changes: 7 additions & 2 deletions x-pack/plugins/cases/server/client/attachments/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { identity } from 'fp-ts/lib/function';

import { SavedObject, SavedObjectsClientContract, Logger } from 'src/core/server';
import { nodeBuilder } from '../../../../../../src/plugins/data/common';
import { decodeCommentRequest, isCommentRequestTypeGenAlert } from '../../routes/api/utils';

import {
throwErrors,
Expand All @@ -33,7 +32,11 @@ import {
} from '../../services/user_actions/helpers';

import { AttachmentService, CaseService, CaseUserActionService } from '../../services';
import { CommentableCase, createAlertUpdateRequest } from '../../common';
import {
CommentableCase,
createAlertUpdateRequest,
isCommentRequestTypeGenAlert,
} from '../../common';
import { CasesClientArgs, CasesClientInternal } from '..';
import { createCaseError } from '../../common/error';
import {
Expand All @@ -42,6 +45,8 @@ import {
ENABLE_CASE_CONNECTOR,
} from '../../../common/constants';

import { decodeCommentRequest } from '../utils';

async function getSubCase({
caseService,
savedObjectsClient,
Expand Down
26 changes: 24 additions & 2 deletions x-pack/plugins/cases/server/client/attachments/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,34 @@
* 2.0.
*/

import { CaseResponse, CommentRequest as AttachmentsRequest } from '../../../common/api';
import {
AllCommentsResponse,
CaseResponse,
CommentRequest as AttachmentsRequest,
CommentResponse,
CommentsResponse,
} from '../../../common/api';

import { CasesClientInternal } from '../client_internal';
import { CasesClientArgs } from '../types';
import { addComment } from './add';
import { DeleteAllArgs, deleteAll, DeleteArgs, deleteComment } from './delete';
import { find, FindArgs, get, getAll, GetAllArgs, GetArgs } from './get';
import { update, UpdateArgs } from './update';

export interface AttachmentsAdd {
interface AttachmentsAdd {
caseId: string;
comment: AttachmentsRequest;
}

export interface AttachmentsSubClient {
add(args: AttachmentsAdd): Promise<CaseResponse>;
deleteAll(deleteAllArgs: DeleteAllArgs): Promise<void>;
delete(deleteArgs: DeleteArgs): Promise<void>;
find(findArgs: FindArgs): Promise<CommentsResponse>;
getAll(getAllArgs: GetAllArgs): Promise<AllCommentsResponse>;
get(getArgs: GetArgs): Promise<CommentResponse>;
update(updateArgs: UpdateArgs): Promise<CaseResponse>;
}

export const createAttachmentsSubClient = (
Expand All @@ -31,6 +47,12 @@ export const createAttachmentsSubClient = (
caseId,
comment,
}),
deleteAll: (deleteAllArgs: DeleteAllArgs) => deleteAll(deleteAllArgs, args),
delete: (deleteArgs: DeleteArgs) => deleteComment(deleteArgs, args),
find: (findArgs: FindArgs) => find(findArgs, args),
getAll: (getAllArgs: GetAllArgs) => getAll(getAllArgs, args),
get: (getArgs: GetArgs) => get(getArgs, args),
update: (updateArgs: UpdateArgs) => update(updateArgs, args),
};

return Object.freeze(attachmentSubClient);
Expand Down
154 changes: 154 additions & 0 deletions x-pack/plugins/cases/server/client/attachments/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import Boom from '@hapi/boom';
import { CASE_SAVED_OBJECT, SUB_CASE_SAVED_OBJECT } from '../../../common/constants';

import { AssociationType } from '../../../common/api';
import { CasesClientArgs } from '../types';
import { buildCommentUserActionItem } from '../../services/user_actions/helpers';
import { createCaseError } from '../../common/error';
import { checkEnabledCaseConnectorOrThrow } from '../../common';

/**
* Parameters for deleting all comments of a case or sub case.
*/
export interface DeleteAllArgs {
caseID: string;
subCaseID?: string;
}

/**
* Parameters for deleting a single comment of a case or sub case.
*/
export interface DeleteArgs {
caseID: string;
attachmentID: string;
subCaseID?: string;
}

/**
* Delete all comments for a case or sub case.
*/
export async function deleteAll(
{ caseID, subCaseID }: DeleteAllArgs,
clientArgs: CasesClientArgs
): Promise<void> {
const {
user,
savedObjectsClient: soClient,
caseService,
attachmentService,
userActionService,
logger,
} = clientArgs;

try {
checkEnabledCaseConnectorOrThrow(subCaseID);

const id = subCaseID ?? caseID;
const comments = await caseService.getCommentsByAssociation({
soClient,
id,
associationType: subCaseID ? AssociationType.subCase : AssociationType.case,
});

await Promise.all(
comments.saved_objects.map((comment) =>
attachmentService.delete({
soClient,
attachmentId: comment.id,
})
)
);

const deleteDate = new Date().toISOString();

await userActionService.bulkCreate({
soClient,
actions: comments.saved_objects.map((comment) =>
buildCommentUserActionItem({
action: 'delete',
actionAt: deleteDate,
actionBy: user,
caseId: caseID,
subCaseId: subCaseID,
commentId: comment.id,
fields: ['comment'],
})
),
});
} catch (error) {
throw createCaseError({
message: `Failed to delete all comments case id: ${caseID} sub case id: ${subCaseID}: ${error}`,
error,
logger,
});
}
}

export async function deleteComment(
{ caseID, attachmentID, subCaseID }: DeleteArgs,
clientArgs: CasesClientArgs
) {
const {
user,
savedObjectsClient: soClient,
attachmentService,
userActionService,
logger,
} = clientArgs;

try {
checkEnabledCaseConnectorOrThrow(subCaseID);

const deleteDate = new Date().toISOString();

const myComment = await attachmentService.get({
soClient,
attachmentId: attachmentID,
});

if (myComment == null) {
throw Boom.notFound(`This comment ${attachmentID} does not exist anymore.`);
}

const type = subCaseID ? SUB_CASE_SAVED_OBJECT : CASE_SAVED_OBJECT;
const id = subCaseID ?? caseID;

const caseRef = myComment.references.find((c) => c.type === type);
if (caseRef == null || (caseRef != null && caseRef.id !== id)) {
throw Boom.notFound(`This comment ${attachmentID} does not exist in ${id}.`);
}

await attachmentService.delete({
soClient,
attachmentId: attachmentID,
});

await userActionService.bulkCreate({
soClient,
actions: [
buildCommentUserActionItem({
action: 'delete',
actionAt: deleteDate,
actionBy: user,
caseId: id,
subCaseId: subCaseID,
commentId: attachmentID,
fields: ['comment'],
}),
],
});
} catch (error) {
throw createCaseError({
message: `Failed to delete comment in route case id: ${caseID} comment id: ${attachmentID} sub case id: ${subCaseID}: ${error}`,
error,
logger,
});
}
}
Loading

0 comments on commit 613e859

Please sign in to comment.