-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[Cases] Add bulk attachments internal route #129092
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
39b9ea2
Add bulkCreate
cnasikas 73f6f72
Finish route
cnasikas b2d6689
Register route
cnasikas 2eac270
Fix URL
cnasikas f115f4c
Fix types
cnasikas 982e2a9
Fix bug
cnasikas c0ddc3a
Add integration tests
cnasikas bcea81a
Merge branch 'main' into bulk_add_attachments
kibanamachine e6d8ea0
Fix types
cnasikas f0b0f44
Merge branch 'bulk_add_attachments' of github.com:cnasikas/kibana int…
cnasikas d5e2399
Merge branch 'main' into bulk_add_attachments
cnasikas 46ca1f9
PR feedback
cnasikas ebae0cd
Merge branch 'main' into bulk_add_attachments
cnasikas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
88 changes: 88 additions & 0 deletions
88
x-pack/plugins/cases/server/client/attachments/bulk_create.ts
This file contains hidden or 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,88 @@ | ||
| /* | ||
| * 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 { pipe } from 'fp-ts/lib/pipeable'; | ||
| import { fold } from 'fp-ts/lib/Either'; | ||
| import { identity } from 'fp-ts/lib/function'; | ||
|
|
||
| import { SavedObjectsUtils } from '../../../../../../src/core/server'; | ||
|
|
||
| import { | ||
| BulkCreateCommentRequest, | ||
| BulkCreateCommentRequestRt, | ||
| CaseResponse, | ||
| CommentRequest, | ||
| throwErrors, | ||
| } from '../../../common/api'; | ||
|
|
||
| import { CaseCommentModel } from '../../common/models'; | ||
| import { createCaseError } from '../../common/error'; | ||
| import { CasesClientArgs } from '..'; | ||
|
|
||
| import { decodeCommentRequest } from '../utils'; | ||
| import { Operations, OwnerEntity } from '../../authorization'; | ||
|
|
||
| export interface BulkCreateArgs { | ||
| caseId: string; | ||
| attachments: BulkCreateCommentRequest; | ||
| } | ||
|
|
||
| /** | ||
| * Create an attachment to a case. | ||
| * | ||
| * @ignore | ||
| */ | ||
| export const bulkCreate = async ( | ||
| args: BulkCreateArgs, | ||
| clientArgs: CasesClientArgs | ||
| ): Promise<CaseResponse> => { | ||
| const { attachments, caseId } = args; | ||
|
|
||
| pipe( | ||
| BulkCreateCommentRequestRt.decode(attachments), | ||
| fold(throwErrors(Boom.badRequest), identity) | ||
| ); | ||
|
|
||
| attachments.forEach((attachment) => { | ||
| decodeCommentRequest(attachment); | ||
| }); | ||
|
|
||
| const { logger, authorization } = clientArgs; | ||
|
|
||
| try { | ||
| const [attachmentsWithIds, entities]: [Array<{ id: string } & CommentRequest>, OwnerEntity[]] = | ||
| attachments.reduce<[Array<{ id: string } & CommentRequest>, OwnerEntity[]]>( | ||
| ([a, e], attachment) => { | ||
| const savedObjectID = SavedObjectsUtils.generateId(); | ||
| return [ | ||
| [...a, { id: savedObjectID, ...attachment }], | ||
| [...e, { owner: attachment.owner, id: savedObjectID }], | ||
| ]; | ||
| }, | ||
| [[], []] | ||
| ); | ||
|
|
||
| await authorization.ensureAuthorized({ | ||
| operation: Operations.createComment, | ||
| entities, | ||
| }); | ||
|
|
||
| const model = await CaseCommentModel.create(caseId, clientArgs); | ||
| const updatedModel = await model.bulkCreate({ | ||
| attachments: attachmentsWithIds, | ||
| }); | ||
|
|
||
| return await updatedModel.encodeWithComments(); | ||
| } catch (error) { | ||
| throw createCaseError({ | ||
| message: `Failed while bulk creating attachment to case id: ${caseId} error: ${error}`, | ||
| error, | ||
| logger, | ||
| }); | ||
| } | ||
| }; |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
11 changes: 11 additions & 0 deletions
11
x-pack/plugins/cases/server/routes/api/get_internal_routes.ts
This file contains hidden or 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,11 @@ | ||
| /* | ||
| * 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 { bulkCreateAttachmentsRoute } from './internal/bulk_create_attachments'; | ||
| import { CaseRoute } from './types'; | ||
|
|
||
| export const getInternalRoutes = () => [bulkCreateAttachmentsRoute] as CaseRoute[]; |
40 changes: 40 additions & 0 deletions
40
x-pack/plugins/cases/server/routes/api/internal/bulk_create_attachments.ts
This file contains hidden or 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,40 @@ | ||
| /* | ||
| * 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 { schema } from '@kbn/config-schema'; | ||
| import { INTERNAL_BULK_CREATE_ATTACHMENTS_URL } from '../../../../common/constants'; | ||
| import { BulkCreateCommentRequest } from '../../../../common/api'; | ||
| import { createCaseError } from '../../../common/error'; | ||
| import { createCasesRoute } from '../create_cases_route'; | ||
| import { escapeHatch } from '../utils'; | ||
|
|
||
| export const bulkCreateAttachmentsRoute = createCasesRoute({ | ||
| method: 'post', | ||
| path: INTERNAL_BULK_CREATE_ATTACHMENTS_URL, | ||
| params: { | ||
| params: schema.object({ | ||
| case_id: schema.string(), | ||
| }), | ||
| body: schema.arrayOf(escapeHatch), | ||
| }, | ||
| handler: async ({ context, request, response }) => { | ||
| try { | ||
| const casesClient = await context.cases.getCasesClient(); | ||
| const caseId = request.params.case_id; | ||
| const attachments = request.body as BulkCreateCommentRequest; | ||
|
|
||
| return response.ok({ | ||
| body: await casesClient.attachments.bulkCreate({ caseId, attachments }), | ||
| }); | ||
| } catch (error) { | ||
| throw createCaseError({ | ||
| message: `Failed to bulk create attachments in route case id: ${request.params.case_id}: ${error}`, | ||
| error, | ||
| }); | ||
| } | ||
| }, | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cnasikas this is one of those cases I mentioned where things can fail but still succeed. e.g. the attachments are created but the user actions are not.
no need to take action, just a good example of it.