1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { ServiceRepository } from 'services/services';
6 import { MiddlewareAPI, Dispatch } from 'redux';
7 import { DataExplorerMiddlewareService, dataExplorerToListParams, listResultsToDataExplorerItemsMeta } from 'store/data-explorer/data-explorer-middleware-service';
8 import { RootState } from 'store/store';
9 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
10 import { DataExplorer, getDataExplorer } from 'store/data-explorer/data-explorer-reducer';
11 import { updateResources } from 'store/resources/resources-actions';
12 import { FilterBuilder } from 'services/api/filter-builder';
13 import { SortDirection } from 'components/data-table/data-column';
14 import { OrderDirection, OrderBuilder } from 'services/api/order-builder';
15 import { ListResults } from 'services/common-service/common-service';
16 import { userBindedActions } from 'store/users/users-actions';
17 import { getSortColumn } from "store/data-explorer/data-explorer-reducer";
18 import { UserResource } from 'models/user';
19 import { UserPanelColumnNames } from 'views/user-panel/user-panel';
20 import { BuiltinGroups, getBuiltinGroupUuid } from 'models/group';
21 import { LinkClass } from 'models/link';
23 export class UserMiddlewareService extends DataExplorerMiddlewareService {
24 constructor(private services: ServiceRepository, id: string) {
28 async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
29 const state = api.getState();
30 const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
32 const users = await this.services.userService.list(getParams(dataExplorer));
33 api.dispatch(updateResources(users.items));
34 api.dispatch(setItems(users));
36 // Get "all users" group memberships
37 const allUsersGroupUuid = getBuiltinGroupUuid(state.auth.localCluster, BuiltinGroups.ALL);
38 const allUserMemberships = await this.services.permissionService.list({
39 filters: new FilterBuilder()
40 .addEqual('head_uuid', allUsersGroupUuid)
41 .addEqual('link_class', LinkClass.PERMISSION)
44 api.dispatch(updateResources(allUserMemberships.items));
46 api.dispatch(couldNotFetchUsers());
51 const getParams = (dataExplorer: DataExplorer) => ({
52 ...dataExplorerToListParams(dataExplorer),
53 order: getOrder(dataExplorer),
54 filters: new FilterBuilder()
55 .addFullTextSearch(dataExplorer.searchValue)
59 const getOrder = (dataExplorer: DataExplorer) => {
60 const sortColumn = getSortColumn<UserResource>(dataExplorer);
61 const order = new OrderBuilder<UserResource>();
62 if (sortColumn && sortColumn.sort) {
63 const sortDirection = sortColumn.sort.direction === SortDirection.ASC
65 : OrderDirection.DESC;
67 if (sortColumn.name === UserPanelColumnNames.NAME) {
68 order.addOrder(sortDirection, "firstName")
69 .addOrder(sortDirection, "lastName");
71 order.addOrder(sortDirection, sortColumn.sort.field);
74 return order.getOrder();
77 export const setItems = (listResults: ListResults<UserResource>) =>
78 userBindedActions.SET_ITEMS({
79 ...listResultsToDataExplorerItemsMeta(listResults),
80 items: listResults.items.map(resource => resource.uuid),
83 const couldNotFetchUsers = () =>
84 snackbarActions.OPEN_SNACKBAR({
85 message: 'Could not fetch users.',
86 kind: SnackbarKind.ERROR