Activate/Deactivate workflow and Discard Draft (#7022)
## Setup This PR can be tested only if some feature flags have specific values: - `IsWorkflowEnabled` equals `true` - `IsQueryRunnerTwentyORMEnabled` equals `false` These feature flags weren't committed to don't break other branches. ## What this PR brings - Display buttons to activate and deactivate a workflow version and a button to discard the current draft version. I also scaffolded a "Test" button, which doesn't do anything for now. - Wired the activate, deactivate and discard draft buttons to the backend. - Made it possible to "edit" active and deactivated versions by automatically creating a new draft version when the user tries to edit the version. - Hide the "Discard Draft", button if the current version is not a draft or is the first version ever created. - On the backend, don't consider discarded drafts when checking if a new draft version can be created. - On the backend, disallow deleting the first created workflow version. Otherwise, we will end up with a blank canvas in the front end, and it will be impossible to recover from it. - On the backend, disallow running deactivation steps if the workflow version is not currently active. Previously, we were throwing, which is unnecessary as it's a valid case. ## Spotted bugs that we must dive into ### Duplicate workflow versions in Apollo cache https://github.com/user-attachments/assets/7cfffd06-11e0-417a-8da0-f9a5f43b84e2 --------- Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
committed by
GitHub
parent
75b493ba6c
commit
729c990546
@ -0,0 +1,45 @@
|
||||
import { useApolloMetadataClient } from '@/object-metadata/hooks/useApolloMetadataClient';
|
||||
import { ApolloClient, useApolloClient, useMutation } from '@apollo/client';
|
||||
|
||||
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
|
||||
import { useFindOneRecordQuery } from '@/object-record/hooks/useFindOneRecordQuery';
|
||||
import { ACTIVATE_WORKFLOW_VERSION } from '@/workflow/graphql/activateWorkflowVersion';
|
||||
import {
|
||||
ActivateWorkflowVersionMutation,
|
||||
ActivateWorkflowVersionMutationVariables,
|
||||
} from '~/generated/graphql';
|
||||
|
||||
export const useActivateWorkflowVersion = () => {
|
||||
const apolloClient = useApolloClient();
|
||||
|
||||
const apolloMetadataClient = useApolloMetadataClient();
|
||||
const [mutate] = useMutation<
|
||||
ActivateWorkflowVersionMutation,
|
||||
ActivateWorkflowVersionMutationVariables
|
||||
>(ACTIVATE_WORKFLOW_VERSION, {
|
||||
client: apolloMetadataClient ?? ({} as ApolloClient<any>),
|
||||
});
|
||||
|
||||
const { findOneRecordQuery: findOneWorkflowVersionQuery } =
|
||||
useFindOneRecordQuery({
|
||||
objectNameSingular: CoreObjectNameSingular.WorkflowVersion,
|
||||
});
|
||||
|
||||
const activateWorkflowVersion = async (workflowVersionId: string) => {
|
||||
await mutate({
|
||||
variables: {
|
||||
workflowVersionId,
|
||||
},
|
||||
});
|
||||
|
||||
await apolloClient.query({
|
||||
query: findOneWorkflowVersionQuery,
|
||||
variables: {
|
||||
objectRecordId: workflowVersionId,
|
||||
},
|
||||
fetchPolicy: 'network-only',
|
||||
});
|
||||
};
|
||||
|
||||
return { activateWorkflowVersion };
|
||||
};
|
||||
@ -0,0 +1,30 @@
|
||||
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
|
||||
import { useCreateOneRecord } from '@/object-record/hooks/useCreateOneRecord';
|
||||
import { WorkflowVersion } from '@/workflow/types/Workflow';
|
||||
|
||||
export const useCreateNewWorkflowVersion = ({
|
||||
workflowId,
|
||||
}: {
|
||||
workflowId: string;
|
||||
}) => {
|
||||
const { createOneRecord: createOneWorkflowVersion } =
|
||||
useCreateOneRecord<WorkflowVersion>({
|
||||
objectNameSingular: CoreObjectNameSingular.WorkflowVersion,
|
||||
});
|
||||
|
||||
const createNewWorkflowVersion = (
|
||||
workflowVersionData: Pick<
|
||||
WorkflowVersion,
|
||||
'name' | 'status' | 'trigger' | 'steps'
|
||||
>,
|
||||
) => {
|
||||
return createOneWorkflowVersion({
|
||||
workflowId,
|
||||
...workflowVersionData,
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
createNewWorkflowVersion,
|
||||
};
|
||||
};
|
||||
@ -1,5 +1,6 @@
|
||||
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
|
||||
import { useUpdateOneRecord } from '@/object-record/hooks/useUpdateOneRecord';
|
||||
import { useCreateNewWorkflowVersion } from '@/workflow/hooks/useCreateNewWorkflowVersion';
|
||||
import { workflowCreateStepFromParentStepIdState } from '@/workflow/states/workflowCreateStepFromParentStepIdState';
|
||||
import { workflowDiagramTriggerNodeSelectionState } from '@/workflow/states/workflowDiagramTriggerNodeSelectionState';
|
||||
import {
|
||||
@ -31,7 +32,11 @@ export const useCreateStep = ({
|
||||
objectNameSingular: CoreObjectNameSingular.WorkflowVersion,
|
||||
});
|
||||
|
||||
const insertNodeAndSave = ({
|
||||
const { createNewWorkflowVersion } = useCreateNewWorkflowVersion({
|
||||
workflowId: workflow.id,
|
||||
});
|
||||
|
||||
const insertNodeAndSave = async ({
|
||||
parentNodeId,
|
||||
nodeToAdd,
|
||||
}: {
|
||||
@ -43,15 +48,28 @@ export const useCreateStep = ({
|
||||
throw new Error("Can't add a node when there is no current version.");
|
||||
}
|
||||
|
||||
return updateOneWorkflowVersion({
|
||||
idToUpdate: currentVersion.id,
|
||||
updateOneRecordInput: {
|
||||
steps: insertStep({
|
||||
steps: currentVersion.steps ?? [],
|
||||
parentStepId: parentNodeId,
|
||||
stepToAdd: nodeToAdd,
|
||||
}),
|
||||
},
|
||||
const updatedSteps = insertStep({
|
||||
steps: currentVersion.steps ?? [],
|
||||
parentStepId: parentNodeId,
|
||||
stepToAdd: nodeToAdd,
|
||||
});
|
||||
|
||||
if (workflow.currentVersion.status === 'DRAFT') {
|
||||
await updateOneWorkflowVersion({
|
||||
idToUpdate: currentVersion.id,
|
||||
updateOneRecordInput: {
|
||||
steps: updatedSteps,
|
||||
},
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
await createNewWorkflowVersion({
|
||||
name: `v${workflow.versions.length + 1}`,
|
||||
status: 'DRAFT',
|
||||
trigger: workflow.currentVersion.trigger,
|
||||
steps: updatedSteps,
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@ -0,0 +1,45 @@
|
||||
import { useApolloMetadataClient } from '@/object-metadata/hooks/useApolloMetadataClient';
|
||||
import { ApolloClient, useApolloClient, useMutation } from '@apollo/client';
|
||||
|
||||
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
|
||||
import { useFindOneRecordQuery } from '@/object-record/hooks/useFindOneRecordQuery';
|
||||
import { DEACTIVATE_WORKFLOW_VERSION } from '@/workflow/graphql/deactivateWorkflowVersion';
|
||||
import {
|
||||
ActivateWorkflowVersionMutation,
|
||||
ActivateWorkflowVersionMutationVariables,
|
||||
} from '~/generated/graphql';
|
||||
|
||||
export const useDeactivateWorkflowVersion = () => {
|
||||
const apolloClient = useApolloClient();
|
||||
|
||||
const apolloMetadataClient = useApolloMetadataClient();
|
||||
const [mutate] = useMutation<
|
||||
ActivateWorkflowVersionMutation,
|
||||
ActivateWorkflowVersionMutationVariables
|
||||
>(DEACTIVATE_WORKFLOW_VERSION, {
|
||||
client: apolloMetadataClient ?? ({} as ApolloClient<any>),
|
||||
});
|
||||
|
||||
const { findOneRecordQuery: findOneWorkflowVersionQuery } =
|
||||
useFindOneRecordQuery({
|
||||
objectNameSingular: CoreObjectNameSingular.WorkflowVersion,
|
||||
});
|
||||
|
||||
const deactivateWorkflowVersion = async (workflowVersionId: string) => {
|
||||
await mutate({
|
||||
variables: {
|
||||
workflowVersionId,
|
||||
},
|
||||
});
|
||||
|
||||
await apolloClient.query({
|
||||
query: findOneWorkflowVersionQuery,
|
||||
variables: {
|
||||
objectRecordId: workflowVersionId,
|
||||
},
|
||||
fetchPolicy: 'network-only',
|
||||
});
|
||||
};
|
||||
|
||||
return { deactivateWorkflowVersion };
|
||||
};
|
||||
@ -0,0 +1,37 @@
|
||||
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
|
||||
import { useDeleteOneRecord } from '@/object-record/hooks/useDeleteOneRecord';
|
||||
import { useFindOneRecordQuery } from '@/object-record/hooks/useFindOneRecordQuery';
|
||||
import { useApolloClient } from '@apollo/client';
|
||||
|
||||
export const useDeleteOneWorkflowVersion = () => {
|
||||
const apolloClient = useApolloClient();
|
||||
|
||||
const { findOneRecordQuery: findOneWorkflowRecordQuery } =
|
||||
useFindOneRecordQuery({
|
||||
objectNameSingular: CoreObjectNameSingular.Workflow,
|
||||
});
|
||||
|
||||
const { deleteOneRecord } = useDeleteOneRecord({
|
||||
objectNameSingular: CoreObjectNameSingular.WorkflowVersion,
|
||||
});
|
||||
|
||||
const deleteOneWorkflowVersion = async ({
|
||||
workflowId,
|
||||
workflowVersionId,
|
||||
}: {
|
||||
workflowId: string;
|
||||
workflowVersionId: string;
|
||||
}) => {
|
||||
await deleteOneRecord(workflowVersionId);
|
||||
|
||||
await apolloClient.query({
|
||||
query: findOneWorkflowRecordQuery,
|
||||
variables: {
|
||||
objectRecordId: workflowId,
|
||||
},
|
||||
fetchPolicy: 'network-only',
|
||||
});
|
||||
};
|
||||
|
||||
return { deleteOneWorkflowVersion };
|
||||
};
|
||||
@ -1,5 +1,6 @@
|
||||
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
|
||||
import { useUpdateOneRecord } from '@/object-record/hooks/useUpdateOneRecord';
|
||||
import { useCreateNewWorkflowVersion } from '@/workflow/hooks/useCreateNewWorkflowVersion';
|
||||
import {
|
||||
WorkflowStep,
|
||||
WorkflowVersion,
|
||||
@ -20,20 +21,37 @@ export const useUpdateWorkflowVersionStep = ({
|
||||
objectNameSingular: CoreObjectNameSingular.WorkflowVersion,
|
||||
});
|
||||
|
||||
const { createNewWorkflowVersion } = useCreateNewWorkflowVersion({
|
||||
workflowId: workflow.id,
|
||||
});
|
||||
|
||||
const updateStep = async (updatedStep: WorkflowStep) => {
|
||||
if (!isDefined(workflow.currentVersion)) {
|
||||
throw new Error('Can not update an undefined workflow version.');
|
||||
}
|
||||
|
||||
await updateOneWorkflowVersion({
|
||||
idToUpdate: workflow.currentVersion.id,
|
||||
updateOneRecordInput: {
|
||||
steps: replaceStep({
|
||||
steps: workflow.currentVersion.steps ?? [],
|
||||
stepId,
|
||||
stepToReplace: updatedStep,
|
||||
}),
|
||||
},
|
||||
const updatedSteps = replaceStep({
|
||||
steps: workflow.currentVersion.steps ?? [],
|
||||
stepId,
|
||||
stepToReplace: updatedStep,
|
||||
});
|
||||
|
||||
if (workflow.currentVersion.status === 'DRAFT') {
|
||||
await updateOneWorkflowVersion({
|
||||
idToUpdate: workflow.currentVersion.id,
|
||||
updateOneRecordInput: {
|
||||
steps: updatedSteps,
|
||||
},
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
await createNewWorkflowVersion({
|
||||
name: `v${workflow.versions.length + 1}`,
|
||||
status: 'DRAFT',
|
||||
trigger: workflow.currentVersion.trigger,
|
||||
steps: updatedSteps,
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
|
||||
import { useUpdateOneRecord } from '@/object-record/hooks/useUpdateOneRecord';
|
||||
import { useCreateNewWorkflowVersion } from '@/workflow/hooks/useCreateNewWorkflowVersion';
|
||||
import {
|
||||
WorkflowTrigger,
|
||||
WorkflowVersion,
|
||||
@ -17,16 +18,31 @@ export const useUpdateWorkflowVersionTrigger = ({
|
||||
objectNameSingular: CoreObjectNameSingular.WorkflowVersion,
|
||||
});
|
||||
|
||||
const { createNewWorkflowVersion } = useCreateNewWorkflowVersion({
|
||||
workflowId: workflow.id,
|
||||
});
|
||||
|
||||
const updateTrigger = async (updatedTrigger: WorkflowTrigger) => {
|
||||
if (!isDefined(workflow.currentVersion)) {
|
||||
throw new Error('Can not update an undefined workflow version.');
|
||||
}
|
||||
|
||||
await updateOneWorkflowVersion({
|
||||
idToUpdate: workflow.currentVersion.id,
|
||||
updateOneRecordInput: {
|
||||
trigger: updatedTrigger,
|
||||
},
|
||||
if (workflow.currentVersion.status === 'DRAFT') {
|
||||
await updateOneWorkflowVersion({
|
||||
idToUpdate: workflow.currentVersion.id,
|
||||
updateOneRecordInput: {
|
||||
trigger: updatedTrigger,
|
||||
},
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
await createNewWorkflowVersion({
|
||||
name: `v${workflow.versions.length + 1}`,
|
||||
status: 'DRAFT',
|
||||
trigger: updatedTrigger,
|
||||
steps: workflow.currentVersion.steps,
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@ -19,6 +19,9 @@ export const useWorkflowWithCurrentVersion = (
|
||||
id: true,
|
||||
name: true,
|
||||
statuses: true,
|
||||
versions: {
|
||||
totalCount: true,
|
||||
},
|
||||
},
|
||||
skip: !isDefined(workflowId),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user