-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Cases] Add bulk attachments internal route (#129092)
Co-authored-by: Kibana Machine <[email protected]>
- Loading branch information
1 parent
b84383e
commit b5817af
Showing
19 changed files
with
1,091 additions
and
118 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
88 changes: 88 additions & 0 deletions
88
x-pack/plugins/cases/server/client/attachments/bulk_create.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,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 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
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
11 changes: 11 additions & 0 deletions
11
x-pack/plugins/cases/server/routes/api/get_internal_routes.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,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 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.