feat(useAuthenticateApp): use REST API endpoint to reset connection

This commit is contained in:
Ali BARIN
2024-09-24 11:40:35 +00:00
parent bc0e18d074
commit 5e6f4bfb88
4 changed files with 34 additions and 59 deletions

View File

@@ -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: [],
},
];

View File

@@ -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;
}

View File

@@ -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 {

View File

@@ -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;
}