Skip to content

Commit

Permalink
Fixing throw and removing user ans soclients
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-buttner committed Apr 8, 2021
1 parent 382315a commit a1677f0
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 73 deletions.
2 changes: 1 addition & 1 deletion x-pack/plugins/cases/server/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
60 changes: 30 additions & 30 deletions x-pack/plugins/cases/server/client/sub_cases/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@

import Boom from '@hapi/boom';

import { SavedObjectsClientContract } from 'kibana/server';
import {
caseStatuses,
SubCaseResponse,
SubCaseResponseRt,
SubCasesFindRequest,
SubCasesFindResponse,
SubCasesFindResponseRt,
SubCasesPatchRequest,
SubCasesResponse,
User,
} from '../../../common/api';
import { CasesClientArgs } from '..';
import { flattenSubCaseSavedObject, transformSubCases } from '../../routes/api/utils';
Expand All @@ -27,34 +26,26 @@ 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;
}

interface GetArgs {
includeComments: boolean;
id: string;
soClient: SavedObjectsClientContract;
}

/**
* The API routes for interacting with sub cases.
*/
export interface SubCasesClient {
delete(deleteArgs: DeleteArgs): Promise<void>;
delete(ids: string[]): Promise<void>;
find(findArgs: FindArgs): Promise<SubCasesFindResponse>;
get(getArgs: GetArgs): Promise<SubCaseResponse>;
update(updateArgs: UpdateArgs): Promise<SubCasesResponse>;
update(subCases: SubCasesPatchRequest): Promise<SubCasesResponse>;
}

/**
Expand All @@ -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<void> {
async function deleteSubCase(ids: string[], clientArgs: CasesClientArgs): Promise<void> {
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);
Expand All @@ -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({
Expand All @@ -133,17 +129,19 @@ async function deleteSubCase(
}

async function find(
{ soClient, caseID, queryParams }: FindArgs,
{ caseID, queryParams }: FindArgs,
clientArgs: CasesClientArgs
): Promise<SubCasesFindResponse> {
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: {
Expand All @@ -161,7 +159,7 @@ async function find(
status,
sortByField: queryParams.sortField,
});
return clientArgs.caseService.findSubCaseStatusStats({
return caseService.findSubCaseStatusStats({
soClient,
options: statusQueryOptions ?? {},
ids,
Expand Down Expand Up @@ -190,11 +188,13 @@ async function find(
}

async function get(
{ includeComments, id, soClient }: GetArgs,
{ includeComments, id }: GetArgs,
clientArgs: CasesClientArgs
): Promise<SubCaseResponse> {
try {
const subCase = await clientArgs.caseService.getSubCase({
const { savedObjectsClient: soClient, caseService } = clientArgs;

const subCase = await caseService.getSubCase({
soClient,
id,
});
Expand All @@ -207,7 +207,7 @@ async function get(
);
}

const theComments = await clientArgs.caseService.getAllSubCaseComments({
const theComments = await caseService.getAllSubCaseComments({
soClient,
id,
options: {
Expand Down
23 changes: 8 additions & 15 deletions x-pack/plugins/cases/server/client/sub_cases/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, SavedObject<SubCaseAttributes>>
Expand Down Expand Up @@ -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<SubCasesResponse> {
Expand All @@ -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),
});
Expand All @@ -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;
Expand Down Expand Up @@ -354,7 +347,7 @@ export async function update(
});

await updateAlerts({
caseService: clientArgs.caseService,
caseService,
soClient,
casesClient,
subCasesToSync: subCasesToSyncAlertsFor,
Expand Down Expand Up @@ -382,7 +375,7 @@ export async function update(
[]
);

await clientArgs.userActionService.bulkCreate({
await userActionService.bulkCreate({
soClient,
actions: buildSubCaseUserActions({
originalSubCases: bulkSubCases.saved_objects,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand All @@ -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,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
}),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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}`);
Expand Down

0 comments on commit a1677f0

Please sign in to comment.