Skip to content

Commit

Permalink
[Response Ops][Cases] Refactor commentable case (#124431)
Browse files Browse the repository at this point in the history
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
jonathan-buttner and kibanamachine authored Mar 31, 2022
1 parent a2ee23d commit 3073231
Show file tree
Hide file tree
Showing 6 changed files with 348 additions and 487 deletions.
112 changes: 9 additions & 103 deletions x-pack/plugins/cases/server/client/attachments/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,62 +10,17 @@ import { pipe } from 'fp-ts/lib/pipeable';
import { fold } from 'fp-ts/lib/Either';
import { identity } from 'fp-ts/lib/function';

import {
SavedObjectsClientContract,
Logger,
SavedObjectsUtils,
} from '../../../../../../src/core/server';
import { LensServerPluginSetup } from '../../../../lens/server';
import { SavedObjectsUtils } from '../../../../../../src/core/server';

import {
Actions,
ActionTypes,
CaseResponse,
CommentRequest,
CommentRequestRt,
CommentType,
throwErrors,
User,
} from '../../../common/api';
import { CaseResponse, CommentRequest, CommentRequestRt, throwErrors } from '../../../common/api';

import { AttachmentService, CasesService } from '../../services';
import { CommentableCase } from '../../common/models';
import { CaseCommentModel } from '../../common/models';
import { createCaseError } from '../../common/error';
import { createAlertUpdateRequest } from '../../common/utils';
import { CasesClientArgs, CasesClientInternal } from '..';
import { CasesClientArgs } from '..';

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

async function createCommentableCase({
caseService,
attachmentService,
unsecuredSavedObjectsClient,
id,
logger,
lensEmbeddableFactory,
}: {
caseService: CasesService;
attachmentService: AttachmentService;
unsecuredSavedObjectsClient: SavedObjectsClientContract;
id: string;
logger: Logger;
lensEmbeddableFactory: LensServerPluginSetup['lensEmbeddableFactory'];
}): Promise<CommentableCase> {
const caseInfo = await caseService.getCase({
id,
});

return new CommentableCase({
logger,
caseInfo,
caseService,
attachmentService,
unsecuredSavedObjectsClient,
lensEmbeddableFactory,
});
}

/**
* The arguments needed for creating a new attachment to a case.
*/
Expand All @@ -87,26 +42,15 @@ export interface AddArgs {
*/
export const addComment = async (
addArgs: AddArgs,
clientArgs: CasesClientArgs,
casesClientInternal: CasesClientInternal
clientArgs: CasesClientArgs
): Promise<CaseResponse> => {
const { comment, caseId } = addArgs;
const query = pipe(
CommentRequestRt.decode(comment),
fold(throwErrors(Boom.badRequest), identity)
);

const {
unsecuredSavedObjectsClient,
caseService,
userActionService,
attachmentService,
user,
logger,
lensEmbeddableFactory,
authorization,
alertsService,
} = clientArgs;
const { logger, authorization } = clientArgs;

decodeCommentRequest(comment);
try {
Expand All @@ -119,53 +63,15 @@ export const addComment = async (

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

const combinedCase = await createCommentableCase({
caseService,
attachmentService,
unsecuredSavedObjectsClient,
id: caseId,
logger,
lensEmbeddableFactory,
});

// eslint-disable-next-line @typescript-eslint/naming-convention
const { username, full_name, email } = user;
const userInfo: User = {
username,
full_name,
email,
};
const model = await CaseCommentModel.create(caseId, clientArgs);

const { comment: newComment, commentableCase: updatedCase } = await combinedCase.createComment({
const updatedModel = await model.createComment({
createdDate,
user: userInfo,
commentReq: query,
id: savedObjectID,
});

if (newComment.attributes.type === CommentType.alert && updatedCase.settings.syncAlerts) {
const alertsToUpdate = createAlertUpdateRequest({
comment: query,
status: updatedCase.status,
});

await alertsService.updateAlertsStatus(alertsToUpdate);
}

await userActionService.createUserAction({
type: ActionTypes.comment,
action: Actions.create,
unsecuredSavedObjectsClient,
caseId,
attachmentId: newComment.id,
payload: {
attachment: query,
},
user,
owner: newComment.attributes.owner,
});

return updatedCase.encode();
return await updatedModel.encodeWithComments();
} catch (error) {
throw createCaseError({
message: `Failed while adding a comment to case id: ${caseId} error: ${error}`,
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/cases/server/client/attachments/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const createAttachmentsSubClient = (
casesClientInternal: CasesClientInternal
): AttachmentsSubClient => {
const attachmentSubClient: AttachmentsSubClient = {
add: (params: AddArgs) => addComment(params, clientArgs, casesClientInternal),
add: (params: AddArgs) => addComment(params, clientArgs),
deleteAll: (deleteAllArgs: DeleteAllArgs) => deleteAll(deleteAllArgs, clientArgs),
delete: (deleteArgs: DeleteArgs) => deleteComment(deleteArgs, clientArgs),
find: (findArgs: FindArgs) => find(findArgs, clientArgs),
Expand Down
85 changes: 12 additions & 73 deletions x-pack/plugins/cases/server/client/attachments/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@

import Boom from '@hapi/boom';

import { SavedObjectsClientContract, Logger } from 'kibana/server';
import { LensServerPluginSetup } from '../../../../lens/server';
import { CommentableCase } from '../../common/models';
import { CaseCommentModel } from '../../common/models';
import { createCaseError } from '../../common/error';
import { Actions, ActionTypes, CaseResponse, CommentPatchRequest } from '../../../common/api';
import { CaseResponse, CommentPatchRequest } from '../../../common/api';
import { CASE_SAVED_OBJECT } from '../../../common/constants';
import { AttachmentService, CasesService } from '../../services';
import { CasesClientArgs } from '..';
import { decodeCommentRequest } from '../utils';
import { Operations } from '../../authorization';
Expand All @@ -32,37 +29,6 @@ export interface UpdateArgs {
updateRequest: CommentPatchRequest;
}

interface CombinedCaseParams {
attachmentService: AttachmentService;
caseService: CasesService;
unsecuredSavedObjectsClient: SavedObjectsClientContract;
caseID: string;
logger: Logger;
lensEmbeddableFactory: LensServerPluginSetup['lensEmbeddableFactory'];
}

async function createCommentableCase({
attachmentService,
caseService,
unsecuredSavedObjectsClient,
caseID,
logger,
lensEmbeddableFactory,
}: CombinedCaseParams) {
const caseInfo = await caseService.getCase({
id: caseID,
});

return new CommentableCase({
attachmentService,
caseService,
caseInfo,
unsecuredSavedObjectsClient,
logger,
lensEmbeddableFactory,
});
}

/**
* Update an attachment.
*
Expand All @@ -72,16 +38,7 @@ export async function update(
{ caseID, updateRequest: queryParams }: UpdateArgs,
clientArgs: CasesClientArgs
): Promise<CaseResponse> {
const {
attachmentService,
caseService,
unsecuredSavedObjectsClient,
logger,
lensEmbeddableFactory,
user,
userActionService,
authorization,
} = clientArgs;
const { attachmentService, unsecuredSavedObjectsClient, logger, authorization } = clientArgs;

try {
const {
Expand All @@ -92,15 +49,6 @@ export async function update(

decodeCommentRequest(queryRestAttributes);

const commentableCase = await createCommentableCase({
attachmentService,
caseService,
unsecuredSavedObjectsClient,
caseID,
logger,
lensEmbeddableFactory,
});

const myComment = await attachmentService.get({
unsecuredSavedObjectsClient,
attachmentId: queryCommentId,
Expand All @@ -115,6 +63,8 @@ export async function update(
operation: Operations.updateComment,
});

const model = await CaseCommentModel.create(caseID, clientArgs);

if (myComment.attributes.type !== queryRestAttributes.type) {
throw Boom.badRequest(`You cannot change the type of the comment.`);
}
Expand All @@ -124,9 +74,9 @@ export async function update(
}

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

Expand All @@ -137,25 +87,14 @@ export async function update(
}

const updatedDate = new Date().toISOString();
const { comment: updatedComment, commentableCase: updatedCase } =
await commentableCase.updateComment({
updateRequest: queryParams,
updatedAt: updatedDate,
user,
});

await userActionService.createUserAction({
type: ActionTypes.comment,
action: Actions.update,
unsecuredSavedObjectsClient,
caseId: caseID,
attachmentId: updatedComment.id,
payload: { attachment: queryRestAttributes },
user,

const updatedModel = await model.updateComment({
updateRequest: queryParams,
updatedAt: updatedDate,
owner: myComment.attributes.owner,
});

return await updatedCase.encode();
return await updatedModel.encodeWithComments();
} catch (error) {
throw createCaseError({
message: `Failed to patch comment case id: ${caseID}: ${error}`,
Expand Down
Loading

0 comments on commit 3073231

Please sign in to comment.