Skip to content

Commit

Permalink
feat(n8n Form Node): Limit wait time parameters (#13160)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-radency authored Feb 19, 2025
1 parent 301f5a5 commit 14b6f8b
Show file tree
Hide file tree
Showing 15 changed files with 261 additions and 149 deletions.
2 changes: 1 addition & 1 deletion packages/nodes-base/nodes/Discord/v2/actions/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as member from './member';
import * as message from './message';
import type { Discord } from './node.type';
import * as webhook from './webhook';
import { configureWaitTillDate } from '../../../../utils/sendAndWait/utils';
import { configureWaitTillDate } from '../../../../utils/sendAndWait/configureWaitTillDate.util';
import { checkAccessToGuild } from '../helpers/utils';
import { discordApiRequest } from '../transport';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import type {

import { fromEmailProperty, toEmailProperty } from './descriptions';
import { configureTransport } from './utils';
import { configureWaitTillDate } from '../../../utils/sendAndWait/configureWaitTillDate.util';
import { createEmailBody } from '../../../utils/sendAndWait/email-templates';
import {
configureWaitTillDate,
createButton,
getSendAndWaitConfig,
getSendAndWaitProperties,
Expand Down
27 changes: 25 additions & 2 deletions packages/nodes-base/nodes/Form/Form.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,34 @@ import {
FORM_TRIGGER_NODE_TYPE,
tryToParseJsonToFormFields,
NodeConnectionType,
WAIT_INDEFINITELY,
} from 'n8n-workflow';

import { renderFormCompletion } from './formCompletionUtils';
import { renderFormNode } from './formNodeUtils';
import { configureWaitTillDate } from '../../utils/sendAndWait/configureWaitTillDate.util';
import { limitWaitTimeProperties } from '../../utils/sendAndWait/descriptions';
import { formDescription, formFields, formTitle } from '../Form/common.descriptions';
import { prepareFormReturnItem, resolveRawData } from '../Form/utils';

const waitTimeProperties: INodeProperties[] = [
{
displayName: 'Limit Wait Time',
name: 'limitWaitTime',
type: 'boolean',
default: false,
description:
'Whether to limit the time this node should wait for a user response before execution resumes',
},
...updateDisplayOptions(
{
show: {
limitWaitTime: [true],
},
},
limitWaitTimeProperties,
),
];

export const formFieldsProperties: INodeProperties[] = [
{
displayName: 'Define Form',
Expand Down Expand Up @@ -71,6 +91,7 @@ const pageProperties = updateDisplayOptions(
},
[
...formFieldsProperties,
...waitTimeProperties,
{
displayName: 'Options',
name: 'options',
Expand Down Expand Up @@ -177,6 +198,7 @@ const completionProperties = updateDisplayOptions(
placeholder: 'e.g. Thanks for filling the form',
description: 'The text to display on the page. Use HTML to show a customized web page.',
},
...waitTimeProperties,
{
displayName: 'Options',
name: 'options',
Expand Down Expand Up @@ -359,7 +381,8 @@ export class Form extends Node {
);
}

await context.putExecutionToWait(WAIT_INDEFINITELY);
const waitTill = configureWaitTillDate(context, 'root');
await context.putExecutionToWait(waitTill);

return [context.getInputData()];
}
Expand Down
4 changes: 4 additions & 0 deletions packages/nodes-base/nodes/Form/test/Form.node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import type {

import { Form } from '../Form.node';

jest.mock('../../../utils/sendAndWait/configureWaitTillDate.util', () => ({
configureWaitTillDate: jest.fn(), // Mocked function
}));

describe('Form Node', () => {
let form: Form;
let mockExecuteFunctions: MockProxy<IExecuteFunctions>;
Expand Down
7 changes: 2 additions & 5 deletions packages/nodes-base/nodes/Google/Chat/GoogleChat.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,9 @@ import {
validateJSON,
} from './GenericFunctions';
import type { IMessage, IMessageUi } from './MessageInterface';
import { configureWaitTillDate } from '../../../utils/sendAndWait/configureWaitTillDate.util';
import { sendAndWaitWebhooksDescription } from '../../../utils/sendAndWait/descriptions';
import {
configureWaitTillDate,
getSendAndWaitProperties,
sendAndWaitWebhook,
} from '../../../utils/sendAndWait/utils';
import { getSendAndWaitProperties, sendAndWaitWebhook } from '../../../utils/sendAndWait/utils';

export class GoogleChat implements INodeType {
description: INodeTypeDescription = {
Expand Down
2 changes: 1 addition & 1 deletion packages/nodes-base/nodes/Google/Gmail/v2/GmailV2.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import { labelFields, labelOperations } from './LabelDescription';
import { getGmailAliases, getLabels, getThreadMessages } from './loadOptions';
import { messageFields, messageOperations } from './MessageDescription';
import { threadFields, threadOperations } from './ThreadDescription';
import { configureWaitTillDate } from '../../../../utils/sendAndWait/configureWaitTillDate.util';
import { sendAndWaitWebhooksDescription } from '../../../../utils/sendAndWait/descriptions';
import type { IEmail } from '../../../../utils/sendAndWait/interfaces';
import {
configureWaitTillDate,
createEmail,
getSendAndWaitProperties,
sendAndWaitWebhook,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as folderMessage from './folderMessage';
import * as message from './message';
import * as messageAttachment from './messageAttachment';
import type { MicrosoftOutlook } from './node.type';
import { configureWaitTillDate } from '../../../../../utils/sendAndWait/utils';
import { configureWaitTillDate } from '../../../../../utils/sendAndWait/configureWaitTillDate.util';

export async function router(this: IExecuteFunctions) {
const items = this.getInputData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as channelMessage from './channelMessage';
import * as chatMessage from './chatMessage';
import type { MicrosoftTeamsType } from './node.type';
import * as task from './task';
import { configureWaitTillDate } from '../../../../../utils/sendAndWait/utils';
import { configureWaitTillDate } from '../../../../../utils/sendAndWait/configureWaitTillDate.util';

export async function router(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
Expand Down
7 changes: 2 additions & 5 deletions packages/nodes-base/nodes/Slack/V2/SlackV2.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,9 @@ import { reactionFields, reactionOperations } from './ReactionDescription';
import { starFields, starOperations } from './StarDescription';
import { userFields, userOperations } from './UserDescription';
import { userGroupFields, userGroupOperations } from './UserGroupDescription';
import { configureWaitTillDate } from '../../../utils/sendAndWait/configureWaitTillDate.util';
import { sendAndWaitWebhooksDescription } from '../../../utils/sendAndWait/descriptions';
import {
configureWaitTillDate,
getSendAndWaitProperties,
sendAndWaitWebhook,
} from '../../../utils/sendAndWait/utils';
import { getSendAndWaitProperties, sendAndWaitWebhook } from '../../../utils/sendAndWait/utils';

export class SlackV2 implements INodeType {
description: INodeTypeDescription;
Expand Down
7 changes: 2 additions & 5 deletions packages/nodes-base/nodes/Telegram/Telegram.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@ import {
getPropertyName,
} from './GenericFunctions';
import { appendAttributionOption } from '../../utils/descriptions';
import { configureWaitTillDate } from '../../utils/sendAndWait/configureWaitTillDate.util';
import { sendAndWaitWebhooksDescription } from '../../utils/sendAndWait/descriptions';
import {
configureWaitTillDate,
getSendAndWaitProperties,
sendAndWaitWebhook,
} from '../../utils/sendAndWait/utils';
import { getSendAndWaitProperties, sendAndWaitWebhook } from '../../utils/sendAndWait/utils';

export class Telegram implements INodeType {
description: INodeTypeDescription = {
Expand Down
2 changes: 1 addition & 1 deletion packages/nodes-base/nodes/Wait/Wait.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const waitTimeProperties: INodeProperties[] = [
type: 'boolean',
default: false,
description:
'Whether the workflow will automatically resume execution after the specified limit type',
'Whether to limit the time this node should wait for a user response before execution resumes',
displayOptions: {
show: {
resume: ['webhook', 'form'],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { ApplicationError, NodeOperationError, WAIT_INDEFINITELY } from 'n8n-workflow';
import type { IExecuteFunctions, IDataObject } from 'n8n-workflow';

export function configureWaitTillDate(
context: IExecuteFunctions,
location: 'options' | 'root' = 'options',
) {
let waitTill = WAIT_INDEFINITELY;
let limitOptions: IDataObject = {};

if (location === 'options') {
limitOptions = context.getNodeParameter('options.limitWaitTime.values', 0, {}) as {
limitType?: string;
resumeAmount?: number;
resumeUnit?: string;
maxDateAndTime?: string;
};
} else {
const limitWaitTime = context.getNodeParameter('limitWaitTime', 0, false);
if (limitWaitTime) {
limitOptions.limitType = context.getNodeParameter('limitType', 0, 'afterTimeInterval');

if (limitOptions.limitType === 'afterTimeInterval') {
limitOptions.resumeAmount = context.getNodeParameter('resumeAmount', 0, 1) as number;
limitOptions.resumeUnit = context.getNodeParameter('resumeUnit', 0, 'hours');
} else {
limitOptions.maxDateAndTime = context.getNodeParameter('maxDateAndTime', 0, '');
}
}
}

if (Object.keys(limitOptions).length) {
try {
if (limitOptions.limitType === 'afterTimeInterval') {
let waitAmount = limitOptions.resumeAmount as number;

if (limitOptions.resumeUnit === 'minutes') {
waitAmount *= 60;
}
if (limitOptions.resumeUnit === 'hours') {
waitAmount *= 60 * 60;
}
if (limitOptions.resumeUnit === 'days') {
waitAmount *= 60 * 60 * 24;
}

waitAmount *= 1000;
waitTill = new Date(new Date().getTime() + waitAmount);
} else {
waitTill = new Date(limitOptions.maxDateAndTime as string);
}

if (isNaN(waitTill.getTime())) {
throw new ApplicationError('Invalid date format');
}
} catch (error) {
throw new NodeOperationError(context.getNode(), 'Could not configure Limit Wait Time', {
description: error.message,
});
}
}

return waitTill;
}
79 changes: 78 additions & 1 deletion packages/nodes-base/utils/sendAndWait/descriptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IWebhookDescription } from 'n8n-workflow';
import type { INodeProperties, IWebhookDescription } from 'n8n-workflow';

export const sendAndWaitWebhooksDescription: IWebhookDescription[] = [
{
Expand All @@ -20,3 +20,80 @@ export const sendAndWaitWebhooksDescription: IWebhookDescription[] = [
isFullPath: true,
},
];

export const limitWaitTimeProperties: INodeProperties[] = [
{
displayName: 'Limit Type',
name: 'limitType',
type: 'options',
default: 'afterTimeInterval',
description:
'Sets the condition for the execution to resume. Can be a specified date or after some time.',
options: [
{
name: 'After Time Interval',
description: 'Waits for a certain amount of time',
value: 'afterTimeInterval',
},
{
name: 'At Specified Time',
description: 'Waits until the set date and time to continue',
value: 'atSpecifiedTime',
},
],
},
{
displayName: 'Amount',
name: 'resumeAmount',
type: 'number',
displayOptions: {
show: {
limitType: ['afterTimeInterval'],
},
},
typeOptions: {
minValue: 0,
numberPrecision: 2,
},
default: 1,
description: 'The time to wait',
},
{
displayName: 'Unit',
name: 'resumeUnit',
type: 'options',
displayOptions: {
show: {
limitType: ['afterTimeInterval'],
},
},
options: [
{
name: 'Minutes',
value: 'minutes',
},
{
name: 'Hours',
value: 'hours',
},
{
name: 'Days',
value: 'days',
},
],
default: 'hours',
description: 'Unit of the interval value',
},
{
displayName: 'Max Date and Time',
name: 'maxDateAndTime',
type: 'dateTime',
displayOptions: {
show: {
limitType: ['atSpecifiedTime'],
},
},
default: '',
description: 'Continue execution after the specified date and time',
},
];
Loading

0 comments on commit 14b6f8b

Please sign in to comment.