21128: cleanup, renamed some types Arvados-DCO-1.1-Signed-off-by: Lisa Knox <lisa...
[arvados.git] / src / store / user-profile / user-profile-groups-middleware-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { ServiceRepository } from 'services/services';
6 import { MiddlewareAPI, Dispatch } from 'redux';
7 import { DataExplorerMiddlewareService, listResultsToDataExplorerItemsMeta } from 'store/data-explorer/data-explorer-middleware-service';
8 import { RootState } from 'store/store';
9 import { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
10 import { getCurrentUserProfilePanelUuid, UserProfileGroupsActions } from 'store/user-profile/user-profile-actions';
11 import { updateResources } from 'store/resources/resources-actions';
12 import { FilterBuilder } from 'services/api/filter-builder';
13 import { LinkClass } from 'models/link';
14 import { ResourceKind } from 'models/resource';
15 import { GroupClass } from 'models/group';
16 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
17
18 export class UserProfileGroupsMiddlewareService extends DataExplorerMiddlewareService {
19     constructor(private services: ServiceRepository, id: string) {
20         super(id);
21     }
22
23     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
24         const state = api.getState();
25         const userUuid = getCurrentUserProfilePanelUuid(state.properties);
26         try {
27             api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
28
29             // Get user
30             const user = await this.services.userService.get(userUuid || '');
31             api.dispatch(updateResources([user]));
32
33             // Get user's group memberships
34             const groupMembershipLinks = await this.services.permissionService.list({
35                 filters: new FilterBuilder()
36                     .addEqual('tail_uuid', userUuid)
37                     .addEqual('link_class', LinkClass.PERMISSION)
38                     .addEqual('head_kind', ResourceKind.GROUP)
39                     .getFilters()
40             });
41             // Update resources, includes "project" groups
42             api.dispatch(updateResources(groupMembershipLinks.items));
43
44             // Get user's groups details and filter to role groups
45             const groups = await this.services.groupsService.list({
46                 filters: new FilterBuilder()
47                     .addIn('uuid', groupMembershipLinks.items
48                         .map(item => item.headUuid))
49                     .addEqual('group_class', GroupClass.ROLE)
50                     .getFilters(),
51                 count: "none"
52             });
53             api.dispatch(updateResources(groups.items));
54
55             // Get permission links for only role groups
56             const roleGroupMembershipLinks = await this.services.permissionService.list({
57                 filters: new FilterBuilder()
58                     .addIn('head_uuid', groups.items.map(item => item.uuid))
59                     .addEqual('tail_uuid', userUuid)
60                     .addEqual('link_class', LinkClass.PERMISSION)
61                     .addEqual('head_kind', ResourceKind.GROUP)
62                     .getFilters()
63             });
64
65             api.dispatch(UserProfileGroupsActions.SET_ITEMS({
66                 ...listResultsToDataExplorerItemsMeta(roleGroupMembershipLinks),
67                 items: roleGroupMembershipLinks.items.map(item => item.uuid),
68             }));
69         } catch {
70             api.dispatch(couldNotFetchGroups());
71         } finally {
72             api.dispatch(progressIndicatorActions.STOP_WORKING(this.getId()));
73         }
74     }
75 }
76
77 const couldNotFetchGroups = () =>
78     snackbarActions.OPEN_SNACKBAR({
79         message: 'Could not fetch groups.',
80         kind: SnackbarKind.ERROR
81     });