From 5e6f4bfb8840381be9dcfb6dbc8b28efa6068649 Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Tue, 24 Sep 2024 11:40:35 +0000 Subject: [PATCH] feat(useAuthenticateApp): use REST API endpoint to reset connection --- .../src/helpers/add-authentication-steps.js | 21 ++--------- .../src/helpers/add-reconnection-steps.js | 35 ++----------------- .../web/src/hooks/useAuthenticateApp.ee.js | 22 +++++++----- packages/web/src/hooks/useResetConnection.js | 15 ++++++++ 4 files changed, 34 insertions(+), 59 deletions(-) create mode 100644 packages/web/src/hooks/useResetConnection.js diff --git a/packages/backend/src/helpers/add-authentication-steps.js b/packages/backend/src/helpers/add-authentication-steps.js index f3c288bb..5e7a462a 100644 --- a/packages/backend/src/helpers/add-authentication-steps.js +++ b/packages/backend/src/helpers/add-authentication-steps.js @@ -27,12 +27,7 @@ const authenticationStepsWithoutAuthUrl = [ { type: 'mutation', name: 'verifyConnection', - arguments: [ - { - name: 'id', - value: '{createConnection.id}', - }, - ], + arguments: [], }, ]; @@ -79,12 +74,7 @@ const authenticationStepsWithAuthUrl = [ { type: 'mutation', name: 'verifyConnection', - arguments: [ - { - name: 'id', - value: '{createConnection.id}', - }, - ], + arguments: [], }, ]; @@ -131,12 +121,7 @@ const sharedAuthenticationStepsWithAuthUrl = [ { type: 'mutation', name: 'verifyConnection', - arguments: [ - { - name: 'id', - value: '{createConnection.id}', - }, - ], + arguments: [], }, ]; diff --git a/packages/backend/src/helpers/add-reconnection-steps.js b/packages/backend/src/helpers/add-reconnection-steps.js index 58b2a530..728498e0 100644 --- a/packages/backend/src/helpers/add-reconnection-steps.js +++ b/packages/backend/src/helpers/add-reconnection-steps.js @@ -1,54 +1,23 @@ import cloneDeep from 'lodash/cloneDeep.js'; -const connectionIdArgument = { - name: 'id', - value: '{connection.id}', -}; - const resetConnectionStep = { type: 'mutation', name: 'resetConnection', - arguments: [connectionIdArgument], + arguments: [], }; -function replaceCreateConnection(string) { - return string.replace('{createConnection.id}', '{connection.id}'); -} - function removeAppKeyArgument(args) { return args.filter((argument) => argument.name !== 'key'); } -function addConnectionId(step) { - step.arguments = step.arguments.map((argument) => { - if (typeof argument.value === 'string') { - argument.value = replaceCreateConnection(argument.value); - } - - if (argument.properties) { - argument.properties = argument.properties.map((property) => { - return { - name: property.name, - value: replaceCreateConnection(property.value), - }; - }); - } - - return argument; - }); - - return step; -} - function replaceCreateConnectionsWithUpdate(steps) { const updatedSteps = cloneDeep(steps); return updatedSteps.map((step) => { - const updatedStep = addConnectionId(step); + const updatedStep = { ...step }; if (step.name === 'createConnection') { updatedStep.name = 'updateConnection'; updatedStep.arguments = removeAppKeyArgument(updatedStep.arguments); - updatedStep.arguments.unshift(connectionIdArgument); return updatedStep; } diff --git a/packages/web/src/hooks/useAuthenticateApp.ee.js b/packages/web/src/hooks/useAuthenticateApp.ee.js index 6a70366e..ebb8c97a 100644 --- a/packages/web/src/hooks/useAuthenticateApp.ee.js +++ b/packages/web/src/hooks/useAuthenticateApp.ee.js @@ -11,6 +11,7 @@ import useAppAuth from './useAppAuth'; import useCreateConnection from './useCreateConnection'; import useCreateConnectionAuthUrl from './useCreateConnectionAuthUrl'; import useUpdateConnection from './useUpdateConnection'; +import useResetConnection from './useResetConnection'; function getSteps(auth, hasConnection, useShared) { if (hasConnection) { @@ -33,6 +34,7 @@ export default function useAuthenticateApp(payload) { const { mutateAsync: createConnection } = useCreateConnection(appKey); const { mutateAsync: createConnectionAuthUrl } = useCreateConnectionAuthUrl(); const { mutateAsync: updateConnection } = useUpdateConnection(); + const { mutateAsync: resetConnection } = useResetConnection(); const [authenticationInProgress, setAuthenticationInProgress] = React.useState(false); const formatMessage = useFormatMessage(); @@ -48,9 +50,7 @@ export default function useAuthenticateApp(payload) { const response = { key: appKey, appAuthClientId: appAuthClientId || payload.appAuthClientId, - connection: { - id: connectionId, - }, + connectionId, fields, }; let stepIndex = 0; @@ -73,19 +73,24 @@ export default function useAuthenticateApp(payload) { if (step.type === 'mutation') { if (step.name === 'createConnection') { const stepResponse = await createConnection(variables); - response[step.name] = stepResponse?.data; + response[step.name] = stepResponse.data; + response.connectionId = stepResponse.data.id; } else if (step.name === 'generateAuthUrl') { const stepResponse = await createConnectionAuthUrl( - response.createConnection.id, + response.connectionId, ); - response[step.name] = stepResponse?.data; + response[step.name] = stepResponse.data; } else if (step.name === 'updateConnection') { const stepResponse = await updateConnection({ ...variables, - connectionId: response.createConnection.id, + connectionId: response.connectionId, }); - response[step.name] = stepResponse?.data; + response[step.name] = stepResponse.data; + } else if (step.name === 'resetConnection') { + const stepResponse = await resetConnection(response.connectionId); + + response[step.name] = stepResponse.data; } else { const stepResponse = await processMutation(step.name, variables); response[step.name] = stepResponse; @@ -116,6 +121,7 @@ export default function useAuthenticateApp(payload) { createConnection, createConnectionAuthUrl, updateConnection, + resetConnection, ]); return { diff --git a/packages/web/src/hooks/useResetConnection.js b/packages/web/src/hooks/useResetConnection.js new file mode 100644 index 00000000..39bdf908 --- /dev/null +++ b/packages/web/src/hooks/useResetConnection.js @@ -0,0 +1,15 @@ +import { useMutation } from '@tanstack/react-query'; + +import api from 'helpers/api'; + +export default function useResetConnection() { + const query = useMutation({ + mutationFn: async (connectionId) => { + const { data } = await api.post(`/v1/connections/${connectionId}/reset`); + + return data; + }, + }); + + return query; +}