20526: Add created_at descending as secondary sort key
[arvados.git] / src / store / users / user-panel-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, 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';
22
23 export class UserMiddlewareService extends DataExplorerMiddlewareService {
24     constructor(private services: ServiceRepository, id: string) {
25         super(id);
26     }
27
28     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
29         const state = api.getState();
30         const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
31         try {
32             const users = await this.services.userService.list(getParams(dataExplorer));
33             api.dispatch(updateResources(users.items));
34             api.dispatch(setItems(users));
35
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)
42                     .getFilters()
43             });
44             api.dispatch(updateResources(allUserMemberships.items));
45         } catch {
46             api.dispatch(couldNotFetchUsers());
47         }
48     }
49 }
50
51 const getParams = (dataExplorer: DataExplorer) => ({
52     ...dataExplorerToListParams(dataExplorer),
53     order: getOrder(dataExplorer),
54     filters: new FilterBuilder()
55         .addFullTextSearch(dataExplorer.searchValue)
56         .getFilters()
57 });
58
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
64             ? OrderDirection.ASC
65             : OrderDirection.DESC;
66
67         if (sortColumn.name === UserPanelColumnNames.NAME) {
68             order.addOrder(sortDirection, "firstName")
69                 .addOrder(sortDirection, "lastName");
70         } else {
71             order.addOrder(sortDirection, sortColumn.sort.field);
72         }
73
74         // Use createdAt as a secondary sort column so we break ties consistently.
75         order.addOrder(OrderDirection.DESC, "createdAt");
76     }
77     return order.getOrder();
78 };
79
80 export const setItems = (listResults: ListResults<UserResource>) =>
81     userBindedActions.SET_ITEMS({
82         ...listResultsToDataExplorerItemsMeta(listResults),
83         items: listResults.items.map(resource => resource.uuid),
84     });
85
86 const couldNotFetchUsers = () =>
87     snackbarActions.OPEN_SNACKBAR({
88         message: 'Could not fetch users.',
89         kind: SnackbarKind.ERROR
90     });