Merge branch '13862-collection-files-picker'
[arvados-workbench2.git] / src / services / ancestors-service / ancestors-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { GroupsService } from "~/services/groups-service/groups-service";
6 import { UserService } from '../user-service/user-service';
7 import { GroupResource } from '~/models/group';
8 import { UserResource } from '~/models/user';
9 import { extractUuidObjectType, ResourceObjectType, TrashableResource } from "~/models/resource";
10
11 export class AncestorService {
12     constructor(
13         private groupsService: GroupsService,
14         private userService: UserService
15     ) { }
16
17     async ancestors(uuid: string, rootUuid: string): Promise<Array<UserResource | GroupResource>> {
18         const service = this.getService(extractUuidObjectType(uuid));
19         if (service) {
20             try {
21                 const resource = await service.get(uuid);
22                 if (uuid === rootUuid) {
23                     return [resource];
24                 } else {
25                     return [
26                         ...await this.ancestors(resource.ownerUuid, rootUuid),
27                         resource
28                     ];
29                 }
30             } catch (e) {
31                 return [];
32             }
33         } else {
34             return [];
35         }
36     }
37
38     private getService = (objectType?: string) => {
39         switch (objectType) {
40             case ResourceObjectType.GROUP:
41                 return this.groupsService;
42             case ResourceObjectType.USER:
43                 return this.userService;
44             default:
45                 return undefined;
46         }
47     }
48 }