From ecddbe8d1d61be691eaa11d20ccfc5f038b7232a Mon Sep 17 00:00:00 2001 From: liza-pohranichna Date: Mon, 10 Jul 2023 19:22:49 +0300 Subject: [PATCH 1/6] feature: refactoring reles api in progress [WTEL-3630] --- .../permissions/modules/roles/api/roles.js | 171 ++++++++++++++---- 1 file changed, 133 insertions(+), 38 deletions(-) diff --git a/src/modules/permissions/modules/roles/api/roles.js b/src/modules/permissions/modules/roles/api/roles.js index e88b6c2c2..021e486c9 100644 --- a/src/modules/permissions/modules/roles/api/roles.js +++ b/src/modules/permissions/modules/roles/api/roles.js @@ -1,60 +1,155 @@ -import deepCopy from 'deep-copy'; import { - EndpointListGetterApiConsumer, - EndpointGetterApiConsumer, - EndpointCreatorApiConsumer, - EndpointUpdaterApiConsumer, - EndpointDeleterApiConsumer, -} from 'webitel-sdk/esm2015/api-consumers'; + getDefaultGetListResponse, + getDefaultGetParams, +} from '@webitel/ui-sdk/src/api/defaults'; +import applyTransform, { + camelToSnake, handleUnauthorized, + merge, notify, snakeToCamel, + starToSearch, log, sanitize, + generateUrl, mergeEach, +} from '@webitel/ui-sdk/src/api/transformers'; +import deepCopy from 'deep-copy'; import ApplicationsAccess from '@webitel/ui-sdk/src/modules/Userinfo/classes/ApplicationsAccess'; import instance from '../../../../../app/api/old/instance'; const baseUrl = '/roles'; - const fieldsToSend = ['name', 'description', 'permissions', 'metadata']; -const defaultSingleObject = { - name: '', - description: '', - permissions: [], - metadata: {}, -}; - -const itemResponseHandler = (item) => { - // eslint-disable-next-line no-param-reassign - item.metadata.access = new ApplicationsAccess({ access: item.metadata.access }).getAccess(); - return item; -}; - const preRequestHandler = (item) => { const itemCopy = deepCopy(item); itemCopy.metadata.access = ApplicationsAccess.minify(item.metadata.access); return itemCopy; }; -const listGetter = new EndpointListGetterApiConsumer({ baseUrl, instance }); -const itemGetter = new EndpointGetterApiConsumer({ baseUrl, instance }, - { defaultSingleObject, itemResponseHandler }); -const extendedRolesListGetter = new EndpointListGetterApiConsumer({ baseUrl, instance }); -const itemCreator = new EndpointCreatorApiConsumer({ baseUrl, instance }, - { fieldsToSend, preRequestHandler }); -const itemUpdater = new EndpointUpdaterApiConsumer({ baseUrl, instance }, - { fieldsToSend, preRequestHandler }); -const itemDeleter = new EndpointDeleterApiConsumer({ baseUrl, instance }); +// const listGetter = new EndpointListGetterApiConsumer({ baseUrl, instance }); +const getRoleList = 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), + ]); + try { + const response = await instance.get(url); + const { items, next } = applyTransform(response.data, [ + snakeToCamel(), + merge(getDefaultGetListResponse()), + ]); + return { + items, + next, + }; + } catch (err) { + throw applyTransform(err, [ + handleUnauthorized, + notify, + ]); + } +}; +// const itemGetter = new EndpointGetterApiConsumer({ baseUrl, instance }, +// { defaultSingleObject, itemResponseHandler }); +const getRole = async ({ itemId: id }) => { + const defaultObject = { + name: '', + description: '', + permissions: [], + metadata: {}, + }; + + const itemResponseHandler = (response) => { + const copy = deepCopy(response); + copy.metadata.access = new ApplicationsAccess({ access: copy.metadata.access }).getAccess(); + return copy; + }; + + const url = `${baseUrl}/${id}`; + + try { + const response = await instance.get(url); + return applyTransform(response.data, [ + snakeToCamel(), + merge(defaultObject), + itemResponseHandler, + ]); + } catch (err) { + throw applyTransform(err, [ + handleUnauthorized, + notify, + ]); + } +}; +// const extendedRolesListGetter = new EndpointListGetterApiConsumer({ baseUrl, instance }); +const getExtendedRoles = (params) => extendedRolesListGetter.getList(params); +// const itemCreator = new EndpointCreatorApiConsumer({ baseUrl, instance }, +// { fieldsToSend, preRequestHandler }); +const addRole = async ({ itemInstance }) => { + const item = applyTransform(itemInstance, [ + preRequestHandler, + sanitize(fieldsToSend), + camelToSnake(), + ]); + try { + const response = await instance.post(baseUrl, item); + return applyTransform(response.data, [ + snakeToCamel(), + ]); + } catch (err) { + throw applyTransform(err, [ + handleUnauthorized, + notify, + ]); + } +}; +// const itemUpdater = new EndpointUpdaterApiConsumer({ baseUrl, instance }, +// { fieldsToSend, preRequestHandler }); +const updateRole = async ({ itemInstance, itemId: id }) => { + const item = applyTransform(itemInstance, [ + preRequestHandler, + sanitize(fieldsToSend), + camelToSnake(), + ]); + + const url = `${baseUrl}/${id}`; + try { + const response = await instance.put(url, item); + return applyTransform(response.data, [ + snakeToCamel(), + ]); + } catch (err) { + throw applyTransform(err, [ + handleUnauthorized, + notify, + ]); + } +}; +// const itemDeleter = new EndpointDeleterApiConsumer({ baseUrl, instance }); +const deleteRole = async ({ id }) => { + const url = `${baseUrl}/${id}`; + try { + const response = await instance.delete(url); + return applyTransform(response.data, []); + } catch (err) { + throw applyTransform(err, [ + handleUnauthorized, + notify, + ]); + } +}; + +const getRolesLookup = (params) => getRoleList({ + ...params, + fields: params.fields || ['id', 'name'], +}); const PERMISSIONS_LIST_URL = '/permissions'; const permissionsListGetter = new EndpointListGetterApiConsumer( { baseUrl: PERMISSIONS_LIST_URL, instance }, ); -const getRoleList = (params) => listGetter.getList(params); -const getExtendedRoles = (params) => extendedRolesListGetter.getList(params); -const getRole = (params) => itemGetter.getItem(params); -const addRole = (params) => itemCreator.createItem(params); -const updateRole = (params) => itemUpdater.updateItem(params); -const deleteRole = (params) => itemDeleter.deleteItem(params); -const getRolesLookup = (params) => listGetter.getLookup(params); - const getPermissionsOptions = (payload) => permissionsListGetter.getList(payload); const RolesAPI = { From b004b3d73196042225256e2ac21a5548c72c4e87 Mon Sep 17 00:00:00 2001 From: lizacoma Date: Tue, 11 Jul 2023 21:21:05 +0300 Subject: [PATCH 2/6] feature: roles api refactoring [WTEL-3630] --- .../permissions/modules/roles/api/roles.js | 73 ++++++++++++++++--- 1 file changed, 64 insertions(+), 9 deletions(-) diff --git a/src/modules/permissions/modules/roles/api/roles.js b/src/modules/permissions/modules/roles/api/roles.js index 021e486c9..0fd6092c1 100644 --- a/src/modules/permissions/modules/roles/api/roles.js +++ b/src/modules/permissions/modules/roles/api/roles.js @@ -10,15 +10,17 @@ import applyTransform, { } from '@webitel/ui-sdk/src/api/transformers'; import deepCopy from 'deep-copy'; import ApplicationsAccess from '@webitel/ui-sdk/src/modules/Userinfo/classes/ApplicationsAccess'; -import instance from '../../../../../app/api/old/instance'; +import instance from '../../../../../app/api/instance'; const baseUrl = '/roles'; const fieldsToSend = ['name', 'description', 'permissions', 'metadata']; const preRequestHandler = (item) => { - const itemCopy = deepCopy(item); - itemCopy.metadata.access = ApplicationsAccess.minify(item.metadata.access); - return itemCopy; + const copy = deepCopy(item); + console.log('copy.metadata.access:', copy.metadata.access); + copy.metadata.access = ApplicationsAccess.minify(copy.metadata.access); + console.log('copy.metadata.access:', copy.metadata.access); + return copy; }; // const listGetter = new EndpointListGetterApiConsumer({ baseUrl, instance }); @@ -83,7 +85,34 @@ const getRole = async ({ itemId: id }) => { } }; // const extendedRolesListGetter = new EndpointListGetterApiConsumer({ baseUrl, instance }); -const getExtendedRoles = (params) => extendedRolesListGetter.getList(params); +const getExtendedRoles = 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), + ]); + try { + const response = await instance.get(url); + const { items, next } = applyTransform(response.data, [ + snakeToCamel(), + merge(getDefaultGetListResponse()), + ]); + return { + items, + next, + }; + } catch (err) { + throw applyTransform(err, [ + handleUnauthorized, + notify, + ]); + } +}; // const itemCreator = new EndpointCreatorApiConsumer({ baseUrl, instance }, // { fieldsToSend, preRequestHandler }); const addRole = async ({ itemInstance }) => { @@ -146,11 +175,37 @@ const getRolesLookup = (params) => getRoleList({ }); const PERMISSIONS_LIST_URL = '/permissions'; -const permissionsListGetter = new EndpointListGetterApiConsumer( - { baseUrl: PERMISSIONS_LIST_URL, instance }, -); +// const permissionsListGetter = new EndpointListGetterApiConsumer( +// { baseUrl: PERMISSIONS_LIST_URL, instance }, +// ); +const getPermissionsOptions = async (params) => { + const fieldsToSend = ['page', 'size', 'q', 'sort', 'fields', 'id']; -const getPermissionsOptions = (payload) => permissionsListGetter.getList(payload); + const url = applyTransform(params, [ + merge(getDefaultGetParams()), + starToSearch('search'), + (params) => ({ ...params, q: params.search }), + sanitize(fieldsToSend), + camelToSnake(), + generateUrl(PERMISSIONS_LIST_URL), + ]); + try { + const response = await instance.get(url); + const { items, next } = applyTransform(response.data, [ + snakeToCamel(), + merge(getDefaultGetListResponse()), + ]); + return { + items, + next, + }; + } catch (err) { + throw applyTransform(err, [ + handleUnauthorized, + notify, + ]); + } +}; const RolesAPI = { getList: getRoleList, From bf3305a2ecba56c28d681bb0a5770addaac1c99d Mon Sep 17 00:00:00 2001 From: lizacoma Date: Tue, 11 Jul 2023 21:43:08 +0300 Subject: [PATCH 3/6] feature: roles api refactoring [WTEL-3630] --- package-lock.json | 14 ++++++------ package.json | 2 +- .../permissions/modules/roles/api/roles.js | 22 +++++++------------ 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/package-lock.json b/package-lock.json index 36f7517ae..86cc2612e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,7 @@ "@vuelidate/core": "^2.0.0", "@vuelidate/validators": "^2.0.0", "@webitel/flow-ui-sdk": "^0.1.14", - "@webitel/ui-sdk": "^3.2.8", + "@webitel/ui-sdk": "^3.2.5", "axios": "^0.27.1", "clipboard-copy": "^4.0.1", "core-js": "^3.6.4", @@ -4247,9 +4247,9 @@ "license": "MIT" }, "node_modules/@webitel/ui-sdk": { - "version": "3.2.8", - "resolved": "https://registry.npmjs.org/@webitel/ui-sdk/-/ui-sdk-3.2.8.tgz", - "integrity": "sha512-FdDUcnO8WZwi00/lWErtDN2KCLH+Q5u4KqfepcqIS4TWqAU6YA19yMdVUrEJYpdvYNr/+OzYIy5DZx7Lf/TpHg==", + "version": "3.2.52", + "resolved": "https://registry.npmjs.org/@webitel/ui-sdk/-/ui-sdk-3.2.52.tgz", + "integrity": "sha512-ASvDFqsCwXDUt9uhI8iv00BNa0vqS+7ky2p//OVOUtycbDcSMDXFNXF9rd7gFR/ZrDsTyM5dMvaXM253I+bL/w==", "dependencies": { "@floating-ui/vue": "^1.0.1", "@vue/compat": "^3.2.47", @@ -22371,9 +22371,9 @@ } }, "@webitel/ui-sdk": { - "version": "3.2.8", - "resolved": "https://registry.npmjs.org/@webitel/ui-sdk/-/ui-sdk-3.2.8.tgz", - "integrity": "sha512-FdDUcnO8WZwi00/lWErtDN2KCLH+Q5u4KqfepcqIS4TWqAU6YA19yMdVUrEJYpdvYNr/+OzYIy5DZx7Lf/TpHg==", + "version": "3.2.52", + "resolved": "https://registry.npmjs.org/@webitel/ui-sdk/-/ui-sdk-3.2.52.tgz", + "integrity": "sha512-ASvDFqsCwXDUt9uhI8iv00BNa0vqS+7ky2p//OVOUtycbDcSMDXFNXF9rd7gFR/ZrDsTyM5dMvaXM253I+bL/w==", "requires": { "@floating-ui/vue": "^1.0.1", "@vue/compat": "^3.2.47", diff --git a/package.json b/package.json index 0424466b5..fb156f2b7 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "@vuelidate/core": "^2.0.0", "@vuelidate/validators": "^2.0.0", "@webitel/flow-ui-sdk": "^0.1.14", - "@webitel/ui-sdk": "^3.2.8", + "@webitel/ui-sdk": "^3.2.5", "axios": "^0.27.1", "clipboard-copy": "^4.0.1", "core-js": "^3.6.4", diff --git a/src/modules/permissions/modules/roles/api/roles.js b/src/modules/permissions/modules/roles/api/roles.js index 0fd6092c1..9d176d554 100644 --- a/src/modules/permissions/modules/roles/api/roles.js +++ b/src/modules/permissions/modules/roles/api/roles.js @@ -17,9 +17,7 @@ const fieldsToSend = ['name', 'description', 'permissions', 'metadata']; const preRequestHandler = (item) => { const copy = deepCopy(item); - console.log('copy.metadata.access:', copy.metadata.access); copy.metadata.access = ApplicationsAccess.minify(copy.metadata.access); - console.log('copy.metadata.access:', copy.metadata.access); return copy; }; @@ -52,8 +50,7 @@ const getRoleList = async (params) => { ]); } }; -// const itemGetter = new EndpointGetterApiConsumer({ baseUrl, instance }, -// { defaultSingleObject, itemResponseHandler }); + const getRole = async ({ itemId: id }) => { const defaultObject = { name: '', @@ -84,7 +81,7 @@ const getRole = async ({ itemId: id }) => { ]); } }; -// const extendedRolesListGetter = new EndpointListGetterApiConsumer({ baseUrl, instance }); + const getExtendedRoles = async (params) => { const fieldsToSend = ['page', 'size', 'q', 'sort', 'fields', 'id']; @@ -113,8 +110,7 @@ const getExtendedRoles = async (params) => { ]); } }; -// const itemCreator = new EndpointCreatorApiConsumer({ baseUrl, instance }, -// { fieldsToSend, preRequestHandler }); + const addRole = async ({ itemInstance }) => { const item = applyTransform(itemInstance, [ preRequestHandler, @@ -133,11 +129,11 @@ const addRole = async ({ itemInstance }) => { ]); } }; -// const itemUpdater = new EndpointUpdaterApiConsumer({ baseUrl, instance }, -// { fieldsToSend, preRequestHandler }); -const updateRole = async ({ itemInstance, itemId: id }) => { + +const updateRole = async ({ itemInstance, id }) => { const item = applyTransform(itemInstance, [ preRequestHandler, + log, sanitize(fieldsToSend), camelToSnake(), ]); @@ -155,7 +151,7 @@ const updateRole = async ({ itemInstance, itemId: id }) => { ]); } }; -// const itemDeleter = new EndpointDeleterApiConsumer({ baseUrl, instance }); + const deleteRole = async ({ id }) => { const url = `${baseUrl}/${id}`; try { @@ -175,9 +171,7 @@ const getRolesLookup = (params) => getRoleList({ }); const PERMISSIONS_LIST_URL = '/permissions'; -// const permissionsListGetter = new EndpointListGetterApiConsumer( -// { baseUrl: PERMISSIONS_LIST_URL, instance }, -// ); + const getPermissionsOptions = async (params) => { const fieldsToSend = ['page', 'size', 'q', 'sort', 'fields', 'id']; From 8e73bbb13b1dfc17d000382382329fa58b3fd551 Mon Sep 17 00:00:00 2001 From: lizacoma Date: Tue, 11 Jul 2023 21:51:58 +0300 Subject: [PATCH 4/6] feature: roles api refactoring [WTEL-3630] --- src/modules/permissions/modules/roles/api/roles.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/permissions/modules/roles/api/roles.js b/src/modules/permissions/modules/roles/api/roles.js index 9d176d554..e524c0bf3 100644 --- a/src/modules/permissions/modules/roles/api/roles.js +++ b/src/modules/permissions/modules/roles/api/roles.js @@ -130,7 +130,7 @@ const addRole = async ({ itemInstance }) => { } }; -const updateRole = async ({ itemInstance, id }) => { +const updateRole = async ({ itemInstance, itemId: id }) => { const item = applyTransform(itemInstance, [ preRequestHandler, log, From f99570eff31a585d63bec010ada01a1f620dfa71 Mon Sep 17 00:00:00 2001 From: lizacoma Date: Wed, 12 Jul 2023 11:25:33 +0300 Subject: [PATCH 5/6] feature: roles api refactoring [WTEL-3630] --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 86cc2612e..15bcc509f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,7 @@ "@vuelidate/core": "^2.0.0", "@vuelidate/validators": "^2.0.0", "@webitel/flow-ui-sdk": "^0.1.14", - "@webitel/ui-sdk": "^3.2.5", + "@webitel/ui-sdk": "^3.2.8", "axios": "^0.27.1", "clipboard-copy": "^4.0.1", "core-js": "^3.6.4", diff --git a/package.json b/package.json index fb156f2b7..0424466b5 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "@vuelidate/core": "^2.0.0", "@vuelidate/validators": "^2.0.0", "@webitel/flow-ui-sdk": "^0.1.14", - "@webitel/ui-sdk": "^3.2.5", + "@webitel/ui-sdk": "^3.2.8", "axios": "^0.27.1", "clipboard-copy": "^4.0.1", "core-js": "^3.6.4", From 29507ac60720c79f9195338f0e16f6fe6fabb946 Mon Sep 17 00:00:00 2001 From: lizacoma Date: Mon, 6 Nov 2023 21:56:09 +0200 Subject: [PATCH 6/6] feature: refactor in progress [WTEL-3630] --- src/modules/permissions/modules/roles/api/roles.js | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/modules/permissions/modules/roles/api/roles.js b/src/modules/permissions/modules/roles/api/roles.js index e524c0bf3..cd42659e5 100644 --- a/src/modules/permissions/modules/roles/api/roles.js +++ b/src/modules/permissions/modules/roles/api/roles.js @@ -3,10 +3,10 @@ import { getDefaultGetParams, } from '@webitel/ui-sdk/src/api/defaults'; import applyTransform, { - camelToSnake, handleUnauthorized, + camelToSnake, merge, notify, snakeToCamel, starToSearch, log, sanitize, - generateUrl, mergeEach, + generateUrl, } from '@webitel/ui-sdk/src/api/transformers'; import deepCopy from 'deep-copy'; import ApplicationsAccess from '@webitel/ui-sdk/src/modules/Userinfo/classes/ApplicationsAccess'; @@ -21,7 +21,6 @@ const preRequestHandler = (item) => { return copy; }; -// const listGetter = new EndpointListGetterApiConsumer({ baseUrl, instance }); const getRoleList = async (params) => { const fieldsToSend = ['page', 'size', 'q', 'sort', 'fields', 'id']; @@ -45,7 +44,6 @@ const getRoleList = async (params) => { }; } catch (err) { throw applyTransform(err, [ - handleUnauthorized, notify, ]); } @@ -76,7 +74,6 @@ const getRole = async ({ itemId: id }) => { ]); } catch (err) { throw applyTransform(err, [ - handleUnauthorized, notify, ]); } @@ -105,7 +102,6 @@ const getExtendedRoles = async (params) => { }; } catch (err) { throw applyTransform(err, [ - handleUnauthorized, notify, ]); } @@ -124,7 +120,6 @@ const addRole = async ({ itemInstance }) => { ]); } catch (err) { throw applyTransform(err, [ - handleUnauthorized, notify, ]); } @@ -146,7 +141,6 @@ const updateRole = async ({ itemInstance, itemId: id }) => { ]); } catch (err) { throw applyTransform(err, [ - handleUnauthorized, notify, ]); } @@ -159,7 +153,6 @@ const deleteRole = async ({ id }) => { return applyTransform(response.data, []); } catch (err) { throw applyTransform(err, [ - handleUnauthorized, notify, ]); } @@ -195,7 +188,6 @@ const getPermissionsOptions = async (params) => { }; } catch (err) { throw applyTransform(err, [ - handleUnauthorized, notify, ]); }