Merge branch '17337-files-not-visible-in-arvados'
[arvados-workbench2.git] / src / services / common-service / common-resource-service.ts
index 471c32fa21020e8b9eb1db9ae248ad3c6aa30f8f..bc24f22796b21001bce09b358e1efe6c657d6248 100644 (file)
@@ -2,14 +2,13 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import * as _ from "lodash";
 import { AxiosInstance } from "axios";
 import { Resource } from "src/models/resource";
 import { ApiActions } from "~/services/api/api-actions";
 import { CommonService } from "~/services/common-service/common-service";
 
 export enum CommonResourceServiceError {
-    UNIQUE_VIOLATION = 'UniqueViolation',
+    UNIQUE_NAME_VIOLATION = 'UniqueNameViolation',
     OWNERSHIP_CYCLE = 'OwnershipCycle',
     MODIFYING_CONTAINER_REQUEST_FINAL_STATE = 'ModifyingContainerRequestFinalState',
     NAME_HAS_ALREADY_BEEN_TAKEN = 'NameHasAlreadyBeenTaken',
@@ -18,11 +17,27 @@ export enum CommonResourceServiceError {
 }
 
 export class CommonResourceService<T extends Resource> extends CommonService<T> {
+    constructor(serverApi: AxiosInstance, resourceType: string, actions: ApiActions, readOnlyFields: string[] = []) {
+        super(serverApi, resourceType, actions, readOnlyFields.concat([
+            'uuid',
+            'etag',
+            'kind'
+        ]));
+    }
 
-    constructor(serverApi: AxiosInstance, resourceType: string, actions: ApiActions) {
-        super(serverApi, resourceType, actions);
+    create(data?: Partial<T>) {
+        if (data !== undefined) {
+            this.readOnlyFields.forEach( field => delete data[field] );
+        }
+        return super.create(data);
+    }
+
+    update(uuid: string, data: Partial<T>) {
+        if (data !== undefined) {
+            this.readOnlyFields.forEach( field => delete data[field] );
+        }
+        return super.update(uuid, data);
     }
-    
 }
 
 export const getCommonResourceServiceError = (errorResponse: any) => {
@@ -30,7 +45,7 @@ export const getCommonResourceServiceError = (errorResponse: any) => {
         const error = errorResponse.errors.join('');
         switch (true) {
             case /UniqueViolation/.test(error):
-                return CommonResourceServiceError.UNIQUE_VIOLATION;
+                return CommonResourceServiceError.UNIQUE_NAME_VIOLATION;
             case /ownership cycle/.test(error):
                 return CommonResourceServiceError.OWNERSHIP_CYCLE;
             case /Mounts cannot be modified in state 'Final'/.test(error):