Skip to content

Commit

Permalink
feat: associated, connected and unconnected queries and functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dbouwman committed Sep 13, 2023
1 parent beb9194 commit f8aaee9
Show file tree
Hide file tree
Showing 32 changed files with 959 additions and 185 deletions.
8 changes: 4 additions & 4 deletions packages/common/e2e/associations.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
IHubInitiative,
fetchRelatedProjects,
fetchConnectedProjects,
fetchAssociatedProjects,
fetchHubEntity,
fetchUnRelatedProjects,
fetchUnConnectedProjects,
} from "../src";
import Artifactory from "./helpers/Artifactory";
import config from "./helpers/config";
Expand Down Expand Up @@ -122,7 +122,7 @@ fdescribe("associations development harness:", () => {
context
)) as IHubInitiative;
// debugger;
const projects = await fetchUnRelatedProjects(
const projects = await fetchUnConnectedProjects(
entity,
context.hubRequestOptions
);
Expand All @@ -138,7 +138,7 @@ fdescribe("associations development harness:", () => {
TEST_ITEMS.initiative,
context
)) as IHubInitiative;
const projects = await fetchRelatedProjects(
const projects = await fetchConnectedProjects(
entity,
context.hubRequestOptions
);
Expand Down
44 changes: 0 additions & 44 deletions packages/common/src/associations/getAssociatedQuery.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/common/src/associations/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from "./addAssociation";
export * from "./getAssociatedQuery";
export * from "./listAssociations";
export * from "./removeAssociation";
export * from "./types";
30 changes: 30 additions & 0 deletions packages/common/src/associations/internal/getTypeByIdsQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { getEntityTypeFromType } from "../../search/_internal/getEntityTypeFromType";
import { IQuery } from "../../search/types/IHubCatalog";

/**
* Get a query that can be used in a Gallery, and will return the associated
* entities, based on the AssociationType
*
* @param entity
* @param type
* @returns
*/
export function getTypeByIdsQuery(itemType: string, ids: string[]): IQuery {
const targetEntity = getEntityTypeFromType(itemType);

const qry: IQuery = {
targetEntity,
filters: [
{
operation: "AND",
predicates: [
{
type: itemType,
id: [...ids],
},
],
},
],
};
return qry;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { getEntityTypeFromType } from "../../search/_internal/getEntityTypeFromType";
import { IQuery } from "../../search/types/IHubCatalog";

/**
* @private
* Return an `IQuery` for a specific item type, with a specific typekeyword
* This is used internally to build queries for "Connected" entities
* @param itemType
* @param keyword
* @returns
*/
export function getTypeWithKeywordQuery(
itemType: string,
keyword: string
): IQuery {
const targetEntity = getEntityTypeFromType(itemType);

return {
targetEntity,
filters: [
{
operation: "AND",
predicates: [
{
type: itemType,
typekeywords: keyword,
},
],
},
],
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { getEntityTypeFromType } from "../../search/_internal/getEntityTypeFromType";
import { IQuery } from "../../search/types/IHubCatalog";

/**
* @private
* Return an `IQuery` for a specific item type, without a specific typekeyword
* This is used internally to build queries for "Not Connected" entities
* @param itemType
* @param keyword
* @returns
*/
export function getTypeWithoutKeywordQuery(
itemType: string,
keyword: string
): IQuery {
const targetEntity = getEntityTypeFromType(itemType);

return {
targetEntity,
filters: [
{
operation: "AND",
predicates: [
{
type: itemType,
typekeywords: { not: [keyword] },
},
],
},
],
};
}
36 changes: 35 additions & 1 deletion packages/common/src/core/HubItemEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@ import {
IWithStoreBehavior,
IWithFeaturedImageBehavior,
IWithPermissionBehavior,
IWithAssociationBehavior,
} from "./behaviors";

import { IWithThumbnailBehavior } from "./behaviors/IWithThumbnailBehavior";
import { IHubItemEntity, SettableAccessLevel } from "./types";
import { sharedWith } from "./_internal/sharedWith";
import { IWithDiscussionsBehavior } from "./behaviors/IWithDiscussionsBehavior";
import { setDiscussableKeyword } from "../discussions";
import { AssociationType, IAssociationInfo } from "../associations/types";
import { listAssociations } from "../associations/listAssociations";
import { addAssociation } from "../associations/addAssociation";
import { removeAssociation } from "../associations/removeAssociation";

const FEATURED_IMAGE_FILENAME = "featuredImage.png";

Expand All @@ -46,7 +51,8 @@ export abstract class HubItemEntity<T extends IHubItemEntity>
IWithThumbnailBehavior,
IWithFeaturedImageBehavior,
IWithPermissionBehavior,
IWithDiscussionsBehavior
IWithDiscussionsBehavior,
IWithAssociationBehavior
{
protected context: IArcGISContext;
protected entity: T;
Expand Down Expand Up @@ -383,4 +389,32 @@ export abstract class HubItemEntity<T extends IHubItemEntity>
);
this.update({ typeKeywords, isDiscussable } as Partial<T>);
}

/**
* Return a list of IAssociationInfo objects representing
* the associations this entity has, to the specified type
* @param type
* @returns
*/
listAssociations(type: AssociationType): IAssociationInfo[] {
return listAssociations(this.entity, type);
}

/**
* Add an association to this entity
* @param info
* @returns
*/
addAssociation(info: IAssociationInfo): void {
return addAssociation(info, this.entity);
}

/**
* Remove an association from this entity
* @param info
* @returns
*/
removeAssociation(info: IAssociationInfo): void {
return removeAssociation(info, this.entity);
}
}
25 changes: 25 additions & 0 deletions packages/common/src/core/behaviors/IWIthAssociationBehavior.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { AssociationType, IAssociationInfo } from "../../associations/types";

/**
* Composable behavior that adds permissions to an entity
*/
export interface IWithAssociationBehavior {
/**
* Get a list of the associations for an AssociationType
* @param type
*/
listAssociations(type: AssociationType): IAssociationInfo[];

/**
* Add an association to the entity.
* Entity needs to be saved after calling this method
* @param info
*/
addAssociation(info: IAssociationInfo): void;
/**
* Remove an association to the entity.
* Entity needs to be saved after calling this method
* @param info
*/
removeAssociation(info: IAssociationInfo): void;
}
1 change: 1 addition & 0 deletions packages/common/src/core/behaviors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from "./IWithEditorBehavior";
export * from "./IWithFeaturedImageBehavior";
export * from "./IWithVersioningBehavior";
export * from "./IWithCardBehavior";
export * from "./IWIthAssociationBehavior";
1 change: 1 addition & 0 deletions packages/common/src/core/traits/IWithAssociations.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export interface IWithAssociations {
typeKeywords?: string[];
[key: string]: any;
}
Loading

0 comments on commit f8aaee9

Please sign in to comment.