Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create base search record action #10135

Merged
merged 2 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion packages/twenty-front/src/modules/workflow/types/Workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ export type WorkflowDeleteRecordActionSettings = BaseWorkflowActionSettings & {
};
};

export type WorkflowFindRecordsActionSettings = BaseWorkflowActionSettings & {
input: {
objectName: string;
limit?: number;
};
};

type BaseWorkflowAction = {
id: string;
name: string;
Expand Down Expand Up @@ -86,12 +93,18 @@ export type WorkflowDeleteRecordAction = BaseWorkflowAction & {
settings: WorkflowDeleteRecordActionSettings;
};

export type WorkflowFindRecordsAction = BaseWorkflowAction & {
type: 'FIND_RECORDS';
settings: WorkflowFindRecordsActionSettings;
};

export type WorkflowAction =
| WorkflowCodeAction
| WorkflowSendEmailAction
| WorkflowCreateRecordAction
| WorkflowUpdateRecordAction
| WorkflowDeleteRecordAction;
| WorkflowDeleteRecordAction
| WorkflowFindRecordsAction;

export type WorkflowActionType = WorkflowAction['type'];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import { assertUnreachable } from '@/workflow/utils/assertUnreachable';
import { getStepDefinitionOrThrow } from '@/workflow/utils/getStepDefinitionOrThrow';
import { WorkflowEditActionFormCreateRecord } from '@/workflow/workflow-steps/workflow-actions/components/WorkflowEditActionFormCreateRecord';
import { WorkflowEditActionFormDeleteRecord } from '@/workflow/workflow-steps/workflow-actions/components/WorkflowEditActionFormDeleteRecord';
import { WorkflowEditActionFormFindRecords } from '@/workflow/workflow-steps/workflow-actions/components/WorkflowEditActionFormFindRecords';
import { WorkflowEditActionFormSendEmail } from '@/workflow/workflow-steps/workflow-actions/components/WorkflowEditActionFormSendEmail';
import { WorkflowEditActionFormUpdateRecord } from '@/workflow/workflow-steps/workflow-actions/components/WorkflowEditActionFormUpdateRecord';
import { WorkflowEditTriggerCronForm } from '@/workflow/workflow-trigger/components/WorkflowEditTriggerCronForm';
import { WorkflowEditTriggerDatabaseEventForm } from '@/workflow/workflow-trigger/components/WorkflowEditTriggerDatabaseEventForm';
import { WorkflowEditTriggerManualForm } from '@/workflow/workflow-trigger/components/WorkflowEditTriggerManualForm';
import { WorkflowEditTriggerCronForm } from '@/workflow/workflow-trigger/components/WorkflowEditTriggerCronForm';
import { Suspense, lazy } from 'react';
import { isDefined } from 'twenty-shared';
import { RightDrawerSkeletonLoader } from '~/loading/components/RightDrawerSkeletonLoader';
Expand Down Expand Up @@ -138,6 +139,16 @@ export const WorkflowStepDetail = ({
/>
);
}

case 'FIND_RECORDS': {
return (
<WorkflowEditActionFormFindRecords
key={stepId}
action={stepDefinition.definition}
actionOptions={props}
/>
);
}
}

return null;
thomtrp marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import { useFilteredObjectMetadataItems } from '@/object-metadata/hooks/useFilteredObjectMetadataItems';
import { Select, SelectOption } from '@/ui/input/components/Select';
import { WorkflowFindRecordsAction } from '@/workflow/types/Workflow';
import { WorkflowStepHeader } from '@/workflow/workflow-steps/components/WorkflowStepHeader';
import { useTheme } from '@emotion/react';
import { useEffect, useState } from 'react';

import { FormNumberFieldInput } from '@/object-record/record-field/form-types/components/FormNumberFieldInput';
import { WorkflowStepBody } from '@/workflow/workflow-steps/components/WorkflowStepBody';
import { getActionIcon } from '@/workflow/workflow-steps/workflow-actions/utils/getActionIcon';
import { isDefined } from 'twenty-shared';
import { HorizontalSeparator, useIcons } from 'twenty-ui';
import { useDebouncedCallback } from 'use-debounce';

type WorkflowEditActionFormFindRecordsProps = {
action: WorkflowFindRecordsAction;
actionOptions:
| {
readonly: true;
}
| {
readonly?: false;
onActionUpdate: (action: WorkflowFindRecordsAction) => void;
};
};

type FindRecordsFormData = {
objectName: string;
limit?: number;
};

