mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-18 02:21:13 +00:00
feat(n8n Form Node): Limit wait time parameters (#13160)
This commit is contained in:
@@ -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;
|
||||
}
|
||||
@@ -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',
|
||||
},
|
||||
];
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user