From 6bdda94020f437b1e2c94470b0f3dd32cba31572 Mon Sep 17 00:00:00 2001 From: Caleb Pomar Date: Mon, 16 Oct 2023 12:12:13 -0600 Subject: [PATCH] feat(hub-common): add "reharvestSiteCatalog" util (#1279) affects: @esri/hub-common --- packages/common/src/sites/index.ts | 1 + .../common/src/sites/reharvestSiteCatalog.ts | 41 +++++++++++++++++++ .../test/sites/reharvestSiteCatalog.test.ts | 22 ++++++++++ 3 files changed, 64 insertions(+) create mode 100644 packages/common/src/sites/reharvestSiteCatalog.ts create mode 100644 packages/common/test/sites/reharvestSiteCatalog.test.ts diff --git a/packages/common/src/sites/index.ts b/packages/common/src/sites/index.ts index b244a8657ea..b341481bd96 100644 --- a/packages/common/src/sites/index.ts +++ b/packages/common/src/sites/index.ts @@ -12,6 +12,7 @@ export * from "./site-schema-version"; export * from "./themes"; export * from "./upgrade-site-schema"; export * from "./feed-configuration"; +export * from "./reharvestSiteCatalog"; // No longer exported b/c site app registration is now handled // by the domain service due to requirement to send signed HMAC request // export * from "./registerSiteAsApplication"; diff --git a/packages/common/src/sites/reharvestSiteCatalog.ts b/packages/common/src/sites/reharvestSiteCatalog.ts new file mode 100644 index 00000000000..8343f85c4b3 --- /dev/null +++ b/packages/common/src/sites/reharvestSiteCatalog.ts @@ -0,0 +1,41 @@ +import { IArcGISContext } from "../ArcGISContext"; + +interface IGroupReharvestInfo { + /** + * Catalog group being harvested + */ + groupId: string; + /** + * Internal job id + */ + jobId: string; + /** + * HTTP status code of the job + */ + status: number; +} + +/** + * Trigger a manual update to reharvest each public item within a site's catalog. + * This should only be used when search metadata has gotten out of sync despite + * the nightly reharvest. + * + * @param siteId site's catalog to reharvest + * @param context + * @returns Job info for each group whose content is being harvested + */ +export async function reharvestSiteCatalog( + siteId: string, + context: IArcGISContext +): Promise { + const apiHost = context.hubUrl; + const url = `${apiHost}/api/v3/jobs/site/${siteId}/harvest`; + const options = { + method: "POST", + headers: { + "content-type": "application/json", + authorization: context.hubRequestOptions.authentication.token, + }, + }; + return fetch(url, options).then((result) => result.json()); +} diff --git a/packages/common/test/sites/reharvestSiteCatalog.test.ts b/packages/common/test/sites/reharvestSiteCatalog.test.ts new file mode 100644 index 00000000000..ab45abeb0a3 --- /dev/null +++ b/packages/common/test/sites/reharvestSiteCatalog.test.ts @@ -0,0 +1,22 @@ +import * as fetchMock from "fetch-mock"; +import { MOCK_CONTEXT } from "../mocks/mock-auth"; +import { reharvestSiteCatalog } from "../../src/sites/reharvestSiteCatalog"; + +describe("reharvestSiteCatalog", () => { + it("correctly calls the reharvest endpoint", async () => { + const url = `${MOCK_CONTEXT.hubUrl}/api/v3/jobs/site/some-site-id/harvest`; + const expectedResults = [ + { + groupId: "some-group-id", + jobId: "some-job-id", + status: 202, + }, + ]; + fetchMock.once(url, expectedResults); + const actualResults = await reharvestSiteCatalog( + "some-site-id", + MOCK_CONTEXT + ); + expect(actualResults).toEqual(expectedResults); + }); +});