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 ]Expose the bulk get cases API from the cases UI client #154235

Merged
merged 3 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion x-pack/plugins/cases/public/api/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { httpServiceMock } from '@kbn/core/public/mocks';
import { getCases, getCasesMetrics } from '.';
import { bulkGetCases, getCases, getCasesMetrics } from '.';
import { allCases, allCasesSnake } from '../containers/mock';

describe('api', () => {
Expand Down Expand Up @@ -47,4 +47,23 @@ describe('api', () => {
});
});
});

describe('bulkGetCases', () => {
const http = httpServiceMock.createStartContract({ basePath: '' });
http.post.mockResolvedValue({ cases: allCasesSnake, errors: [] });

it('should return the correct response', async () => {
expect(await bulkGetCases({ http, params: { ids: ['test'], fields: ['title'] } })).toEqual({
cases: allCasesSnake,
errors: [],
});
});

it('should have been called with the correct path', async () => {
await bulkGetCases({ http, params: { ids: ['test'], fields: ['title'] } });
expect(http.post).toHaveBeenCalledWith('/internal/cases/_bulk_get', {
body: '{"ids":["test"],"fields":["title"]}',
});
});
});
});
28 changes: 27 additions & 1 deletion x-pack/plugins/cases/public/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@

import type { HttpStart } from '@kbn/core/public';
import type { Cases, CasesStatus, CasesMetrics } from '../../common/ui';
import { CASE_FIND_URL, CASE_METRICS_URL, CASE_STATUS_URL } from '../../common/constants';
import {
CASE_FIND_URL,
CASE_METRICS_URL,
CASE_STATUS_URL,
INTERNAL_BULK_GET_CASES_URL,
} from '../../common/constants';
import type {
CaseResponse,
CasesBulkGetRequestCertainFields,
CasesBulkGetResponseCertainFields,
CasesFindRequest,
CasesFindResponse,
CasesMetricsRequest,
Expand Down Expand Up @@ -58,3 +66,21 @@ export const getCasesMetrics = async ({
const res = await http.get<CasesMetricsResponse>(CASE_METRICS_URL, { signal, query });
return convertToCamelCase(decodeCasesMetricsResponse(res));
};

export const bulkGetCases = async <Field extends keyof CaseResponse = keyof CaseResponse>({
http,
signal,
params,
}: HTTPService & { params: CasesBulkGetRequestCertainFields<Field> }): Promise<
CasesBulkGetResponseCertainFields<Field>
> => {
const res = await http.post<CasesBulkGetResponseCertainFields<Field>>(
INTERNAL_BULK_GET_CASES_URL,
{
body: JSON.stringify({ ...params }),
signal,
}
);

return res;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to convert to camel case and decode it to ensure it's in the correct format?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it would be a good time to break this pattern. Do you think I should camelcase the response? I forgot about the decoding. Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love the idea of not doing the camel case conversion anymore haha. Yeah let's not do it anymore. I think I saw you already had an issue to remove the others right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Yes this one here #152706.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed c791b6c

};
20 changes: 20 additions & 0 deletions x-pack/plugins/cases/public/client/api/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,25 @@ describe('createClientAPI', () => {
});
});
});

describe('bulkGet', () => {
const http = httpServiceMock.createStartContract({ basePath: '' });
const api = createClientAPI({ http });
http.post.mockResolvedValue({ cases: [], errors: [] });

it('should return the correct response', async () => {
expect(await api.cases.bulkGet({ ids: ['test'], fields: ['title'] })).toEqual({
cases: [],
errors: [],
});
});

it('should have been called with the correct path', async () => {
await api.cases.bulkGet({ ids: ['test'], fields: ['title'] });
expect(http.post).toHaveBeenCalledWith('/internal/cases/_bulk_get', {
body: '{"ids":["test"],"fields":["title"]}',
});
});
});
});
});
3 changes: 2 additions & 1 deletion x-pack/plugins/cases/public/client/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
} from '../../../common/api';
import { getCasesFromAlertsUrl } from '../../../common/api';
import type { Cases, CasesStatus, CasesMetrics } from '../../../common/ui';
import { getCases, getCasesMetrics, getCasesStatus } from '../../api';
import { bulkGetCases, getCases, getCasesMetrics, getCasesStatus } from '../../api';
import type { CasesUiStart } from '../../types';

export const createClientAPI = ({ http }: { http: HttpStart }): CasesUiStart['api'] => {
Expand All @@ -32,6 +32,7 @@ export const createClientAPI = ({ http }: { http: HttpStart }): CasesUiStart['ap
getCasesStatus({ http, query, signal }),
getCasesMetrics: (query: CasesMetricsRequest, signal?: AbortSignal): Promise<CasesMetrics> =>
getCasesMetrics({ http, signal, query }),
bulkGet: (params, signal?: AbortSignal) => bulkGetCases({ http, signal, params }),
},
};
};
7 changes: 6 additions & 1 deletion x-pack/plugins/cases/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import type { CasesUiStart } from './types';

const apiMock: jest.Mocked<CasesUiStart['api']> = {
getRelatedCases: jest.fn(),
cases: { find: jest.fn(), getCasesMetrics: jest.fn(), getCasesStatus: jest.fn() },
cases: {
find: jest.fn(),
getCasesMetrics: jest.fn(),
getCasesStatus: jest.fn(),
bulkGet: jest.fn(),
},
};

const uiMock: jest.Mocked<CasesUiStart['ui']> = {
Expand Down
7 changes: 7 additions & 0 deletions x-pack/plugins/cases/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import type { LicensingPluginStart } from '@kbn/licensing-plugin/public';
import type { FilesSetup, FilesStart } from '@kbn/files-plugin/public';
import type { SavedObjectsManagementPluginStart } from '@kbn/saved-objects-management-plugin/public';
import type {
CaseResponse,
CasesBulkGetRequestCertainFields,
CasesBulkGetResponseCertainFields,
CasesByAlertId,
CasesByAlertIDRequest,
CasesFindRequest,
Expand Down Expand Up @@ -102,6 +105,10 @@ export interface CasesUiStart {
find: (query: CasesFindRequest, signal?: AbortSignal) => Promise<Cases>;
getCasesStatus: (query: CasesStatusRequest, signal?: AbortSignal) => Promise<CasesStatus>;
getCasesMetrics: (query: CasesMetricsRequest, signal?: AbortSignal) => Promise<CasesMetrics>;
bulkGet: <Field extends keyof CaseResponse = keyof CaseResponse>(
params: CasesBulkGetRequestCertainFields<Field>,
signal?: AbortSignal
) => Promise<CasesBulkGetResponseCertainFields<Field>>;
};
};
ui: {
Expand Down