Skip to content

Commit

Permalink
fix: rename permission, catalog props and simplify adding permission (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dbouwman authored Aug 24, 2022
1 parent 3a06b77 commit 01b296f
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 229 deletions.
6 changes: 3 additions & 3 deletions packages/common/e2e/hub-project-class.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
12 changes: 4 additions & 8 deletions packages/common/src/core/PermissionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/core/traits/IWithCatalogDefinition.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { IHubCatalog } from "../../search";

export interface IWithCatalogDefinition {
export interface IWithCatalog {
/**
* Catalog
*/
catalogDefinition: IHubCatalog;
catalog: IHubCatalog;
}
2 changes: 1 addition & 1 deletion packages/common/src/core/traits/IWithPermissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ export interface IWithPermissionDefinition {
/**
* List of permissions for the entity
*/
permissionDefinition: IHubPermission[];
permissions: IHubPermission[];
}
4 changes: 2 additions & 2 deletions packages/common/src/core/types/IHubProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import {
IWithPermissionDefinition,
IWithSlug,
} from "../traits/index";
import { IWithCatalogDefinition } from "../traits/IWithCatalogDefinition";
import { IWithCatalog } from "../traits/IWithCatalogDefinition";

/**
* Defines the properties of a Hub Project object
*/
export interface IHubProject
extends IHubItemEntity,
IWithSlug,
IWithCatalogDefinition,
IWithCatalog,
IWithLayout,
IWithPermissionDefinition {
/**
Expand Down
195 changes: 9 additions & 186 deletions packages/common/src/projects/HubProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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
Expand Down Expand Up @@ -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
);
}
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/projects/HubProjectManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions packages/common/src/projects/HubProjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ function getProjectPropertyMap(): IPropertyMap[] {
];
const dataProps = [
"contacts",
"catalog",
"display",
"geometry",
"headerImage",
Expand All @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/projects/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export const DEFAULT_PROJECT: Partial<IHubProject> = {
tags: [],
typeKeywords: ["Hub Project"],
status: "inactive",
catalogDefinition: { schemaVersion: 0 },
permissionDefinition: [],
catalog: { schemaVersion: 0 },
permissions: [],
};

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/common/test/core/PermissionManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ describe("PermissionBehavior module:", () => {
});
it("addPermission replaces existing", () => {
const updates = addPermission(
"addInitiative",
{
id: "pnew",
permission: "addInitiative",
Expand All @@ -93,7 +92,6 @@ describe("PermissionBehavior module:", () => {
});
it("addPermission adds new", () => {
const updates = addPermission(
"createProject",
{
id: "p9",
permission: "createProject",
Expand Down
Loading

0 comments on commit 01b296f

Please sign in to comment.