From 11958ed8533259a9dbffd921fa41bd224290d4dc Mon Sep 17 00:00:00 2001 From: Randy Weber Date: Wed, 1 Jul 2020 15:47:47 -0400 Subject: [PATCH 1/5] 136998 - Adjust method signatures to accept ids instead of models --- packages/surveys/src/index.ts | 3 +- .../get-input-feature-service-model.ts | 0 ...-feature-service-model-from-fieldworker.ts | 0 .../get-stakeholder-model.ts | 24 +++---- .../surveys/src/items/get-survey-models.ts | 65 +++++++++++++++++++ packages/surveys/src/items/index.ts | 4 ++ .../src/sharing/get-group-sharing-details.ts | 10 +-- .../surveys/src/sharing/get-survey-models.ts | 61 ----------------- packages/surveys/src/sharing/index.ts | 4 -- packages/surveys/src/sharing/set-access.ts | 12 ++-- .../surveys/src/sharing/share-with-group.ts | 12 ++-- .../surveys/src/sharing/unshare-with-group.ts | 10 +-- .../get-input-feature-service-model.test.ts | 2 +- ...ure-service-model-from-fieldworker.test.ts | 2 +- .../get-stakeholder-model.test.ts | 19 ++---- .../get-survey-models.test.ts | 34 ++++++---- .../sharing/get-group-sharing-details.test.ts | 45 +++++++------ .../surveys/test/sharing/set-access.test.ts | 18 ++--- .../test/sharing/share-with-group.test.ts | 8 +-- .../test/sharing/unshare-with-group.test.ts | 8 +-- 20 files changed, 172 insertions(+), 169 deletions(-) rename packages/surveys/src/{sharing => items}/get-input-feature-service-model.ts (100%) rename packages/surveys/src/{sharing => items}/get-source-feature-service-model-from-fieldworker.ts (100%) rename packages/surveys/src/{sharing => items}/get-stakeholder-model.ts (55%) create mode 100644 packages/surveys/src/items/get-survey-models.ts create mode 100644 packages/surveys/src/items/index.ts delete mode 100644 packages/surveys/src/sharing/get-survey-models.ts rename packages/surveys/test/{sharing => items}/get-input-feature-service-model.test.ts (94%) rename packages/surveys/test/{sharing => items}/get-source-feature-service-model-from-fieldworker.test.ts (96%) rename packages/surveys/test/{sharing => items}/get-stakeholder-model.test.ts (60%) rename packages/surveys/test/{sharing => items}/get-survey-models.test.ts (68%) diff --git a/packages/surveys/src/index.ts b/packages/surveys/src/index.ts index 54dc4f796d6..9cb86bd5ed3 100644 --- a/packages/surveys/src/index.ts +++ b/packages/surveys/src/index.ts @@ -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"; diff --git a/packages/surveys/src/sharing/get-input-feature-service-model.ts b/packages/surveys/src/items/get-input-feature-service-model.ts similarity index 100% rename from packages/surveys/src/sharing/get-input-feature-service-model.ts rename to packages/surveys/src/items/get-input-feature-service-model.ts diff --git a/packages/surveys/src/sharing/get-source-feature-service-model-from-fieldworker.ts b/packages/surveys/src/items/get-source-feature-service-model-from-fieldworker.ts similarity index 100% rename from packages/surveys/src/sharing/get-source-feature-service-model-from-fieldworker.ts rename to packages/surveys/src/items/get-source-feature-service-model-from-fieldworker.ts diff --git a/packages/surveys/src/sharing/get-stakeholder-model.ts b/packages/surveys/src/items/get-stakeholder-model.ts similarity index 55% rename from packages/surveys/src/sharing/get-stakeholder-model.ts rename to packages/surveys/src/items/get-stakeholder-model.ts index f9a4d8ebf5e..a8f3154b1b5 100644 --- a/packages/surveys/src/sharing/get-stakeholder-model.ts +++ b/packages/surveys/src/items/get-stakeholder-model.ts @@ -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} + * @returns {Promise} */ export const getStakeholderModel = ( - fieldworkerId: string, + formId: string, requestOptions: IRequestOptions ): Promise => { 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; }); }; diff --git a/packages/surveys/src/items/get-survey-models.ts b/packages/surveys/src/items/get-survey-models.ts new file mode 100644 index 00000000000..c27927143bf --- /dev/null +++ b/packages/surveys/src/items/get-survey-models.ts @@ -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} + */ +export const getSurveyModels = ( + formId: string, + requestOptions: IRequestOptions +): Promise => { + let fieldworker: IModel; + let stakeholder: IModel; + + return getItem(formId, requestOptions) + .then((item) => { + const promises: Array> = [ + // 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 + }; + }); + }); +}; diff --git a/packages/surveys/src/items/index.ts b/packages/surveys/src/items/index.ts new file mode 100644 index 00000000000..7d28436ec2e --- /dev/null +++ b/packages/surveys/src/items/index.ts @@ -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"; \ No newline at end of file diff --git a/packages/surveys/src/sharing/get-group-sharing-details.ts b/packages/surveys/src/sharing/get-group-sharing-details.ts index dbadf066c3d..b7a3f4404dc 100644 --- a/packages/surveys/src/sharing/get-group-sharing-details.ts +++ b/packages/surveys/src/sharing/get-group-sharing-details.ts @@ -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"; /** @@ -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} */ export const getGroupSharingDetails = ( - formModel: IModel, + formId: string, groupId: string, requestOptions: IRequestOptions ): Promise => { @@ -28,7 +28,7 @@ export const getGroupSharingDetails = ( requestOptions ); const surveyModelsPromise = getSurveyModels( - formModel, + formId, requestOptions ); diff --git a/packages/surveys/src/sharing/get-survey-models.ts b/packages/surveys/src/sharing/get-survey-models.ts deleted file mode 100644 index 0de0e3438ce..00000000000 --- a/packages/surveys/src/sharing/get-survey-models.ts +++ /dev/null @@ -1,61 +0,0 @@ -/* Copyright (c) 2020 Environmental Systems Research Institute, Inc. - * Apache-2.0 */ - -import { IRequestOptions } from "@esri/arcgis-rest-request"; -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 {IModel} formModel The Form model of the survey - * @param {IRequestOptions} requestOptions The request options - * @returns {Promise} - */ -export const getSurveyModels = ( - formModel: IModel, - requestOptions: IRequestOptions -): Promise => { - let featureService: IModel; - let fieldworker: IModel; - - // the primary input will be the fieldworker (if it exists), otherwise - // the source feature service. - return getInputFeatureServiceModel( - formModel.item.id, - requestOptions - ) - .then((featureServiceOrFieldworkerModelResult) => { - if (isFieldworkerView(featureServiceOrFieldworkerModelResult.item)) { - fieldworker = featureServiceOrFieldworkerModelResult; - } else { - featureService = featureServiceOrFieldworkerModelResult; - } - - // if the primary input is the fieldworker, fetch - // the source feature service - if (fieldworker) { - return getSourceFeatureServiceModelFromFieldworker( - fieldworker.item.id, - requestOptions - ) - .then((featureServiceModelResult) => { - featureService = featureServiceModelResult; - }); - } - }) - .then(() => getStakeholderModel( - featureService.item.id, - requestOptions - )) - .then(stakeholder => { - return { - form: formModel, - featureService, - fieldworker, - stakeholder - }; - }); -}; diff --git a/packages/surveys/src/sharing/index.ts b/packages/surveys/src/sharing/index.ts index 9dc20b82ac9..577fa618a5c 100644 --- a/packages/surveys/src/sharing/index.ts +++ b/packages/surveys/src/sharing/index.ts @@ -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"; diff --git a/packages/surveys/src/sharing/set-access.ts b/packages/surveys/src/sharing/set-access.ts index 33fba53cb54..00a43d90688 100644 --- a/packages/surveys/src/sharing/set-access.ts +++ b/packages/surveys/src/sharing/set-access.ts @@ -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} */ export const setAccess = ( - formModel: IModel, + formId: string, access: "private" | "public" | "org", requestOptions: IRequestOptions ): Promise => { - 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[], model: IModel) => { @@ -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}`); }); }; diff --git a/packages/surveys/src/sharing/share-with-group.ts b/packages/surveys/src/sharing/share-with-group.ts index 323b5af010d..d4749a15874 100644 --- a/packages/surveys/src/sharing/share-with-group.ts +++ b/packages/surveys/src/sharing/share-with-group.ts @@ -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} */ export const shareWithGroup = ( - formModel: IModel, + formId: string, groupId: string, requestOptions: IRequestOptions ): Promise => { - return getGroupSharingDetails(formModel, groupId, requestOptions) + return getGroupSharingDetails(formId, groupId, requestOptions) .then(({ modelsToShare, group }) => { const revertableTasks = modelsToShare.map( (model) => shareWithGroupRevertable( @@ -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}`); }); }; diff --git a/packages/surveys/src/sharing/unshare-with-group.ts b/packages/surveys/src/sharing/unshare-with-group.ts index 9472fb11575..e59d38e79aa 100644 --- a/packages/surveys/src/sharing/unshare-with-group.ts +++ b/packages/surveys/src/sharing/unshare-with-group.ts @@ -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} */ export const unshareWithGroup = ( - formModel: IModel, + formId: string, groupId: string, requestOptions: IRequestOptions ): Promise => { - return getGroupSharingDetails(formModel, groupId, requestOptions) + return getGroupSharingDetails(formId, groupId, requestOptions) .then(({ modelsToShare, group }) => { const revertableTasks = modelsToShare.map( (model) => unshareWithGroupRevertable( @@ -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}`); }); }; diff --git a/packages/surveys/test/sharing/get-input-feature-service-model.test.ts b/packages/surveys/test/items/get-input-feature-service-model.test.ts similarity index 94% rename from packages/surveys/test/sharing/get-input-feature-service-model.test.ts rename to packages/surveys/test/items/get-input-feature-service-model.test.ts index 20d9203be29..88db29fa7d9 100644 --- a/packages/surveys/test/sharing/get-input-feature-service-model.test.ts +++ b/packages/surveys/test/items/get-input-feature-service-model.test.ts @@ -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"; diff --git a/packages/surveys/test/sharing/get-source-feature-service-model-from-fieldworker.test.ts b/packages/surveys/test/items/get-source-feature-service-model-from-fieldworker.test.ts similarity index 96% rename from packages/surveys/test/sharing/get-source-feature-service-model-from-fieldworker.test.ts rename to packages/surveys/test/items/get-source-feature-service-model-from-fieldworker.test.ts index 702541f032e..6b1df4a9491 100644 --- a/packages/surveys/test/sharing/get-source-feature-service-model-from-fieldworker.test.ts +++ b/packages/surveys/test/items/get-source-feature-service-model-from-fieldworker.test.ts @@ -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"; diff --git a/packages/surveys/test/sharing/get-stakeholder-model.test.ts b/packages/surveys/test/items/get-stakeholder-model.test.ts similarity index 60% rename from packages/surveys/test/sharing/get-stakeholder-model.test.ts rename to packages/surveys/test/items/get-stakeholder-model.test.ts index a9ce30d99ac..4f4d38e9086 100644 --- a/packages/surveys/test/sharing/get-stakeholder-model.test.ts +++ b/packages/surveys/test/items/get-stakeholder-model.test.ts @@ -3,10 +3,8 @@ import * as restPortal from "@esri/arcgis-rest-portal"; import { mockUserSession } from "@esri/hub-common/test/test-helpers/fake-user-session"; -import { getStakeholderModel } from "../../src/sharing/get-stakeholder-model"; -import { FieldworkerItem } from "../mocks/fieldworker-item"; +import { getStakeholderModel } from "../../src/items/get-stakeholder-model"; import { StakeholderItem } from "../mocks/stakeholder-item"; -import * as isFieldworkerView from "../../src/utils/is-fieldworker-view"; describe("getStakeholderModel", function () { let getRelatedItemsResponse: restPortal.IGetRelatedItemsResponse; @@ -14,41 +12,34 @@ describe("getStakeholderModel", function () { beforeEach(() => { getRelatedItemsResponse = { total: 2, - relatedItems: [FieldworkerItem, StakeholderItem] + relatedItems: [StakeholderItem] }; }); it("should resolve undefined when getRelatedItems returns no Stakeholder", async function () { getRelatedItemsResponse.relatedItems.pop(); const getRelatedItemsSpy = spyOn(restPortal, "getRelatedItems").and.returnValue(Promise.resolve(getRelatedItemsResponse)); - const isFieldworkerViewSpy = spyOn(isFieldworkerView, "isFieldworkerView").and.returnValues(true); const result = await getStakeholderModel("123", mockUserSession); expect(getRelatedItemsSpy.calls.count()).toBe(1); expect(getRelatedItemsSpy.calls.argsFor(0)).toEqual([{ id: "123", - relationshipType: "Service2Service", + relationshipType: "Survey2Data", direction: "forward", ...mockUserSession }]); - expect(isFieldworkerViewSpy.calls.count()).toBe(1); - expect(isFieldworkerViewSpy.calls.argsFor(0)).toEqual([FieldworkerItem]); expect(result).toBeUndefined(); }); - it("should resolve an IModel when getRelatedItems returns a Fieldworker", async function () { + it("should resolve an IModel when getRelatedItems returns a Stakeholder", async function () { const getRelatedItemsSpy = spyOn(restPortal, "getRelatedItems").and.returnValue(Promise.resolve(getRelatedItemsResponse)); - const isFieldworkerViewSpy = spyOn(isFieldworkerView, "isFieldworkerView").and.returnValues(true, false); const result = await getStakeholderModel("123", mockUserSession); expect(getRelatedItemsSpy.calls.count()).toBe(1); expect(getRelatedItemsSpy.calls.argsFor(0)).toEqual([{ id: "123", - relationshipType: "Service2Service", + relationshipType: "Survey2Data", direction: "forward", ...mockUserSession }]); - expect(isFieldworkerViewSpy.calls.count()).toBe(2); - expect(isFieldworkerViewSpy.calls.argsFor(0)).toEqual([FieldworkerItem]); - expect(isFieldworkerViewSpy.calls.argsFor(1)).toEqual([StakeholderItem]); const expected = { item: StakeholderItem }; expect(result).toEqual(expected); }); diff --git a/packages/surveys/test/sharing/get-survey-models.test.ts b/packages/surveys/test/items/get-survey-models.test.ts similarity index 68% rename from packages/surveys/test/sharing/get-survey-models.test.ts rename to packages/surveys/test/items/get-survey-models.test.ts index f4db008069e..846570c0d20 100644 --- a/packages/surveys/test/sharing/get-survey-models.test.ts +++ b/packages/surveys/test/items/get-survey-models.test.ts @@ -2,63 +2,73 @@ * Apache-2.0 */ import { IModel } from "@esri/hub-common"; +import * as restPortal from "@esri/arcgis-rest-portal"; import { mockUserSession } from "@esri/hub-common/test/test-helpers/fake-user-session"; -import { getSurveyModels } from "../../src/sharing/get-survey-models"; +import { getSurveyModels } from "../../src/items/get-survey-models"; import { FormItemPublished } from "../mocks/form-item-published"; import { FeatureServiceItem } from "../mocks/feature-service-item"; import { FieldworkerItem } from "../mocks/fieldworker-item"; import { StakeholderItem } from "../mocks/stakeholder-item"; import * as isFieldworkerView from "../../src/utils/is-fieldworker-view"; -import * as getInputFeatureServiceModel from "../../src/sharing/get-input-feature-service-model"; -import * as getSourceFeatureServiceModelFromFieldworker from "../../src/sharing/get-source-feature-service-model-from-fieldworker"; -import * as getStakeholderModel from "../../src/sharing/get-stakeholder-model"; +import * as getInputFeatureServiceModel from "../../src/items/get-input-feature-service-model"; +import * as getSourceFeatureServiceModelFromFieldworker from "../../src/items/get-source-feature-service-model-from-fieldworker"; +import * as getStakeholderModel from "../../src/items/get-stakeholder-model"; +import { IRequestOptions } from "@esri/hub-common/node_modules/@esri/arcgis-rest-request"; describe("getSurveyModels", function () { let formModel: IModel; let featureServiceModel: IModel; let fieldworkerModel: IModel; let stakeholderModel: IModel; + let requestOptions: IRequestOptions; beforeEach(() => { formModel = { item: FormItemPublished }; featureServiceModel = { item: FeatureServiceItem }; fieldworkerModel = { item: FieldworkerItem }; stakeholderModel = { item: StakeholderItem }; + requestOptions = { authentication: mockUserSession }; }); it("fetches the feature service when a fieldworker is the primary input service", async function () { + const getItemStub = spyOn(restPortal, "getItem").and.returnValue(Promise.resolve(FormItemPublished)); const isFieldworkerViewSpy = spyOn(isFieldworkerView, "isFieldworkerView").and.returnValue(true); const getInputFeatureServiceModelSpy = spyOn(getInputFeatureServiceModel, "getInputFeatureServiceModel").and.returnValue(Promise.resolve(fieldworkerModel)); const getSourceFeatureServiceModelFromFieldworkerSpy = spyOn(getSourceFeatureServiceModelFromFieldworker, "getSourceFeatureServiceModelFromFieldworker").and.returnValue(Promise.resolve(featureServiceModel)); const getStakeholderModelSpy = spyOn(getStakeholderModel, "getStakeholderModel").and.returnValue(Promise.resolve(stakeholderModel)); - const results = await getSurveyModels(formModel, mockUserSession); + const results = await getSurveyModels(formModel.item.id, requestOptions); + expect(getItemStub.calls.count()).toBe(1); + expect(getItemStub.calls.argsFor(0)).toEqual([FormItemPublished.id, requestOptions]); expect(getInputFeatureServiceModelSpy.calls.count()).toBe(1); - expect(getInputFeatureServiceModelSpy.calls.argsFor(0)).toEqual([formModel.item.id, mockUserSession]); + expect(getInputFeatureServiceModelSpy.calls.argsFor(0)).toEqual([formModel.item.id, requestOptions]); expect(getSourceFeatureServiceModelFromFieldworkerSpy.calls.count()).toBe(1); - expect(getSourceFeatureServiceModelFromFieldworkerSpy.calls.argsFor(0)).toEqual([FieldworkerItem.id, mockUserSession]); + expect(getSourceFeatureServiceModelFromFieldworkerSpy.calls.argsFor(0)).toEqual([FieldworkerItem.id, requestOptions]); expect(isFieldworkerViewSpy.calls.count()).toBe(1); expect(isFieldworkerViewSpy.calls.argsFor(0)).toEqual([fieldworkerModel.item]); expect(getStakeholderModelSpy.calls.count()).toBe(1); - expect(getStakeholderModelSpy.calls.argsFor(0)).toEqual([FeatureServiceItem.id, mockUserSession]); + expect(getStakeholderModelSpy.calls.argsFor(0)).toEqual([FormItemPublished.id, requestOptions]); expect(results.form).toEqual(formModel); expect(results.featureService).toEqual(featureServiceModel); expect(results.fieldworker).toEqual(fieldworkerModel); expect(results.stakeholder).toEqual(stakeholderModel); }); - it("fetches does not attempt to fetch the feature service when a fieldworker is absent", async function () { + it("does not attempt to fetch the feature service when a fieldworker is absent", async function () { + const getItemStub = spyOn(restPortal, "getItem").and.returnValue(Promise.resolve(FormItemPublished)); const isFieldworkerViewSpy = spyOn(isFieldworkerView, "isFieldworkerView").and.returnValue(false); const getInputFeatureServiceModelSpy = spyOn(getInputFeatureServiceModel, "getInputFeatureServiceModel").and.returnValue(Promise.resolve(featureServiceModel)); const getSourceFeatureServiceModelFromFieldworkerSpy = spyOn(getSourceFeatureServiceModelFromFieldworker, "getSourceFeatureServiceModelFromFieldworker"); const getStakeholderModelSpy = spyOn(getStakeholderModel, "getStakeholderModel").and.returnValue(Promise.resolve()); - const results = await getSurveyModels(formModel, mockUserSession); + const results = await getSurveyModels(formModel.item.id, requestOptions); + expect(getItemStub.calls.count()).toBe(1); + expect(getItemStub.calls.argsFor(0)).toEqual([FormItemPublished.id, requestOptions]); expect(getInputFeatureServiceModelSpy.calls.count()).toBe(1); - expect(getInputFeatureServiceModelSpy.calls.argsFor(0)).toEqual([formModel.item.id, mockUserSession]); + expect(getInputFeatureServiceModelSpy.calls.argsFor(0)).toEqual([formModel.item.id, requestOptions]); expect(getSourceFeatureServiceModelFromFieldworkerSpy.calls.count()).toBe(0); expect(isFieldworkerViewSpy.calls.count()).toBe(1); expect(isFieldworkerViewSpy.calls.argsFor(0)).toEqual([featureServiceModel.item]); expect(getStakeholderModelSpy.calls.count()).toBe(1); - expect(getStakeholderModelSpy.calls.argsFor(0)).toEqual([FeatureServiceItem.id, mockUserSession]); + expect(getStakeholderModelSpy.calls.argsFor(0)).toEqual([FormItemPublished.id, requestOptions]); expect(results.form).toEqual(formModel); expect(results.featureService).toEqual(featureServiceModel); expect(results.fieldworker).toBeUndefined(); diff --git a/packages/surveys/test/sharing/get-group-sharing-details.test.ts b/packages/surveys/test/sharing/get-group-sharing-details.test.ts index 43fc47b951c..13636089754 100644 --- a/packages/surveys/test/sharing/get-group-sharing-details.test.ts +++ b/packages/surveys/test/sharing/get-group-sharing-details.test.ts @@ -12,8 +12,9 @@ import { UpdateGroup } from "../mocks/update-group"; import { ViewGroup } from "../mocks/view-group"; import * as restPortal from "@esri/arcgis-rest-portal"; import { mockUserSession } from "@esri/hub-common/test/test-helpers/fake-user-session"; -import * as getSurveyModels from "../../src/sharing/get-survey-models"; +import * as getSurveyModels from "../../src/items/get-survey-models"; import * as isPublished from "../../src/utils/is-published"; +import { IRequestOptions } from "@esri/hub-common/node_modules/@esri/arcgis-rest-request"; describe("getGroupSharingDetails", function () { let getSurveyModelsResult: IGetSurveyModelsResponse; @@ -21,6 +22,7 @@ describe("getGroupSharingDetails", function () { let featureServiceModel: IModel; let fieldworkerModel: IModel; let stakeholderModel: IModel; + let requestOptions: IRequestOptions; beforeEach(() => { formModel = { item: FormItemDraft }; @@ -33,6 +35,7 @@ describe("getGroupSharingDetails", function () { fieldworker: fieldworkerModel, stakeholder: stakeholderModel }; + requestOptions = { authentication: mockUserSession }; }); it("should resolve with the form & feature service models when a draft and an update group", async function () { @@ -40,14 +43,14 @@ describe("getGroupSharingDetails", function () { const getSurveyModelsSpy = spyOn(getSurveyModels, "getSurveyModels").and.returnValue(Promise.resolve(getSurveyModelsResult)); const isPublishedSpy = spyOn(isPublished, "isPublished").and.returnValue(false); const result = await getGroupSharingDetails( - formModel, + formModel.item.id, "123", - mockUserSession + requestOptions ); expect(getGroupSpy.calls.count()).toBe(1); - expect(getGroupSpy.calls.argsFor(0)).toEqual(["123", mockUserSession]); + expect(getGroupSpy.calls.argsFor(0)).toEqual(["123", requestOptions]); expect(getSurveyModelsSpy.calls.count()).toBe(1); - expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([formModel, mockUserSession]); + expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([formModel.item.id, requestOptions]); expect(isPublishedSpy.calls.count()).toBe(1); expect(isPublishedSpy.calls.argsFor(0)).toEqual([formModel.item]); expect(result).toEqual({ @@ -61,14 +64,14 @@ describe("getGroupSharingDetails", function () { const getSurveyModelsSpy = spyOn(getSurveyModels, "getSurveyModels").and.returnValue(Promise.resolve(getSurveyModelsResult)); const isPublishedSpy = spyOn(isPublished, "isPublished").and.returnValue(false); const result = await getGroupSharingDetails( - formModel, + formModel.item.id, "123", - mockUserSession + requestOptions ); expect(getGroupSpy.calls.count()).toBe(1); - expect(getGroupSpy.calls.argsFor(0)).toEqual(["123", mockUserSession]); + expect(getGroupSpy.calls.argsFor(0)).toEqual(["123", requestOptions]); expect(getSurveyModelsSpy.calls.count()).toBe(1); - expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([formModel, mockUserSession]); + expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([formModel.item.id, requestOptions]); expect(isPublishedSpy.calls.count()).toBe(1); expect(isPublishedSpy.calls.argsFor(0)).toEqual([formModel.item]); expect(result).toEqual({ @@ -84,14 +87,14 @@ describe("getGroupSharingDetails", function () { const getSurveyModelsSpy = spyOn(getSurveyModels, "getSurveyModels").and.returnValue(Promise.resolve(getSurveyModelsResult)); const isPublishedSpy = spyOn(isPublished, "isPublished").and.returnValue(true); const result = await getGroupSharingDetails( - formModel, + formModel.item.id, "123", - mockUserSession + requestOptions ); expect(getGroupSpy.calls.count()).toBe(1); - expect(getGroupSpy.calls.argsFor(0)).toEqual(["123", mockUserSession]); + expect(getGroupSpy.calls.argsFor(0)).toEqual(["123", requestOptions]); expect(getSurveyModelsSpy.calls.count()).toBe(1); - expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([formModel, mockUserSession]); + expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([formModel.item.id, requestOptions]); expect(isPublishedSpy.calls.count()).toBe(1); expect(isPublishedSpy.calls.argsFor(0)).toEqual([formModel.item]); expect(result).toEqual({ @@ -107,14 +110,14 @@ describe("getGroupSharingDetails", function () { const getSurveyModelsSpy = spyOn(getSurveyModels, "getSurveyModels").and.returnValue(Promise.resolve(getSurveyModelsResult)); const isPublishedSpy = spyOn(isPublished, "isPublished").and.returnValue(true); const result = await getGroupSharingDetails( - formModel, + formModel.item.id, "123", - mockUserSession + requestOptions ); expect(getGroupSpy.calls.count()).toBe(1); - expect(getGroupSpy.calls.argsFor(0)).toEqual(["123", mockUserSession]); + expect(getGroupSpy.calls.argsFor(0)).toEqual(["123", requestOptions]); expect(getSurveyModelsSpy.calls.count()).toBe(1); - expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([formModel, mockUserSession]); + expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([formModel.item.id, requestOptions]); expect(isPublishedSpy.calls.count()).toBe(1); expect(isPublishedSpy.calls.argsFor(0)).toEqual([formModel.item]); expect(result).toEqual({ @@ -131,14 +134,14 @@ describe("getGroupSharingDetails", function () { const getSurveyModelsSpy = spyOn(getSurveyModels, "getSurveyModels").and.returnValue(Promise.resolve(getSurveyModelsResult)); const isPublishedSpy = spyOn(isPublished, "isPublished").and.returnValue(true); const result = await getGroupSharingDetails( - formModel, + formModel.item.id, "123", - mockUserSession + requestOptions ); expect(getGroupSpy.calls.count()).toBe(1); - expect(getGroupSpy.calls.argsFor(0)).toEqual(["123", mockUserSession]); + expect(getGroupSpy.calls.argsFor(0)).toEqual(["123", requestOptions]); expect(getSurveyModelsSpy.calls.count()).toBe(1); - expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([formModel, mockUserSession]); + expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([formModel.item.id, requestOptions]); expect(isPublishedSpy.calls.count()).toBe(1); expect(isPublishedSpy.calls.argsFor(0)).toEqual([formModel.item]); expect(result).toEqual({ diff --git a/packages/surveys/test/sharing/set-access.test.ts b/packages/surveys/test/sharing/set-access.test.ts index 3bed0841e98..e6ffe33bffb 100644 --- a/packages/surveys/test/sharing/set-access.test.ts +++ b/packages/surveys/test/sharing/set-access.test.ts @@ -10,7 +10,7 @@ import { FieldworkerItem } from "../mocks/fieldworker-item"; import { StakeholderItem } from "../mocks/stakeholder-item"; import { setAccess } from "../../src/sharing/set-access"; import * as setAccessRevertable from "../../src/sharing/set-access-revertable"; -import * as getSurveyModels from "../../src/sharing/get-survey-models"; +import * as getSurveyModels from "../../src/items/get-survey-models"; import * as isPublished from "../../src/utils/is-published"; describe("setAccess", function () { @@ -67,12 +67,12 @@ describe("setAccess", function () { const setAccessRevertableSpy = spyOn(setAccessRevertable, "setAccessRevertable").and.returnValues(...setAccessRevertablePromiseResults); const processRevertableTasksSpy = spyOn(hubCommon, "processRevertableTasks").and.returnValue(Promise.resolve(processRevertableTasksResults)); const results = await setAccess( - formModel, + formModel.item.id, "public", requestOptions ); expect(getSurveyModelsSpy.calls.count()).toBe(1); - expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([formModel, requestOptions]); + expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([FormItemPublished.id, requestOptions]); expect(isPublishedSpy.calls.count()).toBe(1); expect(isPublishedSpy.calls.argsFor(0)).toEqual([formModel.item]); expect(setAccessRevertableSpy.calls.count()).toBe(3); @@ -93,12 +93,12 @@ describe("setAccess", function () { const setAccessRevertableSpy = spyOn(setAccessRevertable, "setAccessRevertable").and.returnValues(...setAccessRevertablePromiseResults); const processRevertableTasksSpy = spyOn(hubCommon, "processRevertableTasks").and.returnValue(Promise.resolve(processRevertableTasksResults)); const results = await setAccess( - formModel, + formModel.item.id, "public", requestOptions ); expect(getSurveyModelsSpy.calls.count()).toBe(1); - expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([formModel, requestOptions]); + expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([FormItemPublished.id, requestOptions]); expect(isPublishedSpy.calls.count()).toBe(1); expect(isPublishedSpy.calls.argsFor(0)).toEqual([formModel.item]); expect(setAccessRevertableSpy.calls.count()).toBe(2); @@ -123,12 +123,12 @@ describe("setAccess", function () { const setAccessRevertableSpy = spyOn(setAccessRevertable, "setAccessRevertable").and.returnValues(...setAccessRevertablePromiseResults); const processRevertableTasksSpy = spyOn(hubCommon, "processRevertableTasks").and.returnValue(Promise.resolve(processRevertableTasksResults)); const results = await setAccess( - formModel, + formModel.item.id, "public", requestOptions ); expect(getSurveyModelsSpy.calls.count()).toBe(1); - expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([formModel, requestOptions]); + expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([FormItemPublished.id, requestOptions]); expect(isPublishedSpy.calls.count()).toBe(1); expect(isPublishedSpy.calls.argsFor(0)).toEqual([formModel.item]); expect(setAccessRevertableSpy.calls.count()).toBe(2); @@ -146,14 +146,14 @@ describe("setAccess", function () { const processRevertableTasksSpy = spyOn(hubCommon, "processRevertableTasks").and.returnValue(Promise.reject(new Error("fail"))); try { await setAccess( - formModel, + formModel.item.id, "public", requestOptions ); done.fail("Should have rejected"); } catch (e) { expect(getSurveyModelsSpy.calls.count()).toBe(1); - expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([formModel, requestOptions]); + expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([FormItemPublished.id, requestOptions]); expect(isPublishedSpy.calls.count()).toBe(1); expect(isPublishedSpy.calls.argsFor(0)).toEqual([formModel.item]); expect(setAccessRevertableSpy.calls.count()).toBe(3); diff --git a/packages/surveys/test/sharing/share-with-group.test.ts b/packages/surveys/test/sharing/share-with-group.test.ts index d60fc27bee8..ea049df6993 100644 --- a/packages/surveys/test/sharing/share-with-group.test.ts +++ b/packages/surveys/test/sharing/share-with-group.test.ts @@ -69,12 +69,12 @@ describe("shareWithGroup", function () { const shareWithGroupRevertableSpy = spyOn(shareWithGroupRevertable, "shareWithGroupRevertable").and.returnValues(...shareWithGroupRevertablePromiseResults); const processRevertableTasksSpy = spyOn(hubCommon, "processRevertableTasks").and.returnValue(Promise.resolve(processRevertableTasksResults)); const results = await shareWithGroup( - formModel, + formModel.item.id, ViewGroup.id, requestOptions ); expect(getGroupSharingDetailsSpy.calls.count()).toBe(1); - expect(getGroupSharingDetailsSpy.calls.argsFor(0)).toEqual([formModel, ViewGroup.id, requestOptions]); + expect(getGroupSharingDetailsSpy.calls.argsFor(0)).toEqual([FormItemPublished.id, ViewGroup.id, requestOptions]); expect(shareWithGroupRevertableSpy.calls.count()).toBe(4); expect(shareWithGroupRevertableSpy.calls.argsFor(0)).toEqual([formModel, ViewGroup, requestOptions]); expect(shareWithGroupRevertableSpy.calls.argsFor(1)).toEqual([featureServiceModel, ViewGroup, requestOptions]); @@ -91,14 +91,14 @@ describe("shareWithGroup", function () { const processRevertableTasksSpy = spyOn(hubCommon, "processRevertableTasks").and.returnValue(Promise.reject(new Error("fail"))); try { await shareWithGroup( - formModel, + formModel.item.id, ViewGroup.id, requestOptions ); done.fail("Should have rejected"); } catch (e) { expect(getGroupSharingDetailsSpy.calls.count()).toBe(1); - expect(getGroupSharingDetailsSpy.calls.argsFor(0)).toEqual([formModel, ViewGroup.id, requestOptions]); + expect(getGroupSharingDetailsSpy.calls.argsFor(0)).toEqual([FormItemPublished.id, ViewGroup.id, requestOptions]); expect(shareWithGroupRevertableSpy.calls.count()).toBe(4); expect(shareWithGroupRevertableSpy.calls.argsFor(0)).toEqual([formModel, ViewGroup, requestOptions]); expect(shareWithGroupRevertableSpy.calls.argsFor(1)).toEqual([featureServiceModel, ViewGroup, requestOptions]); diff --git a/packages/surveys/test/sharing/unshare-with-group.test.ts b/packages/surveys/test/sharing/unshare-with-group.test.ts index 8e1e18e4118..c03a2265e4e 100644 --- a/packages/surveys/test/sharing/unshare-with-group.test.ts +++ b/packages/surveys/test/sharing/unshare-with-group.test.ts @@ -61,12 +61,12 @@ describe("unshareWithGroup", function () { const unshareWithGroupRevertableSpy = spyOn(unshareWithGroupRevertable, "unshareWithGroupRevertable").and.returnValues(...unshareWithGroupRevertablePromiseResults); const processRevertableTasksSpy = spyOn(hubCommon, "processRevertableTasks").and.returnValue(Promise.resolve(processRevertableTasksResults)); const results = await unshareWithGroup( - formModel, + formModel.item.id, ViewGroup.id, requestOptions ); expect(getGroupSharingDetailsSpy.calls.count()).toBe(1); - expect(getGroupSharingDetailsSpy.calls.argsFor(0)).toEqual([formModel, ViewGroup.id, requestOptions]); + expect(getGroupSharingDetailsSpy.calls.argsFor(0)).toEqual([FormItemPublished.id, ViewGroup.id, requestOptions]); expect(unshareWithGroupRevertableSpy.calls.count()).toBe(3); expect(unshareWithGroupRevertableSpy.calls.argsFor(0)).toEqual([formModel, ViewGroup, requestOptions]); expect(unshareWithGroupRevertableSpy.calls.argsFor(1)).toEqual([featureServiceModel, ViewGroup, requestOptions]); @@ -82,14 +82,14 @@ describe("unshareWithGroup", function () { const processRevertableTasksSpy = spyOn(hubCommon, "processRevertableTasks").and.returnValue(Promise.reject(new Error("fail"))); try { await unshareWithGroup( - formModel, + formModel.item.id, ViewGroup.id, requestOptions ); done.fail("Should have rejected"); } catch (e) { expect(getGroupSharingDetailsSpy.calls.count()).toBe(1); - expect(getGroupSharingDetailsSpy.calls.argsFor(0)).toEqual([formModel, ViewGroup.id, requestOptions]); + expect(getGroupSharingDetailsSpy.calls.argsFor(0)).toEqual([FormItemPublished.id, ViewGroup.id, requestOptions]); expect(unshareWithGroupRevertableSpy.calls.count()).toBe(3); expect(unshareWithGroupRevertableSpy.calls.argsFor(0)).toEqual([formModel, ViewGroup, requestOptions]); expect(unshareWithGroupRevertableSpy.calls.argsFor(1)).toEqual([featureServiceModel, ViewGroup, requestOptions]); From 6047bb4b77f08ecd8180d0abc1dd2ac996a93238 Mon Sep 17 00:00:00 2001 From: Randy Weber Date: Wed, 1 Jul 2020 16:03:18 -0400 Subject: [PATCH 2/5] Add new line at eof --- packages/surveys/src/items/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/surveys/src/items/index.ts b/packages/surveys/src/items/index.ts index 7d28436ec2e..5f1599ada6e 100644 --- a/packages/surveys/src/items/index.ts +++ b/packages/surveys/src/items/index.ts @@ -1,4 +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"; \ No newline at end of file +export * from "./get-survey-models"; From aa35123441916c6ec31598e8f847b7c73a8558ee Mon Sep 17 00:00:00 2001 From: Randy Weber Date: Wed, 1 Jul 2020 17:09:07 -0400 Subject: [PATCH 3/5] Kick CI From bbf3270fe91c5ba2bcbb94ae719a24bb6990cf32 Mon Sep 17 00:00:00 2001 From: Randy Weber Date: Wed, 1 Jul 2020 15:47:47 -0400 Subject: [PATCH 4/5] 136998 - Adjust method signatures to accept ids instead of models --- packages/surveys/src/index.ts | 3 +- .../get-input-feature-service-model.ts | 0 ...-feature-service-model-from-fieldworker.ts | 0 .../get-stakeholder-model.ts | 24 +++---- .../surveys/src/items/get-survey-models.ts | 65 +++++++++++++++++++ packages/surveys/src/items/index.ts | 4 ++ .../src/sharing/get-group-sharing-details.ts | 10 +-- .../surveys/src/sharing/get-survey-models.ts | 61 ----------------- packages/surveys/src/sharing/index.ts | 4 -- packages/surveys/src/sharing/set-access.ts | 12 ++-- .../surveys/src/sharing/share-with-group.ts | 12 ++-- .../surveys/src/sharing/unshare-with-group.ts | 10 +-- .../get-input-feature-service-model.test.ts | 2 +- ...ure-service-model-from-fieldworker.test.ts | 2 +- .../get-stakeholder-model.test.ts | 19 ++---- .../get-survey-models.test.ts | 34 ++++++---- .../sharing/get-group-sharing-details.test.ts | 45 +++++++------ .../surveys/test/sharing/set-access.test.ts | 18 ++--- .../test/sharing/share-with-group.test.ts | 8 +-- .../test/sharing/unshare-with-group.test.ts | 8 +-- 20 files changed, 172 insertions(+), 169 deletions(-) rename packages/surveys/src/{sharing => items}/get-input-feature-service-model.ts (100%) rename packages/surveys/src/{sharing => items}/get-source-feature-service-model-from-fieldworker.ts (100%) rename packages/surveys/src/{sharing => items}/get-stakeholder-model.ts (55%) create mode 100644 packages/surveys/src/items/get-survey-models.ts create mode 100644 packages/surveys/src/items/index.ts delete mode 100644 packages/surveys/src/sharing/get-survey-models.ts rename packages/surveys/test/{sharing => items}/get-input-feature-service-model.test.ts (94%) rename packages/surveys/test/{sharing => items}/get-source-feature-service-model-from-fieldworker.test.ts (96%) rename packages/surveys/test/{sharing => items}/get-stakeholder-model.test.ts (60%) rename packages/surveys/test/{sharing => items}/get-survey-models.test.ts (68%) diff --git a/packages/surveys/src/index.ts b/packages/surveys/src/index.ts index 54dc4f796d6..9cb86bd5ed3 100644 --- a/packages/surveys/src/index.ts +++ b/packages/surveys/src/index.ts @@ -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"; diff --git a/packages/surveys/src/sharing/get-input-feature-service-model.ts b/packages/surveys/src/items/get-input-feature-service-model.ts similarity index 100% rename from packages/surveys/src/sharing/get-input-feature-service-model.ts rename to packages/surveys/src/items/get-input-feature-service-model.ts diff --git a/packages/surveys/src/sharing/get-source-feature-service-model-from-fieldworker.ts b/packages/surveys/src/items/get-source-feature-service-model-from-fieldworker.ts similarity index 100% rename from packages/surveys/src/sharing/get-source-feature-service-model-from-fieldworker.ts rename to packages/surveys/src/items/get-source-feature-service-model-from-fieldworker.ts diff --git a/packages/surveys/src/sharing/get-stakeholder-model.ts b/packages/surveys/src/items/get-stakeholder-model.ts similarity index 55% rename from packages/surveys/src/sharing/get-stakeholder-model.ts rename to packages/surveys/src/items/get-stakeholder-model.ts index f9a4d8ebf5e..a8f3154b1b5 100644 --- a/packages/surveys/src/sharing/get-stakeholder-model.ts +++ b/packages/surveys/src/items/get-stakeholder-model.ts @@ -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} + * @returns {Promise} */ export const getStakeholderModel = ( - fieldworkerId: string, + formId: string, requestOptions: IRequestOptions ): Promise => { 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; }); }; diff --git a/packages/surveys/src/items/get-survey-models.ts b/packages/surveys/src/items/get-survey-models.ts new file mode 100644 index 00000000000..c27927143bf --- /dev/null +++ b/packages/surveys/src/items/get-survey-models.ts @@ -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} + */ +export const getSurveyModels = ( + formId: string, + requestOptions: IRequestOptions +): Promise => { + let fieldworker: IModel; + let stakeholder: IModel; + + return getItem(formId, requestOptions) + .then((item) => { + const promises: Array> = [ + // 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 + }; + }); + }); +}; diff --git a/packages/surveys/src/items/index.ts b/packages/surveys/src/items/index.ts new file mode 100644 index 00000000000..7d28436ec2e --- /dev/null +++ b/packages/surveys/src/items/index.ts @@ -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"; \ No newline at end of file diff --git a/packages/surveys/src/sharing/get-group-sharing-details.ts b/packages/surveys/src/sharing/get-group-sharing-details.ts index dbadf066c3d..b7a3f4404dc 100644 --- a/packages/surveys/src/sharing/get-group-sharing-details.ts +++ b/packages/surveys/src/sharing/get-group-sharing-details.ts @@ -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"; /** @@ -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} */ export const getGroupSharingDetails = ( - formModel: IModel, + formId: string, groupId: string, requestOptions: IRequestOptions ): Promise => { @@ -28,7 +28,7 @@ export const getGroupSharingDetails = ( requestOptions ); const surveyModelsPromise = getSurveyModels( - formModel, + formId, requestOptions ); diff --git a/packages/surveys/src/sharing/get-survey-models.ts b/packages/surveys/src/sharing/get-survey-models.ts deleted file mode 100644 index 0de0e3438ce..00000000000 --- a/packages/surveys/src/sharing/get-survey-models.ts +++ /dev/null @@ -1,61 +0,0 @@ -/* Copyright (c) 2020 Environmental Systems Research Institute, Inc. - * Apache-2.0 */ - -import { IRequestOptions } from "@esri/arcgis-rest-request"; -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 {IModel} formModel The Form model of the survey - * @param {IRequestOptions} requestOptions The request options - * @returns {Promise} - */ -export const getSurveyModels = ( - formModel: IModel, - requestOptions: IRequestOptions -): Promise => { - let featureService: IModel; - let fieldworker: IModel; - - // the primary input will be the fieldworker (if it exists), otherwise - // the source feature service. - return getInputFeatureServiceModel( - formModel.item.id, - requestOptions - ) - .then((featureServiceOrFieldworkerModelResult) => { - if (isFieldworkerView(featureServiceOrFieldworkerModelResult.item)) { - fieldworker = featureServiceOrFieldworkerModelResult; - } else { - featureService = featureServiceOrFieldworkerModelResult; - } - - // if the primary input is the fieldworker, fetch - // the source feature service - if (fieldworker) { - return getSourceFeatureServiceModelFromFieldworker( - fieldworker.item.id, - requestOptions - ) - .then((featureServiceModelResult) => { - featureService = featureServiceModelResult; - }); - } - }) - .then(() => getStakeholderModel( - featureService.item.id, - requestOptions - )) - .then(stakeholder => { - return { - form: formModel, - featureService, - fieldworker, - stakeholder - }; - }); -}; diff --git a/packages/surveys/src/sharing/index.ts b/packages/surveys/src/sharing/index.ts index 9dc20b82ac9..577fa618a5c 100644 --- a/packages/surveys/src/sharing/index.ts +++ b/packages/surveys/src/sharing/index.ts @@ -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"; diff --git a/packages/surveys/src/sharing/set-access.ts b/packages/surveys/src/sharing/set-access.ts index 33fba53cb54..00a43d90688 100644 --- a/packages/surveys/src/sharing/set-access.ts +++ b/packages/surveys/src/sharing/set-access.ts @@ -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} */ export const setAccess = ( - formModel: IModel, + formId: string, access: "private" | "public" | "org", requestOptions: IRequestOptions ): Promise => { - 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[], model: IModel) => { @@ -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}`); }); }; diff --git a/packages/surveys/src/sharing/share-with-group.ts b/packages/surveys/src/sharing/share-with-group.ts index 323b5af010d..d4749a15874 100644 --- a/packages/surveys/src/sharing/share-with-group.ts +++ b/packages/surveys/src/sharing/share-with-group.ts @@ -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} */ export const shareWithGroup = ( - formModel: IModel, + formId: string, groupId: string, requestOptions: IRequestOptions ): Promise => { - return getGroupSharingDetails(formModel, groupId, requestOptions) + return getGroupSharingDetails(formId, groupId, requestOptions) .then(({ modelsToShare, group }) => { const revertableTasks = modelsToShare.map( (model) => shareWithGroupRevertable( @@ -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}`); }); }; diff --git a/packages/surveys/src/sharing/unshare-with-group.ts b/packages/surveys/src/sharing/unshare-with-group.ts index 9472fb11575..e59d38e79aa 100644 --- a/packages/surveys/src/sharing/unshare-with-group.ts +++ b/packages/surveys/src/sharing/unshare-with-group.ts @@ -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} */ export const unshareWithGroup = ( - formModel: IModel, + formId: string, groupId: string, requestOptions: IRequestOptions ): Promise => { - return getGroupSharingDetails(formModel, groupId, requestOptions) + return getGroupSharingDetails(formId, groupId, requestOptions) .then(({ modelsToShare, group }) => { const revertableTasks = modelsToShare.map( (model) => unshareWithGroupRevertable( @@ -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}`); }); }; diff --git a/packages/surveys/test/sharing/get-input-feature-service-model.test.ts b/packages/surveys/test/items/get-input-feature-service-model.test.ts similarity index 94% rename from packages/surveys/test/sharing/get-input-feature-service-model.test.ts rename to packages/surveys/test/items/get-input-feature-service-model.test.ts index 20d9203be29..88db29fa7d9 100644 --- a/packages/surveys/test/sharing/get-input-feature-service-model.test.ts +++ b/packages/surveys/test/items/get-input-feature-service-model.test.ts @@ -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"; diff --git a/packages/surveys/test/sharing/get-source-feature-service-model-from-fieldworker.test.ts b/packages/surveys/test/items/get-source-feature-service-model-from-fieldworker.test.ts similarity index 96% rename from packages/surveys/test/sharing/get-source-feature-service-model-from-fieldworker.test.ts rename to packages/surveys/test/items/get-source-feature-service-model-from-fieldworker.test.ts index 702541f032e..6b1df4a9491 100644 --- a/packages/surveys/test/sharing/get-source-feature-service-model-from-fieldworker.test.ts +++ b/packages/surveys/test/items/get-source-feature-service-model-from-fieldworker.test.ts @@ -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"; diff --git a/packages/surveys/test/sharing/get-stakeholder-model.test.ts b/packages/surveys/test/items/get-stakeholder-model.test.ts similarity index 60% rename from packages/surveys/test/sharing/get-stakeholder-model.test.ts rename to packages/surveys/test/items/get-stakeholder-model.test.ts index a9ce30d99ac..4f4d38e9086 100644 --- a/packages/surveys/test/sharing/get-stakeholder-model.test.ts +++ b/packages/surveys/test/items/get-stakeholder-model.test.ts @@ -3,10 +3,8 @@ import * as restPortal from "@esri/arcgis-rest-portal"; import { mockUserSession } from "@esri/hub-common/test/test-helpers/fake-user-session"; -import { getStakeholderModel } from "../../src/sharing/get-stakeholder-model"; -import { FieldworkerItem } from "../mocks/fieldworker-item"; +import { getStakeholderModel } from "../../src/items/get-stakeholder-model"; import { StakeholderItem } from "../mocks/stakeholder-item"; -import * as isFieldworkerView from "../../src/utils/is-fieldworker-view"; describe("getStakeholderModel", function () { let getRelatedItemsResponse: restPortal.IGetRelatedItemsResponse; @@ -14,41 +12,34 @@ describe("getStakeholderModel", function () { beforeEach(() => { getRelatedItemsResponse = { total: 2, - relatedItems: [FieldworkerItem, StakeholderItem] + relatedItems: [StakeholderItem] }; }); it("should resolve undefined when getRelatedItems returns no Stakeholder", async function () { getRelatedItemsResponse.relatedItems.pop(); const getRelatedItemsSpy = spyOn(restPortal, "getRelatedItems").and.returnValue(Promise.resolve(getRelatedItemsResponse)); - const isFieldworkerViewSpy = spyOn(isFieldworkerView, "isFieldworkerView").and.returnValues(true); const result = await getStakeholderModel("123", mockUserSession); expect(getRelatedItemsSpy.calls.count()).toBe(1); expect(getRelatedItemsSpy.calls.argsFor(0)).toEqual([{ id: "123", - relationshipType: "Service2Service", + relationshipType: "Survey2Data", direction: "forward", ...mockUserSession }]); - expect(isFieldworkerViewSpy.calls.count()).toBe(1); - expect(isFieldworkerViewSpy.calls.argsFor(0)).toEqual([FieldworkerItem]); expect(result).toBeUndefined(); }); - it("should resolve an IModel when getRelatedItems returns a Fieldworker", async function () { + it("should resolve an IModel when getRelatedItems returns a Stakeholder", async function () { const getRelatedItemsSpy = spyOn(restPortal, "getRelatedItems").and.returnValue(Promise.resolve(getRelatedItemsResponse)); - const isFieldworkerViewSpy = spyOn(isFieldworkerView, "isFieldworkerView").and.returnValues(true, false); const result = await getStakeholderModel("123", mockUserSession); expect(getRelatedItemsSpy.calls.count()).toBe(1); expect(getRelatedItemsSpy.calls.argsFor(0)).toEqual([{ id: "123", - relationshipType: "Service2Service", + relationshipType: "Survey2Data", direction: "forward", ...mockUserSession }]); - expect(isFieldworkerViewSpy.calls.count()).toBe(2); - expect(isFieldworkerViewSpy.calls.argsFor(0)).toEqual([FieldworkerItem]); - expect(isFieldworkerViewSpy.calls.argsFor(1)).toEqual([StakeholderItem]); const expected = { item: StakeholderItem }; expect(result).toEqual(expected); }); diff --git a/packages/surveys/test/sharing/get-survey-models.test.ts b/packages/surveys/test/items/get-survey-models.test.ts similarity index 68% rename from packages/surveys/test/sharing/get-survey-models.test.ts rename to packages/surveys/test/items/get-survey-models.test.ts index f4db008069e..846570c0d20 100644 --- a/packages/surveys/test/sharing/get-survey-models.test.ts +++ b/packages/surveys/test/items/get-survey-models.test.ts @@ -2,63 +2,73 @@ * Apache-2.0 */ import { IModel } from "@esri/hub-common"; +import * as restPortal from "@esri/arcgis-rest-portal"; import { mockUserSession } from "@esri/hub-common/test/test-helpers/fake-user-session"; -import { getSurveyModels } from "../../src/sharing/get-survey-models"; +import { getSurveyModels } from "../../src/items/get-survey-models"; import { FormItemPublished } from "../mocks/form-item-published"; import { FeatureServiceItem } from "../mocks/feature-service-item"; import { FieldworkerItem } from "../mocks/fieldworker-item"; import { StakeholderItem } from "../mocks/stakeholder-item"; import * as isFieldworkerView from "../../src/utils/is-fieldworker-view"; -import * as getInputFeatureServiceModel from "../../src/sharing/get-input-feature-service-model"; -import * as getSourceFeatureServiceModelFromFieldworker from "../../src/sharing/get-source-feature-service-model-from-fieldworker"; -import * as getStakeholderModel from "../../src/sharing/get-stakeholder-model"; +import * as getInputFeatureServiceModel from "../../src/items/get-input-feature-service-model"; +import * as getSourceFeatureServiceModelFromFieldworker from "../../src/items/get-source-feature-service-model-from-fieldworker"; +import * as getStakeholderModel from "../../src/items/get-stakeholder-model"; +import { IRequestOptions } from "@esri/hub-common/node_modules/@esri/arcgis-rest-request"; describe("getSurveyModels", function () { let formModel: IModel; let featureServiceModel: IModel; let fieldworkerModel: IModel; let stakeholderModel: IModel; + let requestOptions: IRequestOptions; beforeEach(() => { formModel = { item: FormItemPublished }; featureServiceModel = { item: FeatureServiceItem }; fieldworkerModel = { item: FieldworkerItem }; stakeholderModel = { item: StakeholderItem }; + requestOptions = { authentication: mockUserSession }; }); it("fetches the feature service when a fieldworker is the primary input service", async function () { + const getItemStub = spyOn(restPortal, "getItem").and.returnValue(Promise.resolve(FormItemPublished)); const isFieldworkerViewSpy = spyOn(isFieldworkerView, "isFieldworkerView").and.returnValue(true); const getInputFeatureServiceModelSpy = spyOn(getInputFeatureServiceModel, "getInputFeatureServiceModel").and.returnValue(Promise.resolve(fieldworkerModel)); const getSourceFeatureServiceModelFromFieldworkerSpy = spyOn(getSourceFeatureServiceModelFromFieldworker, "getSourceFeatureServiceModelFromFieldworker").and.returnValue(Promise.resolve(featureServiceModel)); const getStakeholderModelSpy = spyOn(getStakeholderModel, "getStakeholderModel").and.returnValue(Promise.resolve(stakeholderModel)); - const results = await getSurveyModels(formModel, mockUserSession); + const results = await getSurveyModels(formModel.item.id, requestOptions); + expect(getItemStub.calls.count()).toBe(1); + expect(getItemStub.calls.argsFor(0)).toEqual([FormItemPublished.id, requestOptions]); expect(getInputFeatureServiceModelSpy.calls.count()).toBe(1); - expect(getInputFeatureServiceModelSpy.calls.argsFor(0)).toEqual([formModel.item.id, mockUserSession]); + expect(getInputFeatureServiceModelSpy.calls.argsFor(0)).toEqual([formModel.item.id, requestOptions]); expect(getSourceFeatureServiceModelFromFieldworkerSpy.calls.count()).toBe(1); - expect(getSourceFeatureServiceModelFromFieldworkerSpy.calls.argsFor(0)).toEqual([FieldworkerItem.id, mockUserSession]); + expect(getSourceFeatureServiceModelFromFieldworkerSpy.calls.argsFor(0)).toEqual([FieldworkerItem.id, requestOptions]); expect(isFieldworkerViewSpy.calls.count()).toBe(1); expect(isFieldworkerViewSpy.calls.argsFor(0)).toEqual([fieldworkerModel.item]); expect(getStakeholderModelSpy.calls.count()).toBe(1); - expect(getStakeholderModelSpy.calls.argsFor(0)).toEqual([FeatureServiceItem.id, mockUserSession]); + expect(getStakeholderModelSpy.calls.argsFor(0)).toEqual([FormItemPublished.id, requestOptions]); expect(results.form).toEqual(formModel); expect(results.featureService).toEqual(featureServiceModel); expect(results.fieldworker).toEqual(fieldworkerModel); expect(results.stakeholder).toEqual(stakeholderModel); }); - it("fetches does not attempt to fetch the feature service when a fieldworker is absent", async function () { + it("does not attempt to fetch the feature service when a fieldworker is absent", async function () { + const getItemStub = spyOn(restPortal, "getItem").and.returnValue(Promise.resolve(FormItemPublished)); const isFieldworkerViewSpy = spyOn(isFieldworkerView, "isFieldworkerView").and.returnValue(false); const getInputFeatureServiceModelSpy = spyOn(getInputFeatureServiceModel, "getInputFeatureServiceModel").and.returnValue(Promise.resolve(featureServiceModel)); const getSourceFeatureServiceModelFromFieldworkerSpy = spyOn(getSourceFeatureServiceModelFromFieldworker, "getSourceFeatureServiceModelFromFieldworker"); const getStakeholderModelSpy = spyOn(getStakeholderModel, "getStakeholderModel").and.returnValue(Promise.resolve()); - const results = await getSurveyModels(formModel, mockUserSession); + const results = await getSurveyModels(formModel.item.id, requestOptions); + expect(getItemStub.calls.count()).toBe(1); + expect(getItemStub.calls.argsFor(0)).toEqual([FormItemPublished.id, requestOptions]); expect(getInputFeatureServiceModelSpy.calls.count()).toBe(1); - expect(getInputFeatureServiceModelSpy.calls.argsFor(0)).toEqual([formModel.item.id, mockUserSession]); + expect(getInputFeatureServiceModelSpy.calls.argsFor(0)).toEqual([formModel.item.id, requestOptions]); expect(getSourceFeatureServiceModelFromFieldworkerSpy.calls.count()).toBe(0); expect(isFieldworkerViewSpy.calls.count()).toBe(1); expect(isFieldworkerViewSpy.calls.argsFor(0)).toEqual([featureServiceModel.item]); expect(getStakeholderModelSpy.calls.count()).toBe(1); - expect(getStakeholderModelSpy.calls.argsFor(0)).toEqual([FeatureServiceItem.id, mockUserSession]); + expect(getStakeholderModelSpy.calls.argsFor(0)).toEqual([FormItemPublished.id, requestOptions]); expect(results.form).toEqual(formModel); expect(results.featureService).toEqual(featureServiceModel); expect(results.fieldworker).toBeUndefined(); diff --git a/packages/surveys/test/sharing/get-group-sharing-details.test.ts b/packages/surveys/test/sharing/get-group-sharing-details.test.ts index 43fc47b951c..13636089754 100644 --- a/packages/surveys/test/sharing/get-group-sharing-details.test.ts +++ b/packages/surveys/test/sharing/get-group-sharing-details.test.ts @@ -12,8 +12,9 @@ import { UpdateGroup } from "../mocks/update-group"; import { ViewGroup } from "../mocks/view-group"; import * as restPortal from "@esri/arcgis-rest-portal"; import { mockUserSession } from "@esri/hub-common/test/test-helpers/fake-user-session"; -import * as getSurveyModels from "../../src/sharing/get-survey-models"; +import * as getSurveyModels from "../../src/items/get-survey-models"; import * as isPublished from "../../src/utils/is-published"; +import { IRequestOptions } from "@esri/hub-common/node_modules/@esri/arcgis-rest-request"; describe("getGroupSharingDetails", function () { let getSurveyModelsResult: IGetSurveyModelsResponse; @@ -21,6 +22,7 @@ describe("getGroupSharingDetails", function () { let featureServiceModel: IModel; let fieldworkerModel: IModel; let stakeholderModel: IModel; + let requestOptions: IRequestOptions; beforeEach(() => { formModel = { item: FormItemDraft }; @@ -33,6 +35,7 @@ describe("getGroupSharingDetails", function () { fieldworker: fieldworkerModel, stakeholder: stakeholderModel }; + requestOptions = { authentication: mockUserSession }; }); it("should resolve with the form & feature service models when a draft and an update group", async function () { @@ -40,14 +43,14 @@ describe("getGroupSharingDetails", function () { const getSurveyModelsSpy = spyOn(getSurveyModels, "getSurveyModels").and.returnValue(Promise.resolve(getSurveyModelsResult)); const isPublishedSpy = spyOn(isPublished, "isPublished").and.returnValue(false); const result = await getGroupSharingDetails( - formModel, + formModel.item.id, "123", - mockUserSession + requestOptions ); expect(getGroupSpy.calls.count()).toBe(1); - expect(getGroupSpy.calls.argsFor(0)).toEqual(["123", mockUserSession]); + expect(getGroupSpy.calls.argsFor(0)).toEqual(["123", requestOptions]); expect(getSurveyModelsSpy.calls.count()).toBe(1); - expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([formModel, mockUserSession]); + expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([formModel.item.id, requestOptions]); expect(isPublishedSpy.calls.count()).toBe(1); expect(isPublishedSpy.calls.argsFor(0)).toEqual([formModel.item]); expect(result).toEqual({ @@ -61,14 +64,14 @@ describe("getGroupSharingDetails", function () { const getSurveyModelsSpy = spyOn(getSurveyModels, "getSurveyModels").and.returnValue(Promise.resolve(getSurveyModelsResult)); const isPublishedSpy = spyOn(isPublished, "isPublished").and.returnValue(false); const result = await getGroupSharingDetails( - formModel, + formModel.item.id, "123", - mockUserSession + requestOptions ); expect(getGroupSpy.calls.count()).toBe(1); - expect(getGroupSpy.calls.argsFor(0)).toEqual(["123", mockUserSession]); + expect(getGroupSpy.calls.argsFor(0)).toEqual(["123", requestOptions]); expect(getSurveyModelsSpy.calls.count()).toBe(1); - expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([formModel, mockUserSession]); + expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([formModel.item.id, requestOptions]); expect(isPublishedSpy.calls.count()).toBe(1); expect(isPublishedSpy.calls.argsFor(0)).toEqual([formModel.item]); expect(result).toEqual({ @@ -84,14 +87,14 @@ describe("getGroupSharingDetails", function () { const getSurveyModelsSpy = spyOn(getSurveyModels, "getSurveyModels").and.returnValue(Promise.resolve(getSurveyModelsResult)); const isPublishedSpy = spyOn(isPublished, "isPublished").and.returnValue(true); const result = await getGroupSharingDetails( - formModel, + formModel.item.id, "123", - mockUserSession + requestOptions ); expect(getGroupSpy.calls.count()).toBe(1); - expect(getGroupSpy.calls.argsFor(0)).toEqual(["123", mockUserSession]); + expect(getGroupSpy.calls.argsFor(0)).toEqual(["123", requestOptions]); expect(getSurveyModelsSpy.calls.count()).toBe(1); - expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([formModel, mockUserSession]); + expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([formModel.item.id, requestOptions]); expect(isPublishedSpy.calls.count()).toBe(1); expect(isPublishedSpy.calls.argsFor(0)).toEqual([formModel.item]); expect(result).toEqual({ @@ -107,14 +110,14 @@ describe("getGroupSharingDetails", function () { const getSurveyModelsSpy = spyOn(getSurveyModels, "getSurveyModels").and.returnValue(Promise.resolve(getSurveyModelsResult)); const isPublishedSpy = spyOn(isPublished, "isPublished").and.returnValue(true); const result = await getGroupSharingDetails( - formModel, + formModel.item.id, "123", - mockUserSession + requestOptions ); expect(getGroupSpy.calls.count()).toBe(1); - expect(getGroupSpy.calls.argsFor(0)).toEqual(["123", mockUserSession]); + expect(getGroupSpy.calls.argsFor(0)).toEqual(["123", requestOptions]); expect(getSurveyModelsSpy.calls.count()).toBe(1); - expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([formModel, mockUserSession]); + expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([formModel.item.id, requestOptions]); expect(isPublishedSpy.calls.count()).toBe(1); expect(isPublishedSpy.calls.argsFor(0)).toEqual([formModel.item]); expect(result).toEqual({ @@ -131,14 +134,14 @@ describe("getGroupSharingDetails", function () { const getSurveyModelsSpy = spyOn(getSurveyModels, "getSurveyModels").and.returnValue(Promise.resolve(getSurveyModelsResult)); const isPublishedSpy = spyOn(isPublished, "isPublished").and.returnValue(true); const result = await getGroupSharingDetails( - formModel, + formModel.item.id, "123", - mockUserSession + requestOptions ); expect(getGroupSpy.calls.count()).toBe(1); - expect(getGroupSpy.calls.argsFor(0)).toEqual(["123", mockUserSession]); + expect(getGroupSpy.calls.argsFor(0)).toEqual(["123", requestOptions]); expect(getSurveyModelsSpy.calls.count()).toBe(1); - expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([formModel, mockUserSession]); + expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([formModel.item.id, requestOptions]); expect(isPublishedSpy.calls.count()).toBe(1); expect(isPublishedSpy.calls.argsFor(0)).toEqual([formModel.item]); expect(result).toEqual({ diff --git a/packages/surveys/test/sharing/set-access.test.ts b/packages/surveys/test/sharing/set-access.test.ts index 3bed0841e98..e6ffe33bffb 100644 --- a/packages/surveys/test/sharing/set-access.test.ts +++ b/packages/surveys/test/sharing/set-access.test.ts @@ -10,7 +10,7 @@ import { FieldworkerItem } from "../mocks/fieldworker-item"; import { StakeholderItem } from "../mocks/stakeholder-item"; import { setAccess } from "../../src/sharing/set-access"; import * as setAccessRevertable from "../../src/sharing/set-access-revertable"; -import * as getSurveyModels from "../../src/sharing/get-survey-models"; +import * as getSurveyModels from "../../src/items/get-survey-models"; import * as isPublished from "../../src/utils/is-published"; describe("setAccess", function () { @@ -67,12 +67,12 @@ describe("setAccess", function () { const setAccessRevertableSpy = spyOn(setAccessRevertable, "setAccessRevertable").and.returnValues(...setAccessRevertablePromiseResults); const processRevertableTasksSpy = spyOn(hubCommon, "processRevertableTasks").and.returnValue(Promise.resolve(processRevertableTasksResults)); const results = await setAccess( - formModel, + formModel.item.id, "public", requestOptions ); expect(getSurveyModelsSpy.calls.count()).toBe(1); - expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([formModel, requestOptions]); + expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([FormItemPublished.id, requestOptions]); expect(isPublishedSpy.calls.count()).toBe(1); expect(isPublishedSpy.calls.argsFor(0)).toEqual([formModel.item]); expect(setAccessRevertableSpy.calls.count()).toBe(3); @@ -93,12 +93,12 @@ describe("setAccess", function () { const setAccessRevertableSpy = spyOn(setAccessRevertable, "setAccessRevertable").and.returnValues(...setAccessRevertablePromiseResults); const processRevertableTasksSpy = spyOn(hubCommon, "processRevertableTasks").and.returnValue(Promise.resolve(processRevertableTasksResults)); const results = await setAccess( - formModel, + formModel.item.id, "public", requestOptions ); expect(getSurveyModelsSpy.calls.count()).toBe(1); - expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([formModel, requestOptions]); + expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([FormItemPublished.id, requestOptions]); expect(isPublishedSpy.calls.count()).toBe(1); expect(isPublishedSpy.calls.argsFor(0)).toEqual([formModel.item]); expect(setAccessRevertableSpy.calls.count()).toBe(2); @@ -123,12 +123,12 @@ describe("setAccess", function () { const setAccessRevertableSpy = spyOn(setAccessRevertable, "setAccessRevertable").and.returnValues(...setAccessRevertablePromiseResults); const processRevertableTasksSpy = spyOn(hubCommon, "processRevertableTasks").and.returnValue(Promise.resolve(processRevertableTasksResults)); const results = await setAccess( - formModel, + formModel.item.id, "public", requestOptions ); expect(getSurveyModelsSpy.calls.count()).toBe(1); - expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([formModel, requestOptions]); + expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([FormItemPublished.id, requestOptions]); expect(isPublishedSpy.calls.count()).toBe(1); expect(isPublishedSpy.calls.argsFor(0)).toEqual([formModel.item]); expect(setAccessRevertableSpy.calls.count()).toBe(2); @@ -146,14 +146,14 @@ describe("setAccess", function () { const processRevertableTasksSpy = spyOn(hubCommon, "processRevertableTasks").and.returnValue(Promise.reject(new Error("fail"))); try { await setAccess( - formModel, + formModel.item.id, "public", requestOptions ); done.fail("Should have rejected"); } catch (e) { expect(getSurveyModelsSpy.calls.count()).toBe(1); - expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([formModel, requestOptions]); + expect(getSurveyModelsSpy.calls.argsFor(0)).toEqual([FormItemPublished.id, requestOptions]); expect(isPublishedSpy.calls.count()).toBe(1); expect(isPublishedSpy.calls.argsFor(0)).toEqual([formModel.item]); expect(setAccessRevertableSpy.calls.count()).toBe(3); diff --git a/packages/surveys/test/sharing/share-with-group.test.ts b/packages/surveys/test/sharing/share-with-group.test.ts index d60fc27bee8..ea049df6993 100644 --- a/packages/surveys/test/sharing/share-with-group.test.ts +++ b/packages/surveys/test/sharing/share-with-group.test.ts @@ -69,12 +69,12 @@ describe("shareWithGroup", function () { const shareWithGroupRevertableSpy = spyOn(shareWithGroupRevertable, "shareWithGroupRevertable").and.returnValues(...shareWithGroupRevertablePromiseResults); const processRevertableTasksSpy = spyOn(hubCommon, "processRevertableTasks").and.returnValue(Promise.resolve(processRevertableTasksResults)); const results = await shareWithGroup( - formModel, + formModel.item.id, ViewGroup.id, requestOptions ); expect(getGroupSharingDetailsSpy.calls.count()).toBe(1); - expect(getGroupSharingDetailsSpy.calls.argsFor(0)).toEqual([formModel, ViewGroup.id, requestOptions]); + expect(getGroupSharingDetailsSpy.calls.argsFor(0)).toEqual([FormItemPublished.id, ViewGroup.id, requestOptions]); expect(shareWithGroupRevertableSpy.calls.count()).toBe(4); expect(shareWithGroupRevertableSpy.calls.argsFor(0)).toEqual([formModel, ViewGroup, requestOptions]); expect(shareWithGroupRevertableSpy.calls.argsFor(1)).toEqual([featureServiceModel, ViewGroup, requestOptions]); @@ -91,14 +91,14 @@ describe("shareWithGroup", function () { const processRevertableTasksSpy = spyOn(hubCommon, "processRevertableTasks").and.returnValue(Promise.reject(new Error("fail"))); try { await shareWithGroup( - formModel, + formModel.item.id, ViewGroup.id, requestOptions ); done.fail("Should have rejected"); } catch (e) { expect(getGroupSharingDetailsSpy.calls.count()).toBe(1); - expect(getGroupSharingDetailsSpy.calls.argsFor(0)).toEqual([formModel, ViewGroup.id, requestOptions]); + expect(getGroupSharingDetailsSpy.calls.argsFor(0)).toEqual([FormItemPublished.id, ViewGroup.id, requestOptions]); expect(shareWithGroupRevertableSpy.calls.count()).toBe(4); expect(shareWithGroupRevertableSpy.calls.argsFor(0)).toEqual([formModel, ViewGroup, requestOptions]); expect(shareWithGroupRevertableSpy.calls.argsFor(1)).toEqual([featureServiceModel, ViewGroup, requestOptions]); diff --git a/packages/surveys/test/sharing/unshare-with-group.test.ts b/packages/surveys/test/sharing/unshare-with-group.test.ts index 8e1e18e4118..c03a2265e4e 100644 --- a/packages/surveys/test/sharing/unshare-with-group.test.ts +++ b/packages/surveys/test/sharing/unshare-with-group.test.ts @@ -61,12 +61,12 @@ describe("unshareWithGroup", function () { const unshareWithGroupRevertableSpy = spyOn(unshareWithGroupRevertable, "unshareWithGroupRevertable").and.returnValues(...unshareWithGroupRevertablePromiseResults); const processRevertableTasksSpy = spyOn(hubCommon, "processRevertableTasks").and.returnValue(Promise.resolve(processRevertableTasksResults)); const results = await unshareWithGroup( - formModel, + formModel.item.id, ViewGroup.id, requestOptions ); expect(getGroupSharingDetailsSpy.calls.count()).toBe(1); - expect(getGroupSharingDetailsSpy.calls.argsFor(0)).toEqual([formModel, ViewGroup.id, requestOptions]); + expect(getGroupSharingDetailsSpy.calls.argsFor(0)).toEqual([FormItemPublished.id, ViewGroup.id, requestOptions]); expect(unshareWithGroupRevertableSpy.calls.count()).toBe(3); expect(unshareWithGroupRevertableSpy.calls.argsFor(0)).toEqual([formModel, ViewGroup, requestOptions]); expect(unshareWithGroupRevertableSpy.calls.argsFor(1)).toEqual([featureServiceModel, ViewGroup, requestOptions]); @@ -82,14 +82,14 @@ describe("unshareWithGroup", function () { const processRevertableTasksSpy = spyOn(hubCommon, "processRevertableTasks").and.returnValue(Promise.reject(new Error("fail"))); try { await unshareWithGroup( - formModel, + formModel.item.id, ViewGroup.id, requestOptions ); done.fail("Should have rejected"); } catch (e) { expect(getGroupSharingDetailsSpy.calls.count()).toBe(1); - expect(getGroupSharingDetailsSpy.calls.argsFor(0)).toEqual([formModel, ViewGroup.id, requestOptions]); + expect(getGroupSharingDetailsSpy.calls.argsFor(0)).toEqual([FormItemPublished.id, ViewGroup.id, requestOptions]); expect(unshareWithGroupRevertableSpy.calls.count()).toBe(3); expect(unshareWithGroupRevertableSpy.calls.argsFor(0)).toEqual([formModel, ViewGroup, requestOptions]); expect(unshareWithGroupRevertableSpy.calls.argsFor(1)).toEqual([featureServiceModel, ViewGroup, requestOptions]); From e1ad6b05e51de481dbd6d0c1a7316a1c9026c8dc Mon Sep 17 00:00:00 2001 From: Randy Weber Date: Wed, 1 Jul 2020 16:03:18 -0400 Subject: [PATCH 5/5] Add new line at eof --- packages/surveys/src/items/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/surveys/src/items/index.ts b/packages/surveys/src/items/index.ts index 7d28436ec2e..5f1599ada6e 100644 --- a/packages/surveys/src/items/index.ts +++ b/packages/surveys/src/items/index.ts @@ -1,4 +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"; \ No newline at end of file +export * from "./get-survey-models";