export const WorkflowEditActionFormFindRecords = ({
action,
actionOptions,
}: WorkflowEditActionFormFindRecordsProps) => {
const theme = useTheme();
const { getIcon } = useIcons();

const { activeObjectMetadataItems } = useFilteredObjectMetadataItems();

const availableMetadata: Array<SelectOption<string>> =
activeObjectMetadataItems.map((item) => ({
Icon: getIcon(item.icon),
label: item.labelPlural,
value: item.nameSingular,
}));

const [formData, setFormData] = useState<FindRecordsFormData>({
objectName: action.settings.input.objectName,
limit: action.settings.input.limit,
});
const isFormDisabled = actionOptions.readonly;

const selectedObjectMetadataItemNameSingular = formData.objectName;

const selectedObjectMetadataItem = activeObjectMetadataItems.find(
(item) => item.nameSingular === selectedObjectMetadataItemNameSingular,
);
if (!isDefined(selectedObjectMetadataItem)) {
throw new Error('Should have found the metadata item');
}
thomtrp marked this conversation as resolved.
Show resolved Hide resolved

const saveAction = useDebouncedCallback(
async (formData: FindRecordsFormData) => {
if (actionOptions.readonly === true) {
return;
}

const { objectName: updatedObjectName, limit: updatedLimit } = formData;

actionOptions.onActionUpdate({
...action,
settings: {
...action.settings,
input: {
objectName: updatedObjectName,
limit: updatedLimit ?? 1,
},
},
});
},
1_000,
);

useEffect(() => {
return () => {
saveAction.flush();
};
}, [saveAction]);

const headerTitle = isDefined(action.name) ? action.name : `Search Records`;
const headerIcon = getActionIcon(action.type);

return (
<>
<WorkflowStepHeader
onTitleChange={(newName: string) => {
if (actionOptions.readonly === true) {
return;
}

actionOptions.onActionUpdate({
...action,
name: newName,
});
}}
Icon={getIcon(headerIcon)}
iconColor={theme.font.color.tertiary}
initialTitle={headerTitle}
headerType="Action"
disabled={isFormDisabled}
/>
<WorkflowStepBody>
<Select
dropdownId="workflow-edit-action-record-find-records-object-name"
label="Object"
fullWidth
disabled={isFormDisabled}
value={formData.objectName}
emptyOption={{ label: 'Select an option', value: '' }}
options={availableMetadata}
onChange={(objectName) => {
const newFormData: FindRecordsFormData = {
objectName,
limit: 1,
};
thomtrp marked this conversation as resolved.
Show resolved Hide resolved

setFormData(newFormData);

saveAction(newFormData);
}}
withSearchInput
/>

<HorizontalSeparator noMargin />

<FormNumberFieldInput
label="Limit"
defaultValue={formData.limit}
placeholder="Enter limit"
onPersist={() => {}}
readonly
/>
thomtrp marked this conversation as resolved.
Show resolved Hide resolved
</WorkflowStepBody>
</>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { WorkflowFindRecordsAction } from '@/workflow/types/Workflow';
import { WorkflowEditActionFormFindRecords } from '@/workflow/workflow-steps/workflow-actions/components/WorkflowEditActionFormFindRecords';
import { Meta, StoryObj } from '@storybook/react';
import { expect, fn, userEvent, within } from '@storybook/test';
import { ComponentDecorator, RouterDecorator } from 'twenty-ui';
import { ObjectMetadataItemsDecorator } from '~/testing/decorators/ObjectMetadataItemsDecorator';
import { SnackBarDecorator } from '~/testing/decorators/SnackBarDecorator';
import { WorkflowStepActionDrawerDecorator } from '~/testing/decorators/WorkflowStepActionDrawerDecorator';
import { WorkflowStepDecorator } from '~/testing/decorators/WorkflowStepDecorator';
import { WorkspaceDecorator } from '~/testing/decorators/WorkspaceDecorator';
import { graphqlMocks } from '~/testing/graphqlMocks';
import { getWorkflowNodeIdMock } from '~/testing/mock-data/workflow';

const DEFAULT_ACTION = {
id: getWorkflowNodeIdMock(),
name: 'Search Records',
type: 'FIND_RECORDS',
valid: false,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Default action is set to invalid (valid: false) but there's no validation logic shown to determine when it becomes valid

settings: {
input: {
objectName: 'person',
limit: 1,
},
outputSchema: {},
errorHandlingOptions: {
retryOnFailure: {
value: false,
},
continueOnFailure: {
value: false,
},
},
},
} satisfies WorkflowFindRecordsAction;

const meta: Meta<typeof WorkflowEditActionFormFindRecords> = {
title: 'Modules/Workflow/WorkflowEditActionFormFindRecords',
component: WorkflowEditActionFormFindRecords,
parameters: {
msw: graphqlMocks,
},
args: {
action: DEFAULT_ACTION,
},
decorators: [
WorkflowStepActionDrawerDecorator,
WorkflowStepDecorator,
ComponentDecorator,
ObjectMetadataItemsDecorator,
SnackBarDecorator,
RouterDecorator,
WorkspaceDecorator,
],
};

export default meta;

type Story = StoryObj<typeof WorkflowEditActionFormFindRecords>;

export const Default: Story = {
args: {
actionOptions: {
onActionUpdate: fn(),
},
},
};

export const DisabledWithEmptyValues: Story = {
args: {
actionOptions: {
readonly: true,
},
},
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);

const titleInput = await canvas.findByDisplayValue('Search Records');

expect(titleInput).toBeDisabled();

const objectSelectCurrentValue = await canvas.findByText('People');

await userEvent.click(objectSelectCurrentValue);

{
const searchInputInSelectDropdown =
canvas.queryByPlaceholderText('Search');

expect(searchInputInSelectDropdown).not.toBeInTheDocument();
}
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ export const RECORD_ACTIONS: Array<{
type: 'DELETE_RECORD',
icon: 'IconTrash',
},
{
label: 'Search Records',
type: 'FIND_RECORDS',
icon: 'IconSearch',
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const getActionIcon = (actionType: string) => {
case 'CREATE_RECORD':
case 'UPDATE_RECORD':
case 'DELETE_RECORD':
case 'FIND_RECORDS':
return RECORD_ACTIONS.find((item) => item.type === actionType)?.icon;
default:
return OTHER_ACTIONS.find((item) => item.type === actionType)?.icon;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';

import { isDefined } from 'twenty-shared';
import { Repository } from 'typeorm';
import { v4 } from 'uuid';
import { isDefined } from 'twenty-shared';

import { DatabaseEventAction } from 'src/engine/api/graphql/graphql-query-runner/enums/database-event-action';
import { BASE_TYPESCRIPT_PROJECT_INPUT_SCHEMA } from 'src/engine/core-modules/serverless/drivers/constants/base-typescript-project-input-schema';
Expand Down Expand Up @@ -186,6 +186,26 @@ export class WorkflowVersionStepWorkspaceService {
},
};
}
case WorkflowActionType.FIND_RECORDS: {
const activeObjectMetadataItem =
await this.objectMetadataRepository.findOne({
where: { workspaceId, isActive: true, isSystem: false },
});

return {
id: newStepId,
name: 'Search Records',
type: WorkflowActionType.FIND_RECORDS,
valid: false,
settings: {
...BASE_STEP_DEFINITION,
input: {
objectName: activeObjectMetadataItem?.nameSingular || '',
limit: 1,
},
thomtrp marked this conversation as resolved.
Show resolved Hide resolved
},
};
}
Comment on lines +189 to +208
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: No error handling if activeObjectMetadataItem is undefined. Consider throwing a WorkflowVersionStepException if no active object metadata is found.

Suggested change
case WorkflowActionType.FIND_RECORDS: {
const activeObjectMetadataItem =
await this.objectMetadataRepository.findOne({
where: { workspaceId, isActive: true, isSystem: false },
});
return {
id: newStepId,
name: 'Search Records',
type: WorkflowActionType.FIND_RECORDS,
valid: false,
settings: {
...BASE_STEP_DEFINITION,
input: {
objectName: activeObjectMetadataItem?.nameSingular || '',
limit: 1,
},
},
};
}
case WorkflowActionType.FIND_RECORDS: {
const activeObjectMetadataItem =
await this.objectMetadataRepository.findOne({
where: { workspaceId, isActive: true, isSystem: false },
});
if (!isDefined(activeObjectMetadataItem)) {
throw new WorkflowVersionStepException(
'No active object metadata found',
WorkflowVersionStepExceptionCode.FAILURE,
);
}
return {
id: newStepId,
name: 'Search Records',
type: WorkflowActionType.FIND_RECORDS,
valid: false,
settings: {
...BASE_STEP_DEFINITION,
input: {
objectName: activeObjectMetadataItem.nameSingular,
limit: 1,
},
},
};
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about this advice?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know honestly. I think the current behavior is ok: the user will not have any objects to pick

default:
throw new WorkflowVersionStepException(
`WorkflowActionType '${type}' unknown`,
Expand Down
Loading