Skip to content
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

[Cases] Cases client enchantment #95923

Merged
merged 13 commits into from
Apr 6, 2021
4 changes: 2 additions & 2 deletions x-pack/plugins/cases/server/client/alerts/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

import { CaseStatuses } from '../../../common/api';
import { AlertInfo } from '../../common';
import { CasesSubClientImplementation } from '../types';
import { CasesClientGetAlertsResponse } from './types';
import { get } from './get';
import { updateStatus } from './update_status';
import { CasesClientArgs } from '../types';

/**
* Defines the fields necessary to update an alert's status.
Expand All @@ -34,7 +34,7 @@ export interface AlertSubClient {
updateStatus(args: AlertUpdateStatus): Promise<void>;
}

export const createAlertsSubClient: CasesSubClientImplementation<AlertSubClient> = (args) => {
export const createAlertsSubClient = (args: CasesClientArgs): AlertSubClient => {
const { alertsService, scopedClusterClient, logger } = args;

const alertsSubClient: AlertSubClient = {
Expand Down
5 changes: 2 additions & 3 deletions x-pack/plugins/cases/server/client/attachments/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,13 +277,13 @@ async function getCombinedCase({
interface AddCommentArgs {
caseId: string;
comment: CommentRequest;
getCasesInternalClient: () => CasesClientInternal;
casesClientInternal: CasesClientInternal;
}

export const addComment = async ({
caseId,
comment,
getCasesInternalClient,
casesClientInternal,
...rest
}: AddCommentArgs & CasesClientArgs): Promise<CaseResponse> => {
const query = pipe(
Expand All @@ -299,7 +299,6 @@ export const addComment = async ({
user,
logger,
} = rest;
const casesClientInternal = getCasesInternalClient();

if (isCommentRequestTypeGenAlert(comment)) {
if (!ENABLE_CASE_CONNECTOR) {
Expand Down
14 changes: 7 additions & 7 deletions x-pack/plugins/cases/server/client/attachments/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
*/

import { CaseResponse, CommentRequest as AttachmentsRequest } from '../../../common/api';
import { CasesSubClientImplementation } from '../types';
import { CasesClientInternal } from '../client_internal';
import { CasesClientArgs } from '../types';
import { addComment } from './add';

