Skip to content

Commit

Permalink
fix(hub-common): refactor hubSearchChannels for more general use (#1398)
Browse files Browse the repository at this point in the history
  • Loading branch information
juliannaeapicella authored Feb 6, 2024
1 parent 573cde1 commit 8aba9aa
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 56 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 34 additions & 1 deletion packages/common/src/discussions/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
};
};
79 changes: 30 additions & 49 deletions packages/common/src/search/_internal/hubSearchChannels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -112,61 +113,41 @@ export const processSearchParams = (
* @param {IHubSearchOptions} options
* @returns IHubSearchResponse<IHubSearchResult>
*/
export const toHubSearchResult = async (
export const toHubSearchResults = async (
channelsResponse: IPagedResponse<IChannel>,
query: IQuery,
options: IHubSearchOptions
): Promise<IHubSearchResponse<IHubSearchResult>> => {
const { total, items, nextStart } = channelsResponse;
// Convert IChannel to IHubSearchResult
const channelToSearchResult = async (
channel: IChannel
): Promise<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: {
// 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, {
Expand All @@ -193,5 +174,5 @@ export const hubSearchChannels = async (
// Call to searchChannels
const channelsResponse = await searchChannels(searchOptions);
// Parse into <IHubSearchResponse<IHubSearchResult>>
return toHubSearchResult(channelsResponse, query, options);
return toHubSearchResults(channelsResponse, query, options);
};
10 changes: 5 additions & 5 deletions packages/common/test/search/_internal/hubSearchChannels.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
});
Expand Down

0 comments on commit 8aba9aa

Please sign in to comment.