Skip to content
26 changes: 26 additions & 0 deletions packages/common/src/restHelpersGet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,32 @@ export function getJson(url: string, authentication?: UserSession): Promise<any>
});
}

/** Gets the organization's setting to check for beta app and AI assistant enablement
* @param authentication is the signed in user's credentials
*/
export function getOrganizationSettings(authentication: UserSession): Promise<any> {
return new Promise<any>((resolve, reject) => {
const requestOptions: IRequestOptions = {
httpMethod: "GET",
authentication,
rawResponse: false,
params: {
returnOrgSettings: true,
},
};

const sharingURL = getPortalSharingUrlFromAuth(authentication);
const orgSettingURL = `${sharingURL}/community/self`;

request(orgSettingURL, requestOptions).then(
(response) => {
resolve(response && response.orgSettings ? response.orgSettings : {});
},
(e) => reject(e),
);
});
}

/**
* Extracts the portal sharing url from a supplied authentication.
*
Expand Down
8 changes: 8 additions & 0 deletions packages/common/test/mocks/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,14 @@ export function getShareResponse(id: string) {
};
}

export function getOrgSettingsResponse() {
return {
aiAssistantsEnabled: true,
blockBetaApps: false,
colocateCompute: false,
};
}

export function checkForArcgisRestSuccessRequestError(error: any): boolean {
return (
(error &&
Expand Down
36 changes: 36 additions & 0 deletions packages/common/test/restHelpersGet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,42 @@
});
});

describe("getOrganizationSettings", () => {
const escapedUrl = 'https://myorg\.maps\.arcgis\.com/sharing/rest';

it("can handle an exception on get organization", async () => {
// Use regex to match URL with dynamic query params
fetchMock.get(new RegExp(`${escapedUrl}/community/self.*`), mockItems.get500Failure());

return restHelpersGet.getOrganizationSettings(MOCK_USER_SESSION).then(
() => fail(),
() => Promise.resolve(),
);
});

it("can get the organization's settings", async () => {
const response = { orgSettings: utils.getOrgSettingsResponse() };
fetchMock.get(new RegExp(`${escapedUrl}/community/self.*`), response);

const expectedSettings = {
aiAssistantsEnabled: true,
blockBetaApps: false,
colocateCompute: false,
};

const actual = await restHelpersGet.getOrganizationSettings(MOCK_USER_SESSION);
expect(actual).toEqual(expectedSettings);
});

it("returns empty object when orgSettings is missing", async () => {
const responseWithoutOrgSettings = { someOtherProp: true };
fetchMock.get(new RegExp(`${escapedUrl}/community/self.*`), responseWithoutOrgSettings);

const actual = await restHelpersGet.getOrganizationSettings(MOCK_USER_SESSION);
expect(actual).toEqual({});
});
});

describe("getPortalSharingUrlFromAuth", () => {
it("gets a default portal sharing url when there's no authentication", () => {
expect(restHelpersGet.getPortalSharingUrlFromAuth(undefined)).toEqual("https://www.arcgis.com/sharing/rest");
Expand Down
Loading