export interface AttachmentsAdd {
Expand All @@ -18,16 +19,15 @@ export interface AttachmentsSubClient {
add(args: AttachmentsAdd): Promise<CaseResponse>;
}

export const createAttachmentsSubClient: CasesSubClientImplementation<AttachmentsSubClient> = (
args,
getClientsFactories
) => {
const { getCasesInternalClient } = getClientsFactories;
export const createAttachmentsSubClient = (
args: CasesClientArgs,
casesClientInternal: CasesClientInternal
): AttachmentsSubClient => {
const attachmentSubClient: AttachmentsSubClient = {
add: ({ caseId, comment }: AttachmentsAdd) =>
addComment({
...args,
getCasesInternalClient,
casesClientInternal,
caseId,
comment,
}),
Expand Down
21 changes: 11 additions & 10 deletions x-pack/plugins/cases/server/client/cases/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import {
CasesFindRequest,
CasesFindResponse,
} from '../../../common/api';
import { CasesSubClientImplementation } from '../types';
import { CasesClient } from '../client';
import { CasesClientInternal } from '../client_internal';
import { CasesClientArgs } from '../types';
import { create } from './create';
import { find } from './find';
import { get } from './get';
Expand All @@ -41,10 +43,11 @@ export interface CasesSubClient {
update(args: CasesPatchRequest): Promise<CasesResponse>;
}

export const createCasesSubClient: CasesSubClientImplementation<CasesSubClient> = (
args,
getClientsFactories
) => {
export const createCasesSubClient = (
args: CasesClientArgs,
casesClient: CasesClient,
casesClientInternal: CasesClientInternal
): CasesSubClient => {
const {
attachmentService,
caseConfigureService,
Expand All @@ -56,8 +59,6 @@ export const createCasesSubClient: CasesSubClientImplementation<CasesSubClient>
authorization,
} = args;

const { getCasesClient, getCasesInternalClient } = getClientsFactories;

const casesSubClient: CasesSubClient = {
create: (theCase: CasePostRequest) =>
create({
Expand Down Expand Up @@ -93,8 +94,8 @@ export const createCasesSubClient: CasesSubClientImplementation<CasesSubClient>
caseService,
userActionService,
user,
getCasesClient,
getCasesInternalClient,
casesClient,
casesClientInternal,
caseConfigureService,
logger,
}),
Expand All @@ -105,7 +106,7 @@ export const createCasesSubClient: CasesSubClientImplementation<CasesSubClient>
userActionService,
user,
cases,
getCasesInternalClient,
casesClientInternal,
logger,
}),
};
Expand Down
13 changes: 5 additions & 8 deletions x-pack/plugins/cases/server/client/cases/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ import {
} from '../../services';
import { createCaseError } from '../../common/error';
import { ENABLE_CASE_CONNECTOR } from '../../../common/constants';
import { CasesClient } from '../types';
import { CasesClientInternal } from '..';
import { CasesClient, CasesClientInternal } from '..';

/**
* Returns true if the case should be closed based on the configuration settings and whether the case
Expand All @@ -69,8 +68,8 @@ interface PushParams {
user: User;
caseId: string;
connectorId: string;
getCasesClient: () => CasesClient;
getCasesInternalClient: () => CasesClientInternal;
casesClient: CasesClient;
casesClientInternal: CasesClientInternal;
actionsClient: ActionsClient;
logger: Logger;
}
Expand All @@ -81,8 +80,8 @@ export const push = async ({
caseService,
caseConfigureService,
userActionService,
getCasesClient,
getCasesInternalClient,
casesClient,
casesClientInternal,
actionsClient,
connectorId,
caseId,
Expand All @@ -96,8 +95,6 @@ export const push = async ({
let alerts;
let connectorMappings;
let externalServiceIncident;
const casesClient = getCasesClient();
const casesClientInternal = getCasesInternalClient();

try {
[theCase, connector, userActions] = await Promise.all([
Expand Down
8 changes: 3 additions & 5 deletions x-pack/plugins/cases/server/client/cases/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ import {
SUB_CASE_SAVED_OBJECT,
} from '../../../common/constants';
import { createAlertUpdateRequest } from '../../common';
import { CasesClientInternal } from '../types';
import { createCaseError } from '../../common/error';
import { ENABLE_CASE_CONNECTOR } from '../../../common/constants';
import { UpdateAlertRequest } from '../alerts/client';
import { CasesClientInternal } from '../client_internal';

/**
* Throws an error if any of the requests attempt to update a collection style cases' status field.
Expand Down Expand Up @@ -334,7 +334,7 @@ interface UpdateArgs {
caseService: CaseService;
userActionService: CaseUserActionService;
user: User;
getCasesInternalClient: () => CasesClientInternal;
casesClientInternal: CasesClientInternal;
cases: CasesPatchRequest;
logger: Logger;
}
Expand All @@ -344,7 +344,7 @@ export const update = async ({
caseService,
userActionService,
user,
getCasesInternalClient,
casesClientInternal,
cases,
logger,
}: UpdateArgs): Promise<CasesResponse> => {
Expand All @@ -354,8 +354,6 @@ export const update = async ({
);

try {
const casesClientInternal = getCasesInternalClient();

const myCases = await caseService.getCases({
client: savedObjectsClient,
caseIds: query.cases.map((q) => q.id),
Expand Down
55 changes: 33 additions & 22 deletions x-pack/plugins/cases/server/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,40 @@
* 2.0.
*/

import { CasesClient, CasesClientArgs } from './types';
import { createCasesSubClient } from './cases/client';
import { createAttachmentsSubClient } from './attachments/client';
import { createUserActionsSubClient } from './user_actions/client';
import { createCasesClientInternal } from './client_internal';
import { CasesClientArgs } from './types';
import { CasesSubClient, createCasesSubClient } from './cases/client';
import { AttachmentsSubClient, createAttachmentsSubClient } from './attachments/client';
import { UserActionsSubClient, createUserActionsSubClient } from './user_actions/client';
import { CasesClientInternal, createCasesClientInternal } from './client_internal';

export const createCasesClient = (args: CasesClientArgs): CasesClient => {
const casesInternalClient = createCasesClientInternal(args, () => casesClient);
export class CasesClient {
private readonly args: CasesClientArgs;
cnasikas marked this conversation as resolved.
Show resolved Hide resolved
private readonly casesClientInternal: CasesClientInternal;
private readonly _cases: CasesSubClient;
private readonly _attachments: AttachmentsSubClient;
private readonly _userActions: UserActionsSubClient;

constructor(args: CasesClientArgs) {
this.args = args;
this.casesClientInternal = createCasesClientInternal(args);
this._cases = createCasesSubClient(this.args, this, this.casesClientInternal);
this._attachments = createAttachmentsSubClient(this.args, this.casesClientInternal);
this._userActions = createUserActionsSubClient(this.args);
}

public get cases() {
return this._cases;
}

const casesClient: CasesClient = {
cases: createCasesSubClient(args, {
getCasesClient: () => casesClient,
getCasesInternalClient: () => casesInternalClient,
}),
attachments: createAttachmentsSubClient(args, {
getCasesClient: () => casesClient,
getCasesInternalClient: () => casesInternalClient,
}),
userActions: createUserActionsSubClient(args, {
getCasesClient: () => casesClient,
getCasesInternalClient: () => casesInternalClient,
}),
};
public get attachments() {
return this._attachments;
}

return Object.freeze(casesClient);
public get userActions() {
return this._userActions;
}
}

export const createCasesClient = (args: CasesClientArgs): CasesClient => {
cnasikas marked this conversation as resolved.
Show resolved Hide resolved
return new CasesClient(args);
};
42 changes: 24 additions & 18 deletions x-pack/plugins/cases/server/client/client_internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,30 @@
* 2.0.
*/

import { CasesClientInternal, CasesClientArgs, GetClientsFactories } from './types';
import { createAlertsSubClient } from './alerts/client';
import { createConfigurationSubClient } from './configure/client';
import { CasesClientArgs } from './types';
import { AlertSubClient, createAlertsSubClient } from './alerts/client';
import { ConfigureSubClient, createConfigurationSubClient } from './configure/client';

export const createCasesClientInternal = (
args: CasesClientArgs,
getCasesClient: GetClientsFactories['getCasesClient']
): CasesClientInternal => {
const casesClientInternal: CasesClientInternal = {
alerts: createAlertsSubClient(args, {
getCasesClient,
getCasesInternalClient: () => casesClientInternal,
}),
configuration: createConfigurationSubClient(args, {
getCasesClient,
getCasesInternalClient: () => casesClientInternal,
}),
};
export class CasesClientInternal {
private readonly args: CasesClientArgs;
private readonly _alerts: AlertSubClient;
private readonly _configuration: ConfigureSubClient;

return Object.freeze(casesClientInternal);
constructor(args: CasesClientArgs) {
this.args = args;
this._alerts = createAlertsSubClient(this.args);
this._configuration = createConfigurationSubClient(this.args, this);
}

public get alerts() {
return this._alerts;
}

public get configuration() {
return this._configuration;
}
}

export const createCasesClientInternal = (args: CasesClientArgs): CasesClientInternal => {
return new CasesClientInternal(args);
};
14 changes: 7 additions & 7 deletions x-pack/plugins/cases/server/client/configure/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

import { ActionsClient } from '../../../../actions/server';
import { ConnectorMappingsAttributes, GetFieldsResponse } from '../../../common/api';
import { CasesSubClientImplementation } from '../types';
import { CasesClientInternal } from '../client_internal';
import { CasesClientArgs } from '../types';
import { getFields } from './get_fields';
import { getMappings } from './get_mappings';

Expand All @@ -28,12 +29,11 @@ export interface ConfigureSubClient {
getMappings(args: ConfigurationGetMappings): Promise<ConnectorMappingsAttributes[]>;
}

export const createConfigurationSubClient: CasesSubClientImplementation<ConfigureSubClient> = (
args,
getClientsFactories
) => {
export const createConfigurationSubClient = (
args: CasesClientArgs,
casesClientInternal: CasesClientInternal
): ConfigureSubClient => {
const { savedObjectsClient, connectorMappingsService, logger } = args;
const { getCasesInternalClient } = getClientsFactories;

const configureSubClient: ConfigureSubClient = {
getFields: (fields: ConfigurationGetFields) => getFields(fields),
Expand All @@ -42,7 +42,7 @@ export const createConfigurationSubClient: CasesSubClientImplementation<Configur
...params,
savedObjectsClient,
connectorMappingsService,
getCasesInternalClient,
casesClientInternal,
logger,
}),
};
Expand Down
5 changes: 2 additions & 3 deletions x-pack/plugins/cases/server/client/configure/get_mappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface GetMappingsArgs {
savedObjectsClient: SavedObjectsClientContract;
connectorMappingsService: ConnectorMappingsService;
actionsClient: ActionsClient;
getCasesInternalClient: () => CasesClientInternal;
casesClientInternal: CasesClientInternal;
connectorType: string;
connectorId: string;
logger: Logger;
Expand All @@ -28,13 +28,12 @@ export const getMappings = async ({
savedObjectsClient,
connectorMappingsService,
actionsClient,
getCasesInternalClient,
casesClientInternal,
connectorType,
connectorId,
logger,
}: GetMappingsArgs): Promise<ConnectorMappingsAttributes[]> => {
try {
const casesClientInternal = getCasesInternalClient();
if (connectorType === ConnectorTypes.none) {
return [];
}
Expand Down
3 changes: 1 addition & 2 deletions x-pack/plugins/cases/server/client/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ import {
AttachmentService,
} from '../services';
import { PluginStartContract as FeaturesPluginStart } from '../../../features/server';
import { CasesClient } from './types';
import { createCasesClient } from '.';
import { CasesClient, createCasesClient } from '.';

interface CasesClientFactoryArgs {
caseConfigureService: CaseConfigureService;
Expand Down
Loading