From e2a3935e81d8744d9abb7398e00b8b615011b005 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Fri, 19 Jul 2024 08:52:30 +0000 Subject: [PATCH] [workspace] Fix not able to query legacy data when have at least 1 workspace (#7254) * Fix not able to query legacy data when have at least 1 workspace Signed-off-by: Hailong Cui * fix typo Signed-off-by: Hailong Cui * update snapshot Signed-off-by: Hailong Cui --------- Signed-off-by: Hailong Cui Co-authored-by: Yulong Ruan (cherry picked from commit 3bb41703c42478b778cbcf98a4231726767373ae) Signed-off-by: github-actions[bot] --- .../__snapshots__/feature_cards.test.tsx.snap | 18 +++++++++++++- .../public/lib/get_saved_object_counts.ts | 1 + .../saved_objects_table.test.tsx | 6 ++--- .../objects_table/saved_objects_table.tsx | 13 +++++----- .../public/utils.test.ts | 24 +++++++++++++++++++ .../saved_objects_management/public/utils.ts | 10 ++++---- .../server/routes/scroll_count.ts | 7 ++++-- 7 files changed, 61 insertions(+), 18 deletions(-) diff --git a/src/plugins/management/public/components/feature_cards/__snapshots__/feature_cards.test.tsx.snap b/src/plugins/management/public/components/feature_cards/__snapshots__/feature_cards.test.tsx.snap index d777d7d9e2c0..0718fb98bf4f 100644 --- a/src/plugins/management/public/components/feature_cards/__snapshots__/feature_cards.test.tsx.snap +++ b/src/plugins/management/public/components/feature_cards/__snapshots__/feature_cards.test.tsx.snap @@ -3,7 +3,23 @@ exports[` render with complex navLinks 1`] = `
+
+
+

+

+
+
+
diff --git a/src/plugins/saved_objects_management/public/lib/get_saved_object_counts.ts b/src/plugins/saved_objects_management/public/lib/get_saved_object_counts.ts index 374f2720b537..b46389fe54f3 100644 --- a/src/plugins/saved_objects_management/public/lib/get_saved_object_counts.ts +++ b/src/plugins/saved_objects_management/public/lib/get_saved_object_counts.ts @@ -35,6 +35,7 @@ export interface SavedObjectCountOptions { namespacesToInclude?: string[]; searchString?: string; workspaces?: string[]; + availableWorkspaces?: string[]; } export async function getSavedObjectCounts( diff --git a/src/plugins/saved_objects_management/public/management_section/objects_table/saved_objects_table.test.tsx b/src/plugins/saved_objects_management/public/management_section/objects_table/saved_objects_table.test.tsx index bee992e0196d..a33a59786617 100644 --- a/src/plugins/saved_objects_management/public/management_section/objects_table/saved_objects_table.test.tsx +++ b/src/plugins/saved_objects_management/public/management_section/objects_table/saved_objects_table.test.tsx @@ -812,7 +812,7 @@ describe('SavedObjectsTable', () => { }); }); - it('all visible workspaces in find options when not in any workspace', async () => { + it('no workspaces in find options when not in any workspace', async () => { findObjectsMock.mockClear(); const applications = applicationServiceMock.createStartContract(); applications.capabilities = { @@ -851,8 +851,8 @@ describe('SavedObjectsTable', () => { await waitFor(() => { expect(findObjectsMock).toBeCalledWith( http, - expect.objectContaining({ - workspaces: expect.arrayContaining(['workspace1', 'workspace2']), + expect.not.objectContaining({ + workspaces, }) ); }); diff --git a/src/plugins/saved_objects_management/public/management_section/objects_table/saved_objects_table.tsx b/src/plugins/saved_objects_management/public/management_section/objects_table/saved_objects_table.tsx index 162daab66e56..38f69a05b35c 100644 --- a/src/plugins/saved_objects_management/public/management_section/objects_table/saved_objects_table.tsx +++ b/src/plugins/saved_objects_management/public/management_section/objects_table/saved_objects_table.tsx @@ -187,15 +187,14 @@ export class SavedObjectsTable extends Component ws.id); + return undefined; } else { return [currentWorkspaceId]; } @@ -250,6 +249,7 @@ export class SavedObjectsTable extends Component ws.id), }); if (availableNamespaces.length) { @@ -286,6 +286,7 @@ export class SavedObjectsTable extends Component ws.id), }); if (availableNamespaces.length) { @@ -958,9 +959,7 @@ export class SavedObjectsTable extends Component { - return this.workspaceIdQuery?.includes(ws.id); - }) + .filter((ws) => (currentWorkspaceId ? currentWorkspaceId === ws.id : true)) .map((ws) => { return { name: ws.name, diff --git a/src/plugins/saved_objects_management/public/utils.test.ts b/src/plugins/saved_objects_management/public/utils.test.ts index 3b714a169219..eebfdec8f61d 100644 --- a/src/plugins/saved_objects_management/public/utils.test.ts +++ b/src/plugins/saved_objects_management/public/utils.test.ts @@ -17,4 +17,28 @@ describe('Utils', () => { const obj = formatWorkspaceIdParams({ foo: 'bar', workspaces: ['foo'] }); expect(obj).toEqual({ foo: 'bar', workspaces: ['foo'] }); }); + + it('formatWorkspaceIdParams with availableWorkspaces exists', async () => { + const obj = formatWorkspaceIdParams({ foo: 'bar', availableWorkspaces: ['foo'] }); + expect(obj).toEqual({ foo: 'bar', availableWorkspaces: ['foo'] }); + }); + + it('formatWorkspaceIdParams with availableWorkspaces is empty array', async () => { + const obj = formatWorkspaceIdParams({ foo: 'bar', availableWorkspaces: [] }); + expect(obj).toEqual({ foo: 'bar' }); + }); + + it('formatWorkspaceIdParams with availableWorkspaces is null/undefined', async () => { + const obj = formatWorkspaceIdParams({ foo: 'bar', availableWorkspaces: null }); + expect(obj).toEqual({ foo: 'bar' }); + }); + + it('formatWorkspaceIdParams with both workspaces and availableWorkspaces are not empty', async () => { + const obj = formatWorkspaceIdParams({ + foo: 'bar', + availableWorkspaces: ['foo', 'bar'], + workspaces: ['foo'], + }); + expect(obj).toEqual({ foo: 'bar', availableWorkspaces: ['foo', 'bar'], workspaces: ['foo'] }); + }); }); diff --git a/src/plugins/saved_objects_management/public/utils.ts b/src/plugins/saved_objects_management/public/utils.ts index 84727ab6a356..937e7767702d 100644 --- a/src/plugins/saved_objects_management/public/utils.ts +++ b/src/plugins/saved_objects_management/public/utils.ts @@ -3,11 +3,11 @@ * SPDX-License-Identifier: Apache-2.0 */ -export function formatWorkspaceIdParams( - obj: T -): T | Omit { - const { workspaces, ...others } = obj; - if (workspaces) { +export function formatWorkspaceIdParams< + T extends { workspaces?: string[] | null; availableWorkspaces?: string[] | null } +>(obj: T): T | Omit { + const { workspaces, availableWorkspaces, ...others } = obj; + if (workspaces || (availableWorkspaces && availableWorkspaces.length)) { return obj; } return others; diff --git a/src/plugins/saved_objects_management/server/routes/scroll_count.ts b/src/plugins/saved_objects_management/server/routes/scroll_count.ts index fc5237805378..c200d6c82dd1 100644 --- a/src/plugins/saved_objects_management/server/routes/scroll_count.ts +++ b/src/plugins/saved_objects_management/server/routes/scroll_count.ts @@ -42,6 +42,7 @@ export const registerScrollForCountRoute = (router: IRouter) => { namespacesToInclude: schema.maybe(schema.arrayOf(schema.string())), searchString: schema.maybe(schema.string()), workspaces: schema.maybe(schema.arrayOf(schema.string())), + availableWorkspaces: schema.maybe(schema.arrayOf(schema.string())), }), }, }, @@ -59,7 +60,9 @@ export const registerScrollForCountRoute = (router: IRouter) => { const requestHasNamespaces = Array.isArray(req.body.namespacesToInclude) && req.body.namespacesToInclude.length; - const requestHasWorkspaces = Array.isArray(req.body.workspaces) && req.body.workspaces.length; + const requestHasWorkspaces = + (Array.isArray(req.body.workspaces) && req.body.workspaces.length) || + (Array.isArray(req.body.availableWorkspaces) && req.body.availableWorkspaces.length); if (requestHasNamespaces) { counts.namespaces = {}; @@ -114,7 +117,7 @@ export const registerScrollForCountRoute = (router: IRouter) => { } } - const workspacesToInclude = req.body.workspaces || []; + const workspacesToInclude = req.body.workspaces || req.body.availableWorkspaces || []; for (const ws of workspacesToInclude) { if (!counts.workspaces[ws]) { counts.workspaces[ws] = 0;