X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/83cc752d85a910c19ffaf5c74433fbf7fa76e038..16d7bc7663c99f73f8f129d60021e6f3ebaec2a4:/src/services/common-service/common-resource-service.ts diff --git a/src/services/common-service/common-resource-service.ts b/src/services/common-service/common-resource-service.ts index d29ea15642..d9be8dae9f 100644 --- a/src/services/common-service/common-resource-service.ts +++ b/src/services/common-service/common-resource-service.ts @@ -3,9 +3,10 @@ // 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_NAME_VIOLATION = 'UniqueNameViolation', @@ -17,13 +18,42 @@ export enum CommonResourceServiceError { } export class CommonResourceService extends CommonService { - 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, showErrors?: boolean) { + 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, showErrors); + } + + update(uuid: string, data: Partial, showErrors?: boolean, 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, showErrors); } } export const getCommonResourceServiceError = (errorResponse: any) => { - if ('errors' in errorResponse && 'errorToken' in errorResponse) { + if ('errors' in errorResponse) { const error = errorResponse.errors.join(''); switch (true) { case /UniqueViolation/.test(error):