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 { ListArguments, 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';
22 import { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
23 import { couldNotFetchItemsAvailable } from 'store/data-explorer/data-explorer-action';
25 export class UserMiddlewareService extends DataExplorerMiddlewareService {
26 constructor(private services: ServiceRepository, id: string) {
30 async requestItems(api: MiddlewareAPI<Dispatch, RootState>, criteriaChanged?: boolean, background?: boolean) {
31 const state = api.getState();
32 const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
34 if (!background) { api.dispatch(progressIndicatorActions.START_WORKING(this.getId())); }
35 const users = await this.services.userService.list(getParams(dataExplorer));
36 api.dispatch(updateResources(users.items));
37 api.dispatch(setItems(users));
39 // Get "all users" group memberships for account status
40 const allUsersGroupUuid = getBuiltinGroupUuid(state.auth.localCluster, BuiltinGroups.ALL);
41 const allUserMemberships = await this.services.permissionService.list({
42 filters: new FilterBuilder()
43 .addEqual('head_uuid', allUsersGroupUuid)
44 .addEqual('link_class', LinkClass.PERMISSION)
47 api.dispatch(updateResources(allUserMemberships.items));
49 api.dispatch(couldNotFetchUsers());
51 api.dispatch(progressIndicatorActions.STOP_WORKING(this.getId()));
55 async requestCount(api: MiddlewareAPI<Dispatch, RootState>, criteriaChanged?: boolean, background?: boolean) {
56 const state = api.getState();
57 const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
59 if (criteriaChanged) {
61 return this.services.userService.list(getCountParams(dataExplorer))
62 .then((results: ListResults<UserResource>) => {
63 if (results.itemsAvailable !== undefined) {
64 api.dispatch<any>(userBindedActions.SET_ITEMS_AVAILABLE(results.itemsAvailable));
66 couldNotFetchItemsAvailable();
73 const getFilters = (dataExplorer: DataExplorer) => (
75 .addFullTextSearch(dataExplorer.searchValue)
79 const getParams = (dataExplorer: DataExplorer): ListArguments => ({
80 ...dataExplorerToListParams(dataExplorer),
81 order: getOrder(dataExplorer),
82 filters: getFilters(dataExplorer),
86 const getCountParams = (dataExplorer: DataExplorer): ListArguments => ({
89 filters: getFilters(dataExplorer),
92 const getOrder = (dataExplorer: DataExplorer) => {
93 const sortColumn = getSortColumn<UserResource>(dataExplorer);
94 const order = new OrderBuilder<UserResource>();
95 if (sortColumn && sortColumn.sort) {
96 const sortDirection = sortColumn.sort.direction === SortDirection.ASC
98 : OrderDirection.DESC;
100 if (sortColumn.name === UserPanelColumnNames.NAME) {
101 order.addOrder(sortDirection, "firstName")
102 .addOrder(sortDirection, "lastName");
104 order.addOrder(sortDirection, sortColumn.sort.field);
107 // Use createdAt as a secondary sort column so we break ties consistently.
108 order.addOrder(OrderDirection.DESC, "createdAt");
110 return order.getOrder();
113 export const setItems = (listResults: ListResults<UserResource>) =>
114 userBindedActions.SET_ITEMS({
115 ...listResultsToDataExplorerItemsMeta(listResults),
116 items: listResults.items.map(resource => resource.uuid),
119 const couldNotFetchUsers = () =>
120 snackbarActions.OPEN_SNACKBAR({
121 message: 'Could not fetch users.',
122 kind: SnackbarKind.ERROR