// SPDX-License-Identifier: AGPL-3.0
import * as _ from "lodash";
-import { FilterBuilder } from "./filter-builder";
-import { OrderBuilder } from "./order-builder";
import { AxiosInstance, AxiosPromise } from "axios";
-import { Resource } from "../../models/resource";
+import { Resource } from "~/models/resource";
export interface ListArguments {
limit?: number;
offset?: number;
- filters?: FilterBuilder;
- order?: OrderBuilder;
+ filters?: string;
+ order?: string;
select?: string[];
distinct?: boolean;
count?: string;
errorToken: string;
}
+export enum CommonResourceServiceError {
+ UNIQUE_VIOLATION = 'UniqueViolation',
+ OWNERSHIP_CYCLE = 'OwnershipCycle',
+ UNKNOWN = 'Unknown',
+ NONE = 'None'
+}
+
export class CommonResourceService<T extends Resource> {
static mapResponseKeys = (response: any): Promise<any> =>
this.resourceType = '/' + resourceType + '/';
}
- create(data: Partial<T>) {
+ create(data?: Partial<T> | any) {
return CommonResourceService.defaultResponse(
this.serverApi
- .post<T>(this.resourceType, CommonResourceService.mapKeys(_.snakeCase)(data)));
+ .post<T>(this.resourceType, data && CommonResourceService.mapKeys(_.snakeCase)(data)));
}
delete(uuid: string): Promise<T> {
const { filters, order, ...other } = args;
const params = {
...other,
- filters: filters ? filters.serialize() : undefined,
- order: order ? order.getOrder() : undefined
+ filters: filters ? `[${filters}]` : undefined,
+ order: order ? order : undefined
};
return CommonResourceService.defaultResponse(
this.serverApi
}));
}
- update(uuid: string, data: any) {
+ update(uuid: string, data: Partial<T>) {
return CommonResourceService.defaultResponse(
this.serverApi
- .put<T>(this.resourceType + uuid, data));
-
+ .put<T>(this.resourceType + uuid, data && CommonResourceService.mapKeys(_.snakeCase)(data)));
+
}
+
}
+export const getCommonResourceServiceError = (errorResponse: any) => {
+ if ('errors' in errorResponse && 'errorToken' in errorResponse) {
+ const error = errorResponse.errors.join('');
+ switch (true) {
+ case /UniqueViolation/.test(error):
+ return CommonResourceServiceError.UNIQUE_VIOLATION;
+ case /ownership cycle/.test(error):
+ return CommonResourceServiceError.OWNERSHIP_CYCLE;
+ default:
+ return CommonResourceServiceError.UNKNOWN;
+ }
+ }
+ return CommonResourceServiceError.NONE;
+};
+
+