feat(n8n Form Node): Limit wait time parameters (#13160)

This commit is contained in:
Michael Kret 2025-02-19 13:47:19 +02:00 committed by GitHub
parent 301f5a50d9
commit 14b6f8b972
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 261 additions and 149 deletions

View file

@ -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';

View file

@ -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,

View file

@ -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',
@ -71,6 +91,7 @@ const pageProperties = updateDisplayOptions(
},
[
...formFieldsProperties,
...waitTimeProperties,
{
displayName: 'Options',
name: 'options',
@ -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',
@ -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()];
}

View file

@ -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>;

View file

@ -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 = {

View file

@ -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,

View file

@ -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();

View file

@ -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();

View file

@ -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;

View file

@ -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 = {

View file

@ -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'],

View file

@ -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;
}

View file

@ -1,4 +1,4 @@
import type { IWebhookDescription } from 'n8n-workflow';
import type { INodeProperties, IWebhookDescription } from 'n8n-workflow';
export const sendAndWaitWebhooksDescription: IWebhookDescription[] = [
{
@ -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',
},
];

View file

@ -2,12 +2,12 @@ import { type MockProxy, mock } from 'jest-mock-extended';
import type { IExecuteFunctions, INodeProperties, IWebhookFunctions } from 'n8n-workflow';
import { NodeOperationError, WAIT_INDEFINITELY } from 'n8n-workflow';
import { configureWaitTillDate } from '../configureWaitTillDate.util';
import {
getSendAndWaitProperties,
getSendAndWaitConfig,
createEmail,
sendAndWaitWebhook,
configureWaitTillDate,
} from '../utils';
describe('Send and Wait utils tests', () => {
@ -466,4 +466,76 @@ describe('configureWaitTillDate', () => {
'Could not configure Limit Wait Time',
);
});
it('should return WAIT_INDEFINITELY when limitWaitTime is false', () => {
mockExecuteFunctions.getNodeParameter.mockReturnValueOnce(false);
const result = configureWaitTillDate(mockExecuteFunctions, 'root');
expect(result).toBe(WAIT_INDEFINITELY);
});
it('should calculate minutes correctly in root location', () => {
mockExecuteFunctions.getNodeParameter
.mockReturnValueOnce(true) // limitWaitTime
.mockReturnValueOnce('afterTimeInterval') // limitType
.mockReturnValueOnce(15) // resumeAmount
.mockReturnValueOnce('minutes'); // resumeUnit
const result = configureWaitTillDate(mockExecuteFunctions, 'root');
const expectedDate = new Date(new Date().getTime() + 15 * 60 * 1000);
expect(result.getTime()).toBeCloseTo(expectedDate.getTime(), -2);
});
it('should calculate hours correctly in root location', () => {
mockExecuteFunctions.getNodeParameter
.mockReturnValueOnce(true)
.mockReturnValueOnce('afterTimeInterval')
.mockReturnValueOnce(3)
.mockReturnValueOnce('hours');
const result = configureWaitTillDate(mockExecuteFunctions, 'root');
const expectedDate = new Date(new Date().getTime() + 3 * 60 * 60 * 1000);
expect(result.getTime()).toBeCloseTo(expectedDate.getTime(), -2);
});
it('should calculate days correctly in root location', () => {
mockExecuteFunctions.getNodeParameter
.mockReturnValueOnce(true)
.mockReturnValueOnce('afterTimeInterval')
.mockReturnValueOnce(5)
.mockReturnValueOnce('days');
const result = configureWaitTillDate(mockExecuteFunctions, 'root');
const expectedDate = new Date(new Date().getTime() + 5 * 24 * 60 * 60 * 1000);
expect(result.getTime()).toBeCloseTo(expectedDate.getTime(), -2);
});
it('should handle maxDateAndTime in root location', () => {
const maxDateAndTime = '2024-12-31T23:59:59Z';
mockExecuteFunctions.getNodeParameter
.mockReturnValueOnce(true)
.mockReturnValueOnce('maxDateAndTime')
.mockReturnValueOnce(maxDateAndTime);
const result = configureWaitTillDate(mockExecuteFunctions, 'root');
expect(result).toEqual(new Date(maxDateAndTime));
});
it('should throw error for invalid date in root location', () => {
mockExecuteFunctions.getNodeParameter
.mockReturnValueOnce(true)
.mockReturnValueOnce('maxDateAndTime')
.mockReturnValueOnce('not-a-valid-date');
expect(() => configureWaitTillDate(mockExecuteFunctions, 'root')).toThrow(NodeOperationError);
});
it('should throw error for invalid resumeAmount in root location', () => {
mockExecuteFunctions.getNodeParameter
.mockReturnValueOnce(true)
.mockReturnValueOnce('afterTimeInterval')
.mockReturnValueOnce('not-a-number')
.mockReturnValueOnce('minutes');
expect(() => configureWaitTillDate(mockExecuteFunctions, 'root')).toThrow(NodeOperationError);
});
});

View file

@ -1,10 +1,8 @@
import {
ApplicationError,
NodeOperationError,
SEND_AND_WAIT_OPERATION,
tryToParseJsonToFormFields,
updateDisplayOptions,
WAIT_INDEFINITELY,
} from 'n8n-workflow';
import type {
INodeProperties,
@ -14,6 +12,7 @@ import type {
FormFieldsParameter,
} from 'n8n-workflow';
import { limitWaitTimeProperties } from './descriptions';
import {
ACTION_RECORDED_PAGE,
BUTTON_STYLE_PRIMARY,
@ -41,7 +40,7 @@ type FormResponseTypeOptions = {
const INPUT_FIELD_IDENTIFIER = 'field-0';
const limitWaitTimeProperties: INodeProperties = {
const limitWaitTimeOption: INodeProperties = {
displayName: 'Limit Wait Time',
name: 'limitWaitTime',
type: 'fixedCollection',
@ -52,82 +51,7 @@ const limitWaitTimeProperties: INodeProperties = {
{
displayName: 'Values',
name: 'values',
values: [
{
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',
},
],
values: limitWaitTimeProperties,
},
],
};
@ -307,7 +231,7 @@ export function getSendAndWaitProperties(
type: 'collection',
placeholder: 'Add option',
default: {},
options: [limitWaitTimeProperties],
options: [limitWaitTimeOption],
displayOptions: {
show: {
responseType: ['approval'],
@ -347,7 +271,7 @@ export function getSendAndWaitProperties(
type: 'string',
default: 'Submit',
},
limitWaitTimeProperties,
limitWaitTimeOption,
],
displayOptions: {
show: {
@ -605,46 +529,3 @@ export function createEmail(context: IExecuteFunctions) {
return email;
}
export function configureWaitTillDate(context: IExecuteFunctions) {
let waitTill = WAIT_INDEFINITELY;
const limitWaitTime = context.getNodeParameter('options.limitWaitTime.values', 0, {}) as {
limitType?: string;
resumeAmount?: number;
resumeUnit?: string;
maxDateAndTime?: string;
};
if (Object.keys(limitWaitTime).length) {
try {
if (limitWaitTime.limitType === 'afterTimeInterval') {
let waitAmount = limitWaitTime.resumeAmount as number;
if (limitWaitTime.resumeUnit === 'minutes') {
waitAmount *= 60;
}
if (limitWaitTime.resumeUnit === 'hours') {
waitAmount *= 60 * 60;
}
if (limitWaitTime.resumeUnit === 'days') {
waitAmount *= 60 * 60 * 24;
}
waitAmount *= 1000;
waitTill = new Date(new Date().getTime() + waitAmount);
} else {
waitTill = new Date(limitWaitTime.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;
}