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

[workspace] Fix not able to query legacy data when have at least 1 workspace #7254

Merged
merged 9 commits into from
Jul 19, 2024
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[];
availiableWorkspaces?: 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,
availiableWorkspaces: 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,
availiableWorkspaces: 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 availiableWorkspaces exists', async () => {
const obj = formatWorkspaceIdParams({ foo: 'bar', availiableWorkspaces: ['foo'] });
expect(obj).toEqual({ foo: 'bar', availiableWorkspaces: ['foo'] });
});

it('formatWorkspaceIdParams with availiableWorkspaces is empty array', async () => {
const obj = formatWorkspaceIdParams({ foo: 'bar', availiableWorkspaces: [] });
expect(obj).toEqual({ foo: 'bar' });
});

it('formatWorkspaceIdParams with availiableWorkspaces is null/undefined', async () => {
const obj = formatWorkspaceIdParams({ foo: 'bar', availiableWorkspaces: null });
expect(obj).toEqual({ foo: 'bar' });
});

it('formatWorkspaceIdParams with both workspaces and availiableWorkspaces are not empty', async () => {
const obj = formatWorkspaceIdParams({
foo: 'bar',
availiableWorkspaces: ['foo', 'bar'],
workspaces: ['foo'],
});
expect(obj).toEqual({ foo: 'bar', availiableWorkspaces: ['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; availiableWorkspaces?: string[] | null }
>(obj: T): T | Omit<T, 'workspaces' | 'availiableWorkspaces'> {
const { workspaces, availiableWorkspaces, ...others } = obj;

Check warning on line 9 in src/plugins/saved_objects_management/public/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/saved_objects_management/public/utils.ts#L9

Added line #L9 was not covered by tests
if (workspaces || (availiableWorkspaces && availiableWorkspaces.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())),
availiableWorkspaces: schema.maybe(schema.arrayOf(schema.string())),
Hailong-am marked this conversation as resolved.
Show resolved Hide resolved
}),
},
},
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.availiableWorkspaces) && req.body.availiableWorkspaces.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.availiableWorkspaces || [];
for (const ws of workspacesToInclude) {
if (!counts.workspaces[ws]) {
counts.workspaces[ws] = 0;
Expand Down
Loading