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

[Backport 2.x] [workspace] Fix not able to query legacy data when have at least 1 workspace #7320

Merged
merged 1 commit into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface SavedObjectCountOptions {
namespacesToInclude?: string[];
searchString?: string;
workspaces?: string[];
availableWorkspaces?: string[];
}

export async function getSavedObjectCounts(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -851,8 +851,8 @@ describe('SavedObjectsTable', () => {
await waitFor(() => {
expect(findObjectsMock).toBeCalledWith(
http,
expect.objectContaining({
workspaces: expect.arrayContaining(['workspace1', 'workspace2']),
expect.not.objectContaining({
workspaces,
})
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,14 @@ export class SavedObjectsTable extends Component<SavedObjectsTableProps, SavedOb
}

private get workspaceIdQuery() {
const { availableWorkspaces, currentWorkspaceId, workspaceEnabled } = this.state;
const { currentWorkspaceId, workspaceEnabled } = this.state;
// workspace is turned off
if (!workspaceEnabled) {
return undefined;
} else {
// application home
// not in any workspace
if (!currentWorkspaceId) {
// public workspace is virtual at this moment
return availableWorkspaces?.map((ws) => ws.id);
return undefined;
} else {
return [currentWorkspaceId];
}
Expand Down Expand Up @@ -250,6 +249,7 @@ export class SavedObjectsTable extends Component<SavedObjectsTableProps, SavedOb
typesToInclude: filteredTypes,
searchString: queryText,
workspaces: this.workspaceIdQuery,
availableWorkspaces: this.state.availableWorkspaces?.map((ws) => ws.id),
});

if (availableNamespaces.length) {
Expand Down Expand Up @@ -286,6 +286,7 @@ export class SavedObjectsTable extends Component<SavedObjectsTableProps, SavedOb
typesToInclude: allowedTypes,
searchString: queryText,
workspaces: this.workspaceIdQuery,
availableWorkspaces: this.state.availableWorkspaces?.map((ws) => ws.id),
});

if (availableNamespaces.length) {
Expand Down Expand Up @@ -958,9 +959,7 @@ export class SavedObjectsTable extends Component<SavedObjectsTableProps, SavedOb
if (workspaceEnabled && availableWorkspaces?.length) {
const wsCounts = savedObjectCounts.workspaces || {};
const wsFilterOptions = availableWorkspaces
.filter((ws) => {
return this.workspaceIdQuery?.includes(ws.id);
})
.filter((ws) => (currentWorkspaceId ? currentWorkspaceId === ws.id : true))
.map((ws) => {
return {
name: ws.name,
Expand Down
24 changes: 24 additions & 0 deletions src/plugins/saved_objects_management/public/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'] });
});
});
10 changes: 5 additions & 5 deletions src/plugins/saved_objects_management/public/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* SPDX-License-Identifier: Apache-2.0
*/

export function formatWorkspaceIdParams<T extends { workspaces?: string[] | null }>(
obj: T
): T | Omit<T, 'workspaces'> {
const { workspaces, ...others } = obj;
if (workspaces) {
export function formatWorkspaceIdParams<
T extends { workspaces?: string[] | null; availableWorkspaces?: string[] | null }
>(obj: T): T | Omit<T, 'workspaces' | 'availableWorkspaces'> {
const { workspaces, availableWorkspaces, ...others } = obj;
if (workspaces || (availableWorkspaces && availableWorkspaces.length)) {
return obj;
}
return others;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())),
}),
},
},
Expand All @@ -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 = {};
Expand Down Expand Up @@ -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;
Expand Down
Loading