Skip to content

Commit

Permalink
[Cases] Split remaining cases types (#155444)
Browse files Browse the repository at this point in the history
This PR separates the http API io-ts types from the types that are used
in the cases service layer to interact with the saved object client.
This PR is splits up the remaining types within the service layer
(configure and connector mappings).

Issue: #153726
  • Loading branch information
jonathan-buttner authored Apr 27, 2023
1 parent 9f9eb79 commit 4ea7182
Show file tree
Hide file tree
Showing 16 changed files with 243 additions and 163 deletions.
26 changes: 4 additions & 22 deletions x-pack/plugins/cases/server/common/types/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import type { SavedObject } from '@kbn/core-saved-objects-server';
import type { CaseAttributes } from '../../../common/api';
import type { ConnectorPersisted } from './connectors';
import type { ExternalServicePersisted } from './external_service';
import type { User, UserProfile } from './user';

export enum CasePersistedSeverity {
Expand All @@ -22,36 +24,16 @@ export enum CasePersistedStatus {
CLOSED = 20,
}

export interface CasePersistedExternalService {
connector_name: string;
external_id: string;
external_title: string;
external_url: string;
pushed_at: string;
pushed_by: User;
}

export type CasePersistedConnectorFields = Array<{
key: string;
value: unknown;
}>;

export interface CasePersistedConnector {
name: string;
type: string;
fields: CasePersistedConnectorFields | null;
}

export interface CasePersistedAttributes {
assignees: UserProfile[];
closed_at: string | null;
closed_by: User | null;
created_at: string;
created_by: User;
connector: CasePersistedConnector;
connector: ConnectorPersisted;
description: string;
duration: number | null;
external_service: CasePersistedExternalService | null;
external_service: ExternalServicePersisted | null;
owner: string;
settings: { syncAlerts: boolean };
severity: CasePersistedSeverity;
Expand Down
19 changes: 19 additions & 0 deletions x-pack/plugins/cases/server/common/types/configure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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 type { ConnectorPersisted } from './connectors';
import type { User } from './user';

export interface ConfigurePersistedAttributes {
connector: ConnectorPersisted;
closure_type: string;
owner: string;
created_at: string;
created_by: User;
updated_at: string | null;
updated_by: User | null;
}
15 changes: 15 additions & 0 deletions x-pack/plugins/cases/server/common/types/connector_mappings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* 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.
*/

export interface ConnectorMappingsPersistedAttributes {
mappings: Array<{
action_type: string;
source: string;
target: string;
}>;
owner: string;
}
17 changes: 17 additions & 0 deletions x-pack/plugins/cases/server/common/types/connectors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* 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.
*/

export type ConnectorPersistedFields = Array<{
key: string;
value: unknown;
}>;

export interface ConnectorPersisted {
name: string;
type: string;
fields: ConnectorPersistedFields | null;
}
17 changes: 17 additions & 0 deletions x-pack/plugins/cases/server/common/types/external_service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* 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 type { User } from './user';

export interface ExternalServicePersisted {
connector_name: string;
external_id: string;
external_title: string;
external_url: string;
pushed_at: string;
pushed_by: User;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
} from './user_actions/connector_id';
import { CASE_TYPE_INDIVIDUAL } from './constants';
import { pipeMigrations } from './utils';
import type { CasePersistedConnectorFields } from '../../common/types/case';
import type { ConnectorPersistedFields } from '../../common/types/connectors';
import { CasePersistedSeverity, CasePersistedStatus } from '../../common/types/case';

interface UnsanitizedCaseConnector {
Expand All @@ -38,7 +38,7 @@ interface SanitizedCaseConnector {
id: string;
name: string | null;
type: string | null;
fields: null | CasePersistedConnectorFields;
fields: null | ConnectorPersistedFields;
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import { CASE_CONFIGURE_SAVED_OBJECT, SECURITY_SOLUTION_OWNER } from '../../../c
import { CONNECTOR_ID_REFERENCE_NAME } from '../../common/constants';
import { getNoneCaseConnector } from '../../common/utils';
import type { ESCaseConnectorWithId } from '../../services/test_utils';
import type { ESCasesConfigureAttributes } from '../../services/configure/types';
import type { UnsanitizedConfigureConnector } from './configuration';
import { createConnectorAttributeMigration, configureConnectorIdMigration } from './configuration';
import type { ConfigurePersistedAttributes } from '../../common/types/configure';

// eslint-disable-next-line @typescript-eslint/naming-convention
const create_7_14_0_configSchema = (connector?: ESCaseConnectorWithId) => ({
Expand Down Expand Up @@ -135,7 +135,7 @@ describe('configuration migrations', () => {

const migratedConnector = configureConnectorIdMigration(
configureSavedObject
) as SavedObjectSanitizedDoc<ESCasesConfigureAttributes>;
) as SavedObjectSanitizedDoc<ConfigurePersistedAttributes>;

expect(migratedConnector.references.length).toBe(0);
expect(migratedConnector.attributes.connector).not.toHaveProperty('id');
Expand All @@ -146,7 +146,7 @@ describe('configuration migrations', () => {

const migratedConnector = configureConnectorIdMigration(
configureSavedObject
) as SavedObjectSanitizedDoc<ESCasesConfigureAttributes>;
) as SavedObjectSanitizedDoc<ConfigurePersistedAttributes>;

expect(migratedConnector.references.length).toBe(0);
expect(migratedConnector.attributes.connector).toMatchInlineSnapshot(`
Expand All @@ -168,7 +168,7 @@ describe('configuration migrations', () => {

const migratedConnector = configureConnectorIdMigration(
configureSavedObject
) as SavedObjectSanitizedDoc<ESCasesConfigureAttributes>;
) as SavedObjectSanitizedDoc<ConfigurePersistedAttributes>;

expect(migratedConnector.references).toEqual([
{ id: '123', type: ACTION_SAVED_OBJECT_TYPE, name: CONNECTOR_ID_REFERENCE_NAME },
Expand All @@ -181,7 +181,7 @@ describe('configuration migrations', () => {

const migratedConnector = configureConnectorIdMigration(
configureSavedObject
) as SavedObjectSanitizedDoc<ESCasesConfigureAttributes>;
) as SavedObjectSanitizedDoc<ConfigurePersistedAttributes>;

expect(migratedConnector).toMatchInlineSnapshot(`
Object {
Expand Down
9 changes: 3 additions & 6 deletions x-pack/plugins/cases/server/services/cases/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,8 @@ import {
transformESConnectorToExternalModel,
} from '../transform';
import { ConnectorReferenceHandler } from '../connector_reference_handler';
import type {
CasePersistedExternalService,
CasePersistedAttributes,
CaseTransformedAttributes,
} from '../../common/types/case';
import type { CasePersistedAttributes, CaseTransformedAttributes } from '../../common/types/case';
import type { ExternalServicePersisted } from '../../common/types/external_service';

export function transformUpdateResponsesToExternalModels(
response: SavedObjectsBulkUpdateResponse<CasePersistedAttributes>
Expand Down Expand Up @@ -229,7 +226,7 @@ export function transformSavedObjectToExternalModel(
function transformESExternalService(
// this type needs to match that of CaseFullExternalService except that it does not include the connector_id, see: x-pack/plugins/cases/common/api/cases/case.ts
// that's why it can be null here
externalService: CasePersistedExternalService | null | undefined,
externalService: ExternalServicePersisted | null | undefined,
references: SavedObjectReference[] | undefined
): CaseFullExternalService | null {
const connectorIdRef = findConnectorIdReference(PUSH_CONNECTOR_ID_REFERENCE_NAME, references);
Expand Down
36 changes: 18 additions & 18 deletions x-pack/plugins/cases/server/services/configure/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import type {
import { ACTION_SAVED_OBJECT_TYPE } from '@kbn/actions-plugin/server';
import { loggerMock } from '@kbn/logging-mocks';
import { CaseConfigureService } from '.';
import type { ESCasesConfigureAttributes } from './types';
import { CONNECTOR_ID_REFERENCE_NAME } from '../../common/constants';
import { getNoneCaseConnector } from '../../common/utils';
import type { ESCaseConnectorWithId } from '../test_utils';
import { createESJiraConnector, createJiraConnector } from '../test_utils';
import type { ConfigurePersistedAttributes } from '../../common/types/configure';

const basicConfigFields = {
closure_type: 'close-by-pushing' as const,
Expand Down Expand Up @@ -60,7 +60,7 @@ const createConfigPostParams = (connector: CaseConnector): CasesConfigureAttribu

const createUpdateConfigSO = (
connector?: ESCaseConnectorWithId
): SavedObjectsUpdateResponse<ESCasesConfigureAttributes> => {
): SavedObjectsUpdateResponse<ConfigurePersistedAttributes> => {
const references: SavedObjectReference[] =
connector && connector.id !== 'none'
? [
Expand All @@ -87,7 +87,7 @@ const createUpdateConfigSO = (

const createConfigSO = (
connector?: ESCaseConnectorWithId
): SavedObject<ESCasesConfigureAttributes> => {
): SavedObject<ConfigurePersistedAttributes> => {
const references: SavedObjectReference[] = connector
? [
{
Expand Down Expand Up @@ -119,17 +119,17 @@ const createConfigSO = (

const createConfigSOPromise = (
connector?: ESCaseConnectorWithId
): Promise<SavedObject<ESCasesConfigureAttributes>> => Promise.resolve(createConfigSO(connector));
): Promise<SavedObject<ConfigurePersistedAttributes>> => Promise.resolve(createConfigSO(connector));

const createConfigFindSO = (
connector?: ESCaseConnectorWithId
): SavedObjectsFindResult<ESCasesConfigureAttributes> => ({
): SavedObjectsFindResult<ConfigurePersistedAttributes> => ({
...createConfigSO(connector),
score: 0,
});

const createSOFindResponse = (
savedObjects: Array<SavedObjectsFindResult<ESCasesConfigureAttributes>>
savedObjects: Array<SavedObjectsFindResult<ConfigurePersistedAttributes>>
) => ({
saved_objects: savedObjects,
total: savedObjects.length,
Expand Down Expand Up @@ -163,7 +163,7 @@ describe('CaseConfigureService', () => {
});

const { connector: ignoreConnector, ...restUpdateAttributes } = unsecuredSavedObjectsClient
.update.mock.calls[0][2] as Partial<ESCasesConfigureAttributes>;
.update.mock.calls[0][2] as Partial<ConfigurePersistedAttributes>;

expect(restUpdateAttributes).toMatchInlineSnapshot(`
Object {
Expand Down Expand Up @@ -198,7 +198,7 @@ describe('CaseConfigureService', () => {
});

const { connector } = unsecuredSavedObjectsClient.update.mock
.calls[0][2] as Partial<ESCasesConfigureAttributes>;
.calls[0][2] as Partial<ConfigurePersistedAttributes>;

expect(connector?.fields).toMatchInlineSnapshot(`
Array [
Expand Down Expand Up @@ -231,7 +231,7 @@ describe('CaseConfigureService', () => {
});

const { connector } = unsecuredSavedObjectsClient.update.mock
.calls[0][2] as Partial<ESCasesConfigureAttributes>;
.calls[0][2] as Partial<ConfigurePersistedAttributes>;

expect(connector).toMatchInlineSnapshot(`
Object {
Expand Down Expand Up @@ -269,7 +269,7 @@ describe('CaseConfigureService', () => {
});

const updateAttributes = unsecuredSavedObjectsClient.update.mock
.calls[0][2] as Partial<ESCasesConfigureAttributes>;
.calls[0][2] as Partial<ConfigurePersistedAttributes>;

expect(updateAttributes.connector).not.toHaveProperty('id');

Expand Down Expand Up @@ -400,7 +400,7 @@ describe('CaseConfigureService', () => {
describe('post', () => {
it('includes the creation attributes excluding the connector.id field', async () => {
unsecuredSavedObjectsClient.create.mockReturnValue(
Promise.resolve({} as SavedObject<ESCasesConfigureAttributes>)
Promise.resolve({} as SavedObject<ConfigurePersistedAttributes>)
);

await service.post({
Expand All @@ -410,7 +410,7 @@ describe('CaseConfigureService', () => {
});

const creationAttributes = unsecuredSavedObjectsClient.create.mock
.calls[0][1] as ESCasesConfigureAttributes;
.calls[0][1] as ConfigurePersistedAttributes;
expect(creationAttributes.connector).not.toHaveProperty('id');
expect(creationAttributes).toMatchInlineSnapshot(`
Object {
Expand Down Expand Up @@ -452,7 +452,7 @@ describe('CaseConfigureService', () => {

it('moves the connector.id to the references', async () => {
unsecuredSavedObjectsClient.create.mockReturnValue(
Promise.resolve({} as SavedObject<ESCasesConfigureAttributes>)
Promise.resolve({} as SavedObject<ConfigurePersistedAttributes>)
);

await service.post({
Expand All @@ -478,7 +478,7 @@ describe('CaseConfigureService', () => {

it('sets connector.fields to an empty array when it is not included', async () => {
unsecuredSavedObjectsClient.create.mockReturnValue(
Promise.resolve({} as SavedObject<ESCasesConfigureAttributes>)
Promise.resolve({} as SavedObject<ConfigurePersistedAttributes>)
);

await service.post({
Expand All @@ -500,7 +500,7 @@ describe('CaseConfigureService', () => {

it('does not create a reference for a none connector', async () => {
unsecuredSavedObjectsClient.create.mockReturnValue(
Promise.resolve({} as SavedObject<ESCasesConfigureAttributes>)
Promise.resolve({} as SavedObject<ConfigurePersistedAttributes>)
);

await service.post({
Expand Down Expand Up @@ -545,7 +545,7 @@ describe('CaseConfigureService', () => {

it('returns an undefined connector if it is not returned by the update', async () => {
unsecuredSavedObjectsClient.update.mockReturnValue(
Promise.resolve({} as SavedObjectsUpdateResponse<ESCasesConfigureAttributes>)
Promise.resolve({} as SavedObjectsUpdateResponse<ConfigurePersistedAttributes>)
);

const res = await service.patch({
Expand All @@ -564,7 +564,7 @@ describe('CaseConfigureService', () => {

it('returns the default none connector when it cannot find the reference', async () => {
const { name, type, fields } = createESJiraConnector();
const returnValue: SavedObjectsUpdateResponse<ESCasesConfigureAttributes> = {
const returnValue: SavedObjectsUpdateResponse<ConfigurePersistedAttributes> = {
type: CASE_CONFIGURE_SAVED_OBJECT,
id: '1',
attributes: {
Expand Down Expand Up @@ -707,7 +707,7 @@ describe('CaseConfigureService', () => {
type: ACTION_SAVED_OBJECT_TYPE,
},
],
} as unknown as SavedObject<ESCasesConfigureAttributes>)
} as unknown as SavedObject<ConfigurePersistedAttributes>)
);
const res = await service.get({ unsecuredSavedObjectsClient, configurationId: '1' });

Expand Down
Loading

0 comments on commit 4ea7182

Please sign in to comment.