From 01b296f135ec43a7a38f43285cb7cb1124ebd5fc Mon Sep 17 00:00:00 2001 From: Dave Bouwman Date: Wed, 24 Aug 2022 11:07:02 -0600 Subject: [PATCH] fix: rename permission, catalog props and simplify adding permission (#863) --- packages/common/e2e/hub-project-class.e2e.ts | 6 +- packages/common/src/core/PermissionManager.ts | 12 +- .../core/behaviors/IWithPermissionBehavior.ts | 3 +- .../src/core/traits/IWithCatalogDefinition.ts | 4 +- .../src/core/traits/IWithPermissions.ts | 2 +- packages/common/src/core/types/IHubProject.ts | 4 +- packages/common/src/projects/HubProject.ts | 195 +----------------- .../common/src/projects/HubProjectManager.ts | 1 + packages/common/src/projects/HubProjects.ts | 5 +- packages/common/src/projects/defaults.ts | 4 +- .../test/core/PermissionManager.test.ts | 4 +- .../core/behaviors/PermissionBehavior.test.ts | 2 - .../common/test/projects/HubProject.test.ts | 26 +-- .../common/test/projects/HubProjects.test.ts | 4 +- 14 files changed, 43 insertions(+), 229 deletions(-) diff --git a/packages/common/e2e/hub-project-class.e2e.ts b/packages/common/e2e/hub-project-class.e2e.ts index e252d565573..37496a410cc 100644 --- a/packages/common/e2e/hub-project-class.e2e.ts +++ b/packages/common/e2e/hub-project-class.e2e.ts @@ -43,7 +43,7 @@ fdescribe("HubProject Class", () => { const group = groups[0]; if (group) { // add the project to the project - project.permissions.add("addEvent", { + project.permissions.add({ permission: "addEvent", target: "group", targetId: group.id, @@ -57,8 +57,8 @@ fdescribe("HubProject Class", () => { await project.save(); const json = project.toJson(); - expect(json.permissionDefinition).toBeDefined(); - expect(json.permissionDefinition[0].targetId).toBe(group.id); + expect(json.permissions).toBeDefined(); + expect(json.permissions[0].targetId).toBe(group.id); } // change something else and save it again diff --git a/packages/common/src/core/PermissionManager.ts b/packages/common/src/core/PermissionManager.ts index 8ea3fbf001f..1e90552c952 100644 --- a/packages/common/src/core/PermissionManager.ts +++ b/packages/common/src/core/PermissionManager.ts @@ -57,15 +57,11 @@ export class PermissionManager { * Set a permission for the given entity * @param permission */ - add(permission: HubPermission, definition: IHubPermission): void { - if (!definition.id) { - definition.id = createId("p"); + add(permission: IHubPermission): void { + if (!permission.id) { + permission.id = createId("p"); } - this._permissions = addPermission( - permission, - definition, - this._permissions - ); + this._permissions = addPermission(permission, this._permissions); } /** diff --git a/packages/common/src/core/behaviors/IWithPermissionBehavior.ts b/packages/common/src/core/behaviors/IWithPermissionBehavior.ts index 512b947b123..d74414e5e95 100644 --- a/packages/common/src/core/behaviors/IWithPermissionBehavior.ts +++ b/packages/common/src/core/behaviors/IWithPermissionBehavior.ts @@ -69,12 +69,11 @@ export function getPermissions( * @returns */ export function addPermission( - permission: HubPermission, definition: IHubPermission, permissions: IHubPermission[] ): IHubPermission[] { const otherPermissions = removePermission( - permission, + definition.permission, definition.targetId, permissions ); diff --git a/packages/common/src/core/traits/IWithCatalogDefinition.ts b/packages/common/src/core/traits/IWithCatalogDefinition.ts index d293993e184..ddb2c273fc4 100644 --- a/packages/common/src/core/traits/IWithCatalogDefinition.ts +++ b/packages/common/src/core/traits/IWithCatalogDefinition.ts @@ -1,8 +1,8 @@ import { IHubCatalog } from "../../search"; -export interface IWithCatalogDefinition { +export interface IWithCatalog { /** * Catalog */ - catalogDefinition: IHubCatalog; + catalog: IHubCatalog; } diff --git a/packages/common/src/core/traits/IWithPermissions.ts b/packages/common/src/core/traits/IWithPermissions.ts index 0e403826898..2b650ff6d61 100644 --- a/packages/common/src/core/traits/IWithPermissions.ts +++ b/packages/common/src/core/traits/IWithPermissions.ts @@ -7,5 +7,5 @@ export interface IWithPermissionDefinition { /** * List of permissions for the entity */ - permissionDefinition: IHubPermission[]; + permissions: IHubPermission[]; } diff --git a/packages/common/src/core/types/IHubProject.ts b/packages/common/src/core/types/IHubProject.ts index 8c3784e8bf0..271f45d1da8 100644 --- a/packages/common/src/core/types/IHubProject.ts +++ b/packages/common/src/core/types/IHubProject.ts @@ -4,7 +4,7 @@ import { IWithPermissionDefinition, IWithSlug, } from "../traits/index"; -import { IWithCatalogDefinition } from "../traits/IWithCatalogDefinition"; +import { IWithCatalog } from "../traits/IWithCatalogDefinition"; /** * Defines the properties of a Hub Project object @@ -12,7 +12,7 @@ import { IWithCatalogDefinition } from "../traits/IWithCatalogDefinition"; export interface IHubProject extends IHubItemEntity, IWithSlug, - IWithCatalogDefinition, + IWithCatalog, IWithLayout, IWithPermissionDefinition { /** diff --git a/packages/common/src/projects/HubProject.ts b/packages/common/src/projects/HubProject.ts index 1755e674318..e2ba3f50b40 100644 --- a/packages/common/src/projects/HubProject.ts +++ b/packages/common/src/projects/HubProject.ts @@ -51,185 +51,13 @@ export class HubProject private constructor(project: IHubProject, context: IArcGISContext) { this.context = context; this.entity = project; - this._catalog = Catalog.fromJson(project.catalogDefinition, this.context); + this._catalog = Catalog.fromJson(project.catalog, this.context); this._permissionManager = PermissionManager.fromJson( - project.permissionDefinition, + project.permissions, this.context ); } - //#region Properties - coverage skipped for now - // TODO: decide if we want ambient dirty tracking or if we - // implement that as async check against the server - - // // istanbul ignore next - // get permissionDefinition(): IHubPermission[] { - // return this.entity.permissionDefinition; - // } - // // istanbul ignore next - // set permissionDefinition(permissions: IHubPermission[]) { - // this.entity.permissionDefinition = permissions; - // } - - // // istanbul ignore next - // get timeline(): IHubTimeline | undefined { - // return this.entity.timeline; - // } - // // istanbul ignore next - // set timeline(value: IHubTimeline | undefined) { - // this.entity.timeline = value; - // } - // // istanbul ignore next - // get status(): "inactive" | "active" { - // return this.entity.status; - // } - // // istanbul ignore next - // set status(value: "inactive" | "active") { - // this.entity.status = value; - // } - - // // istanbul ignore next - // get thumbnailUrl(): string | undefined { - // return this.entity.thumbnailUrl; - // } - // // istanbul ignore next - // set thumbnailUrl(value: string | undefined) { - // this.entity.thumbnailUrl = value; - // } - - // // istanbul ignore next - // get owner(): string { - // return this.entity.owner; - // } - - // // istanbul ignore next - // get description(): string | undefined { - // return this.entity.description; - // } - // // istanbul ignore next - // set description(value: string | undefined) { - // this.entity.description = value; - // } - // // istanbul ignore next - // get boundary(): IHubGeography | undefined { - // return this.entity.boundary; - // } - // // istanbul ignore next - // set boundary(value: IHubGeography | undefined) { - // this.entity.boundary = value; - // } - // // istanbul ignore next - // get culture(): string | undefined { - // return this.entity.culture; - // } - // // istanbul ignore next - // set culture(value: string | undefined) { - // this.entity.culture = value; - // } - // // istanbul ignore next - // get tags(): string[] { - // return this.entity.tags; - // } - // // istanbul ignore next - // set tags(value: string[]) { - // this.entity.tags = value; - // } - // // istanbul ignore next - // get typeKeywords(): string[] | undefined { - // return this.entity.typeKeywords; - // } - // // istanbul ignore next - // set typeKeywords(value: string[] | undefined) { - // this.entity.typeKeywords = value; - // } - // // istanbul ignore next - // get url(): string | undefined { - // return this.entity.url; - // } - // // istanbul ignore next - // set url(value: string | undefined) { - // this.entity.url = value; - // } - // // istanbul ignore next - // get id(): string { - // return this.entity.id; - // } - - // // istanbul ignore next - // get name(): string { - // return this.entity.name; - // } - // // istanbul ignore next - // set name(value: string) { - // this.entity.name = value; - // } - // // istanbul ignore next - // get summary(): string | undefined { - // return this.entity.summary; - // } - // // istanbul ignore next - // set summary(value: string | undefined) { - // this.entity.summary = value; - // } - // // istanbul ignore next - // get createdDate(): Date { - // return this.entity.createdDate; - // } - // // istanbul ignore next - // get createdDateSource(): string { - // return this.entity.createdDateSource; - // } - // // istanbul ignore next - // get updatedDate(): Date { - // return this.entity.updatedDate; - // } - // // istanbul ignore next - // get updatedDateSource(): string { - // return this.entity.updatedDateSource; - // } - // // istanbul ignore next - // get type(): string { - // return this.entity.type; - // } - // // istanbul ignore next - // get source(): string | undefined { - // return this.entity.source; - // } - // // istanbul ignore next - // set source(value: string | undefined) { - // this.entity.source = value; - // } - // // istanbul ignore next - // get slug(): string { - // return this.entity.slug; - // } - // // istanbul ignore next - // set slug(value: string) { - // this.entity.slug = value; - // } - // // istanbul ignore next - // get orgUrlKey(): string { - // return this.entity.orgUrlKey; - // } - // // istanbul ignore next - // get layout(): IHubLayout | undefined { - // return this.entity.layout; - // } - // // istanbul ignore next - // set layout(value: IHubLayout | undefined) { - // this.entity.layout = value; - // } - // // istanbul ignore next - // get catalogDefinition(): IHubCatalog { - // return this.entity.catalogDefinition; - // } - - // set catalogDefinition(value: IHubCatalog) { - // this.entity.catalogDefinition = value; - // // update the catalog instance - // this._catalog = Catalog.fromJson(value, this.context); - // } - /** * @returns Catalog instance for this project. Note: Do not hold direct references to this object; always access it from the project. */ @@ -244,8 +72,6 @@ export class HubProject return this._permissionManager; } - // #endregion - /** * Create an instance from an IHubProject object * @param json - JSON object to create a HubProject from @@ -345,15 +171,12 @@ export class HubProject this.entity = { ...this.entity, ...changes }; // update internal instances - if (changes.catalogDefinition) { - this._catalog = Catalog.fromJson( - this.entity.catalogDefinition, - this.context - ); + if (changes.catalog) { + this._catalog = Catalog.fromJson(this.entity.catalog, this.context); } - if (changes.permissionDefinition) { + if (changes.permissions) { this._permissionManager = PermissionManager.fromJson( - this.entity.permissionDefinition, + this.entity.permissions, this.context ); } @@ -367,9 +190,9 @@ export class HubProject if (this.isDestroyed) { throw new Error("HubProject is already destroyed."); } - // get the catalog definition out of the instance - this.entity.catalogDefinition = this._catalog.toJson(); - this.entity.permissionDefinition = this._permissionManager.toJson(); + // get the catalog, and permission configs + this.entity.catalog = this._catalog.toJson(); + this.entity.permissions = this._permissionManager.toJson(); if (this.entity.id) { // update it diff --git a/packages/common/src/projects/HubProjectManager.ts b/packages/common/src/projects/HubProjectManager.ts index 722101f8a03..b4ca56833a7 100644 --- a/packages/common/src/projects/HubProjectManager.ts +++ b/packages/common/src/projects/HubProjectManager.ts @@ -30,6 +30,7 @@ import { import { failSafe, isUpdateGroup } from "../utils"; /** + * @private * Centralized functions used to manage IHubProject instances * * This class is a convenience wrapper over util functions which diff --git a/packages/common/src/projects/HubProjects.ts b/packages/common/src/projects/HubProjects.ts index 9405e85ff01..019b3b5f641 100644 --- a/packages/common/src/projects/HubProjects.ts +++ b/packages/common/src/projects/HubProjects.ts @@ -74,6 +74,7 @@ function getProjectPropertyMap(): IPropertyMap[] { ]; const dataProps = [ "contacts", + "catalog", "display", "geometry", "headerImage", @@ -94,10 +95,6 @@ function getProjectPropertyMap(): IPropertyMap[] { objectKey: "slug", modelKey: "item.properties.slug", }); - map.push({ - objectKey: "catalogDefinition", - modelKey: "data.catalog", - }); map.push({ objectKey: "orgUrlKey", modelKey: "item.properties.orgUrlKey", diff --git a/packages/common/src/projects/defaults.ts b/packages/common/src/projects/defaults.ts index 7b0e4397715..83397ece8fb 100644 --- a/packages/common/src/projects/defaults.ts +++ b/packages/common/src/projects/defaults.ts @@ -11,8 +11,8 @@ export const DEFAULT_PROJECT: Partial = { tags: [], typeKeywords: ["Hub Project"], status: "inactive", - catalogDefinition: { schemaVersion: 0 }, - permissionDefinition: [], + catalog: { schemaVersion: 0 }, + permissions: [], }; /** diff --git a/packages/common/test/core/PermissionManager.test.ts b/packages/common/test/core/PermissionManager.test.ts index c057334d376..3ff257d27fb 100644 --- a/packages/common/test/core/PermissionManager.test.ts +++ b/packages/common/test/core/PermissionManager.test.ts @@ -84,8 +84,8 @@ describe("PermissionManager Class:", () => { targetId: "bchy8ad", }; - pm.add(p2.permission, p2); - pm.add(p3.permission, p3); + pm.add(p2); + pm.add(p3); expect(addPermissionSpy.calls.count()).toBe(2); expect(pm.get("addInitiative").length).toBe(2); pm.remove("addInitiative", p2.targetId); diff --git a/packages/common/test/core/behaviors/PermissionBehavior.test.ts b/packages/common/test/core/behaviors/PermissionBehavior.test.ts index 8aa3418aef5..7ad6692c176 100644 --- a/packages/common/test/core/behaviors/PermissionBehavior.test.ts +++ b/packages/common/test/core/behaviors/PermissionBehavior.test.ts @@ -77,7 +77,6 @@ describe("PermissionBehavior module:", () => { }); it("addPermission replaces existing", () => { const updates = addPermission( - "addInitiative", { id: "pnew", permission: "addInitiative", @@ -93,7 +92,6 @@ describe("PermissionBehavior module:", () => { }); it("addPermission adds new", () => { const updates = addPermission( - "createProject", { id: "p9", permission: "createProject", diff --git a/packages/common/test/projects/HubProject.test.ts b/packages/common/test/projects/HubProject.test.ts index 29ae9433b7e..df4c5977ee6 100644 --- a/packages/common/test/projects/HubProject.test.ts +++ b/packages/common/test/projects/HubProject.test.ts @@ -40,8 +40,8 @@ describe("HubProject Class:", () => { expect(chk.toJson().name).toEqual("Test Project"); // adds empty permissions and catalog const json = chk.toJson(); - expect(json.permissionDefinition).toEqual([]); - expect(json.catalogDefinition).toEqual({ schemaVersion: 0 }); + expect(json.permissions).toEqual([]); + expect(json.catalog).toEqual({ schemaVersion: 0 }); }); it("loads based on identifier", async () => { const fetchSpy = spyOn(HubProjectsModule, "fetchProject").and.callFake( @@ -135,18 +135,18 @@ describe("HubProject Class:", () => { it("update applies partial chagnes to internal state", () => { const chk = HubProject.fromJson( - { name: "Test Project", catalogDefinition: { schemaVersion: 0 } }, + { name: "Test Project", catalog: { schemaVersion: 0 } }, authdCtxMgr.context ); chk.update({ name: "Test Project 2", - permissionDefinition: [ + permissions: [ { permission: "addEvent", target: "group", targetId: "3ef" }, ], - catalogDefinition: { schemaVersion: 2 }, + catalog: { schemaVersion: 2 }, }); expect(chk.toJson().name).toEqual("Test Project 2"); - expect(chk.toJson().catalogDefinition).toEqual({ schemaVersion: 2 }); + expect(chk.toJson().catalog).toEqual({ schemaVersion: 2 }); chk.update({ tags: ["one", "two"] }); expect(chk.toJson().tags).toEqual(["one", "two"]); @@ -162,7 +162,7 @@ describe("HubProject Class:", () => { { id: "bc3", name: "Test Project", - catalogDefinition: { schemaVersion: 0 }, + catalog: { schemaVersion: 0 }, }, authdCtxMgr.context ); @@ -207,7 +207,7 @@ describe("HubProject Class:", () => { it("internal instance accessors", () => { const chk = HubProject.fromJson( - { name: "Test Project", catalogDefinition: { schemaVersion: 0 } }, + { name: "Test Project", catalog: { schemaVersion: 0 } }, authdCtxMgr.context ); @@ -215,13 +215,13 @@ describe("HubProject Class:", () => { expect(chk.permissions instanceof PermissionManager).toBeTruthy(); }); - it("setting catalogDefinition updates catalog instance", () => { + it("setting catalog updates catalog instance", () => { const chk = HubProject.fromJson( - { name: "Test Project", catalogDefinition: { schemaVersion: 0 } }, + { name: "Test Project", catalog: { schemaVersion: 0 } }, authdCtxMgr.context ); - chk.update({ catalogDefinition: { schemaVersion: 2 } }); - expect(chk.toJson().catalogDefinition).toEqual({ schemaVersion: 2 }); + chk.update({ catalog: { schemaVersion: 2 } }); + expect(chk.toJson().catalog).toEqual({ schemaVersion: 2 }); expect(chk.catalog.schemaVersion).toEqual(2); }); @@ -233,7 +233,7 @@ describe("HubProject Class:", () => { id: "00c", name: "Test Project", owner: "deke", - catalogDefinition: { schemaVersion: 0 }, + catalog: { schemaVersion: 0 }, }, authdCtxMgr.context ); diff --git a/packages/common/test/projects/HubProjects.test.ts b/packages/common/test/projects/HubProjects.test.ts index 5a72fce2ba1..3be08903a70 100644 --- a/packages/common/test/projects/HubProjects.test.ts +++ b/packages/common/test/projects/HubProjects.test.ts @@ -298,8 +298,8 @@ describe("HubProjects:", () => { updatedDateSource: "item.modified", status: "active", thumbnailUrl: "", - permissionDefinition: [], - catalogDefinition: { + permissions: [], + catalog: { schemaVersion: 0, }, };