diff --git a/src/modules/permissions/modules/objects/api/objects.js b/src/modules/permissions/modules/objects/api/objects.js index 27dcc778d..c47e1dc36 100644 --- a/src/modules/permissions/modules/objects/api/objects.js +++ b/src/modules/permissions/modules/objects/api/objects.js @@ -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, diff --git a/src/modules/permissions/modules/objects/modules/rbac/api/objectsRbac.js b/src/modules/permissions/modules/objects/modules/rbac/api/objectsRbac.js index d3899d4ee..b8223199e 100644 --- a/src/modules/permissions/modules/objects/modules/rbac/api/objectsRbac.js +++ b/src/modules/permissions/modules/objects/modules/rbac/api/objectsRbac.js @@ -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, + ]); + } +};