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) (#7519)

* update saved object client function params structure



* delete options workspace field



* delete options workspace field



* delete workspace in create



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




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




* update type



* update params



* remove workspace field in overwrite create



---------




(cherry picked from commit 2268738)

Signed-off-by: tygao <tygao@amazon.com>
Signed-off-by: Tianyu Gao <tygao@amazon.com>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: SuZhou-Joe <suzhou@amazon.com>
  • Loading branch information
3 people authored Jul 26, 2024
1 parent e2e3f46 commit c1eeef0
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 c1eeef0

Please sign in to comment.