-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Cases] Move remaining HTTP functionality to client (#96507)
* 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
1 parent
34f2d86
commit 613e859
Showing
66 changed files
with
2,715 additions
and
2,328 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
x-pack/plugins/cases/server/client/attachments/delete.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} | ||
} |
Oops, something went wrong.