Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: small fixes for breadcrumbs and deep containment checks #1692

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 2 additions & 74 deletions packages/common/src/core/_internal/deepContains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,79 +4,7 @@ import { getEntityTypeFromType } from "../../search/_internal/getEntityTypeFromT
import { Catalog } from "../../search/Catalog";
import { asyncForEach } from "../../utils/asyncForEach";
import { HubEntityType } from "../types/HubEntityType";

/**
* @internal
* Mapping of path segments to entity types.
*/
const pathMap: Record<string, HubEntityType> = {
initiatives: "initiative",
projects: "project",
content: "content",
events: "event",
discussions: "discussion",
sites: "site",
apps: "content",
surveys: "content",
maps: "content",
datasets: "content",
pages: "page",
};

/**
* @internal
* Parsed path object, with validation information.
*/
interface IParsedPath {
valid: boolean;
parts?: string[];
reason?: string;
}

/**
* Parse a path string into its parts, and validate that it is a valid path.
* @param path
* @returns
*/
export function parsePath(path: string): IParsedPath {
// if the path starts with a /, remove it
if (path && path.startsWith("/")) {
path = path.slice(1);
}
// split the path into parts - it will be structured like: /{type}/{id}/{type}/{id}
const pathParts = path.split("/");

const result: IParsedPath = {
valid: true,
reason: "",
parts: pathParts,
};

// if the path is not structured correctly, return false
if (pathParts.length % 2 !== 0) {
result.valid = false;
result.reason = "Path does not contain an even number of parts.";
}

// if the path is > 10 parts, return false
if (pathParts.length > 10) {
result.valid = false;
result.reason = "Path is > 5 entities deep.";
}

// if the path contains invalid types, return false
if (result.valid) {
const validPaths = Object.keys(pathMap);
for (let i = 0; i < pathParts.length; i += 2) {
if (!validPaths.includes(pathParts[i])) {
result.valid = false;
result.reason = `Path contains invalid segment: ${pathParts[i]}.`;
}
}
}

return result;
}
import { parseContainmentPath, pathMap } from "../parseContainmentPath";

