Skip to content

Commit

Permalink
Merge pull request #286 from Esri/c/136998-change-method-signatures-t…
Browse files Browse the repository at this point in the history
…o-accept-ids-instead-of-models

136998 - Adjust method signatures to accept ids instead of models
  • Loading branch information
rweber-esri authored Jul 2, 2020
2 parents 3317a15 + ee06d1c commit 22577cb
Show file tree
Hide file tree
Showing 20 changed files with 172 additions and 169 deletions.
3 changes: 2 additions & 1 deletion packages/surveys/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* Copyright (c) 2020 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

export * from "./utils";
export * from "./items";
export * from "./sharing";
export * from "./utils";
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,29 @@
import { IRequestOptions } from "@esri/arcgis-rest-request";
import { getRelatedItems } from "@esri/arcgis-rest-portal";
import { IModel } from "@esri/hub-common";
import { isFieldworkerView } from "../utils/is-fieldworker-view";

/**
* Fetches a Survey's Stakeholder View for a given
* Fieldworker View ID
* @param {string} fieldworkerId The Fieldworker View ID
* Form ID
* @param {string} formId A Form ID
* @param {IRequestOptions} requestOptions The request options
* @returns {Promise<IFeatureServiceModel>}
* @returns {Promise<IModel>}
*/
export const getStakeholderModel = (
fieldworkerId: string,
formId: string,
requestOptions: IRequestOptions
): Promise<IModel> => {
return getRelatedItems({
id: fieldworkerId,
relationshipType: "Service2Service",
id: formId,
relationshipType: "Survey2Data",
direction: "forward",
...requestOptions
})
.then(({ relatedItems }) => {
const [featureService] = relatedItems.filter(
service => !isFieldworkerView(service)
);
.then(({ relatedItems: [stakeholderView] }) => {
let model;

if (featureService) {
model = { item: featureService };
if (stakeholderView) {
model = { item: stakeholderView } as IModel;
}

return model;
});
};
65 changes: 65 additions & 0 deletions packages/surveys/src/items/get-survey-models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* Copyright (c) 2020 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import { IRequestOptions } from "@esri/arcgis-rest-request";
import { getItem } from "@esri/arcgis-rest-portal";
import { IModel, IGetSurveyModelsResponse } from "@esri/hub-common";
import { getInputFeatureServiceModel } from "./get-input-feature-service-model";
import { getSourceFeatureServiceModelFromFieldworker } from "./get-source-feature-service-model-from-fieldworker";
import { getStakeholderModel } from "./get-stakeholder-model";
import { isFieldworkerView } from "../utils/is-fieldworker-view";

/**
* Builds a dictionary of Survey items for the given Form model
* @param {string} formId The Form ID of the survey
* @param {IRequestOptions} requestOptions The request options
* @returns {Promise<IGetSurveyModelsResponse>}
*/
export const getSurveyModels = (
formId: string,
requestOptions: IRequestOptions
): Promise<IGetSurveyModelsResponse> => {
let fieldworker: IModel;
let stakeholder: IModel;

return getItem(formId, requestOptions)
.then((item) => {
const promises: Array<Promise<IModel>> = [
// the primary input will be the fieldworker (if it exists), otherwise
// the source feature service.
getInputFeatureServiceModel(
formId,
requestOptions
),
getStakeholderModel(
formId,
requestOptions
)
];

return Promise.all(promises)
.then(([featureServiceOrFieldworkerModelResult, stakeholderResult]) => {
stakeholder = stakeholderResult;

if (isFieldworkerView(featureServiceOrFieldworkerModelResult.item)) {
fieldworker = featureServiceOrFieldworkerModelResult;
// if the primary input is the fieldworker, fetch
// the source feature service
return getSourceFeatureServiceModelFromFieldworker(
fieldworker.item.id,
requestOptions
);
} else {
return featureServiceOrFieldworkerModelResult;
}
})
.then((featureService) => {
return {
form: { item },
featureService,
fieldworker,
stakeholder
};
});
});
};
4 changes: 4 additions & 0 deletions packages/surveys/src/items/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from "./get-input-feature-service-model";
export * from "./get-source-feature-service-model-from-fieldworker";
export * from "./get-stakeholder-model";
export * from "./get-survey-models";
10 changes: 5 additions & 5 deletions packages/surveys/src/sharing/get-group-sharing-details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import { IRequestOptions } from "@esri/arcgis-rest-request";
import { getGroup } from "@esri/arcgis-rest-portal";
import { IModel, IGetGroupSharingDetailsResults } from "@esri/hub-common";
import { getSurveyModels } from "./get-survey-models";
import { IGetGroupSharingDetailsResults } from "@esri/hub-common";
import { getSurveyModels } from "../items/get-survey-models";
import { isPublished } from "../utils/is-published";

/**
Expand All @@ -13,13 +13,13 @@ import { isPublished } from "../utils/is-published";
* Form & Fieldworker View are targeted for published surveys and
* View groups. The Form, FeatureService, Fieldworker View & Stakeholder
* View are targeted for published surveys Update groups.
* @param {IModel} formModel A Form model
* @param {string} formId A Form ID
* @param {string} groupId A Group ID
* @param {IRequestOptions} requestOptions The request options
* @returns {Promise<IGetGroupSharingDetailsResults>}
*/
export const getGroupSharingDetails = (
formModel: IModel,
formId: string,
groupId: string,
requestOptions: IRequestOptions
): Promise<IGetGroupSharingDetailsResults> => {
Expand All @@ -28,7 +28,7 @@ export const getGroupSharingDetails = (
requestOptions
);
const surveyModelsPromise = getSurveyModels(
formModel,
formId,
requestOptions
);

Expand Down
61 changes: 0 additions & 61 deletions packages/surveys/src/sharing/get-survey-models.ts

This file was deleted.

4 changes: 0 additions & 4 deletions packages/surveys/src/sharing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
* Apache-2.0 */

export * from "./get-group-sharing-details";
export * from "./get-input-feature-service-model";
export * from "./get-source-feature-service-model-from-fieldworker";
export * from "./get-stakeholder-model";
export * from "./get-survey-models";
export * from "./set-access";
export * from "./set-access-revertable";
export * from "./share-with-group";
Expand Down
12 changes: 6 additions & 6 deletions packages/surveys/src/sharing/set-access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@

import { IRequestOptions } from "@esri/arcgis-rest-request";
import { processRevertableTasks, IRevertableTaskResult, IModel } from "@esri/hub-common";
import { getSurveyModels } from "./get-survey-models";
import { getSurveyModels } from "../items/get-survey-models";
import { isPublished } from "../utils/is-published";
import { setAccessRevertable } from "./set-access-revertable";

/**
* Sets eligible Survey items to the provided access
* @param {IModel} formModel A Form model
* @param {string} formId A Form ID
* @param {string} access The desired access
* @param {IrequestOptions} requestOptions
* @returns {Promise<any[]>}
*/
export const setAccess = (
formModel: IModel,
formId: string,
access: "private" | "public" | "org",
requestOptions: IRequestOptions
): Promise<any[]> => {
return getSurveyModels(formModel, requestOptions)
return getSurveyModels(formId, requestOptions)
.then(({ form, featureService, fieldworker }) => {
const modelsToChangeAccess = [form, featureService];
if (isPublished(formModel.item)) {
if (isPublished(form.item)) {
modelsToChangeAccess.push(fieldworker);
}
const toRevertablePromise = (memo: Promise<IRevertableTaskResult>[], model: IModel) => {
Expand All @@ -41,6 +41,6 @@ export const setAccess = (
return processRevertableTasks(revertableTasks);
})
.catch(() => {
throw new Error(`Failed to set survey ${formModel.item.id} items access to ${access}`);
throw new Error(`Failed to set survey ${formId} items access to ${access}`);
});
};
12 changes: 6 additions & 6 deletions packages/surveys/src/sharing/share-with-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@
* Apache-2.0 */

import { IRequestOptions } from "@esri/arcgis-rest-request";
import { IModel, processRevertableTasks } from "@esri/hub-common";
import { processRevertableTasks } from "@esri/hub-common";
import { getGroupSharingDetails } from "./get-group-sharing-details";
import { shareWithGroupRevertable } from "./share-with-group-revertable";

/**
* Shares eligible Survey items for the provided Form model
* Shares eligible Survey items for the provided formId
* with the provided groupId
* @param {IModel} formModel A Form model
* @param {string} formId A Form ID
* @param {string} groupId A group ID
* @param {IRequestOptions} requestOptions The request options
* @returns {Promise<any[]>}
*/
export const shareWithGroup = (
formModel: IModel,
formId: string,
groupId: string,
requestOptions: IRequestOptions
): Promise<any[]> => {
return getGroupSharingDetails(formModel, groupId, requestOptions)
return getGroupSharingDetails(formId, groupId, requestOptions)
.then(({ modelsToShare, group }) => {
const revertableTasks = modelsToShare.map(
(model) => shareWithGroupRevertable(
Expand All @@ -31,6 +31,6 @@ export const shareWithGroup = (
return processRevertableTasks(revertableTasks);;
})
.catch(() => {
throw new Error(`Failed to share survey ${formModel.item.id} items to group ${groupId}`);
throw new Error(`Failed to share survey ${formId} items to group ${groupId}`);
});
};
10 changes: 5 additions & 5 deletions packages/surveys/src/sharing/unshare-with-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import { getGroupSharingDetails } from "./get-group-sharing-details";
import { unshareWithGroupRevertable } from "./unshare-with-group-revertable";

/**
* Unshares eligible Survey items for the provided Form model
* Unshares eligible Survey items for the provided Form ID
* from the provided groupId
* @param {IModel} formModel A Form model
* @param {string} formId A Form ID
* @param {string} groupId A group ID
* @param {IRequestOptions} requestOptions The request options
* @returns {Promise<any[]>}
*/
export const unshareWithGroup = (
formModel: IModel,
formId: string,
groupId: string,
requestOptions: IRequestOptions
): Promise<any[]> => {
return getGroupSharingDetails(formModel, groupId, requestOptions)
return getGroupSharingDetails(formId, groupId, requestOptions)
.then(({ modelsToShare, group }) => {
const revertableTasks = modelsToShare.map(
(model) => unshareWithGroupRevertable(
Expand All @@ -31,6 +31,6 @@ export const unshareWithGroup = (
return processRevertableTasks(revertableTasks);
})
.catch(() => {
throw new Error(`Failed to unshare survey ${formModel.item.id} items with group ${groupId}`);
throw new Error(`Failed to unshare survey ${formId} items with group ${groupId}`);
});
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import { getInputFeatureServiceModel } from "../../src/sharing/get-input-feature-service-model";
import { getInputFeatureServiceModel } from "../../src/items/get-input-feature-service-model";
import { FieldworkerItem } from "../mocks/fieldworker-item";
import * as restPortal from "@esri/arcgis-rest-portal";
import { mockUserSession } from "@esri/hub-common/test/test-helpers/fake-user-session";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import { getSourceFeatureServiceModelFromFieldworker } from "../../src/sharing/get-source-feature-service-model-from-fieldworker";
import { getSourceFeatureServiceModelFromFieldworker } from "../../src/items/get-source-feature-service-model-from-fieldworker";
import { FeatureServiceItem } from "../mocks/feature-service-item";
import * as restPortal from "@esri/arcgis-rest-portal";
import { mockUserSession } from "@esri/hub-common/test/test-helpers/fake-user-session";
Expand Down
Loading

0 comments on commit 22577cb

Please sign in to comment.