Merge branch '20455-noopener' refs #20455
[arvados.git] / services / workbench2 / src / store / group-details-panel / group-details-panel-members-middleware-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Dispatch, MiddlewareAPI } from "redux";
6 import { DataExplorerMiddlewareService, dataExplorerToListParams, listResultsToDataExplorerItemsMeta } from "store/data-explorer/data-explorer-middleware-service";
7 import { RootState } from "store/store";
8 import { ServiceRepository } from "services/services";
9 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
10 import { DataExplorer, getDataExplorer } from "store/data-explorer/data-explorer-reducer";
11 import { FilterBuilder } from 'services/api/filter-builder';
12 import { updateResources } from 'store/resources/resources-actions';
13 import { getCurrentGroupDetailsPanelUuid, GroupMembersPanelActions } from 'store/group-details-panel/group-details-panel-actions';
14 import { LinkClass } from 'models/link';
15 import { ResourceKind } from 'models/resource';
16 import { progressIndicatorActions } from 'store/progress-indicator/progress-indicator-actions';
17
18 export class GroupDetailsPanelMembersMiddlewareService extends DataExplorerMiddlewareService {
19
20     constructor(private services: ServiceRepository, id: string) {
21         super(id);
22     }
23
24     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
25         const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
26         const groupUuid = getCurrentGroupDetailsPanelUuid(api.getState().properties);
27         if (!dataExplorer || !groupUuid) {
28             // Noop if data explorer refresh is triggered from another panel
29             return;
30         } else {
31             try {
32                 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
33                 const groupResource = await this.services.groupsService.get(groupUuid);
34                 api.dispatch(updateResources([groupResource]));
35
36                 const permissionsIn = await this.services.permissionService.list(getParams(dataExplorer, groupUuid));
37                 api.dispatch(updateResources(permissionsIn.items));
38
39                 api.dispatch(GroupMembersPanelActions.SET_ITEMS({
40                     ...listResultsToDataExplorerItemsMeta(permissionsIn),
41                     items: permissionsIn.items.map(item => item.uuid),
42                 }));
43
44                 const usersIn = await this.services.userService.list({
45                     filters: new FilterBuilder()
46                         .addIn('uuid', permissionsIn.items
47                             .filter((item) => item.tailKind === ResourceKind.USER)
48                             .map(item => item.tailUuid))
49                         .getFilters(),
50                     count: "none"
51                 });
52                 api.dispatch(updateResources(usersIn.items));
53
54                 const projectsIn = await this.services.projectService.list({
55                     filters: new FilterBuilder()
56                         .addIn('uuid', permissionsIn.items
57                             .filter((item) => item.tailKind === ResourceKind.PROJECT)
58                             .map(item => item.tailUuid))
59                         .getFilters(),
60                     count: "none"
61                 });
62                 api.dispatch(updateResources(projectsIn.items));
63             } catch (e) {
64                 api.dispatch(couldNotFetchGroupDetailsContents());
65             } finally {
66                 api.dispatch(progressIndicatorActions.STOP_WORKING(this.getId()));
67             }
68         }
69     }
70 }
71
72 export const getParams = (dataExplorer: DataExplorer, groupUuid: string) => ({
73     ...dataExplorerToListParams(dataExplorer),
74     filters: getFilters(groupUuid),
75 });
76
77 export const getFilters = (groupUuid: string) => {
78     const filters = new FilterBuilder()
79         .addEqual('head_uuid', groupUuid)
80         .addEqual('link_class', LinkClass.PERMISSION)
81         .getFilters();
82
83     return filters;
84 };
85
86 const couldNotFetchGroupDetailsContents = () =>
87     snackbarActions.OPEN_SNACKBAR({
88         message: 'Could not fetch group members.',
89         kind: SnackbarKind.ERROR
90     });