Merge pull request #2086 from automatisch/aut-1260
feat: use REST API endpoint to delete current user
This commit is contained in:
@@ -5,7 +5,6 @@ import executeFlow from './mutations/execute-flow.js';
|
||||
import updateUser from './mutations/update-user.ee.js';
|
||||
import deleteStep from './mutations/delete-step.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 generateAuthUrl from './mutations/generate-auth-url.js';
|
||||
import createConnection from './mutations/create-connection.js';
|
||||
@@ -18,7 +17,6 @@ import updateFlowStatus from './mutations/update-flow-status.js';
|
||||
const mutationResolvers = {
|
||||
createConnection,
|
||||
createUser,
|
||||
deleteCurrentUser,
|
||||
deleteFlow,
|
||||
deleteStep,
|
||||
executeFlow,
|
||||
|
@@ -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;
|
@@ -4,7 +4,6 @@ type Query {
|
||||
type Mutation {
|
||||
createConnection(input: CreateConnectionInput): Connection
|
||||
createUser(input: CreateUserInput): UserWithAcceptInvitationUrl
|
||||
deleteCurrentUser: Boolean
|
||||
deleteFlow(input: DeleteFlowInput): Boolean
|
||||
deleteStep(input: DeleteStepInput): Step
|
||||
executeFlow(input: ExecuteFlowInput): executeFlowType
|
||||
|
@@ -1,31 +1,36 @@
|
||||
import PropTypes from 'prop-types';
|
||||
import * as React from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { useMutation } from '@apollo/client';
|
||||
|
||||
import * as URLS from 'config/urls';
|
||||
import ConfirmationDialog from 'components/ConfirmationDialog';
|
||||
import apolloClient from 'graphql/client';
|
||||
import { DELETE_CURRENT_USER } from 'graphql/mutations/delete-current-user.ee';
|
||||
import useAuthentication from 'hooks/useAuthentication';
|
||||
import useFormatMessage from 'hooks/useFormatMessage';
|
||||
import useCurrentUser from 'hooks/useCurrentUser';
|
||||
import useDeleteCurrentUser from 'hooks/useDeleteCurrentUser';
|
||||
|
||||
function DeleteAccountDialog(props) {
|
||||
const [deleteCurrentUser] = useMutation(DELETE_CURRENT_USER);
|
||||
const formatMessage = useFormatMessage();
|
||||
const { data } = useCurrentUser();
|
||||
const currentUser = data?.data;
|
||||
|
||||
const { mutateAsync: deleteCurrentUser } = useDeleteCurrentUser(
|
||||
currentUser.id,
|
||||
);
|
||||
|
||||
const authentication = useAuthentication();
|
||||
const navigate = useNavigate();
|
||||
|
||||
const handleConfirm = React.useCallback(async () => {
|
||||
await deleteCurrentUser();
|
||||
|
||||
authentication.removeToken();
|
||||
|
||||
await apolloClient.clearStore();
|
||||
|
||||
navigate(URLS.LOGIN);
|
||||
}, [deleteCurrentUser, currentUser]);
|
||||
}, [deleteCurrentUser, authentication, navigate]);
|
||||
|
||||
return (
|
||||
<ConfirmationDialog
|
||||
|
@@ -1,6 +0,0 @@
|
||||
import { gql } from '@apollo/client';
|
||||
export const DELETE_CURRENT_USER = gql`
|
||||
mutation DeleteCurrentUser {
|
||||
deleteCurrentUser
|
||||
}
|
||||
`;
|
14
packages/web/src/hooks/useDeleteCurrentUser.js
Normal file
14
packages/web/src/hooks/useDeleteCurrentUser.js
Normal 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;
|
||||
}
|
Reference in New Issue
Block a user