diff --git a/package-lock.json b/package-lock.json index 912daae5c7b..05895fb678f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -65010,7 +65010,7 @@ }, "packages/common": { "name": "@esri/hub-common", - "version": "14.80.1", + "version": "14.83.0", "license": "Apache-2.0", "dependencies": { "@terraformer/arcgis": "^2.1.2", diff --git a/packages/common/src/discussions/utils.ts b/packages/common/src/discussions/utils.ts index 4ac6e864b67..282e2c0a164 100644 --- a/packages/common/src/discussions/utils.ts +++ b/packages/common/src/discussions/utils.ts @@ -7,7 +7,7 @@ import { IChannel, SharingAccess, } from "./api/types"; -import { IFilter, IPredicate, IQuery } from "../search"; +import { IFilter, IHubSearchResult, IPredicate, IQuery } from "../search"; /** * Utility to determine if a given IGroup, IItem, IHubContent, or IHubItemEntity @@ -196,3 +196,36 @@ export function getChannelUsersQuery( }; return query; } + +/** + * Transforms a given channel and optional channel groups array into a IHubSearchResult + * @param channel + * @param groups + * @returns + */ +export const channelToSearchResult = ( + channel: IChannel, + groups?: IGroup[] +): IHubSearchResult => { + return { + ...channel, + id: channel.id, + name: channel.name, + createdDate: new Date(channel.createdAt), + createdDateSource: "channel", + updatedDate: new Date(channel.updatedAt), + updatedDateSource: "channel", + type: "channel", + access: channel.access, + family: "channel", + owner: channel.creator, + links: { + // TODO: add links? + thumbnail: null, + self: null, + siteRelative: null, + }, + includes: { groups }, + rawResult: channel, + }; +}; diff --git a/packages/common/src/search/_internal/hubSearchChannels.ts b/packages/common/src/search/_internal/hubSearchChannels.ts index 145bf4fc063..6633c6b521c 100644 --- a/packages/common/src/search/_internal/hubSearchChannels.ts +++ b/packages/common/src/search/_internal/hubSearchChannels.ts @@ -12,8 +12,9 @@ import { IPagedResponse, ISearchChannels, ISearchChannelsParams, + channelToSearchResult, } from "../../discussions"; -import { getGroup } from "@esri/arcgis-rest-portal"; +import { IGroup, getGroup } from "@esri/arcgis-rest-portal"; /** * @private @@ -112,61 +113,41 @@ export const processSearchParams = ( * @param {IHubSearchOptions} options * @returns IHubSearchResponse */ -export const toHubSearchResult = async ( +export const toHubSearchResults = async ( channelsResponse: IPagedResponse, query: IQuery, options: IHubSearchOptions ): Promise> => { const { total, items, nextStart } = channelsResponse; // Convert IChannel to IHubSearchResult - const channelToSearchResult = async ( - channel: IChannel - ): Promise => { - return { - ...channel, - id: channel.id, - name: channel.name, - createdDate: new Date(channel.createdAt), - createdDateSource: "channel", - updatedDate: new Date(channel.updatedAt), - updatedDateSource: "channel", - type: "channel", - access: channel.access, - family: "channel", - owner: channel.creator, - links: { - // TODO: add links? - thumbnail: null, - self: null, - siteRelative: null, - }, - includes: { - // when 'include' requests 'groups' enrichment, get group details - groups: options.include?.includes("groups") - ? await Promise.all( - channel.groups.map(async (groupId) => { - let group; - try { - group = await getGroup(groupId, options.requestOptions); - } catch (e) { - group = null; - /* tslint:disable-next-line: no-console */ - console.warn( - `Cannot fetch group enhancement for id = ${groupId}`, - e - ); - } - return group; - }) - ) - : [], - }, - rawResult: channel, - }; - }; + const itemsAndGroups = await Promise.all( + items.map(async (channel) => { + const groups = options.include?.includes("groups") + ? await Promise.all( + channel.groups.map(async (groupId) => { + let group; + try { + group = await getGroup(groupId, options.requestOptions); + } catch (e) { + group = null; + /* tslint:disable-next-line: no-console */ + console.warn( + `Cannot fetch group enhancement for id = ${groupId}`, + e + ); + } + return group; + }) + ) + : []; + return { channel, groups }; + }) + ); return { total, - results: await Promise.all(items.map(channelToSearchResult)), + results: itemsAndGroups.map(({ channel, groups }) => + channelToSearchResult(channel, groups) + ), hasNext: nextStart > -1, next: () => { return hubSearchChannels(query, { @@ -193,5 +174,5 @@ export const hubSearchChannels = async ( // Call to searchChannels const channelsResponse = await searchChannels(searchOptions); // Parse into > - return toHubSearchResult(channelsResponse, query, options); + return toHubSearchResults(channelsResponse, query, options); }; diff --git a/packages/common/test/search/_internal/hubSearchChannels.test.ts b/packages/common/test/search/_internal/hubSearchChannels.test.ts index bde3cc7ff22..7f036970db0 100644 --- a/packages/common/test/search/_internal/hubSearchChannels.test.ts +++ b/packages/common/test/search/_internal/hubSearchChannels.test.ts @@ -11,7 +11,7 @@ import * as arcgisRestPortal from "@esri/arcgis-rest-portal"; describe("discussionsSearchItems Module |", () => { let processSearchParamsSpy: jasmine.Spy; - let toHubSearchResultSpy: jasmine.Spy; + let toHubSearchResultsSpy: jasmine.Spy; let searchChannelsSpy: jasmine.Spy; let getGroupSpy: jasmine.Spy; @@ -20,9 +20,9 @@ describe("discussionsSearchItems Module |", () => { hubSearchChannels, "processSearchParams" ).and.callThrough(); - toHubSearchResultSpy = spyOn( + toHubSearchResultsSpy = spyOn( hubSearchChannels, - "toHubSearchResult" + "toHubSearchResults" ).and.callThrough(); searchChannelsSpy = spyOn(API, "searchChannels").and.callFake(() => { return Promise.resolve(SEARCH_CHANNELS_RESPONSE); @@ -60,7 +60,7 @@ describe("discussionsSearchItems Module |", () => { }; const result = await hubSearchChannels.hubSearchChannels(qry, opts); expect(processSearchParamsSpy).toHaveBeenCalledTimes(1); - expect(toHubSearchResultSpy).toHaveBeenCalledTimes(1); + expect(toHubSearchResultsSpy).toHaveBeenCalledTimes(1); expect(searchChannelsSpy).toHaveBeenCalledTimes(1); expect(result).toBeTruthy(); const nextResult = await result.next(); @@ -117,7 +117,7 @@ describe("discussionsSearchItems Module |", () => { }; const result = await hubSearchChannels.hubSearchChannels(qry, opts); expect(processSearchParamsSpy).toHaveBeenCalledTimes(1); - expect(toHubSearchResultSpy).toHaveBeenCalledTimes(1); + expect(toHubSearchResultsSpy).toHaveBeenCalledTimes(1); expect(searchChannelsSpy).toHaveBeenCalledTimes(1); expect(result).toBeTruthy(); });