Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rafactor/objects api [WTEL-4023] #548

Merged
merged 3 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 81 additions & 19 deletions src/modules/permissions/modules/objects/api/objects.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,91 @@
import {
EndpointGetterApiConsumer,
EndpointListGetterApiConsumer,
EndpointPatcherApiConsumer,
} from 'webitel-sdk/esm2015/api-consumers';
import instance from '../../../../../app/api/old/instance';
getDefaultGetListResponse,
getDefaultGetParams,
} from '@webitel/ui-sdk/src/api/defaults';
import applyTransform, {
camelToSnake,
generateUrl,
merge,
mergeEach,
notify,
sanitize,
snakeToCamel,
starToSearch,
} from '@webitel/ui-sdk/src/api/transformers';
import instance from '../../../../../app/api/instance';

const baseUrl = '/objclass';

const defaultListObject = { // default object prototype, to merge response with it to get all fields
class: '',
obac: false,
rbac: false,
id: 0,
const getObjectList = 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),
]);
try {
const response = await instance.get(url);
const { items, next } = applyTransform(response.data, [
snakeToCamel(),
merge(getDefaultGetListResponse()),
]);
return {
items: applyTransform(items, [
mergeEach(defaultObject),
]),
next,
};
} catch (err) {
throw applyTransform(err, [
notify,
]);
}
};

const getObject = async ({ itemId: id }) => {
const url = `${baseUrl}/${id}`;

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

const listGetter = new EndpointListGetterApiConsumer({
baseUrl,
instance,
}, { defaultListObject });
const itemGetter = new EndpointGetterApiConsumer({ baseUrl, instance });
const itemPatcher = new EndpointPatcherApiConsumer({ baseUrl, instance });
const patchObject = async ({ changes, id }) => {
const body = applyTransform(changes, [
camelToSnake(),
]);
const url = `${baseUrl}/${id}`;
try {
const response = await instance.patch(url, body);
return applyTransform(response.data, [
snakeToCamel(),
]);
} catch (err) {
throw applyTransform(err, [
notify,
]);
}
};

const getObjectList = (params) => listGetter.getList({ searchQuery: 'class', ...params });
const patchObject = (params) => itemPatcher.patchItem(params);
const getObject = (params) => itemGetter.getItem(params);

const ObjectsAPI = {
getList: getObjectList,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,68 @@
import {
EndpointListGetterApiConsumer,
EndpointPatcherApiConsumer,
} from 'webitel-sdk/esm2015/api-consumers';
import instance from '../../../../../../../app/api/old/instance';
getDefaultGetListResponse,
getDefaultGetParams,
} from '@webitel/ui-sdk/src/api/defaults';
import applyTransform, {
camelToSnake,
generateUrl,
merge,
notify,
sanitize,
snakeToCamel,
starToSearch,
} from '@webitel/ui-sdk/src/api/transformers';
import instance from '../../../../../../../app/api/instance';
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);
export const getObjclassDefaultList = async (params) => {
const fieldsToSend = ['page', 'size', 'q', 'sort', 'fields', 'id'];

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

try {
const response = await instance.get(url);
const { items, next } = applyTransform(response.data, [
snakeToCamel(),
merge(getDefaultGetListResponse()),
APIPermissionsGetter.handlePermissionsListResponse,
]);
return {
items,
next,
};
} catch (err) {
throw applyTransform(err, [
notify,
]);
}
};

const listGetter = new EndpointListGetterApiConsumer({ baseUrl, instance }, {
listResponseHandler: APIPermissionsGetter.handlePermissionsListResponse,
}).setGetListMethod(_getObjclassDefaultList);
const itemPatcher = new EndpointPatcherApiConsumer({
baseUrl,
instance,
}, { nestedUrl: 'grantor' });
export const patchObjclassDefaultMode = async ({ changes, parentId, id }) => {
const body = applyTransform(changes, [
camelToSnake(),
]);

export const getObjclassDefaultList = (params) => listGetter.getList(params);
export const patchObjclassDefaultMode = (params) => itemPatcher.patchNestedItem(params);
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,
]);
}
};