Skip to content

Commit

Permalink
PR feedback & fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Apr 26, 2023
1 parent 51d9760 commit 3e81ea1
Show file tree
Hide file tree
Showing 19 changed files with 206 additions and 205 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ export const EMPTY_MAPPING_WARNING_DESC = i18n.translate(
'xpack.cases.connectors.swimlane.emptyMappingWarningDesc',
{
defaultMessage:
'This connector cannot be selected because it is missing the required case field mappings. You can edit this connector to add required field mappings or select a connector of type Cases',
'This connector cannot be selected because it is missing the required case field mappings. You can edit this connector to add required field mappings or select a connector of type Cases.',
}
);
31 changes: 15 additions & 16 deletions x-pack/plugins/cases/public/containers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import type {
CasesFindResponse,
GetCaseConnectorsResponse,
CaseUserActionStatsResponse,
Case,
Cases,
} from '../../common/api';
import {
CommentType,
Expand Down Expand Up @@ -90,7 +92,7 @@ export const getCase = async (
includeComments: boolean = true,
signal: AbortSignal
): Promise<CaseUI> => {
const response = await KibanaServices.get().http.fetch<CaseUI>(getCaseDetailsUrl(caseId), {
const response = await KibanaServices.get().http.fetch<Case>(getCaseDetailsUrl(caseId), {
method: 'GET',
query: {
includeComments,
Expand Down Expand Up @@ -244,7 +246,7 @@ export const getCases = async ({
};

export const postCase = async (newCase: CasePostRequest, signal: AbortSignal): Promise<CaseUI> => {
const response = await KibanaServices.get().http.fetch<CaseUI>(CASES_URL, {
const response = await KibanaServices.get().http.fetch<Case>(CASES_URL, {
method: 'POST',
body: JSON.stringify(newCase),
signal,
Expand All @@ -261,7 +263,7 @@ export const patchCase = async (
version: string,
signal: AbortSignal
): Promise<CaseUI[]> => {
const response = await KibanaServices.get().http.fetch<CasesUI>(CASES_URL, {
const response = await KibanaServices.get().http.fetch<Cases>(CASES_URL, {
method: 'PATCH',
body: JSON.stringify({ cases: [{ ...updatedCase, id: caseId, version }] }),
signal,
Expand All @@ -277,7 +279,7 @@ export const updateCases = async (
return [];
}

const response = await KibanaServices.get().http.fetch<CasesUI>(CASES_URL, {
const response = await KibanaServices.get().http.fetch<Cases>(CASES_URL, {
method: 'PATCH',
body: JSON.stringify({ cases }),
signal,
Expand All @@ -291,14 +293,11 @@ export const postComment = async (
caseId: string,
signal: AbortSignal
): Promise<CaseUI> => {
const response = await KibanaServices.get().http.fetch<CaseUI>(
`${CASES_URL}/${caseId}/comments`,
{
method: 'POST',
body: JSON.stringify(newComment),
signal,
}
);
const response = await KibanaServices.get().http.fetch<Case>(`${CASES_URL}/${caseId}/comments`, {
method: 'POST',
body: JSON.stringify(newComment),
signal,
});
return convertCaseToCamelCase(decodeCaseResponse(response));
};

Expand All @@ -317,7 +316,7 @@ export const patchComment = async ({
signal: AbortSignal;
owner: string;
}): Promise<CaseUI> => {
const response = await KibanaServices.get().http.fetch<CaseUI>(getCaseCommentsUrl(caseId), {
const response = await KibanaServices.get().http.fetch<Case>(getCaseCommentsUrl(caseId), {
method: 'PATCH',
body: JSON.stringify({
comment: commentUpdate,
Expand All @@ -340,7 +339,7 @@ export const deleteComment = async ({
commentId: string;
signal: AbortSignal;
}): Promise<void> => {
await KibanaServices.get().http.fetch<CaseUI>(getCaseCommentDeleteUrl(caseId, commentId), {
await KibanaServices.get().http.fetch<Case>(getCaseCommentDeleteUrl(caseId, commentId), {
method: 'DELETE',
signal,
});
Expand All @@ -360,7 +359,7 @@ export const pushCase = async (
connectorId: string,
signal: AbortSignal
): Promise<CaseUI> => {
const response = await KibanaServices.get().http.fetch<CaseUI>(
const response = await KibanaServices.get().http.fetch<Case>(
getCasePushUrl(caseId, connectorId),
{
method: 'POST',
Expand Down Expand Up @@ -389,7 +388,7 @@ export const createAttachments = async (
caseId: string,
signal: AbortSignal
): Promise<CaseUI> => {
const response = await KibanaServices.get().http.fetch<CaseUI>(
const response = await KibanaServices.get().http.fetch<Case>(
INTERNAL_BULK_CREATE_ATTACHMENTS_URL.replace('{case_id}', caseId),
{
method: 'POST',
Expand Down
8 changes: 5 additions & 3 deletions x-pack/plugins/cases/public/containers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import type {
SingleCaseMetricsResponse,
User,
CaseUserActionStatsResponse,
Case,
Cases,
} from '../../common/api';
import {
CaseRt,
Expand All @@ -34,7 +36,7 @@ import {
SingleCaseMetricsResponseRt,
CaseUserActionStatsResponseRt,
} from '../../common/api';
import type { CaseUI, CasesUI, FilterOptions, UpdateByKey } from './types';
import type { CaseUI, FilterOptions, UpdateByKey } from './types';
import * as i18n from './translations';

export const getTypedPayload = <T>(a: unknown): T => a as T;
Expand All @@ -47,7 +49,7 @@ export const covertToSnakeCase = (obj: Record<string, unknown>) =>

export const createToasterPlainError = (message: string) => new ToasterError([message]);

export const decodeCaseResponse = (respCase?: CaseUI) =>
export const decodeCaseResponse = (respCase?: Case) =>
pipe(CaseRt.decode(respCase), fold(throwErrors(createToasterPlainError), identity));

export const decodeCaseResolveResponse = (respCase?: CaseResolveResponse) =>
Expand All @@ -62,7 +64,7 @@ export const decodeSingleCaseMetricsResponse = (respCase?: SingleCaseMetricsResp
fold(throwErrors(createToasterPlainError), identity)
);

export const decodeCasesResponse = (respCase?: CasesUI) =>
export const decodeCasesResponse = (respCase?: Cases) =>
pipe(CasesRt.decode(respCase), fold(throwErrors(createToasterPlainError), identity));

export const decodeCaseConfigurationsResponse = (respCase?: CasesConfigurationsResponse) => {
Expand Down
16 changes: 8 additions & 8 deletions x-pack/plugins/cases/server/client/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
constructSearch,
convertSortField,
} from './utils';
import { CaseSeveritySavedObject, CaseStatusSavedObject } from '../common/types/case';
import { CasePersistedSeverity, CasePersistedStatus } from '../common/types/case';

describe('utils', () => {
describe('convertSortField', () => {
Expand Down Expand Up @@ -401,9 +401,9 @@ describe('utils', () => {
});

it.each([
[CaseStatuses.open, CaseStatusSavedObject.OPEN],
[CaseStatuses['in-progress'], CaseStatusSavedObject.IN_PROGRESS],
[CaseStatuses.closed, CaseStatusSavedObject.CLOSED],
[CaseStatuses.open, CasePersistedStatus.OPEN],
[CaseStatuses['in-progress'], CasePersistedStatus.IN_PROGRESS],
[CaseStatuses.closed, CasePersistedStatus.CLOSED],
])('creates a filter for status "%s"', (status, expectedStatus) => {
expect(constructQueryOptions({ status }).filter).toMatchInlineSnapshot(`
Object {
Expand All @@ -426,10 +426,10 @@ describe('utils', () => {
});

it.each([
[CaseSeverity.LOW, CaseSeveritySavedObject.LOW],
[CaseSeverity.MEDIUM, CaseSeveritySavedObject.MEDIUM],
[CaseSeverity.HIGH, CaseSeveritySavedObject.HIGH],
[CaseSeverity.CRITICAL, CaseSeveritySavedObject.CRITICAL],
[CaseSeverity.LOW, CasePersistedSeverity.LOW],
[CaseSeverity.MEDIUM, CasePersistedSeverity.MEDIUM],
[CaseSeverity.HIGH, CasePersistedSeverity.HIGH],
[CaseSeverity.CRITICAL, CasePersistedSeverity.CRITICAL],
])('creates a filter for severity "%s"', (severity, expectedSeverity) => {
expect(constructQueryOptions({ severity }).filter).toMatchInlineSnapshot(`
Object {
Expand Down
38 changes: 19 additions & 19 deletions x-pack/plugins/cases/server/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { CaseSeverity, CaseStatuses } from '../../common/api';
import { CASE_COMMENT_SAVED_OBJECT, CASE_SAVED_OBJECT } from '../../common/constants';
import { CaseSeveritySavedObject, CaseStatusSavedObject } from './types/case';
import { CasePersistedSeverity, CasePersistedStatus } from './types/case';

/**
* The name of the saved object reference indicating the action connector ID. This is stored in the Saved Object reference
Expand Down Expand Up @@ -40,28 +40,28 @@ export const EXTERNAL_REFERENCE_REF_NAME = 'externalReferenceId';
*/
export const LICENSING_CASE_ASSIGNMENT_FEATURE = 'Cases user assignment';

export const SEVERITY_EXTERNAL_TO_ESMODEL: Record<CaseSeverity, CaseSeveritySavedObject> = {
[CaseSeverity.LOW]: CaseSeveritySavedObject.LOW,
[CaseSeverity.MEDIUM]: CaseSeveritySavedObject.MEDIUM,
[CaseSeverity.HIGH]: CaseSeveritySavedObject.HIGH,
[CaseSeverity.CRITICAL]: CaseSeveritySavedObject.CRITICAL,
export const SEVERITY_EXTERNAL_TO_ESMODEL: Record<CaseSeverity, CasePersistedSeverity> = {
[CaseSeverity.LOW]: CasePersistedSeverity.LOW,
[CaseSeverity.MEDIUM]: CasePersistedSeverity.MEDIUM,
[CaseSeverity.HIGH]: CasePersistedSeverity.HIGH,
[CaseSeverity.CRITICAL]: CasePersistedSeverity.CRITICAL,
};

export const SEVERITY_ESMODEL_TO_EXTERNAL: Record<CaseSeveritySavedObject, CaseSeverity> = {
[CaseSeveritySavedObject.LOW]: CaseSeverity.LOW,
[CaseSeveritySavedObject.MEDIUM]: CaseSeverity.MEDIUM,
[CaseSeveritySavedObject.HIGH]: CaseSeverity.HIGH,
[CaseSeveritySavedObject.CRITICAL]: CaseSeverity.CRITICAL,
export const SEVERITY_ESMODEL_TO_EXTERNAL: Record<CasePersistedSeverity, CaseSeverity> = {
[CasePersistedSeverity.LOW]: CaseSeverity.LOW,
[CasePersistedSeverity.MEDIUM]: CaseSeverity.MEDIUM,
[CasePersistedSeverity.HIGH]: CaseSeverity.HIGH,
[CasePersistedSeverity.CRITICAL]: CaseSeverity.CRITICAL,
};

export const STATUS_EXTERNAL_TO_ESMODEL: Record<CaseStatuses, CaseStatusSavedObject> = {
[CaseStatuses.open]: CaseStatusSavedObject.OPEN,
[CaseStatuses['in-progress']]: CaseStatusSavedObject.IN_PROGRESS,
[CaseStatuses.closed]: CaseStatusSavedObject.CLOSED,
export const STATUS_EXTERNAL_TO_ESMODEL: Record<CaseStatuses, CasePersistedStatus> = {
[CaseStatuses.open]: CasePersistedStatus.OPEN,
[CaseStatuses['in-progress']]: CasePersistedStatus.IN_PROGRESS,
[CaseStatuses.closed]: CasePersistedStatus.CLOSED,
};

export const STATUS_ESMODEL_TO_EXTERNAL: Record<CaseStatusSavedObject, CaseStatuses> = {
[CaseStatusSavedObject.OPEN]: CaseStatuses.open,
[CaseStatusSavedObject.IN_PROGRESS]: CaseStatuses['in-progress'],
[CaseStatusSavedObject.CLOSED]: CaseStatuses.closed,
export const STATUS_ESMODEL_TO_EXTERNAL: Record<CasePersistedStatus, CaseStatuses> = {
[CasePersistedStatus.OPEN]: CaseStatuses.open,
[CasePersistedStatus.IN_PROGRESS]: CaseStatuses['in-progress'],
[CasePersistedStatus.CLOSED]: CaseStatuses.closed,
};
20 changes: 10 additions & 10 deletions x-pack/plugins/cases/server/common/types/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ import type { SavedObject } from '@kbn/core-saved-objects-server';
import type { CaseAttributes } from '../../../common/api';
import type { User, UserProfile } from './user';

export enum CaseSeveritySavedObject {
export enum CasePersistedSeverity {
LOW = 0,
MEDIUM = 10,
HIGH = 20,
CRITICAL = 30,
}

export enum CaseStatusSavedObject {
export enum CasePersistedStatus {
OPEN = 0,
IN_PROGRESS = 10,
CLOSED = 20,
}

export interface CaseExternalServiceSavedObject {
export interface CasePersistedExternalService {
connector_name: string;
external_id: string;
external_title: string;
Expand All @@ -31,15 +31,15 @@ export interface CaseExternalServiceSavedObject {
pushed_by: User;
}

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

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

export interface CasePersistedAttributes {
Expand All @@ -48,14 +48,14 @@ export interface CasePersistedAttributes {
closed_by: User | null;
created_at: string;
created_by: User;
connector: ConnectorSavedObject;
connector: CasePersistedConnector;
description: string;
duration: number | null;
external_service: CaseExternalServiceSavedObject | null;
external_service: CasePersistedExternalService | null;
owner: string;
settings: { syncAlerts: boolean };
severity: CaseSeveritySavedObject;
status: CaseStatusSavedObject;
severity: CasePersistedSeverity;
status: CasePersistedStatus;
tags: string[];
title: string;
total_alerts: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { SavedObjectSanitizedDoc, SavedObjectUnsanitizedDoc } from '@kbn/co
import type { CaseAttributes, CaseFullExternalService } from '../../../common/api';
import { CaseSeverity, CaseStatuses, ConnectorTypes, NONE_CONNECTOR_ID } from '../../../common/api';
import { CASE_SAVED_OBJECT } from '../../../common/constants';
import { CaseSeveritySavedObject, CaseStatusSavedObject } from '../../common/types/case';
import { CasePersistedSeverity, CasePersistedStatus } from '../../common/types/case';
import { getNoneCaseConnector } from '../../common/utils';
import type { ESCaseConnectorWithId } from '../../services/test_utils';
import { createExternalService } from '../../services/test_utils';
Expand Down Expand Up @@ -585,10 +585,10 @@ describe('case migrations', () => {

describe('update severity', () => {
it.each([
[CaseSeverity.LOW, CaseSeveritySavedObject.LOW],
[CaseSeverity.MEDIUM, CaseSeveritySavedObject.MEDIUM],
[CaseSeverity.HIGH, CaseSeveritySavedObject.HIGH],
[CaseSeverity.CRITICAL, CaseSeveritySavedObject.CRITICAL],
[CaseSeverity.LOW, CasePersistedSeverity.LOW],
[CaseSeverity.MEDIUM, CasePersistedSeverity.MEDIUM],
[CaseSeverity.HIGH, CasePersistedSeverity.HIGH],
[CaseSeverity.CRITICAL, CasePersistedSeverity.CRITICAL],
])(
'migrates "%s" severity keyword value to matching short',
(oldSeverityValue, expectedSeverityValue) => {
Expand Down Expand Up @@ -624,7 +624,7 @@ describe('case migrations', () => {
...doc,
attributes: {
...doc.attributes,
severity: CaseSeveritySavedObject.LOW,
severity: CasePersistedSeverity.LOW,
},
references: [],
});
Expand All @@ -633,9 +633,9 @@ describe('case migrations', () => {

describe('update status', () => {
it.each([
[CaseStatuses.open, CaseStatusSavedObject.OPEN],
[CaseStatuses['in-progress'], CaseStatusSavedObject.IN_PROGRESS],
[CaseStatuses.closed, CaseStatusSavedObject.CLOSED],
[CaseStatuses.open, CasePersistedStatus.OPEN],
[CaseStatuses['in-progress'], CasePersistedStatus.IN_PROGRESS],
[CaseStatuses.closed, CasePersistedStatus.CLOSED],
])(
'migrates "%s" status keyword value to matching short',
(oldStatusValue, expectedStatusValue) => {
Expand Down Expand Up @@ -671,7 +671,7 @@ describe('case migrations', () => {
...doc,
attributes: {
...doc.attributes,
status: CaseStatusSavedObject.OPEN,
status: CasePersistedStatus.OPEN,
},
references: [],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import {
} from './user_actions/connector_id';
import { CASE_TYPE_INDIVIDUAL } from './constants';
import { pipeMigrations } from './utils';
import type { ConnectorFieldsSavedObject } from '../../common/types/case';
import { CaseSeveritySavedObject, CaseStatusSavedObject } from '../../common/types/case';
import type { CasePersistedConnectorFields } from '../../common/types/case';
import { CasePersistedSeverity, CasePersistedStatus } from '../../common/types/case';

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

Expand Down Expand Up @@ -138,10 +138,10 @@ export const addAssignees = (
export const convertSeverity = (
doc: SavedObjectUnsanitizedDoc<CaseAttributes>
): SavedObjectSanitizedDoc<
Omit<CaseAttributes, 'severity'> & { severity: CaseSeveritySavedObject }
Omit<CaseAttributes, 'severity'> & { severity: CasePersistedSeverity }
> => {
const severity =
SEVERITY_EXTERNAL_TO_ESMODEL[doc.attributes.severity] ?? CaseSeveritySavedObject.LOW;
SEVERITY_EXTERNAL_TO_ESMODEL[doc.attributes.severity] ?? CasePersistedSeverity.LOW;
return {
...doc,
attributes: { ...doc.attributes, severity },
Expand All @@ -151,8 +151,8 @@ export const convertSeverity = (

export const convertStatus = (
doc: SavedObjectUnsanitizedDoc<CaseAttributes>
): SavedObjectSanitizedDoc<Omit<CaseAttributes, 'status'> & { status: CaseStatusSavedObject }> => {
const status = STATUS_EXTERNAL_TO_ESMODEL[doc.attributes?.status] ?? CaseStatusSavedObject.OPEN;
): SavedObjectSanitizedDoc<Omit<CaseAttributes, 'status'> & { status: CasePersistedStatus }> => {
const status = STATUS_EXTERNAL_TO_ESMODEL[doc.attributes?.status] ?? CasePersistedStatus.OPEN;
return {
...doc,
attributes: { ...doc.attributes, status },
Expand Down
Loading

0 comments on commit 3e81ea1

Please sign in to comment.