Skip to content

Commit

Permalink
[Workspace] refactor saved object client addToWorkspaces and deleteFr…
Browse files Browse the repository at this point in the history
…omWorkspaces function params structure (#7484)

* update saved object client function params structure

Signed-off-by: tygao <[email protected]>

* delete options workspace field

Signed-off-by: tygao <[email protected]>

* delete options workspace field

Signed-off-by: tygao <[email protected]>

* delete workspace in create

Signed-off-by: tygao <[email protected]>

* Update src/core/server/saved_objects/service/saved_objects_client.ts

Co-authored-by: SuZhou-Joe <[email protected]>
Signed-off-by: Tianyu Gao <[email protected]>

* Update src/core/server/saved_objects/service/saved_objects_client.ts

Co-authored-by: SuZhou-Joe <[email protected]>
Signed-off-by: Tianyu Gao <[email protected]>

* update type

Signed-off-by: tygao <[email protected]>

* update params

Signed-off-by: tygao <[email protected]>

* remove workspace field in overwrite create

Signed-off-by: tygao <[email protected]>

---------

Signed-off-by: tygao <[email protected]>
Signed-off-by: Tianyu Gao <[email protected]>
Co-authored-by: SuZhou-Joe <[email protected]>
  • Loading branch information
raintygao and SuZhou-Joe authored Jul 26, 2024
1 parent fc1adfa commit 2268738
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ test(`#deleteFromWorkspaces Should use update if there is existing workspaces`,

const type = Symbol();
const id = Symbol();
await client.deleteFromWorkspaces(type, id, ['id2']);
const workspaces = ['id2'];
await client.deleteFromWorkspaces(type, id, workspaces);
expect(mockRepository.get).toHaveBeenCalledWith(type, id, {});
expect(mockRepository.update).toHaveBeenCalledWith(type, id, undefined, {
version: undefined,
Expand All @@ -259,7 +260,8 @@ test(`#deleteFromWorkspaces Should use overwrite create if there is no existing

const type = Symbol();
const id = Symbol();
await client.deleteFromWorkspaces(type, id, ['id1']);
const workspaces = ['id1'];
await client.deleteFromWorkspaces(type, id, workspaces);
expect(mockRepository.get).toHaveBeenCalledWith(type, id, {});
expect(mockRepository.create).toHaveBeenCalledWith(
type,
Expand Down Expand Up @@ -292,6 +294,7 @@ test(`#addToWorkspaces`, async () => {

expect(mockRepository.get).toHaveBeenCalledWith(type, id, {});
expect(mockRepository.update).toHaveBeenCalledWith(type, id, undefined, {
version: undefined,
workspaces: [workspaces],
});

Expand Down
34 changes: 24 additions & 10 deletions src/core/server/saved_objects/service/saved_objects_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,19 +468,29 @@ export class SavedObjectsClient {
* Remove a saved object from workspaces
* @param type
* @param id
* @param workspaces
* @param targetWorkspaces
* @param options
*/
deleteFromWorkspaces = async <T = unknown>(type: string, id: string, workspaces: string[]) => {
if (!workspaces || workspaces.length === 0) {
deleteFromWorkspaces = async <T = unknown>(
type: string,
id: string,
targetWorkspaces: string[],
options: SavedObjectsBaseOptions = {}
) => {
if (!targetWorkspaces || targetWorkspaces.length === 0) {
throw new TypeError(`Workspaces is required.`);
}
const object = await this.get<T>(type, id);
if ('workspaces' in options && options.workspaces) {
throw new TypeError('Invalid options, options.workspaces should not exist.');
}
const object = await this.get<T>(type, id, options);
const existingWorkspaces = object.workspaces ?? [];
const newWorkspaces = existingWorkspaces.filter((item) => {
return workspaces.indexOf(item) === -1;
return targetWorkspaces.indexOf(item) === -1;
});
if (newWorkspaces.length > 0) {
return await this.update<T>(type, id, object.attributes, {
...options,
workspaces: newWorkspaces,
version: object.version,
});
Expand All @@ -492,6 +502,7 @@ export class SavedObjectsClient {
...object.attributes,
},
{
...options,
id,
permissions: object.permissions,
overwrite: true,
Expand All @@ -505,22 +516,25 @@ export class SavedObjectsClient {
* Add a saved object to workspaces
* @param type
* @param id
* @param workspaces
* @param targetWorkspaces
* @param options
*/
addToWorkspaces = async <T = unknown>(
type: string,
id: string,
workspaces: string[]
targetWorkspaces: string[],
options: SavedObjectsBaseOptions = {}
): Promise<any> => {
if (!workspaces || workspaces.length === 0) {
if (!targetWorkspaces || targetWorkspaces.length === 0) {
throw new TypeError(`Workspaces is required.`);
}
const object = await this.get<T>(type, id);
const object = await this.get<T>(type, id, options);
const existingWorkspaces = object.workspaces ?? [];
const mergedWorkspaces = existingWorkspaces.concat(workspaces);
const mergedWorkspaces = existingWorkspaces.concat(targetWorkspaces);
const nonDuplicatedWorkspaces = Array.from(new Set(mergedWorkspaces));

return await this.update<T>(type, id, object.attributes, {
...options,
workspaces: nonDuplicatedWorkspaces,
version: object.version,
});
Expand Down

0 comments on commit 2268738

Please sign in to comment.