Skip to content

Commit

Permalink
refactor: objects api and module [WTEL-4023]
Browse files Browse the repository at this point in the history
  • Loading branch information
lizacoma committed Nov 7, 2023
1 parent fcc4854 commit 7bec445
Showing 1 changed file with 23 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import applyTransform, {
camelToSnake,
generateUrl,
merge,
mergeEach,
notify,
sanitize,
snakeToCamel,
Expand All @@ -17,44 +16,29 @@ import APIPermissionsGetter
from '../../../../../../../app/api/old/PermissionsAPIService/APIPermissionsGetter';

const baseUrl = '/acl/objclass';
const nestedURL = 'grantor';

const _getObjclassDefaultList = (method) => function(params) {
const baseUrl = `${this.baseUrl}/${params.parentId}`;
return method(params, baseUrl);
};

// const listGetter = new EndpointListGetterApiConsumer({ baseUrl, instance }, {
// listResponseHandler: APIPermissionsGetter.handlePermissionsListResponse,
// }).setGetListMethod(_getObjclassDefaultList);
export const getObjclassDefaultList = async (params) => {
const fieldsToSend = ['page', 'size', 'q', 'sort', 'fields', 'id'];

const defaultObject = {
// default object prototype, to merge response with it to get all fields
class: '',
obac: false,
rbac: false,
id: 0,
};

const url = applyTransform(params, [
merge(getDefaultGetParams()),
starToSearch('search'),
(params) => ({ ...params, q: params.search }),
sanitize(fieldsToSend),
camelToSnake(),
generateUrl(baseUrl),
generateUrl(`${baseUrl}/${params.parentId}`),
]);

try {
const response = await instance.get(url);
const { items, next } = applyTransform(response.data, [
snakeToCamel(),
merge(getDefaultGetListResponse()),
APIPermissionsGetter.handlePermissionsListResponse,
]);
return {
items: applyTransform(items, [
mergeEach(defaultObject),
]),
items,
next,
};
} catch (err) {
Expand All @@ -64,8 +48,21 @@ export const getObjclassDefaultList = async (params) => {
}
};

// const itemPatcher = new EndpointPatcherApiConsumer({
// baseUrl,
// instance,
// }, { nestedUrl: 'grantor' });
export const patchObjclassDefaultMode = (params) => itemPatcher.patchNestedItem(params);
export const patchObjclassDefaultMode = async ({ changes, parentId, id }) => {
const body = applyTransform(changes, [
camelToSnake(),
]);

const url = `${baseUrl}/${parentId}/${nestedURL}/${id}`;

try {
const response = await instance.patch(url, body);
return applyTransform(response.data, [
snakeToCamel(),
]);
} catch (err) {
throw applyTransform(err, [
notify,
]);
}
};

0 comments on commit 7bec445

Please sign in to comment.