Update schema and add tests (#12314)
For database event triggers, we remove the before / after logic. We go directly with the properties <img width="211" alt="Capture d’écran 2025-05-27 à 11 40 36" src="https://github.com/user-attachments/assets/a05bd3c1-104b-477b-be52-d56846ce7e63" /> To achieve this without changing the shape of events, we need to handle keys using dots, such: ``` 'properties.after.name': { icon: 'IconBuildingSkyscraper', type: FieldMetadataType.TEXT, label: 'Name', value: 'My text', isLeaf: true, }, ``` This PR: - adds logic to handle the case where the key has dot included - adds tests
This commit is contained in:
@ -1,191 +1,64 @@
|
||||
import { StepOutputSchema } from '@/workflow/workflow-variables/types/StepOutputSchema';
|
||||
import { searchVariableThroughOutputSchema } from '@/workflow/workflow-variables/utils/searchVariableThroughOutputSchema';
|
||||
|
||||
const mockStep = {
|
||||
id: 'step-1',
|
||||
name: 'Step 1',
|
||||
outputSchema: {
|
||||
company: {
|
||||
isLeaf: false,
|
||||
icon: 'company',
|
||||
label: 'Company',
|
||||
value: {
|
||||
object: {
|
||||
nameSingular: 'company',
|
||||
fieldIdName: 'id',
|
||||
label: 'Company',
|
||||
value: 'John',
|
||||
isLeaf: true,
|
||||
},
|
||||
fields: {
|
||||
name: { label: 'Name', value: 'Twenty', isLeaf: true },
|
||||
address: { label: 'Address', value: '123 Main St', isLeaf: true },
|
||||
},
|
||||
_outputSchemaType: 'RECORD',
|
||||
},
|
||||
},
|
||||
person: {
|
||||
isLeaf: false,
|
||||
icon: 'person',
|
||||
label: 'Person',
|
||||
value: {
|
||||
object: {
|
||||
nameSingular: 'person',
|
||||
fieldIdName: 'id',
|
||||
label: 'Person',
|
||||
value: 'Jane',
|
||||
isLeaf: true,
|
||||
},
|
||||
fields: {
|
||||
firstName: { label: 'First Name', value: 'Jane', isLeaf: true },
|
||||
lastName: { label: 'Last Name', value: 'Doe', isLeaf: true },
|
||||
email: { label: 'Email', value: 'jane@example.com', isLeaf: true },
|
||||
},
|
||||
_outputSchemaType: 'RECORD',
|
||||
},
|
||||
},
|
||||
simpleData: {
|
||||
isLeaf: true,
|
||||
label: 'Simple Data',
|
||||
value: 'Simple value',
|
||||
},
|
||||
nestedData: {
|
||||
isLeaf: false,
|
||||
label: 'Nested Data',
|
||||
value: {
|
||||
field1: { label: 'Field 1', value: 'Value 1', isLeaf: true },
|
||||
field2: { label: 'Field 2', value: 'Value 2', isLeaf: true },
|
||||
},
|
||||
},
|
||||
},
|
||||
} satisfies StepOutputSchema;
|
||||
import { FieldMetadataType } from '~/generated/graphql';
|
||||
|
||||
describe('searchVariableThroughOutputSchema', () => {
|
||||
it('should not break with wrong path', () => {
|
||||
const result = searchVariableThroughOutputSchema({
|
||||
stepOutputSchema: mockStep,
|
||||
rawVariableName: '{{step-1.wrong.wrong.wrong}}',
|
||||
isFullRecord: false,
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
variableLabel: undefined,
|
||||
variablePathLabel: 'Step 1 > undefined',
|
||||
});
|
||||
});
|
||||
|
||||
it('should find a company field variable', () => {
|
||||
const result = searchVariableThroughOutputSchema({
|
||||
stepOutputSchema: mockStep,
|
||||
rawVariableName: '{{step-1.company.name}}',
|
||||
isFullRecord: false,
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
variableLabel: 'Name',
|
||||
variablePathLabel: 'Step 1 > Company > Name',
|
||||
});
|
||||
});
|
||||
|
||||
it('should find a person field variable', () => {
|
||||
const result = searchVariableThroughOutputSchema({
|
||||
stepOutputSchema: mockStep,
|
||||
rawVariableName: '{{step-1.person.email}}',
|
||||
isFullRecord: false,
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
variableLabel: 'Email',
|
||||
variablePathLabel: 'Step 1 > Person > Email',
|
||||
});
|
||||
});
|
||||
|
||||
it('should find a company object variable', () => {
|
||||
const result = searchVariableThroughOutputSchema({
|
||||
stepOutputSchema: mockStep,
|
||||
rawVariableName: '{{step-1.company.id}}',
|
||||
isFullRecord: true,
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
variableLabel: 'Company',
|
||||
variablePathLabel: 'Step 1 > Company > Company',
|
||||
});
|
||||
});
|
||||
|
||||
it('should find a person object variable', () => {
|
||||
const result = searchVariableThroughOutputSchema({
|
||||
stepOutputSchema: mockStep,
|
||||
rawVariableName: '{{step-1.person.id}}',
|
||||
isFullRecord: true,
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
variableLabel: 'Person',
|
||||
variablePathLabel: 'Step 1 > Person > Person',
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle simple data fields', () => {
|
||||
const result = searchVariableThroughOutputSchema({
|
||||
stepOutputSchema: mockStep,
|
||||
rawVariableName: '{{step-1.simpleData}}',
|
||||
isFullRecord: false,
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
variableLabel: 'Simple Data',
|
||||
variablePathLabel: 'Step 1 > Simple Data',
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle nested data fields', () => {
|
||||
const result = searchVariableThroughOutputSchema({
|
||||
stepOutputSchema: mockStep,
|
||||
rawVariableName: '{{step-1.nestedData.field1}}',
|
||||
isFullRecord: false,
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
variableLabel: 'Field 1',
|
||||
variablePathLabel: 'Step 1 > Nested Data > Field 1',
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle invalid variable names', () => {
|
||||
const result = searchVariableThroughOutputSchema({
|
||||
stepOutputSchema: mockStep,
|
||||
rawVariableName: '{{invalid}}',
|
||||
isFullRecord: false,
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
variableLabel: undefined,
|
||||
variablePathLabel: 'Step 1 > undefined',
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle non-existent paths', () => {
|
||||
const result = searchVariableThroughOutputSchema({
|
||||
stepOutputSchema: mockStep,
|
||||
rawVariableName: '{{step-1.nonExistent.field}}',
|
||||
isFullRecord: false,
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
variableLabel: undefined,
|
||||
variablePathLabel: 'Step 1 > undefined',
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle the case where the path has dots in field names', () => {
|
||||
const mockStepWithDotInField = {
|
||||
describe('step tests', () => {
|
||||
const mockStep = {
|
||||
id: 'step-1',
|
||||
name: 'Step 1',
|
||||
outputSchema: {
|
||||
'complex.field': {
|
||||
company: {
|
||||
isLeaf: false,
|
||||
label: 'Complex Field',
|
||||
icon: 'company',
|
||||
label: 'Company',
|
||||
value: {
|
||||
object: {
|
||||
nameSingular: 'company',
|
||||
fieldIdName: 'id',
|
||||
label: 'Company',
|
||||
value: 'John',
|
||||
isLeaf: true,
|
||||
},
|
||||
fields: {
|
||||
name: { label: 'Name', value: 'Twenty', isLeaf: true },
|
||||
address: { label: 'Address', value: '123 Main St', isLeaf: true },
|
||||
},
|
||||
_outputSchemaType: 'RECORD',
|
||||
},
|
||||
},
|
||||
person: {
|
||||
isLeaf: false,
|
||||
icon: 'person',
|
||||
label: 'Person',
|
||||
value: {
|
||||
object: {
|
||||
nameSingular: 'person',
|
||||
fieldIdName: 'id',
|
||||
label: 'Person',
|
||||
value: 'Jane',
|
||||
isLeaf: true,
|
||||
},
|
||||
fields: {
|
||||
firstName: { label: 'First Name', value: 'Jane', isLeaf: true },
|
||||
lastName: { label: 'Last Name', value: 'Doe', isLeaf: true },
|
||||
email: {
|
||||
label: 'Email',
|
||||
value: 'jane@example.com',
|
||||
isLeaf: true,
|
||||
},
|
||||
},
|
||||
_outputSchemaType: 'RECORD',
|
||||
},
|
||||
},
|
||||
simpleData: {
|
||||
isLeaf: true,
|
||||
label: 'Simple Data',
|
||||
value: 'Simple value',
|
||||
},
|
||||
nestedData: {
|
||||
isLeaf: false,
|
||||
label: 'Nested Data',
|
||||
value: {
|
||||
field1: { label: 'Field 1', value: 'Value 1', isLeaf: true },
|
||||
field2: { label: 'Field 2', value: 'Value 2', isLeaf: true },
|
||||
@ -193,16 +66,255 @@ describe('searchVariableThroughOutputSchema', () => {
|
||||
},
|
||||
},
|
||||
} satisfies StepOutputSchema;
|
||||
it('should not break with wrong path', () => {
|
||||
const result = searchVariableThroughOutputSchema({
|
||||
stepOutputSchema: mockStep,
|
||||
rawVariableName: '{{step-1.wrong.wrong.wrong}}',
|
||||
isFullRecord: false,
|
||||
});
|
||||
|
||||
const result = searchVariableThroughOutputSchema({
|
||||
stepOutputSchema: mockStepWithDotInField,
|
||||
rawVariableName: '{{step-1.complex.field.field1}}',
|
||||
isFullRecord: false,
|
||||
expect(result).toEqual({
|
||||
variableLabel: undefined,
|
||||
variablePathLabel: 'Step 1 > undefined',
|
||||
});
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
variableLabel: 'Field 1',
|
||||
variablePathLabel: 'Step 1 > Complex Field > Field 1',
|
||||
it('should find a company field variable', () => {
|
||||
const result = searchVariableThroughOutputSchema({
|
||||
stepOutputSchema: mockStep,
|
||||
rawVariableName: '{{step-1.company.name}}',
|
||||
isFullRecord: false,
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
variableLabel: 'Name',
|
||||
variablePathLabel: 'Step 1 > Company > Name',
|
||||
});
|
||||
});
|
||||
|
||||
it('should find a person field variable', () => {
|
||||
const result = searchVariableThroughOutputSchema({
|
||||
stepOutputSchema: mockStep,
|
||||
rawVariableName: '{{step-1.person.email}}',
|
||||
isFullRecord: false,
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
variableLabel: 'Email',
|
||||
variablePathLabel: 'Step 1 > Person > Email',
|
||||
});
|
||||
});
|
||||
|
||||
it('should find a company object variable', () => {
|
||||
const result = searchVariableThroughOutputSchema({
|
||||
stepOutputSchema: mockStep,
|
||||
rawVariableName: '{{step-1.company.id}}',
|
||||
isFullRecord: true,
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
variableLabel: 'Company',
|
||||
variablePathLabel: 'Step 1 > Company > Company',
|
||||
});
|
||||
});
|
||||
|
||||
it('should find a person object variable', () => {
|
||||
const result = searchVariableThroughOutputSchema({
|
||||
stepOutputSchema: mockStep,
|
||||
rawVariableName: '{{step-1.person.id}}',
|
||||
isFullRecord: true,
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
variableLabel: 'Person',
|
||||
variablePathLabel: 'Step 1 > Person > Person',
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle simple data fields', () => {
|
||||
const result = searchVariableThroughOutputSchema({
|
||||
stepOutputSchema: mockStep,
|
||||
rawVariableName: '{{step-1.simpleData}}',
|
||||
isFullRecord: false,
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
variableLabel: 'Simple Data',
|
||||
variablePathLabel: 'Step 1 > Simple Data',
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle nested data fields', () => {
|
||||
const result = searchVariableThroughOutputSchema({
|
||||
stepOutputSchema: mockStep,
|
||||
rawVariableName: '{{step-1.nestedData.field1}}',
|
||||
isFullRecord: false,
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
variableLabel: 'Field 1',
|
||||
variablePathLabel: 'Step 1 > Nested Data > Field 1',
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle invalid variable names', () => {
|
||||
const result = searchVariableThroughOutputSchema({
|
||||
stepOutputSchema: mockStep,
|
||||
rawVariableName: '{{invalid}}',
|
||||
isFullRecord: false,
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
variableLabel: undefined,
|
||||
variablePathLabel: 'Step 1 > undefined',
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle non-existent paths', () => {
|
||||
const result = searchVariableThroughOutputSchema({
|
||||
stepOutputSchema: mockStep,
|
||||
rawVariableName: '{{step-1.nonExistent.field}}',
|
||||
isFullRecord: false,
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
variableLabel: undefined,
|
||||
variablePathLabel: 'Step 1 > undefined',
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle the case where the path has dots in field names', () => {
|
||||
const mockStepWithDotInField = {
|
||||
id: 'step-1',
|
||||
name: 'Step 1',
|
||||
outputSchema: {
|
||||
'complex.field': {
|
||||
isLeaf: false,
|
||||
label: 'Complex Field',
|
||||
value: {
|
||||
field1: { label: 'Field 1', value: 'Value 1', isLeaf: true },
|
||||
field2: { label: 'Field 2', value: 'Value 2', isLeaf: true },
|
||||
},
|
||||
},
|
||||
},
|
||||
} satisfies StepOutputSchema;
|
||||
|
||||
const result = searchVariableThroughOutputSchema({
|
||||
stepOutputSchema: mockStepWithDotInField,
|
||||
rawVariableName: '{{step-1.complex.field.field1}}',
|
||||
isFullRecord: false,
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
variableLabel: 'Field 1',
|
||||
variablePathLabel: 'Step 1 > Complex Field > Field 1',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('trigger tests', () => {
|
||||
const mockTrigger = {
|
||||
id: 'trigger',
|
||||
name: 'Record is Created',
|
||||
icon: 'IconPlaylistAdd',
|
||||
outputSchema: {
|
||||
fields: {
|
||||
'properties.after.id': {
|
||||
icon: 'Icon123',
|
||||
type: FieldMetadataType.UUID,
|
||||
label: 'Id',
|
||||
value: '123e4567-e89b-12d3-a456-426614174000',
|
||||
isLeaf: true,
|
||||
},
|
||||
'properties.after.name': {
|
||||
icon: 'IconBuildingSkyscraper',
|
||||
type: FieldMetadataType.TEXT,
|
||||
label: 'Name',
|
||||
value: 'My text',
|
||||
isLeaf: true,
|
||||
},
|
||||
'properties.after.annualRecurringRevenue': {
|
||||
icon: 'IconMoneybag',
|
||||
label: 'ARR',
|
||||
value: {
|
||||
amountMicros: {
|
||||
type: FieldMetadataType.NUMERIC,
|
||||
label: ' Amount Micros',
|
||||
value: null,
|
||||
isLeaf: true,
|
||||
},
|
||||
currencyCode: {
|
||||
type: FieldMetadataType.TEXT,
|
||||
label: ' Currency Code',
|
||||
value: 'My text',
|
||||
isLeaf: true,
|
||||
},
|
||||
},
|
||||
isLeaf: false,
|
||||
},
|
||||
},
|
||||
object: {
|
||||
icon: 'IconBuildingSkyscraper',
|
||||
label: 'Company',
|
||||
value: 'A company',
|
||||
isLeaf: true,
|
||||
fieldIdName: 'properties.after.id',
|
||||
nameSingular: 'company',
|
||||
},
|
||||
_outputSchemaType: 'RECORD',
|
||||
},
|
||||
} satisfies StepOutputSchema;
|
||||
it('should find a simple field from trigger', () => {
|
||||
const result = searchVariableThroughOutputSchema({
|
||||
stepOutputSchema: mockTrigger,
|
||||
rawVariableName: '{{trigger.properties.after.name}}',
|
||||
isFullRecord: false,
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
variableLabel: 'Name',
|
||||
variablePathLabel: 'Record is Created > Name',
|
||||
});
|
||||
});
|
||||
|
||||
it('should find a nested field from trigger', () => {
|
||||
const result = searchVariableThroughOutputSchema({
|
||||
stepOutputSchema: mockTrigger,
|
||||
rawVariableName:
|
||||
'{{trigger.properties.after.annualRecurringRevenue.amountMicros}}',
|
||||
isFullRecord: false,
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
variableLabel: ' Amount Micros',
|
||||
variablePathLabel: 'Record is Created > ARR > Amount Micros',
|
||||
});
|
||||
});
|
||||
|
||||
it('should find the object field from trigger', () => {
|
||||
const result = searchVariableThroughOutputSchema({
|
||||
stepOutputSchema: mockTrigger,
|
||||
rawVariableName: '{{trigger.object}}',
|
||||
isFullRecord: true,
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
variableLabel: 'Company',
|
||||
variablePathLabel: 'Record is Created > Company',
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle invalid trigger field path', () => {
|
||||
const result = searchVariableThroughOutputSchema({
|
||||
stepOutputSchema: mockTrigger,
|
||||
rawVariableName: '{{trigger.nonExistent}}',
|
||||
isFullRecord: false,
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
variableLabel: undefined,
|
||||
variablePathLabel: 'Record is Created > undefined',
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -45,27 +45,42 @@ const searchCurrentStepOutputSchema = ({
|
||||
let nextKeyIndex = 0;
|
||||
let nextKey = path[nextKeyIndex];
|
||||
let variablePathLabel = stepOutputSchema.name;
|
||||
let isSelectedFieldInNextKey = false;
|
||||
|
||||
const handleFieldNotFound = () => {
|
||||
if (nextKeyIndex + 1 < path.length) {
|
||||
// If the key is not found in the step, we handle the case where the path has been wrongly split
|
||||
// For example, if there is a dot in the field name
|
||||
nextKey = `${nextKey}.${path[nextKeyIndex + 1]}`;
|
||||
} else {
|
||||
// If we already reached the end of the path, we add the selected field to the next key
|
||||
nextKey = `${nextKey}.${selectedField}`;
|
||||
isSelectedFieldInNextKey = true;
|
||||
}
|
||||
};
|
||||
|
||||
while (nextKeyIndex < path.length) {
|
||||
if (!isDefined(currentSubStep)) {
|
||||
break;
|
||||
} else if (isRecordOutputSchema(currentSubStep)) {
|
||||
}
|
||||
|
||||
if (isRecordOutputSchema(currentSubStep)) {
|
||||
const currentField = currentSubStep.fields[nextKey];
|
||||
currentSubStep = currentField?.value;
|
||||
nextKey = path[nextKeyIndex + 1];
|
||||
variablePathLabel = `${variablePathLabel} > ${currentField?.label}`;
|
||||
if (isDefined(currentField)) {
|
||||
currentSubStep = currentField.value;
|
||||
nextKey = path[nextKeyIndex + 1];
|
||||
variablePathLabel = `${variablePathLabel} > ${currentField.label}`;
|
||||
} else {
|
||||
handleFieldNotFound();
|
||||
}
|
||||
} else if (isBaseOutputSchema(currentSubStep)) {
|
||||
if (isDefined(currentSubStep[nextKey])) {
|
||||
const currentField = currentSubStep[nextKey];
|
||||
currentSubStep = currentField?.value;
|
||||
currentSubStep = currentField.value;
|
||||
nextKey = path[nextKeyIndex + 1];
|
||||
variablePathLabel = `${variablePathLabel} > ${currentField?.label}`;
|
||||
variablePathLabel = `${variablePathLabel} > ${currentField.label}`;
|
||||
} else {
|
||||
// If the key is not found in the step, we handle the case where the path has been wrongly split
|
||||
// For example, if there is a dot in the field name
|
||||
if (nextKeyIndex + 1 < path.length) {
|
||||
nextKey = `${nextKey}.${path[nextKeyIndex + 1]}`;
|
||||
}
|
||||
handleFieldNotFound();
|
||||
}
|
||||
}
|
||||
nextKeyIndex++;
|
||||
@ -81,7 +96,10 @@ const searchCurrentStepOutputSchema = ({
|
||||
return {
|
||||
variableLabel: isFullRecord
|
||||
? getDisplayedSubStepObjectLabel(currentSubStep)
|
||||
: getDisplayedSubStepFieldLabel(selectedField, currentSubStep),
|
||||
: getDisplayedSubStepFieldLabel(
|
||||
isSelectedFieldInNextKey ? nextKey : selectedField,
|
||||
currentSubStep,
|
||||
),
|
||||
variablePathLabel,
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user