From 4aeed36266818feb2fce50f7e011d40889ec91e2 Mon Sep 17 00:00:00 2001 From: veselovnk Date: Sat, 27 Apr 2024 16:25:43 +0300 Subject: [PATCH] MSP-3203 create configService --- src/constants/api/apiPaths.ts | 17 +++++++- src/services/configService.ts | 76 +++++++++++++++++++++++++++++++++++ src/stores/index.tsx | 5 ++- src/type/version.type.ts | 7 ++++ 4 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 src/services/configService.ts create mode 100644 src/type/version.type.ts diff --git a/src/constants/api/apiPaths.ts b/src/constants/api/apiPaths.ts index 17155ac..d3f4eda 100644 --- a/src/constants/api/apiPaths.ts +++ b/src/constants/api/apiPaths.ts @@ -44,6 +44,13 @@ type PathKeys = | 'getTokensByAppId' | 'revokeTokens' | 'revokeTokensForApp' + | 'getConfigsByModuleId' + | 'getConfigById' + | 'getAllVersions' + | 'createUpdateConfig' + | 'deleteVersion' + | 'deleteConfig' + | 'markConfigAsActive' | 'getUI' export const apiPaths: Record = { @@ -103,5 +110,13 @@ export const apiPaths: Record = { createToken: '/token/create_token', getTokensByAppId: '/token/get_tokens_by_app_id', revokeTokens: '/token/revoke_tokens', - revokeTokensForApp: '/token/revoke_tokens_for_app' + revokeTokensForApp: '/token/revoke_tokens_for_app', + // ======= CONFIGS ====== + getConfigsByModuleId: '/config/get_configs_by_module_id', + getConfigById: '/config/get_config_by_id', + getAllVersions: '/config/get_all_version', + createUpdateConfig: '/config/create_update_config', + deleteVersion: '/config/delete_version', + deleteConfig: '/config/delete_config', + markConfigAsActive: '/config/mark_config_as_active' } diff --git a/src/services/configService.ts b/src/services/configService.ts new file mode 100644 index 0000000..4930a1d --- /dev/null +++ b/src/services/configService.ts @@ -0,0 +1,76 @@ +import { createApi } from '@reduxjs/toolkit/query/react' + +import { apiPaths } from '@constants/api/apiPaths.ts' + +import { ConfigResponse, ConfigType } from '@pages/ModulesPage/module.type.ts' + +import { axiosBaseQuery } from '@utils/apiUtils.ts' + +import { VersionType } from '@type/version.type.ts' + + +const configServiceApi = createApi({ + reducerPath: 'configApi', + tagTypes: ['configs', 'versions', 'config'], + baseQuery: axiosBaseQuery({ baseUrl: apiPaths.baseConfigUrl }), + endpoints: (builder) => ({ + getConfigsByModuleId: builder.query({ + query: (moduleId) => ({ + url: apiPaths.getConfigsByModuleId, + data: { moduleId } + }), + transformResponse: (response: ConfigType[]): ConfigResponse => { + const activeObjects: ConfigType[] = [] + const inactiveObjects: ConfigType[] = [] + response.forEach((obj) => { + if (obj.active) { + activeObjects.push(obj) + } else { + inactiveObjects.push(obj) + } + }) + return { + originalResponse: response, + activeConfigs: activeObjects, + inactiveConfigs: inactiveObjects + } + }, + providesTags: () => ['configs'] + }), + getConfigById: builder.query({ + query: (id) => ({ + url: apiPaths.getConfigById, + data: { id } + }), + providesTags: () => ['config'] + }), + getAllVersion: builder.query({ + query: (id) => ({ + url: apiPaths.getAllVersions, + data: { id } + }), + transformResponse: (response: VersionType[]) => { + return response.sort((a, b) => b.configVersion - a.configVersion) + }, + providesTags: () => ['versions'] + }), + createUpdateConfig: builder.mutation({ + query: (data) => ({ url: apiPaths.createUpdateConfig, data }), + invalidatesTags: () => ['configs', 'versions'] + }), + deleteVersion: builder.mutation({ + query: (id) => ({ url: apiPaths.deleteVersion, data: { id } }), + invalidatesTags: () => ['versions'] + }), + deleteConfig: builder.mutation({ + query: (id) => ({ url: apiPaths.deleteVersion, data: [id] }), + invalidatesTags: () => ['versions'] + }), + markConfigAsActive: builder.mutation({ + query: (id) => ({ url: apiPaths.markConfigAsActive, data: { id } }), + invalidatesTags: () => ['configs'] + }) + }) +}) + +export default configServiceApi diff --git a/src/stores/index.tsx b/src/stores/index.tsx index c836c78..57b7e0f 100644 --- a/src/stores/index.tsx +++ b/src/stores/index.tsx @@ -7,6 +7,7 @@ import accessListApi from '@services/accessListService.ts' import appApi from '@services/appService.ts' import applicationsGroupApi from '@services/applicationsGroupService.ts' import applicationsApi from '@services/applicationsService.ts' +import configServiceApi from '@services/configService.ts' import modulesServiceApi from '@services/modulesService.ts' import roleApi from '@services/roleService.ts' import routeApi from '@services/routeService.ts' @@ -41,6 +42,7 @@ const rootReducer = combineReducers({ [modulesServiceApi.reducerPath]: modulesServiceApi.reducer, [applicationsGroupApi.reducerPath]: applicationsGroupApi.reducer, [applicationsApi.reducerPath]: applicationsApi.reducer, + [configServiceApi.reducerPath]: configServiceApi.reducer, [tokensApi.reducerPath]: tokensApi.reducer }) @@ -60,7 +62,8 @@ export const setupStore = () => modulesServiceApi.middleware, applicationsGroupApi.middleware, applicationsApi.middleware, - tokensApi.middleware + tokensApi.middleware, + configServiceApi.middleware ) }) diff --git a/src/type/version.type.ts b/src/type/version.type.ts new file mode 100644 index 0000000..8f46dc3 --- /dev/null +++ b/src/type/version.type.ts @@ -0,0 +1,7 @@ +export interface VersionType { + id: string + configId: string + configVersion: number + createdAt: string + data: Record +}