18215: Requests back only updated fields on collection update calls.
[arvados-workbench2.git] / src / services / common-service / common-resource-service.ts
index 17c287d22ebaf6fad2d60b774d0fc400f29af5b7..c6306779a9ee8cef4bb6eff287c485462f5e898a 100644 (file)
@@ -3,12 +3,13 @@
 // SPDX-License-Identifier: AGPL-3.0
 
 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";
+import { snakeCase } from "lodash";
+import { Resource } from "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',
@@ -17,8 +18,37 @@ export enum CommonResourceServiceError {
 }
 
 export class CommonResourceService<T extends Resource> extends CommonService<T> {
-   constructor(serverApi: AxiosInstance, resourceType: string, actions: ApiActions) {
-        super(serverApi, resourceType, actions);
+    constructor(serverApi: AxiosInstance, resourceType: string, actions: ApiActions, readOnlyFields: string[] = []) {
+        super(serverApi, resourceType, actions, readOnlyFields.concat([
+            'uuid',
+            'etag',
+            'kind'
+        ]));
+    }
+
+    create(data?: Partial<T>) {
+        let payload: any;
+        if (data !== undefined) {
+            this.readOnlyFields.forEach( field => delete data[field] );
+            payload = {
+                [this.resourceType.slice(0, -1)]: CommonService.mapKeys(snakeCase)(data),
+            };
+        }
+        return super.create(payload);
+    }
+
+    update(uuid: string, data: Partial<T>, select?: string[]) {
+        let payload: any;
+        if (data !== undefined) {
+            this.readOnlyFields.forEach( field => delete data[field] );
+            payload = {
+                [this.resourceType.slice(0, -1)]: CommonService.mapKeys(snakeCase)(data),
+            };
+            if (select !== undefined && select.length > 0) {
+                payload.select = ['uuid', ...select.map(field => snakeCase(field))];
+            };
+        }
+        return super.update(uuid, payload);
     }
 }
 
@@ -27,7 +57,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):