/
unshareEventWithGroups.ts
49 lines (48 loc) · 1.33 KB
/
unshareEventWithGroups.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { IArcGISContext } from "../../ArcGISContext";
import { IHubEvent } from "../../core/types/IHubEvent";
import { updateEvent } from "../api/events";
/**
* Unshares an event with one or more groups
* @param groupIds An array of group IDs to unshare the group from
* @param entity An IHubEvent object
* @param context An IArcGISContext object
* @returns An updated IHubEvent object
*/
export async function unshareEventWithGroups(
groupIds: string[],
entity: IHubEvent,
context: IArcGISContext
): Promise<IHubEvent> {
if (groupIds.length) {
try {
const {
readGroups: updatedReadGroupIds,
editGroups: updatedEditGroupIds,
} = await updateEvent({
eventId: entity.id,
data: {
readGroups: entity.readGroupIds.filter(
(groupId) => !groupIds.includes(groupId)
),
editGroups: entity.editGroupIds.filter(
(groupId) => !groupIds.includes(groupId)
),
},
...context.hubRequestOptions,
});
return {
...entity,
readGroupIds: updatedReadGroupIds,
editGroupIds: updatedEditGroupIds,
};
} catch (e) {
throw new Error(
`Entity: ${
entity.id
} could not be unshared with groups: ${groupIds.join(", ")}`
);
}
} else {
return entity;
}
}