Skip to content

Commit

Permalink
Addressing PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-buttner committed Apr 13, 2021
1 parent 25b6b5e commit e3a95a1
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 121 deletions.
7 changes: 6 additions & 1 deletion x-pack/plugins/cases/server/client/cases/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import {
CasesResponse,
CasesFindRequest,
CasesFindResponse,
User,
} from '../../../common/api';
import { CasesClient } from '../client';
import { CasesClientInternal } from '../client_internal';
import { CasesClientArgs } from '../types';
import { create } from './create';
import { deleteCases } from './delete';
import { find } from './find';
import { get } from './get';
import { get, getReporters, getTags } from './get';
import { push } from './push';
import { update } from './update';

Expand All @@ -46,6 +47,8 @@ export interface CasesSubClient {
push(args: CasePush): Promise<CaseResponse>;
update(args: CasesPatchRequest): Promise<CasesResponse>;
delete(ids: string[]): Promise<void>;
getTags(): Promise<string[]>;
getReporters(): Promise<User[]>;
}

/**
Expand Down Expand Up @@ -121,6 +124,8 @@ export const createCasesSubClient = (
logger,
}),
delete: (ids: string[]) => deleteCases(ids, args),
getTags: () => getTags(args),
getReporters: () => getReporters(args),
};

return Object.freeze(casesSubClient);
Expand Down
38 changes: 37 additions & 1 deletion x-pack/plugins/cases/server/client/cases/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
import Boom from '@hapi/boom';

import { SavedObjectsClientContract, Logger, SavedObject } from 'kibana/server';
import { CaseResponseRt, CaseResponse, ESCaseAttributes } from '../../../common/api';
import { CaseResponseRt, CaseResponse, ESCaseAttributes, User, UsersRt } from '../../../common/api';
import { CaseService } from '../../services';
import { countAlertsForID, flattenCaseSavedObject } from '../../common';
import { createCaseError } from '../../common/error';
import { ENABLE_CASE_CONNECTOR } from '../../../common/constants';
import { CasesClientArgs } from '..';

interface GetParams {
savedObjectsClient: SavedObjectsClientContract;
Expand Down Expand Up @@ -92,3 +93,38 @@ export const get = async ({
throw createCaseError({ message: `Failed to get case id: ${id}: ${error}`, error, logger });
}
};

/**
* Retrieves the tags from all the cases.
*/
export async function getTags({
savedObjectsClient: soClient,
caseService,
logger,
}: CasesClientArgs): Promise<string[]> {
try {
return await caseService.getTags({
soClient,
});
} catch (error) {
throw createCaseError({ message: `Failed to get tags: ${error}`, error, logger });
}
}

/**
* Retrieves the reporters from all the cases.
*/
export async function getReporters({
savedObjectsClient: soClient,
caseService,
logger,
}: CasesClientArgs): Promise<User[]> {
try {
const reporters = await caseService.getReporters({
soClient,
});
return UsersRt.encode(reporters);
} catch (error) {
throw createCaseError({ message: `Failed to get reporters: ${error}`, error, logger });
}
}
1 change: 0 additions & 1 deletion x-pack/plugins/cases/server/client/cases/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
} from 'kibana/server';

import { nodeBuilder } from '../../../../../../src/plugins/data/common';
import {} from '../../routes/api/utils';

