From 2268738d29913359d23da3c460fb9695392f952d Mon Sep 17 00:00:00 2001 From: Tianyu Gao Date: Fri, 26 Jul 2024 22:34:52 +0800 Subject: [PATCH] [Workspace] refactor saved object client addToWorkspaces and deleteFromWorkspaces function params structure (#7484) * update saved object client function params structure Signed-off-by: tygao * delete options workspace field Signed-off-by: tygao * delete options workspace field Signed-off-by: tygao * delete workspace in create Signed-off-by: tygao * Update src/core/server/saved_objects/service/saved_objects_client.ts Co-authored-by: SuZhou-Joe Signed-off-by: Tianyu Gao * Update src/core/server/saved_objects/service/saved_objects_client.ts Co-authored-by: SuZhou-Joe Signed-off-by: Tianyu Gao * update type Signed-off-by: tygao * update params Signed-off-by: tygao * remove workspace field in overwrite create Signed-off-by: tygao --------- Signed-off-by: tygao Signed-off-by: Tianyu Gao Co-authored-by: SuZhou-Joe --- .../service/saved_objects_client.test.js | 7 ++-- .../service/saved_objects_client.ts | 34 +++++++++++++------ 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/core/server/saved_objects/service/saved_objects_client.test.js b/src/core/server/saved_objects/service/saved_objects_client.test.js index b3511df29580..aa597f98379a 100644 --- a/src/core/server/saved_objects/service/saved_objects_client.test.js +++ b/src/core/server/saved_objects/service/saved_objects_client.test.js @@ -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, @@ -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, @@ -292,6 +294,7 @@ test(`#addToWorkspaces`, async () => { expect(mockRepository.get).toHaveBeenCalledWith(type, id, {}); expect(mockRepository.update).toHaveBeenCalledWith(type, id, undefined, { + version: undefined, workspaces: [workspaces], }); diff --git a/src/core/server/saved_objects/service/saved_objects_client.ts b/src/core/server/saved_objects/service/saved_objects_client.ts index 7c0b16ac4585..f17e6e35f0bd 100644 --- a/src/core/server/saved_objects/service/saved_objects_client.ts +++ b/src/core/server/saved_objects/service/saved_objects_client.ts @@ -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 (type: string, id: string, workspaces: string[]) => { - if (!workspaces || workspaces.length === 0) { + deleteFromWorkspaces = async ( + type: string, + id: string, + targetWorkspaces: string[], + options: SavedObjectsBaseOptions = {} + ) => { + if (!targetWorkspaces || targetWorkspaces.length === 0) { throw new TypeError(`Workspaces is required.`); } - const object = await this.get(type, id); + if ('workspaces' in options && options.workspaces) { + throw new TypeError('Invalid options, options.workspaces should not exist.'); + } + const object = await this.get(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(type, id, object.attributes, { + ...options, workspaces: newWorkspaces, version: object.version, }); @@ -492,6 +502,7 @@ export class SavedObjectsClient { ...object.attributes, }, { + ...options, id, permissions: object.permissions, overwrite: true, @@ -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 ( type: string, id: string, - workspaces: string[] + targetWorkspaces: string[], + options: SavedObjectsBaseOptions = {} ): Promise => { - if (!workspaces || workspaces.length === 0) { + if (!targetWorkspaces || targetWorkspaces.length === 0) { throw new TypeError(`Workspaces is required.`); } - const object = await this.get(type, id); + const object = await this.get(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(type, id, object.attributes, { + ...options, workspaces: nonDuplicatedWorkspaces, version: object.version, });