diff --git a/x-pack/plugins/cases/server/client/client.ts b/x-pack/plugins/cases/server/client/client.ts index f02a1a542246f..702329f7bcca2 100644 --- a/x-pack/plugins/cases/server/client/client.ts +++ b/x-pack/plugins/cases/server/client/client.ts @@ -43,7 +43,7 @@ export class CasesClient { public get subCases() { if (!ENABLE_CASE_CONNECTOR) { - throw Boom.badRequest('The case connector feature is disabled'); + throw new Error('The case connector feature is disabled'); } return this._subCases; } diff --git a/x-pack/plugins/cases/server/client/sub_cases/client.ts b/x-pack/plugins/cases/server/client/sub_cases/client.ts index 1ed328d0a8be6..aef780ecb3ac9 100644 --- a/x-pack/plugins/cases/server/client/sub_cases/client.ts +++ b/x-pack/plugins/cases/server/client/sub_cases/client.ts @@ -7,7 +7,6 @@ import Boom from '@hapi/boom'; -import { SavedObjectsClientContract } from 'kibana/server'; import { caseStatuses, SubCaseResponse, @@ -15,8 +14,8 @@ import { SubCasesFindRequest, SubCasesFindResponse, SubCasesFindResponseRt, + SubCasesPatchRequest, SubCasesResponse, - User, } from '../../../common/api'; import { CasesClientArgs } from '..'; import { flattenSubCaseSavedObject, transformSubCases } from '../../routes/api/utils'; @@ -27,16 +26,9 @@ import { buildCaseUserActionItem } from '../../services/user_actions/helpers'; import { constructQueryOptions } from '../../routes/api/cases/helpers'; import { defaultPage, defaultPerPage } from '../../routes/api'; import { CasesClient } from '../client'; -import { update, UpdateArgs } from './update'; - -interface DeleteArgs { - soClient: SavedObjectsClientContract; - ids: string[]; - user: User; -} +import { update } from './update'; interface FindArgs { - soClient: SavedObjectsClientContract; caseID: string; queryParams: SubCasesFindRequest; } @@ -44,17 +36,16 @@ interface FindArgs { interface GetArgs { includeComments: boolean; id: string; - soClient: SavedObjectsClientContract; } /** * The API routes for interacting with sub cases. */ export interface SubCasesClient { - delete(deleteArgs: DeleteArgs): Promise; + delete(ids: string[]): Promise; find(findArgs: FindArgs): Promise; get(getArgs: GetArgs): Promise; - update(updateArgs: UpdateArgs): Promise; + update(subCases: SubCasesPatchRequest): Promise; } /** @@ -65,21 +56,26 @@ export function createSubCasesClient( casesClient: CasesClient ): SubCasesClient { return Object.freeze({ - delete: (deleteArgs: DeleteArgs) => deleteSubCase(deleteArgs, clientArgs), + delete: (ids: string[]) => deleteSubCase(ids, clientArgs), find: (findArgs: FindArgs) => find(findArgs, clientArgs), get: (getArgs: GetArgs) => get(getArgs, clientArgs), - update: (updateArgs: UpdateArgs) => update(updateArgs, clientArgs, casesClient), + update: (subCases: SubCasesPatchRequest) => update(subCases, clientArgs, casesClient), }); } -async function deleteSubCase( - { soClient, ids, user }: DeleteArgs, - clientArgs: CasesClientArgs -): Promise { +async function deleteSubCase(ids: string[], clientArgs: CasesClientArgs): Promise { try { + const { + savedObjectsClient: soClient, + user, + userActionService, + caseService, + attachmentService, + } = clientArgs; + const [comments, subCases] = await Promise.all([ - clientArgs.caseService.getAllSubCaseComments({ soClient, id: ids }), - clientArgs.caseService.getSubCases({ soClient, ids }), + caseService.getAllSubCaseComments({ soClient, id: ids }), + caseService.getSubCases({ soClient, ids }), ]); const subCaseErrors = subCases.saved_objects.filter((subCase) => subCase.error !== undefined); @@ -100,15 +96,15 @@ async function deleteSubCase( await Promise.all( comments.saved_objects.map((comment) => - clientArgs.attachmentService.delete({ soClient, attachmentId: comment.id }) + attachmentService.delete({ soClient, attachmentId: comment.id }) ) ); - await Promise.all(ids.map((id) => clientArgs.caseService.deleteSubCase(soClient, id))); + await Promise.all(ids.map((id) => caseService.deleteSubCase(soClient, id))); const deleteDate = new Date().toISOString(); - await clientArgs.userActionService.bulkCreate({ + await userActionService.bulkCreate({ soClient, actions: ids.map((id) => buildCaseUserActionItem({ @@ -133,17 +129,19 @@ async function deleteSubCase( } async function find( - { soClient, caseID, queryParams }: FindArgs, + { caseID, queryParams }: FindArgs, clientArgs: CasesClientArgs ): Promise { try { + const { savedObjectsClient: soClient, caseService } = clientArgs; + const ids = [caseID]; const { subCase: subCaseQueryOptions } = constructQueryOptions({ status: queryParams.status, sortByField: queryParams.sortField, }); - const subCases = await clientArgs.caseService.findSubCasesGroupByCase({ + const subCases = await caseService.findSubCasesGroupByCase({ soClient, ids, options: { @@ -161,7 +159,7 @@ async function find( status, sortByField: queryParams.sortField, }); - return clientArgs.caseService.findSubCaseStatusStats({ + return caseService.findSubCaseStatusStats({ soClient, options: statusQueryOptions ?? {}, ids, @@ -190,11 +188,13 @@ async function find( } async function get( - { includeComments, id, soClient }: GetArgs, + { includeComments, id }: GetArgs, clientArgs: CasesClientArgs ): Promise { try { - const subCase = await clientArgs.caseService.getSubCase({ + const { savedObjectsClient: soClient, caseService } = clientArgs; + + const subCase = await caseService.getSubCase({ soClient, id, }); @@ -207,7 +207,7 @@ async function get( ); } - const theComments = await clientArgs.caseService.getAllSubCaseComments({ + const theComments = await caseService.getAllSubCaseComments({ soClient, id, options: { diff --git a/x-pack/plugins/cases/server/client/sub_cases/update.ts b/x-pack/plugins/cases/server/client/sub_cases/update.ts index a328e6dbb0d15..27e6e1261c0af 100644 --- a/x-pack/plugins/cases/server/client/sub_cases/update.ts +++ b/x-pack/plugins/cases/server/client/sub_cases/update.ts @@ -47,15 +47,6 @@ import { createCaseError } from '../../common/error'; import { UpdateAlertRequest } from '../../client/alerts/client'; import { CasesClientArgs } from '../types'; -/** - * The parameters that come from the API route itself. - */ -export interface UpdateArgs { - soClient: SavedObjectsClientContract; - user: User; - subCases: SubCasesPatchRequest; -} - function checkNonExistingOrConflict( toUpdate: SubCasePatchRequest[], fromStorage: Map> @@ -266,7 +257,7 @@ async function updateAlerts({ * Handles updating the fields in a sub case. */ export async function update( - { soClient, user, subCases }: UpdateArgs, + subCases: SubCasesPatchRequest, clientArgs: CasesClientArgs, casesClient: CasesClient ): Promise { @@ -276,7 +267,9 @@ export async function update( ); try { - const bulkSubCases = await clientArgs.caseService.getSubCases({ + const { savedObjectsClient: soClient, user, caseService, userActionService } = clientArgs; + + const bulkSubCases = await caseService.getSubCases({ soClient, ids: query.subCases.map((q) => q.id), }); @@ -296,13 +289,13 @@ export async function update( const subIDToParentCase = await getParentCases({ soClient, - caseService: clientArgs.caseService, + caseService, subCaseIDs: nonEmptySubCaseRequests.map((subCase) => subCase.id), subCasesMap, }); const updatedAt = new Date().toISOString(); - const updatedCases = await clientArgs.caseService.patchSubCases({ + const updatedCases = await caseService.patchSubCases({ soClient, subCases: nonEmptySubCaseRequests.map((thisCase) => { const { id: subCaseId, version, ...updateSubCaseAttributes } = thisCase; @@ -354,7 +347,7 @@ export async function update( }); await updateAlerts({ - caseService: clientArgs.caseService, + caseService, soClient, casesClient, subCasesToSync: subCasesToSyncAlertsFor, @@ -382,7 +375,7 @@ export async function update( [] ); - await clientArgs.userActionService.bulkCreate({ + await userActionService.bulkCreate({ soClient, actions: buildSubCaseUserActions({ originalSubCases: bulkSubCases.saved_objects, diff --git a/x-pack/plugins/cases/server/routes/api/cases/sub_case/delete_sub_cases.ts b/x-pack/plugins/cases/server/routes/api/cases/sub_case/delete_sub_cases.ts index 3f05dc1aa989b..4f4870496f77f 100644 --- a/x-pack/plugins/cases/server/routes/api/cases/sub_case/delete_sub_cases.ts +++ b/x-pack/plugins/cases/server/routes/api/cases/sub_case/delete_sub_cases.ts @@ -8,7 +8,7 @@ import { schema } from '@kbn/config-schema'; import { RouteDeps } from '../../types'; import { wrapError } from '../../utils'; -import { SUB_CASES_PATCH_DEL_URL, SAVED_OBJECT_TYPES } from '../../../../../common/constants'; +import { SUB_CASES_PATCH_DEL_URL } from '../../../../../common/constants'; export function initDeleteSubCasesApi({ caseService, router, logger }: RouteDeps) { router.delete( @@ -22,14 +22,8 @@ export function initDeleteSubCasesApi({ caseService, router, logger }: RouteDeps }, async (context, request, response) => { try { - const soClient = context.core.savedObjects.getClient({ - includedHiddenTypes: SAVED_OBJECT_TYPES, - }); - - const user = caseService.getUser({ request }); - const client = await context.cases.getCasesClient(); - await client.subCases.delete({ soClient, ids: request.query.ids, user }); + await client.subCases.delete(request.query.ids); return response.noContent(); } catch (error) { diff --git a/x-pack/plugins/cases/server/routes/api/cases/sub_case/find_sub_cases.ts b/x-pack/plugins/cases/server/routes/api/cases/sub_case/find_sub_cases.ts index 5f1ba235d8953..80cfbbd6b584f 100644 --- a/x-pack/plugins/cases/server/routes/api/cases/sub_case/find_sub_cases.ts +++ b/x-pack/plugins/cases/server/routes/api/cases/sub_case/find_sub_cases.ts @@ -15,7 +15,7 @@ import { identity } from 'fp-ts/lib/function'; import { SubCasesFindRequestRt, throwErrors } from '../../../../../common/api'; import { RouteDeps } from '../../types'; import { escapeHatch, wrapError } from '../../utils'; -import { SUB_CASES_URL, SAVED_OBJECT_TYPES } from '../../../../../common/constants'; +import { SUB_CASES_URL } from '../../../../../common/constants'; export function initFindSubCasesApi({ caseService, router, logger }: RouteDeps) { router.get( @@ -30,9 +30,6 @@ export function initFindSubCasesApi({ caseService, router, logger }: RouteDeps) }, async (context, request, response) => { try { - const soClient = context.core.savedObjects.getClient({ - includedHiddenTypes: SAVED_OBJECT_TYPES, - }); const queryParams = pipe( SubCasesFindRequestRt.decode(request.query), fold(throwErrors(Boom.badRequest), identity) @@ -41,7 +38,6 @@ export function initFindSubCasesApi({ caseService, router, logger }: RouteDeps) const client = await context.cases.getCasesClient(); return response.ok({ body: await client.subCases.find({ - soClient, caseID: request.params.case_id, queryParams, }), diff --git a/x-pack/plugins/cases/server/routes/api/cases/sub_case/get_sub_case.ts b/x-pack/plugins/cases/server/routes/api/cases/sub_case/get_sub_case.ts index e5e11fbfd8ea2..44ec5d68e9653 100644 --- a/x-pack/plugins/cases/server/routes/api/cases/sub_case/get_sub_case.ts +++ b/x-pack/plugins/cases/server/routes/api/cases/sub_case/get_sub_case.ts @@ -9,7 +9,7 @@ import { schema } from '@kbn/config-schema'; import { RouteDeps } from '../../types'; import { wrapError } from '../../utils'; -import { SUB_CASE_DETAILS_URL, SAVED_OBJECT_TYPES } from '../../../../../common/constants'; +import { SUB_CASE_DETAILS_URL } from '../../../../../common/constants'; export function initGetSubCaseApi({ router, logger }: RouteDeps) { router.get( @@ -27,16 +27,11 @@ export function initGetSubCaseApi({ router, logger }: RouteDeps) { }, async (context, request, response) => { try { - const soClient = context.core.savedObjects.getClient({ - includedHiddenTypes: SAVED_OBJECT_TYPES, - }); - const client = await context.cases.getCasesClient(); return response.ok({ body: await client.subCases.get({ id: request.params.sub_case_id, - soClient, includeComments: request.query.includeComments, }), }); diff --git a/x-pack/plugins/cases/server/routes/api/cases/sub_case/patch_sub_cases.ts b/x-pack/plugins/cases/server/routes/api/cases/sub_case/patch_sub_cases.ts index f021e4bc6bd13..c1cd4b317da9b 100644 --- a/x-pack/plugins/cases/server/routes/api/cases/sub_case/patch_sub_cases.ts +++ b/x-pack/plugins/cases/server/routes/api/cases/sub_case/patch_sub_cases.ts @@ -6,7 +6,7 @@ */ import { SubCasesPatchRequest } from '../../../../../common/api'; -import { SUB_CASES_PATCH_DEL_URL, SAVED_OBJECT_TYPES } from '../../../../../common/constants'; +import { SUB_CASES_PATCH_DEL_URL } from '../../../../../common/constants'; import { RouteDeps } from '../../types'; import { escapeHatch, wrapError } from '../../utils'; @@ -20,16 +20,10 @@ export function initPatchSubCasesApi({ router, caseService, logger }: RouteDeps) }, async (context, request, response) => { try { - const soClient = context.core.savedObjects.getClient({ - includedHiddenTypes: SAVED_OBJECT_TYPES, - }); - - const user = caseService.getUser({ request }); - const casesClient = await context.cases.getCasesClient(); const subCases = request.body as SubCasesPatchRequest; return response.ok({ - body: await casesClient.subCases.update({ soClient, subCases, user }), + body: await casesClient.subCases.update(subCases), }); } catch (error) { logger.error(`Failed to patch sub cases in route: ${error}`);