101 featch available variables from previous steps (#8062)
- add outputSchema in workflow step settings - use outputSchemas to compute step available variables https://github.com/user-attachments/assets/6b851d8e-625c-49ff-b29c-074cd86cbfee
This commit is contained in:
@ -4,7 +4,7 @@ import { ApolloClient, useApolloClient, useMutation } from '@apollo/client';
|
||||
import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem';
|
||||
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
|
||||
import { modifyRecordFromCache } from '@/object-record/cache/utils/modifyRecordFromCache';
|
||||
import { ACTIVATE_WORKFLOW_VERSION } from '@/workflow/graphql/activateWorkflowVersion';
|
||||
import { ACTIVATE_WORKFLOW_VERSION } from '@/workflow/graphql/mutations/activateWorkflowVersion';
|
||||
import { WorkflowVersion } from '@/workflow/types/Workflow';
|
||||
import {
|
||||
ActivateWorkflowVersionMutation,
|
||||
|
||||
@ -0,0 +1,67 @@
|
||||
import { capitalize } from '~/utils/string/capitalize';
|
||||
import { useRecoilValue } from 'recoil';
|
||||
import { workflowIdState } from '@/workflow/states/workflowIdState';
|
||||
import { useWorkflowWithCurrentVersion } from '@/workflow/hooks/useWorkflowWithCurrentVersion';
|
||||
import { workflowSelectedNodeState } from '@/workflow/states/workflowSelectedNodeState';
|
||||
import { getStepDefinitionOrThrow } from '@/workflow/utils/getStepDefinitionOrThrow';
|
||||
import { isDefined } from 'twenty-ui';
|
||||
import { StepOutputSchema } from '@/workflow/search-variables/types/StepOutputSchema';
|
||||
|
||||
export const useAvailableVariablesInWorkflowStep = (): StepOutputSchema[] => {
|
||||
const workflowId = useRecoilValue(workflowIdState);
|
||||
const workflow = useWorkflowWithCurrentVersion(workflowId);
|
||||
const workflowSelectedNode = useRecoilValue(workflowSelectedNodeState);
|
||||
|
||||
if (!isDefined(workflowSelectedNode) || !isDefined(workflow)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const stepDefinition = getStepDefinitionOrThrow({
|
||||
stepId: workflowSelectedNode,
|
||||
workflowVersion: workflow.currentVersion,
|
||||
});
|
||||
|
||||
if (
|
||||
!isDefined(stepDefinition) ||
|
||||
stepDefinition.type === 'trigger' ||
|
||||
!isDefined(workflow.currentVersion.steps)
|
||||
) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const previousSteps = [];
|
||||
|
||||
for (const step of workflow.currentVersion.steps) {
|
||||
if (step.id === workflowSelectedNode) {
|
||||
break;
|
||||
}
|
||||
previousSteps.push(step);
|
||||
}
|
||||
|
||||
const result = [];
|
||||
|
||||
if (
|
||||
workflow.currentVersion.trigger?.type === 'DATABASE_EVENT' &&
|
||||
isDefined(workflow.currentVersion.trigger?.settings?.outputSchema)
|
||||
) {
|
||||
const [object, action] =
|
||||
workflow.currentVersion.trigger.settings.eventName.split('.');
|
||||
result.push({
|
||||
id: 'trigger',
|
||||
name: `${capitalize(object)} is ${capitalize(action)}`,
|
||||
outputSchema: workflow.currentVersion.trigger.settings.outputSchema,
|
||||
});
|
||||
}
|
||||
|
||||
previousSteps.forEach((previousStep) => {
|
||||
if (isDefined(previousStep.settings.outputSchema)) {
|
||||
result.push({
|
||||
id: previousStep.id,
|
||||
name: previousStep.name,
|
||||
outputSchema: previousStep.settings.outputSchema,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
};
|
||||
@ -0,0 +1,26 @@
|
||||
import { useApolloMetadataClient } from '@/object-metadata/hooks/useApolloMetadataClient';
|
||||
import { ApolloClient, useMutation } from '@apollo/client';
|
||||
import {
|
||||
ComputeStepOutputSchemaInput,
|
||||
ComputeStepOutputSchemaMutation,
|
||||
ComputeStepOutputSchemaMutationVariables,
|
||||
} from '~/generated/graphql';
|
||||
import { COMPUTE_STEP_OUTPUT_SCHEMA } from '@/workflow/graphql/mutations/computeStepOutputSchema';
|
||||
|
||||
export const useComputeStepOutputSchema = () => {
|
||||
const apolloMetadataClient = useApolloMetadataClient();
|
||||
const [mutate] = useMutation<
|
||||
ComputeStepOutputSchemaMutation,
|
||||
ComputeStepOutputSchemaMutationVariables
|
||||
>(COMPUTE_STEP_OUTPUT_SCHEMA, {
|
||||
client: apolloMetadataClient ?? ({} as ApolloClient<any>),
|
||||
});
|
||||
|
||||
const computeStepOutputSchema = async (
|
||||
input: ComputeStepOutputSchemaInput,
|
||||
) => {
|
||||
return await mutate({ variables: { input } });
|
||||
};
|
||||
|
||||
return { computeStepOutputSchema };
|
||||
};
|
||||
@ -16,6 +16,7 @@ import { getStepDefaultDefinition } from '@/workflow/utils/getStepDefaultDefinit
|
||||
import { insertStep } from '@/workflow/utils/insertStep';
|
||||
import { useRecoilValue, useSetRecoilState } from 'recoil';
|
||||
import { isDefined } from 'twenty-ui';
|
||||
import { useComputeStepOutputSchema } from '@/workflow/hooks/useComputeStepOutputSchema';
|
||||
|
||||
export const useCreateStep = ({
|
||||
workflow,
|
||||
@ -40,6 +41,8 @@ export const useCreateStep = ({
|
||||
|
||||
const { createNewWorkflowVersion } = useCreateNewWorkflowVersion();
|
||||
|
||||
const { computeStepOutputSchema } = useComputeStepOutputSchema();
|
||||
|
||||
const insertNodeAndSave = async ({
|
||||
parentNodeId,
|
||||
nodeToAdd,
|
||||
@ -85,6 +88,17 @@ export const useCreateStep = ({
|
||||
|
||||
const newStep = getStepDefaultDefinition(newStepType);
|
||||
|
||||
const outputSchema = (
|
||||
await computeStepOutputSchema({
|
||||
step: newStep,
|
||||
})
|
||||
)?.data?.computeStepOutputSchema;
|
||||
|
||||
newStep.settings = {
|
||||
...newStep.settings,
|
||||
outputSchema: outputSchema || {},
|
||||
};
|
||||
|
||||
await insertNodeAndSave({
|
||||
parentNodeId: workflowCreateStepFromParentStepId,
|
||||
nodeToAdd: newStep,
|
||||
|
||||
@ -4,7 +4,7 @@ import { ApolloClient, useApolloClient, useMutation } from '@apollo/client';
|
||||
import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem';
|
||||
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
|
||||
import { modifyRecordFromCache } from '@/object-record/cache/utils/modifyRecordFromCache';
|
||||
import { DEACTIVATE_WORKFLOW_VERSION } from '@/workflow/graphql/deactivateWorkflowVersion';
|
||||
import { DEACTIVATE_WORKFLOW_VERSION } from '@/workflow/graphql/mutations/deactivateWorkflowVersion';
|
||||
import {
|
||||
ActivateWorkflowVersionMutation,
|
||||
ActivateWorkflowVersionMutationVariables,
|
||||
|
||||
@ -8,6 +8,7 @@ import {
|
||||
} from '@/workflow/types/Workflow';
|
||||
import { replaceStep } from '@/workflow/utils/replaceStep';
|
||||
import { isDefined } from 'twenty-ui';
|
||||
import { useComputeStepOutputSchema } from '@/workflow/hooks/useComputeStepOutputSchema';
|
||||
|
||||
export const useUpdateWorkflowVersionStep = ({
|
||||
workflow,
|
||||
@ -22,12 +23,24 @@ export const useUpdateWorkflowVersionStep = ({
|
||||
});
|
||||
|
||||
const { createNewWorkflowVersion } = useCreateNewWorkflowVersion();
|
||||
const { computeStepOutputSchema } = useComputeStepOutputSchema();
|
||||
|
||||
const updateStep = async <T extends WorkflowStep>(updatedStep: T) => {
|
||||
if (!isDefined(workflow.currentVersion)) {
|
||||
throw new Error('Can not update an undefined workflow version.');
|
||||
}
|
||||
|
||||
const outputSchema = (
|
||||
await computeStepOutputSchema({
|
||||
step: updatedStep,
|
||||
})
|
||||
)?.data?.computeStepOutputSchema;
|
||||
|
||||
updatedStep.settings = {
|
||||
...updatedStep.settings,
|
||||
outputSchema: outputSchema || {},
|
||||
};
|
||||
|
||||
const updatedSteps = replaceStep({
|
||||
steps: workflow.currentVersion.steps ?? [],
|
||||
stepId,
|
||||
|
||||
@ -7,6 +7,7 @@ import {
|
||||
WorkflowWithCurrentVersion,
|
||||
} from '@/workflow/types/Workflow';
|
||||
import { isDefined } from 'twenty-ui';
|
||||
import { useComputeStepOutputSchema } from '@/workflow/hooks/useComputeStepOutputSchema';
|
||||
|
||||
export const useUpdateWorkflowVersionTrigger = ({
|
||||
workflow,
|
||||
@ -20,12 +21,25 @@ export const useUpdateWorkflowVersionTrigger = ({
|
||||
|
||||
const { createNewWorkflowVersion } = useCreateNewWorkflowVersion();
|
||||
|
||||
const { computeStepOutputSchema } = useComputeStepOutputSchema();
|
||||
|
||||
const updateTrigger = async (updatedTrigger: WorkflowTrigger) => {
|
||||
if (!isDefined(workflow.currentVersion)) {
|
||||
throw new Error('Can not update an undefined workflow version.');
|
||||
}
|
||||
|
||||
if (workflow.currentVersion.status === 'DRAFT') {
|
||||
const outputSchema = (
|
||||
await computeStepOutputSchema({
|
||||
step: updatedTrigger,
|
||||
})
|
||||
)?.data?.computeStepOutputSchema;
|
||||
|
||||
updatedTrigger.settings = {
|
||||
...updatedTrigger.settings,
|
||||
outputSchema: outputSchema || {},
|
||||
};
|
||||
|
||||
await updateOneWorkflowVersion({
|
||||
idToUpdate: workflow.currentVersion.id,
|
||||
updateOneRecordInput: {
|
||||
|
||||
Reference in New Issue
Block a user