18123: Invert group member is visible logic
[arvados.git] / 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, 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 { 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
17 export class GroupDetailsPanelMembersMiddlewareService extends DataExplorerMiddlewareService {
18
19     constructor(private services: ServiceRepository, id: string) {
20         super(id);
21     }
22
23     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
24         const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
25         const groupUuid = getCurrentGroupDetailsPanelUuid(api.getState().properties);
26         if (!dataExplorer || !groupUuid) {
27             api.dispatch(groupsDetailsPanelDataExplorerIsNotSet());
28         } else {
29             try {
30                 const groupResource = await this.services.groupsService.get(groupUuid);
31                 api.dispatch(updateResources([groupResource]));
32
33                 const permissionsIn = await this.services.permissionService.list({
34                     filters: new FilterBuilder()
35                         .addEqual('head_uuid', groupUuid)
36                         .addEqual('link_class', LinkClass.PERMISSION)
37                         .getFilters()
38                 });
39                 api.dispatch(updateResources(permissionsIn.items));
40
41                 api.dispatch(GroupMembersPanelActions.SET_ITEMS({
42                     ...listResultsToDataExplorerItemsMeta(permissionsIn),
43                     items: permissionsIn.items.map(item => item.uuid),
44                 }));
45
46                 const usersIn = await this.services.userService.list({
47                     filters: new FilterBuilder()
48                         .addIn('uuid', permissionsIn.items
49                             .filter((item) => item.tailKind === ResourceKind.USER)
50                             .map(item => item.tailUuid))
51                         .getFilters(),
52                     count: "none"
53                 });
54                 api.dispatch(updateResources(usersIn.items));
55
56                 const projectsIn = await this.services.projectService.list({
57                     filters: new FilterBuilder()
58                         .addIn('uuid', permissionsIn.items
59                             .filter((item) => item.tailKind === ResourceKind.PROJECT)
60                             .map(item => item.tailUuid))
61                         .getFilters(),
62                     count: "none"
63                 });
64                 api.dispatch(updateResources(projectsIn.items));
65             } catch (e) {
66                 api.dispatch(couldNotFetchGroupDetailsContents());
67             }
68         }
69     }
70 }
71
72 const groupsDetailsPanelDataExplorerIsNotSet = () =>
73     snackbarActions.OPEN_SNACKBAR({
74         message: 'Group members panel is not ready.',
75         kind: SnackbarKind.ERROR
76     });
77
78 const couldNotFetchGroupDetailsContents = () =>
79     snackbarActions.OPEN_SNACKBAR({
80         message: 'Could not fetch group members.',
81         kind: SnackbarKind.ERROR
82     });