Merge pull request #2086 from automatisch/aut-1260

feat: use REST API endpoint to delete current user
This commit is contained in:
Ömer Faruk Aydın
2024-09-19 12:34:35 +03:00
committed by GitHub
6 changed files with 23 additions and 71 deletions

View File

@@ -5,7 +5,6 @@ import executeFlow from './mutations/execute-flow.js';
import updateUser from './mutations/update-user.ee.js'; import updateUser from './mutations/update-user.ee.js';
import deleteStep from './mutations/delete-step.js'; import deleteStep from './mutations/delete-step.js';
import verifyConnection from './mutations/verify-connection.js'; import verifyConnection from './mutations/verify-connection.js';
import deleteCurrentUser from './mutations/delete-current-user.ee.js';
import updateCurrentUser from './mutations/update-current-user.js'; import updateCurrentUser from './mutations/update-current-user.js';
import generateAuthUrl from './mutations/generate-auth-url.js'; import generateAuthUrl from './mutations/generate-auth-url.js';
import createConnection from './mutations/create-connection.js'; import createConnection from './mutations/create-connection.js';
@@ -18,7 +17,6 @@ import updateFlowStatus from './mutations/update-flow-status.js';
const mutationResolvers = { const mutationResolvers = {
createConnection, createConnection,
createUser, createUser,
deleteCurrentUser,
deleteFlow, deleteFlow,
deleteStep, deleteStep,
executeFlow, executeFlow,

View File

@@ -1,58 +0,0 @@
import { Duration } from 'luxon';
import deleteUserQueue from '../../queues/delete-user.ee.js';
import flowQueue from '../../queues/flow.js';
import Flow from '../../models/flow.js';
import ExecutionStep from '../../models/execution-step.js';
import appConfig from '../../config/app.js';
const deleteCurrentUser = async (_parent, params, context) => {
const id = context.currentUser.id;
const flows = await context.currentUser.$relatedQuery('flows').where({
active: true,
});
const repeatableJobs = await flowQueue.getRepeatableJobs();
for (const flow of flows) {
const job = repeatableJobs.find((job) => job.id === flow.id);
if (job) {
await flowQueue.removeRepeatableByKey(job.key);
}
}
const executionIds = (
await context.currentUser
.$relatedQuery('executions')
.select('executions.id')
).map((execution) => execution.id);
const flowIds = flows.map((flow) => flow.id);
await ExecutionStep.query().delete().whereIn('execution_id', executionIds);
await context.currentUser.$relatedQuery('executions').delete();
await context.currentUser.$relatedQuery('steps').delete();
await Flow.query().whereIn('id', flowIds).delete();
await context.currentUser.$relatedQuery('connections').delete();
await context.currentUser.$relatedQuery('identities').delete();
if (appConfig.isCloud) {
await context.currentUser.$relatedQuery('subscriptions').delete();
await context.currentUser.$relatedQuery('usageData').delete();
}
await context.currentUser.$query().delete();
const jobName = `Delete user - ${id}`;
const jobPayload = { id };
const millisecondsFor30Days = Duration.fromObject({ days: 30 }).toMillis();
const jobOptions = {
delay: millisecondsFor30Days,
};
await deleteUserQueue.add(jobName, jobPayload, jobOptions);
return true;
};
export default deleteCurrentUser;

View File

@@ -4,7 +4,6 @@ type Query {
type Mutation { type Mutation {
createConnection(input: CreateConnectionInput): Connection createConnection(input: CreateConnectionInput): Connection
createUser(input: CreateUserInput): UserWithAcceptInvitationUrl createUser(input: CreateUserInput): UserWithAcceptInvitationUrl
deleteCurrentUser: Boolean
deleteFlow(input: DeleteFlowInput): Boolean deleteFlow(input: DeleteFlowInput): Boolean
deleteStep(input: DeleteStepInput): Step deleteStep(input: DeleteStepInput): Step
executeFlow(input: ExecuteFlowInput): executeFlowType executeFlow(input: ExecuteFlowInput): executeFlowType

View File

@@ -1,31 +1,36 @@
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import * as React from 'react'; import * as React from 'react';
import { useNavigate } from 'react-router-dom'; import { useNavigate } from 'react-router-dom';
import { useMutation } from '@apollo/client';
import * as URLS from 'config/urls'; import * as URLS from 'config/urls';
import ConfirmationDialog from 'components/ConfirmationDialog'; import ConfirmationDialog from 'components/ConfirmationDialog';
import apolloClient from 'graphql/client'; import apolloClient from 'graphql/client';
import { DELETE_CURRENT_USER } from 'graphql/mutations/delete-current-user.ee';
import useAuthentication from 'hooks/useAuthentication'; import useAuthentication from 'hooks/useAuthentication';
import useFormatMessage from 'hooks/useFormatMessage'; import useFormatMessage from 'hooks/useFormatMessage';
import useCurrentUser from 'hooks/useCurrentUser'; import useCurrentUser from 'hooks/useCurrentUser';
import useDeleteCurrentUser from 'hooks/useDeleteCurrentUser';
function DeleteAccountDialog(props) { function DeleteAccountDialog(props) {
const [deleteCurrentUser] = useMutation(DELETE_CURRENT_USER);
const formatMessage = useFormatMessage(); const formatMessage = useFormatMessage();
const { data } = useCurrentUser(); const { data } = useCurrentUser();
const currentUser = data?.data; const currentUser = data?.data;
const { mutateAsync: deleteCurrentUser } = useDeleteCurrentUser(
currentUser.id,
);
const authentication = useAuthentication(); const authentication = useAuthentication();
const navigate = useNavigate(); const navigate = useNavigate();
const handleConfirm = React.useCallback(async () => { const handleConfirm = React.useCallback(async () => {
await deleteCurrentUser(); await deleteCurrentUser();
authentication.removeToken(); authentication.removeToken();
await apolloClient.clearStore(); await apolloClient.clearStore();
navigate(URLS.LOGIN); navigate(URLS.LOGIN);
}, [deleteCurrentUser, currentUser]); }, [deleteCurrentUser, authentication, navigate]);
return ( return (
<ConfirmationDialog <ConfirmationDialog

View File

@@ -1,6 +0,0 @@
import { gql } from '@apollo/client';
export const DELETE_CURRENT_USER = gql`
mutation DeleteCurrentUser {
deleteCurrentUser
}
`;

View File

@@ -0,0 +1,14 @@
import { useMutation } from '@tanstack/react-query';
import api from 'helpers/api';
export default function useDeleteCurrentUser(userId) {
const query = useMutation({
mutationFn: async () => {
const { data } = await api.delete(`/v1/users/${userId}`);
return data;
},
});
return query;
}