18874: Add 'services/workbench2/' from commit 'f6f88d9ca9cdeeeebfadcfe999789bfb9f69e5c6'
[arvados.git] / services / workbench2 / src / services / common-service / common-resource-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { AxiosInstance } from "axios";
6 import { snakeCase } from "lodash";
7 import { Resource } from "models/resource";
8 import { ApiActions } from "services/api/api-actions";
9 import { CommonService } from "services/common-service/common-service";
10
11 export enum CommonResourceServiceError {
12     UNIQUE_NAME_VIOLATION = 'UniqueNameViolation',
13     OWNERSHIP_CYCLE = 'OwnershipCycle',
14     MODIFYING_CONTAINER_REQUEST_FINAL_STATE = 'ModifyingContainerRequestFinalState',
15     NAME_HAS_ALREADY_BEEN_TAKEN = 'NameHasAlreadyBeenTaken',
16     PERMISSION_ERROR_FORBIDDEN = 'PermissionErrorForbidden',
17     SOURCE_DESTINATION_CANNOT_BE_SAME = 'SourceDestinationCannotBeSame',
18     UNKNOWN = 'Unknown',
19     NONE = 'None'
20 }
21
22 export class CommonResourceService<T extends Resource> extends CommonService<T> {
23     constructor(serverApi: AxiosInstance, resourceType: string, actions: ApiActions, readOnlyFields: string[] = []) {
24         super(serverApi, resourceType, actions, readOnlyFields.concat([
25             'uuid',
26             'etag',
27             'kind'
28         ]));
29     }
30
31     create(data?: Partial<T>, showErrors?: boolean) {
32         let payload: any;
33         if (data !== undefined) {
34             this.readOnlyFields.forEach( field => delete data[field] );
35             payload = {
36                 [this.resourceType.slice(0, -1)]: CommonService.mapKeys(snakeCase)(data),
37             };
38         }
39         return super.create(payload, showErrors);
40     }
41
42     update(uuid: string, data: Partial<T>, showErrors?: boolean, select?: string[]) {
43         let payload: any;
44         if (data !== undefined) {
45             this.readOnlyFields.forEach( field => delete data[field] );
46             payload = {
47                 [this.resourceType.slice(0, -1)]: CommonService.mapKeys(snakeCase)(data),
48             };
49             if (select !== undefined && select.length > 0) {
50                 payload.select = ['uuid', ...select.map(field => snakeCase(field))];
51             };
52         }
53         return super.update(uuid, payload, showErrors);
54     }
55 }
56
57 export const getCommonResourceServiceError = (errorResponse: any) => {
58     if (errorResponse && 'errors' in errorResponse) {
59         const error = errorResponse.errors.join('');
60         switch (true) {
61             case /UniqueViolation/.test(error):
62                 return CommonResourceServiceError.UNIQUE_NAME_VIOLATION;
63             case /ownership cycle/.test(error):
64                 return CommonResourceServiceError.OWNERSHIP_CYCLE;
65             case /Mounts cannot be modified in state 'Final'/.test(error):
66                 return CommonResourceServiceError.MODIFYING_CONTAINER_REQUEST_FINAL_STATE;
67             case /Name has already been taken/.test(error):
68                 return CommonResourceServiceError.NAME_HAS_ALREADY_BEEN_TAKEN;
69             case /403 Forbidden/.test(error):
70                 return CommonResourceServiceError.PERMISSION_ERROR_FORBIDDEN;
71             case new RegExp(CommonResourceServiceError.SOURCE_DESTINATION_CANNOT_BE_SAME).test(error):
72                 return CommonResourceServiceError.SOURCE_DESTINATION_CANNOT_BE_SAME;
73             default:
74                 return CommonResourceServiceError.UNKNOWN;
75         }
76     }
77     return CommonResourceServiceError.NONE;
78 };