Skip to content

Commit

Permalink
feat(content): datasetToContent returns server, layer, and org enrich…
Browse files Browse the repository at this point in the history
…ments

affects: @esri/hub-content

also enrichContent is now tolerant of content not having errors
  • Loading branch information
tomwayson committed Jun 15, 2021
1 parent 8333f3c commit a689e1d
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 18 deletions.
23 changes: 22 additions & 1 deletion packages/common/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import { IItem, IUser, IGroup, IGeometry } from "@esri/arcgis-rest-types";
import {
IItem,
IUser,
IGroup,
IGeometry,
IFeatureServiceDefinition,
ILayerDefinition
} from "@esri/arcgis-rest-types";
import { IPortal, ISearchResult } from "@esri/arcgis-rest-portal";
import { IUserRequestOptions } from "@esri/arcgis-rest-auth";

Expand Down Expand Up @@ -341,11 +348,25 @@ export interface IHubContent extends IHubResource, IItem {
data?: {
[propName: string]: any;
};
// service types: Feature Service, Map Service
/** service information (currentVersion, capabilities, maxRecordCount etc) */
server?: Partial<IFeatureServiceDefinition>;
/** layer information (geometryType, fields, etc) for related layers in the service */
layers?: Array<Partial<ILayerDefinition>>;
// layer types: Feature Layers, Raster Layers
/** layer information (geometryType, fields, etc) */
layer?: Partial<ILayerDefinition>;
recordCount?: number;
// TODO: statistics?: ???
// NOTE: this is usually(? always?) returned by the item endpoint
// but it's not on IItem, possibly b/c it's not listed here:
// https://developers.arcgis.com/rest/users-groups-and-items/item.htm
/** The owner's organization id */
orgId?: string;
/**
* The owner's organization (portal) details (id, name, extent, etc)
*/
org?: Partial<IPortal>;
/** Whether the content is downloadable in the Hub app */
isDownloadable: boolean;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/content/src/enrichments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,13 +428,14 @@ export const enrichContent = (
// fetch any missing or requested enrichments
return fetchEnrichments(content, requestOptions).then(
(enrichments: Partial<IHubContent>) => {
const serverErrors = content.errors || [];
// merge derived portal URLs & fetched enrichments into content
const merged = {
...content,
...portalUrls,
...enrichments,
// include any previous errors (if any)
errors: [...content.errors, ...enrichments.errors]
errors: [...serverErrors, ...enrichments.errors]
};
// return the content with enriched dates
return _enrichDates(merged);
Expand Down
59 changes: 44 additions & 15 deletions packages/content/src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,38 +138,67 @@ export function datasetToContent(dataset: DatasetResource): IHubContent {
structuredLicense,
// map and feature server enrichments
server,
// TODO: layers, etc
layers,
// NOTE: the Hub API also returns the following server properties
// but we should be able to get them from the above server object
// currentVersion, capabilities, tileInfo, serviceSpatialReference
// maxRecordCount, supportedQueryFormats, etc
// feature and raster layer enrichments
layer
// TODO: recordCount, fields, geometryType, etc
layer,
recordCount,
statistics,
// NOTE: the Hub API also returns the following layer properties
// but we should be able to get them from the above layer object
// supportedQueryFormats, supportsAdvancedQueries, advancedQueryCapabilities, useStandardizedQueries
// geometryType, objectIdField, displayField, fields,
// org properties?
orgId,
orgName,
organization,
orgExtent
} = attributes;
// NOTE: we could throw or return if there are errors
// to prevent type errors trying to read properties below
content.errors = errors;
// common enrichments
content.boundary = boundary;
// setting this to null signals to enrichMetadata to skip this
content.metadata = metadata || null;
content.slug = slug;
content.groupIds = groupIds;
content.structuredLicense = structuredLicense;
content.layer = layer;
content.server = server;
if (!item.extent.length && extent && extent.coordinates) {
// we fall back to the extent derived by the API
// which prefers layer or service extents and ultimately
// falls back to the org's extent
content.extent = extent.coordinates;
}
if (searchDescription) {
// overwrite default summary (from snippet) w/ search description
content.summary = searchDescription;
}
// setting this to null signals to enrichMetadata to skip this
content.metadata = metadata || null;
if (content.modified !== modified) {
// capture the enriched modified date
// NOTE: the item modified date is still available on content.item.modified
content.modified = modified;
content.updatedDate = new Date(modified);
content.updatedDateSource = modifiedProvenance;
}
// TODO: any remaining enrichments?
content.slug = slug;
if (searchDescription) {
// overwrite default summary (from snippet) w/ search description
content.summary = searchDescription;
}
content.groupIds = groupIds;
content.structuredLicense = structuredLicense;
// server enrichments
content.server = server;
content.layers = layers;
// layer enrichments
content.layer = layer;
content.recordCount = recordCount;
content.statistics = statistics;
// org enrichments
if (orgId) {
content.org = {
id: orgId,
name: orgName || organization,
extent: orgExtent
};
}
return content;
}

Expand Down
1 change: 0 additions & 1 deletion packages/content/test/enrichments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ describe("enrichContent", () => {
const getItemGroupsSpy = spyOn(arcgisRestPortal, "getItemGroups");
const getContentMetadataSpy = spyOn(metadataModule, "getContentMetadata");
const content = {
errors: [],
id: "3ae",
// signature for a Hub created web map would trigger getUser
type: "Web Map",
Expand Down
22 changes: 22 additions & 0 deletions packages/content/test/hub.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,34 @@ describe("hub", () => {
expect(content.updatedDate).toEqual(new Date(attributes.modified));
expect(content.updatedDateSource).toBe(attributes.modifiedProvenance);
});
it("has org", () => {
const dataset = cloneObject(featureLayerJson.data) as DatasetResource;
const {
orgId: id,
orgExtent: extent,
orgName: name,
organization
} = dataset.attributes;
let content = datasetToContent(dataset);
expect(content.org).toEqual({ id, extent, name });
delete dataset.attributes.orgName;
content = datasetToContent(dataset);
expect(content.org).toEqual(
{ id, extent, name: organization },
"name falls back to organization"
);
});
it("only uses enrichment attributes when they exist", () => {
const dataset = cloneObject(documentsJson.data) as DatasetResource;
// NOTE: I don't necessarily expect the API to return w/o these
// but our code depends on them, this test is mostly here for coverage
delete dataset.attributes.searchDescription;
delete dataset.attributes.errors;
const content = datasetToContent(dataset);
expect(content.summary).toBe(dataset.attributes.snippet);
expect(content.extent).toEqual([]);
// NOTE: the document JSON does not have org attributes
expect(content.org).toBeUndefined();
});
// NOTE: other use cases are covered by getContent() tests
});
Expand Down

0 comments on commit a689e1d

Please sign in to comment.