-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[Cases] Move remaining HTTP functionality to client #96507
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
84d1ca7
6ac82ac
f3e47d0
683f50a
a266a08
2008a5f
25b6b5e
e3a95a1
12a705d
0c3c544
bc299ea
c39266b
ab14d32
0207f04
477ad97
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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({ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question about
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @oatkiller That's a good question. Yes, some of them are being shown to the users. I think
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I don't know of an official stance, but personally I'd vote to keep them in English. That makes for a consistent auditing, debugging, and triaging experience, and makes searching for solutions based on error messages much easier
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jonathan-buttner I don't think we have an official stance, but +1 for keeping them in English.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should still i18n the messages that are show to the user.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @legrego @kobelb @jonathan-buttner @cnasikas I opened a discuss issue about this: #97650 |
||
| message: `Failed to delete comment in route case id: ${caseID} comment id: ${attachmentID} sub case id: ${subCaseID}: ${error}`, | ||
| error, | ||
| logger, | ||
| }); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.