15019: Changes joinFilters() to accept variable number of args.
[arvados.git] / src / store / collections-content-address-panel / collections-content-address-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 } from '~/store/data-explorer/data-explorer-middleware-service';
8 import { RootState } from '~/store/store';
9 import { getUserUuid } from "~/common/getuser";
10 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
11 import { getDataExplorer } from '~/store/data-explorer/data-explorer-reducer';
12 import { resourcesActions } from '~/store/resources/resources-actions';
13 import { FilterBuilder } from '~/services/api/filter-builder';
14 import { SortDirection } from '~/components/data-table/data-column';
15 import { OrderDirection, OrderBuilder } from '~/services/api/order-builder';
16 import { getSortColumn } from "~/store/data-explorer/data-explorer-reducer";
17 import { FavoritePanelColumnNames } from '~/views/favorite-panel/favorite-panel';
18 import { GroupContentsResource, GroupContentsResourcePrefix } from '~/services/groups-service/groups-service';
19 import { progressIndicatorActions } from '~/store/progress-indicator/progress-indicator-actions';
20 import { collectionsContentAddressActions } from './collections-content-address-panel-actions';
21 import { navigateTo } from '~/store/navigation/navigation-action';
22 import { updateFavorites } from '~/store/favorites/favorites-actions';
23 import { updatePublicFavorites } from '~/store/public-favorites/public-favorites-actions';
24 import { setBreadcrumbs } from '../breadcrumbs/breadcrumbs-actions';
25 import { ResourceKind, extractUuidKind } from '~/models/resource';
26 import { ownerNameActions } from '~/store/owner-name/owner-name-actions';
27
28 export class CollectionsWithSameContentAddressMiddlewareService extends DataExplorerMiddlewareService {
29     constructor(private services: ServiceRepository, id: string) {
30         super(id);
31     }
32
33     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
34         const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
35         if (!dataExplorer) {
36             api.dispatch(collectionPanelDataExplorerIsNotSet());
37         } else {
38             const sortColumn = getSortColumn(dataExplorer);
39
40             const contentOrder = new OrderBuilder<GroupContentsResource>();
41
42             if (sortColumn && sortColumn.name === FavoritePanelColumnNames.NAME) {
43                 const direction = sortColumn.sortDirection === SortDirection.ASC
44                     ? OrderDirection.ASC
45                     : OrderDirection.DESC;
46
47                 contentOrder
48                     .addOrder(direction, "name", GroupContentsResourcePrefix.COLLECTION);
49             }
50             try {
51                 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
52                 const userUuid = getUserUuid(api.getState());
53                 const pathname = api.getState().router.location!.pathname;
54                 const contentAddress = pathname.split('/')[2];
55                 const response = await this.services.collectionService.list({
56                     limit: dataExplorer.rowsPerPage,
57                     offset: dataExplorer.page * dataExplorer.rowsPerPage,
58                     filters: new FilterBuilder()
59                         .addEqual('portable_data_hash', contentAddress)
60                         .addILike("name", dataExplorer.searchValue)
61                         .getFilters()
62                 });
63                 const userUuids = response.items.map(it => {
64                     if (extractUuidKind(it.ownerUuid) === ResourceKind.USER) {
65                         return it.ownerUuid;
66                     } else {
67                         return '';
68                     }
69                 }
70                 );
71                 const groupUuids = response.items.map(it => {
72                     if (extractUuidKind(it.ownerUuid) === ResourceKind.GROUP) {
73                         return it.ownerUuid;
74                     } else {
75                         return '';
76                     }
77                 });
78                 const responseUsers = await this.services.userService.list({
79                     limit: dataExplorer.rowsPerPage,
80                     offset: dataExplorer.page * dataExplorer.rowsPerPage,
81                     filters: new FilterBuilder()
82                         .addIn('uuid', userUuids)
83                         .getFilters()
84                 });
85                 const responseGroups = await this.services.groupsService.list({
86                     limit: dataExplorer.rowsPerPage,
87                     offset: dataExplorer.page * dataExplorer.rowsPerPage,
88                     filters: new FilterBuilder()
89                         .addIn('uuid', groupUuids)
90                         .getFilters()
91                 });
92                 responseUsers.items.map(it => {
93                     api.dispatch<any>(ownerNameActions.SET_OWNER_NAME({ name: it.uuid === userUuid ? 'User: Me' : `User: ${it.firstName} ${it.lastName}`, uuid: it.uuid }));
94                 });
95                 responseGroups.items.map(it => {
96                     api.dispatch<any>(ownerNameActions.SET_OWNER_NAME({ name: `Project: ${it.name}`, uuid: it.uuid }));
97                 });
98                 api.dispatch<any>(setBreadcrumbs([{ label: 'Projects', uuid: userUuid }]));
99                 api.dispatch<any>(updateFavorites(response.items.map(item => item.uuid)));
100                 api.dispatch<any>(updatePublicFavorites(response.items.map(item => item.uuid)));
101                 if (response.itemsAvailable === 1) {
102                     api.dispatch<any>(navigateTo(response.items[0].uuid));
103                     api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
104                 } else {
105                     api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
106                     api.dispatch(resourcesActions.SET_RESOURCES(response.items));
107                     api.dispatch(collectionsContentAddressActions.SET_ITEMS({
108                         items: response.items.map((resource: any) => resource.uuid),
109                         itemsAvailable: response.itemsAvailable,
110                         page: Math.floor(response.offset / response.limit),
111                         rowsPerPage: response.limit
112                     }));
113                 }
114             } catch (e) {
115                 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
116                 api.dispatch(collectionsContentAddressActions.SET_ITEMS({
117                     items: [],
118                     itemsAvailable: 0,
119                     page: 0,
120                     rowsPerPage: dataExplorer.rowsPerPage
121                 }));
122                 api.dispatch(couldNotFetchCollections());
123             }
124         }
125     }
126 }
127
128 const collectionPanelDataExplorerIsNotSet = () =>
129     snackbarActions.OPEN_SNACKBAR({
130         message: 'Collection panel is not ready.',
131         kind: SnackbarKind.ERROR
132     });
133
134 const couldNotFetchCollections = () =>
135     snackbarActions.OPEN_SNACKBAR({
136         message: 'Could not fetch collection with this content address.',
137         kind: SnackbarKind.ERROR
138     });