import {
throwErrors,
Expand Down
29 changes: 5 additions & 24 deletions x-pack/plugins/cases/server/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import { CasesClientInternal, createCasesClientInternal } from './client_interna
import { createSubCasesClient, SubCasesClient } from './sub_cases/client';
import { ENABLE_CASE_CONNECTOR } from '../../common/constants';
import { ConfigureSubClient, createConfigurationSubClient } from './configure/client';
import { createReportersSubClient, ReportersSubClient } from './reporters/client';
import { createStatusStatsSubClient, StatusStatsSubClient } from './status_stats/client';
import { createTagsSubClient, TagsSubClient } from './tags/client';

export class CasesClient {
private readonly _casesClientInternal: CasesClientInternal;
Expand All @@ -24,20 +22,16 @@ export class CasesClient {
private readonly _userActions: UserActionsSubClient;
private readonly _subCases: SubCasesClient;
private readonly _configure: ConfigureSubClient;
private readonly _reporters: ReportersSubClient;
private readonly _statusStats: StatusStatsSubClient;
private readonly _tags: TagsSubClient;
private readonly _stats: StatusStatsSubClient;

constructor(args: CasesClientArgs) {
this._casesClientInternal = createCasesClientInternal(args);
this._cases = createCasesSubClient(args, this, this._casesClientInternal);
this._attachments = createAttachmentsSubClient(args, this._casesClientInternal);
this._userActions = createUserActionsSubClient(args);
this._subCases = createSubCasesClient(args, this);
this._subCases = createSubCasesClient(args, this._casesClientInternal);
this._configure = createConfigurationSubClient(args, this._casesClientInternal);
this._reporters = createReportersSubClient(args);
this._statusStats = createStatusStatsSubClient(args);
this._tags = createTagsSubClient(args);
this._stats = createStatusStatsSubClient(args);
}

public get cases() {
Expand All @@ -63,21 +57,8 @@ export class CasesClient {
return this._configure;
}

public get reporters() {
return this._reporters;
}

public get statusStats() {
return this._statusStats;
}

public get tags() {
return this._tags;
}

// TODO: Remove it when all routes will be moved to the cases client.
public get casesClientInternal() {
return this._casesClientInternal;
public get stats() {
return this._stats;
}
}

Expand Down
41 changes: 0 additions & 41 deletions x-pack/plugins/cases/server/client/reporters/client.ts

This file was deleted.

8 changes: 4 additions & 4 deletions x-pack/plugins/cases/server/client/sub_cases/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ import {
SubCasesPatchRequest,
SubCasesResponse,
} from '../../../common/api';
import { CasesClientArgs } from '..';
import { CasesClientArgs, CasesClientInternal } from '..';
import { countAlertsForID, flattenSubCaseSavedObject, transformSubCases } from '../../common';
import { createCaseError } from '../../common/error';
import { CASE_SAVED_OBJECT } from '../../../common/constants';
import { buildCaseUserActionItem } from '../../services/user_actions/helpers';
import { constructQueryOptions } from '../utils';
import { defaultPage, defaultPerPage } from '../../routes/api';
import { CasesClient } from '../client';
import { update } from './update';

interface FindArgs {
Expand Down Expand Up @@ -52,13 +51,14 @@ export interface SubCasesClient {
*/
export function createSubCasesClient(
clientArgs: CasesClientArgs,
casesClient: CasesClient
casesClientInternal: CasesClientInternal
): SubCasesClient {
return Object.freeze({
delete: (ids: string[]) => deleteSubCase(ids, clientArgs),
find: (findArgs: FindArgs) => find(findArgs, clientArgs),
get: (getArgs: GetArgs) => get(getArgs, clientArgs),
update: (subCases: SubCasesPatchRequest) => update(subCases, clientArgs, casesClient),
update: (subCases: SubCasesPatchRequest) =>
update({ subCases, clientArgs, casesClientInternal }),
});
}

Expand Down
24 changes: 14 additions & 10 deletions x-pack/plugins/cases/server/client/sub_cases/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
} from 'kibana/server';

import { nodeBuilder } from '../../../../../../src/plugins/data/common';
import { CasesClient } from '../../client';
import { CaseService } from '../../services';
import {
CaseStatuses,
Expand Down Expand Up @@ -46,6 +45,7 @@ import {
import { createCaseError } from '../../common/error';
import { UpdateAlertRequest } from '../../client/alerts/client';
import { CasesClientArgs } from '../types';
import { CasesClientInternal } from '../client_internal';

function checkNonExistingOrConflict(
toUpdate: SubCasePatchRequest[],
Expand Down Expand Up @@ -207,13 +207,13 @@ async function getAlertComments({
async function updateAlerts({
caseService,
soClient,
casesClient,
casesClientInternal,
logger,
subCasesToSync,
}: {
caseService: CaseService;
soClient: SavedObjectsClientContract;
casesClient: CasesClient;
casesClientInternal: CasesClientInternal;
logger: Logger;
subCasesToSync: SubCasePatchRequest[];
}) {
Expand Down Expand Up @@ -241,7 +241,7 @@ async function updateAlerts({
[]
);

await casesClient.casesClientInternal.alerts.updateStatus({ alerts: alertsToUpdate });
await casesClientInternal.alerts.updateStatus({ alerts: alertsToUpdate });
} catch (error) {
throw createCaseError({
message: `Failed to update alert status while updating sub cases: ${JSON.stringify(
Expand All @@ -256,11 +256,15 @@ async function updateAlerts({
/**
* Handles updating the fields in a sub case.
*/
export async function update(
subCases: SubCasesPatchRequest,
clientArgs: CasesClientArgs,
casesClient: CasesClient
): Promise<SubCasesResponse> {
export async function update({
subCases,
clientArgs,
casesClientInternal,
}: {
subCases: SubCasesPatchRequest;
clientArgs: CasesClientArgs;
casesClientInternal: CasesClientInternal;
}): Promise<SubCasesResponse> {
const query = pipe(
excess(SubCasesPatchRequestRt).decode(subCases),
fold(throwErrors(Boom.badRequest), identity)
Expand Down Expand Up @@ -349,7 +353,7 @@ export async function update(
await updateAlerts({
caseService,
soClient,
casesClient,
casesClientInternal,
subCasesToSync: subCasesToSyncAlertsFor,
logger: clientArgs.logger,
});
Expand Down
39 changes: 0 additions & 39 deletions x-pack/plugins/cases/server/client/tags/client.ts

This file was deleted.

0 comments on commit e3a95a1

Please sign in to comment.