/**
* Convert a path string into an array of `IDeepCatalogInfo` objects.
Expand All @@ -86,7 +14,7 @@ export function parsePath(path: string): IParsedPath {
*/
export function pathToCatalogInfo(path: string): IDeepCatalogInfo[] {
// validate path, throw if invalid
const parsedPath = parsePath(path);
const parsedPath = parseContainmentPath(path);
if (!parsedPath.valid) {
throw new Error(parsedPath.reason);
}
Expand Down
75 changes: 75 additions & 0 deletions packages/common/src/core/parseContainmentPath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { HubEntityType } from "./types/HubEntityType";

/**
* @internal
* Mapping of path segments to entity types.
*/
export const pathMap: Record<string, HubEntityType> = {
initiatives: "initiative",
projects: "project",
content: "content",
events: "event",
discussions: "discussion",
sites: "site",
apps: "content",
surveys: "content",
maps: "content",
datasets: "content",
pages: "page",
};

/**
* @internal
* Parsed path object, with validation information.
*/
export interface IParsedPath {
valid: boolean;
parts?: string[];
reason?: string;
}

/**
* Parse a path string into its parts, and validate that it is a valid path.
* @param path
* @returns
*/

export function parseContainmentPath(path: string): IParsedPath {
// if the path starts with a /, remove it
if (path && path.startsWith("/")) {
path = path.slice(1);
}
// split the path into parts - it will be structured like: /{type}/{id}/{type}/{id}
const pathParts = path.split("/");

const result: IParsedPath = {
valid: true,
reason: "",
parts: pathParts,
};

// if the path is not structured correctly, return false
if (pathParts.length % 2 !== 0) {
result.valid = false;
result.reason = "Path does not contain an even number of parts.";
}

// if the path is > 10 parts, return false
if (pathParts.length > 10) {
result.valid = false;
result.reason = "Path is > 5 entities deep.";
}

// if the path contains invalid types, return false
if (result.valid) {
const validPaths = Object.keys(pathMap);
for (let i = 0; i < pathParts.length; i += 2) {
if (!validPaths.includes(pathParts[i])) {
result.valid = false;
result.reason = `Path contains invalid segment: ${pathParts[i]}.`;
}
}
}

return result;
}
1 change: 1 addition & 0 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export * from "./search/_internal/getCatalogGroups";
export * from "./search/getPredicateValues";
export * from "./core/hubHistory";
export * from "./core/deepCatalogContains";
export * from "./core/parseContainmentPath";

import OperationStack from "./OperationStack";
import OperationError from "./OperationError";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export function getPropertyMap(): IPropertyMap[] {
// Type specific mappings
map.push({ entityKey: "status", storeKey: "data.status" });
map.push({ entityKey: "catalog", storeKey: "data.catalog" });
map.push({ entityKey: "catalogs", storeKey: "data.catalogs" });
map.push({ entityKey: "permissions", storeKey: "data.permissions" });

map.push({ entityKey: "contacts", storeKey: "data.contacts" });
Expand Down
42 changes: 34 additions & 8 deletions packages/common/src/search/Catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ export class Catalog implements IHubCatalog {
* Return the existing scopes hash
*/
get scopes(): ICatalogScope {
return this._catalog.scopes;
return this._catalog.scopes || {};
}
/**
* Return an array of the entity types available in this Catalog
*/
get availableScopes(): EntityType[] {
return Object.keys(this.scopes || {}) as unknown as EntityType[];
return Object.keys(this.scopes) as unknown as EntityType[];
}

/**
Expand Down Expand Up @@ -312,16 +312,39 @@ export class Catalog implements IHubCatalog {
// construct the queries
const queries = [];
if (options.entityType) {
// --------------------------------------------------------
// Check the scope for the entity type
// then check any collections that match the entity type
// if there are no scopes or collections with the entity type
// then the entity is not contained in the catalog
// --------------------------------------------------------
if (this.scopes[options.entityType]) {
// Scope is a "bounding query" for all collections involving the entity type
// so we can check if it's in the catalog by checking if it's in the scope
queries.push({
targetEntity: options.entityType,
filters: [{ predicates: [pred] }],
});
} else {
// no scope for this entity type, thus it cannot be in the catalog
response.duration = Date.now() - start;
this._containsCache[identifier] = response;
return Promise.resolve(response);
// If there is no scope for the entity type, we check the collections
const typeCollections = this.collections.filter(
(c) => c.targetEntity === options.entityType
);
if (typeCollections.length) {
// If there are collections for the entity type, we check them
typeCollections.forEach((collection) => {
queries.push({
targetEntity: collection.targetEntity,
filters: [{ predicates: [pred] }, ...collection.scope.filters],
});
});
} else {
// no collections or scope for this entity type
// thus it cannot be in the catalog
response.duration = Date.now() - start;
this._containsCache[identifier] = response;
return Promise.resolve(response);
}
}
} else {
// Construct a query for each scope
Expand Down Expand Up @@ -581,8 +604,11 @@ export class Catalog implements IHubCatalog {
qry = cloneObject(query);
}

// Now merge in catalog scope level filters
qry.filters = [...qry.filters, ...this.getScope(targetEntity).filters];
// Now merge in catalog scope level filters if they exist
const scope = this.getScope(targetEntity);
if (scope) {
qry.filters = [...qry.filters, ...scope.filters];
}

const opts = cloneObject(options);
// An Catalog instance always uses the context so we remove/replace any passed in auth
Expand Down
40 changes: 0 additions & 40 deletions packages/common/test/core/_internal/deepContains.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
deepContains,
parsePath,
pathToCatalogInfo,
} from "../../../src/core/_internal/deepContains";
import { ArcGISContextManager } from "../../../src/ArcGISContextManager";
Expand Down Expand Up @@ -166,45 +165,6 @@ describe("deepContains:", () => {
});
});

describe("parsePath:", () => {
// add tests for the parsePath function

it("path needs even number of parts", () => {
const path = "/sites/00b/initiatives";

const result = parsePath(path);
expect(result.valid).toBeFalsy();
expect(result.reason).toBe(
"Path does not contain an even number of parts."
);
});

it("path needs less than 10 parts", () => {
const path =
"sites/00a/initiatives/00b/projects/00c/content/00d/content/00c/content/00e";

const result = parsePath(path);
expect(result.valid).toBeFalsy();
expect(result.reason).toBe("Path is > 5 entities deep.");
});

it("path needs valid paths", () => {
const path = "sites/00a/initiatives/00b/projects/00c/wallaby/00d";

const result = parsePath(path);
expect(result.valid).toBeFalsy();
expect(result.reason).toBe("Path contains invalid segment: wallaby.");
});

it("good path parses clean", () => {
const path = "sites/00a/initiatives/00b/projects/00c";
const result = parsePath(path);
expect(result.valid).toBeTruthy();
expect(result.reason).toBe("");
expect(result.parts).toEqual(path.split("/"));
});
});

describe("pathToCatalogInfo:", () => {
it("converts a path string into an array of IDeepCatalogInfo objects", () => {
const path = "sites/a00a/initiatives/b00b/projects/c00c";
Expand Down
38 changes: 38 additions & 0 deletions packages/common/test/core/parseContainmentPath.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { parseContainmentPath } from "../../src";

describe("parseContainmentPath:", () => {
it("path needs even number of parts", () => {
const path = "/sites/00b/initiatives";

const result = parseContainmentPath(path);
expect(result.valid).toBeFalsy();
expect(result.reason).toBe(
"Path does not contain an even number of parts."
);
});

it("path needs less than 10 parts", () => {
const path =
"sites/00a/initiatives/00b/projects/00c/content/00d/content/00c/content/00e";

const result = parseContainmentPath(path);
expect(result.valid).toBeFalsy();
expect(result.reason).toBe("Path is > 5 entities deep.");
});

it("path needs valid paths", () => {
const path = "sites/00a/initiatives/00b/projects/00c/wallaby/00d";

const result = parseContainmentPath(path);
expect(result.valid).toBeFalsy();
expect(result.reason).toBe("Path contains invalid segment: wallaby.");
});

it("good path parses clean", () => {
const path = "sites/00a/initiatives/00b/projects/00c";
const result = parseContainmentPath(path);
expect(result.valid).toBeTruthy();
expect(result.reason).toBe("");
expect(result.parts).toEqual(path.split("/"));
});
});
Loading
Loading