2022-04-08 14:32:08 -07:00
|
|
|
import moment from 'moment';
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { IExecuteFunctions } from 'n8n-core';
|
|
|
|
import type {
|
2019-06-23 03:35:23 -07:00
|
|
|
INodeExecutionData,
|
|
|
|
INodeParameters,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
NodeParameterValue,
|
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeOperationError } from 'n8n-workflow';
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
export class If implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'IF',
|
|
|
|
name: 'if',
|
2019-07-26 02:27:46 -07:00
|
|
|
icon: 'fa:map-signs',
|
2019-06-23 03:35:23 -07:00
|
|
|
group: ['transform'],
|
|
|
|
version: 1,
|
2021-07-03 05:40:16 -07:00
|
|
|
description: 'Splits a stream based on comparisons',
|
2019-06-23 03:35:23 -07:00
|
|
|
defaults: {
|
|
|
|
name: 'IF',
|
|
|
|
color: '#408000',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
2022-04-22 09:29:51 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-outputs-wrong
|
2019-06-23 03:35:23 -07:00
|
|
|
outputs: ['main', 'main'],
|
|
|
|
outputNames: ['true', 'false'],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Conditions',
|
|
|
|
name: 'conditions',
|
|
|
|
placeholder: 'Add Condition',
|
|
|
|
type: 'fixedCollection',
|
|
|
|
typeOptions: {
|
|
|
|
multipleValues: true,
|
2021-02-15 00:53:19 -08:00
|
|
|
sortable: true,
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'The type of values to compare',
|
2019-06-23 03:35:23 -07:00
|
|
|
default: {},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'boolean',
|
|
|
|
displayName: 'Boolean',
|
|
|
|
values: [
|
|
|
|
{
|
|
|
|
displayName: 'Value 1',
|
|
|
|
name: 'value1',
|
|
|
|
type: 'boolean',
|
|
|
|
default: false,
|
2022-06-20 07:54:01 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-param-description-boolean-without-whether
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'The value to compare with the second one',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
2022-05-20 14:47:24 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-param-operation-without-no-data-expression
|
2019-06-23 03:35:23 -07:00
|
|
|
{
|
|
|
|
displayName: 'Operation',
|
|
|
|
name: 'operation',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Equal',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: 'equal',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Not Equal',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: 'notEqual',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'equal',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Operation to decide where the the data should be mapped to',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Value 2',
|
|
|
|
name: 'value2',
|
|
|
|
type: 'boolean',
|
|
|
|
default: false,
|
2022-06-20 07:54:01 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-param-description-boolean-without-whether
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'The value to compare with the first one',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2021-02-27 03:54:45 -08:00
|
|
|
{
|
|
|
|
name: 'dateTime',
|
2021-03-02 15:17:51 -08:00
|
|
|
displayName: 'Date & Time',
|
2021-02-27 03:54:45 -08:00
|
|
|
values: [
|
|
|
|
{
|
|
|
|
displayName: 'Value 1',
|
|
|
|
name: 'value1',
|
|
|
|
type: 'dateTime',
|
|
|
|
default: '',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'The value to compare with the second one',
|
2021-02-27 03:54:45 -08:00
|
|
|
},
|
2022-05-20 14:47:24 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-param-operation-without-no-data-expression
|
2021-02-27 03:54:45 -08:00
|
|
|
{
|
|
|
|
displayName: 'Operation',
|
|
|
|
name: 'operation',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
2022-06-03 10:23:49 -07:00
|
|
|
name: 'Occurred After',
|
2021-02-27 03:54:45 -08:00
|
|
|
value: 'after',
|
|
|
|
},
|
|
|
|
{
|
2022-06-03 10:23:49 -07:00
|
|
|
name: 'Occurred Before',
|
2021-02-27 03:54:45 -08:00
|
|
|
value: 'before',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'after',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Operation to decide where the the data should be mapped to',
|
2021-02-27 03:54:45 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Value 2',
|
|
|
|
name: 'value2',
|
|
|
|
type: 'dateTime',
|
|
|
|
default: '',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'The value to compare with the first one',
|
2021-02-27 03:54:45 -08:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2019-06-23 03:35:23 -07:00
|
|
|
{
|
|
|
|
name: 'number',
|
|
|
|
displayName: 'Number',
|
|
|
|
values: [
|
|
|
|
{
|
|
|
|
displayName: 'Value 1',
|
|
|
|
name: 'value1',
|
|
|
|
type: 'number',
|
|
|
|
default: 0,
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'The value to compare with the second one',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Operation',
|
|
|
|
name: 'operation',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2022-06-03 10:23:49 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-param-options-type-unsorted-items
|
2019-06-23 03:35:23 -07:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Smaller',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: 'smaller',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
2019-12-23 11:22:15 -08:00
|
|
|
{
|
2022-03-31 09:25:11 -07:00
|
|
|
name: 'Smaller or Equal',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: 'smallerEqual',
|
2019-12-23 11:22:15 -08:00
|
|
|
},
|
2019-06-23 03:35:23 -07:00
|
|
|
{
|
|
|
|
name: 'Equal',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: 'equal',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Not Equal',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: 'notEqual',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Larger',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: 'larger',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
2019-12-23 11:22:15 -08:00
|
|
|
{
|
2022-03-31 09:25:11 -07:00
|
|
|
name: 'Larger or Equal',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: 'largerEqual',
|
2019-12-23 11:22:15 -08:00
|
|
|
},
|
2020-12-11 09:15:07 -08:00
|
|
|
{
|
|
|
|
name: 'Is Empty',
|
|
|
|
value: 'isEmpty',
|
|
|
|
},
|
2022-03-12 09:52:15 -08:00
|
|
|
{
|
|
|
|
name: 'Is Not Empty',
|
|
|
|
value: 'isNotEmpty',
|
|
|
|
},
|
2019-06-23 03:35:23 -07:00
|
|
|
],
|
|
|
|
default: 'smaller',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Operation to decide where the the data should be mapped to',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Value 2',
|
|
|
|
name: 'value2',
|
|
|
|
type: 'number',
|
2020-12-11 09:15:07 -08:00
|
|
|
displayOptions: {
|
|
|
|
hide: {
|
2022-08-17 08:50:24 -07:00
|
|
|
operation: ['isEmpty', 'isNotEmpty'],
|
2020-12-11 09:15:07 -08:00
|
|
|
},
|
|
|
|
},
|
2019-06-23 03:35:23 -07:00
|
|
|
default: 0,
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'The value to compare with the first one',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'string',
|
|
|
|
displayName: 'String',
|
|
|
|
values: [
|
|
|
|
{
|
|
|
|
displayName: 'Value 1',
|
|
|
|
name: 'value1',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'The value to compare with the second one',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Operation',
|
|
|
|
name: 'operation',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2022-06-03 10:23:49 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-param-options-type-unsorted-items
|
2019-06-23 03:35:23 -07:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Contains',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: 'contains',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
2022-03-12 09:52:15 -08:00
|
|
|
{
|
|
|
|
name: 'Not Contains',
|
|
|
|
value: 'notContains',
|
|
|
|
},
|
2020-12-27 11:33:46 -08:00
|
|
|
{
|
|
|
|
name: 'Ends With',
|
|
|
|
value: 'endsWith',
|
|
|
|
},
|
2019-06-23 03:35:23 -07:00
|
|
|
{
|
2022-03-12 09:52:15 -08:00
|
|
|
name: 'Not Ends With',
|
|
|
|
value: 'notEndsWith',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
{
|
2022-03-12 09:52:15 -08:00
|
|
|
name: 'Equal',
|
|
|
|
value: 'equal',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Not Equal',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: 'notEqual',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
{
|
2022-03-12 09:52:15 -08:00
|
|
|
name: 'Regex Match',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: 'regex',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
2022-03-12 09:52:15 -08:00
|
|
|
{
|
|
|
|
name: 'Regex Not Match',
|
|
|
|
value: 'notRegex',
|
|
|
|
},
|
2020-12-27 11:33:46 -08:00
|
|
|
{
|
|
|
|
name: 'Starts With',
|
|
|
|
value: 'startsWith',
|
|
|
|
},
|
2022-03-12 09:52:15 -08:00
|
|
|
{
|
|
|
|
name: 'Not Starts With',
|
|
|
|
value: 'notStartsWith',
|
|
|
|
},
|
2020-12-11 09:15:07 -08:00
|
|
|
{
|
|
|
|
name: 'Is Empty',
|
|
|
|
value: 'isEmpty',
|
|
|
|
},
|
2022-03-12 09:52:15 -08:00
|
|
|
{
|
|
|
|
name: 'Is Not Empty',
|
|
|
|
value: 'isNotEmpty',
|
|
|
|
},
|
2019-06-23 03:35:23 -07:00
|
|
|
],
|
|
|
|
default: 'equal',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Operation to decide where the the data should be mapped to',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Value 2',
|
|
|
|
name: 'value2',
|
|
|
|
type: 'string',
|
|
|
|
displayOptions: {
|
|
|
|
hide: {
|
2022-08-17 08:50:24 -07:00
|
|
|
operation: ['isEmpty', 'isNotEmpty', 'regex', 'notRegex'],
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
default: '',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'The value to compare with the first one',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Regex',
|
|
|
|
name: 'value2',
|
|
|
|
type: 'string',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
operation: ['regex', 'notRegex'],
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
default: '',
|
|
|
|
placeholder: '/text/i',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'The regex which has to match',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Combine',
|
|
|
|
name: 'combineOperation',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'ALL',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Only if all conditions are meet it goes into "true" branch',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: 'all',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'ANY',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'If any of the conditions is meet it goes into "true" branch',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: 'any',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'all',
|
2022-08-17 08:50:24 -07:00
|
|
|
description:
|
|
|
|
'If multiple rules got set this settings decides if it is true as soon as ANY condition matches or only if ALL get meet',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const returnDataTrue: INodeExecutionData[] = [];
|
|
|
|
const returnDataFalse: INodeExecutionData[] = [];
|
|
|
|
|
|
|
|
const items = this.getInputData();
|
|
|
|
|
|
|
|
let item: INodeExecutionData;
|
|
|
|
let combineOperation: string;
|
|
|
|
|
2022-11-22 04:43:28 -08:00
|
|
|
const isDateObject = (value: NodeParameterValue) =>
|
|
|
|
Object.prototype.toString.call(value) === '[object Date]';
|
2022-11-21 09:26:59 -08:00
|
|
|
const isDateInvalid = (value: NodeParameterValue) => value?.toString() === 'Invalid Date';
|
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
// The compare operations
|
|
|
|
const compareOperationFunctions: {
|
|
|
|
[key: string]: (value1: NodeParameterValue, value2: NodeParameterValue) => boolean;
|
|
|
|
} = {
|
2022-08-17 08:50:24 -07:00
|
|
|
after: (value1: NodeParameterValue, value2: NodeParameterValue) =>
|
2023-01-19 04:37:19 -08:00
|
|
|
(value1 || 0) > (value2 || 0),
|
2022-08-17 08:50:24 -07:00
|
|
|
before: (value1: NodeParameterValue, value2: NodeParameterValue) =>
|
2023-01-19 04:37:19 -08:00
|
|
|
(value1 || 0) < (value2 || 0),
|
2022-08-17 08:50:24 -07:00
|
|
|
contains: (value1: NodeParameterValue, value2: NodeParameterValue) =>
|
2023-01-19 04:37:19 -08:00
|
|
|
(value1 || '').toString().includes((value2 || '').toString()),
|
2022-08-17 08:50:24 -07:00
|
|
|
notContains: (value1: NodeParameterValue, value2: NodeParameterValue) =>
|
2023-01-19 04:37:19 -08:00
|
|
|
!(value1 || '').toString().includes((value2 || '').toString()),
|
2022-08-17 08:50:24 -07:00
|
|
|
endsWith: (value1: NodeParameterValue, value2: NodeParameterValue) =>
|
|
|
|
(value1 as string).endsWith(value2 as string),
|
|
|
|
notEndsWith: (value1: NodeParameterValue, value2: NodeParameterValue) =>
|
|
|
|
!(value1 as string).endsWith(value2 as string),
|
2019-06-23 03:35:23 -07:00
|
|
|
equal: (value1: NodeParameterValue, value2: NodeParameterValue) => value1 === value2,
|
|
|
|
notEqual: (value1: NodeParameterValue, value2: NodeParameterValue) => value1 !== value2,
|
2022-08-17 08:50:24 -07:00
|
|
|
larger: (value1: NodeParameterValue, value2: NodeParameterValue) =>
|
2023-01-19 04:37:19 -08:00
|
|
|
(value1 || 0) > (value2 || 0),
|
2022-08-17 08:50:24 -07:00
|
|
|
largerEqual: (value1: NodeParameterValue, value2: NodeParameterValue) =>
|
2023-01-19 04:37:19 -08:00
|
|
|
(value1 || 0) >= (value2 || 0),
|
2022-08-17 08:50:24 -07:00
|
|
|
smaller: (value1: NodeParameterValue, value2: NodeParameterValue) =>
|
2023-01-19 04:37:19 -08:00
|
|
|
(value1 || 0) < (value2 || 0),
|
2022-08-17 08:50:24 -07:00
|
|
|
smallerEqual: (value1: NodeParameterValue, value2: NodeParameterValue) =>
|
2023-01-19 04:37:19 -08:00
|
|
|
(value1 || 0) <= (value2 || 0),
|
2022-08-17 08:50:24 -07:00
|
|
|
startsWith: (value1: NodeParameterValue, value2: NodeParameterValue) =>
|
|
|
|
(value1 as string).startsWith(value2 as string),
|
|
|
|
notStartsWith: (value1: NodeParameterValue, value2: NodeParameterValue) =>
|
|
|
|
!(value1 as string).startsWith(value2 as string),
|
|
|
|
isEmpty: (value1: NodeParameterValue) =>
|
2022-09-30 08:19:33 -07:00
|
|
|
[undefined, null, '', NaN].includes(value1 as string) ||
|
2022-11-21 09:26:59 -08:00
|
|
|
(typeof value1 === 'object' && value1 !== null && !isDateObject(value1)
|
2022-08-17 08:50:24 -07:00
|
|
|
? Object.entries(value1 as string).length === 0
|
2022-11-21 09:26:59 -08:00
|
|
|
: false) ||
|
|
|
|
(isDateObject(value1) && isDateInvalid(value1)),
|
2022-08-17 08:50:24 -07:00
|
|
|
isNotEmpty: (value1: NodeParameterValue) =>
|
|
|
|
!(
|
2022-09-30 08:19:33 -07:00
|
|
|
[undefined, null, '', NaN].includes(value1 as string) ||
|
2022-11-21 09:26:59 -08:00
|
|
|
(typeof value1 === 'object' && value1 !== null && !isDateObject(value1)
|
2022-08-17 08:50:24 -07:00
|
|
|
? Object.entries(value1 as string).length === 0
|
2022-11-21 09:26:59 -08:00
|
|
|
: false) ||
|
|
|
|
(isDateObject(value1) && isDateInvalid(value1))
|
2022-08-17 08:50:24 -07:00
|
|
|
),
|
2019-06-23 03:35:23 -07:00
|
|
|
regex: (value1: NodeParameterValue, value2: NodeParameterValue) => {
|
2023-01-19 04:37:19 -08:00
|
|
|
const regexMatch = (value2 || '').toString().match(new RegExp('^/(.*?)/([gimusy]*)$'));
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
let regex: RegExp;
|
|
|
|
if (!regexMatch) {
|
2023-01-19 04:37:19 -08:00
|
|
|
regex = new RegExp((value2 || '').toString());
|
2019-06-23 03:35:23 -07:00
|
|
|
} else if (regexMatch.length === 1) {
|
|
|
|
regex = new RegExp(regexMatch[1]);
|
|
|
|
} else {
|
|
|
|
regex = new RegExp(regexMatch[1], regexMatch[2]);
|
|
|
|
}
|
|
|
|
|
2023-01-19 04:37:19 -08:00
|
|
|
return !!(value1 || '').toString().match(regex);
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
2022-03-12 09:52:15 -08:00
|
|
|
notRegex: (value1: NodeParameterValue, value2: NodeParameterValue) => {
|
2023-01-19 04:37:19 -08:00
|
|
|
const regexMatch = (value2 || '').toString().match(new RegExp('^/(.*?)/([gimusy]*)$'));
|
2022-03-12 09:52:15 -08:00
|
|
|
|
|
|
|
let regex: RegExp;
|
|
|
|
if (!regexMatch) {
|
2023-01-19 04:37:19 -08:00
|
|
|
regex = new RegExp((value2 || '').toString());
|
2022-03-12 09:52:15 -08:00
|
|
|
} else if (regexMatch.length === 1) {
|
|
|
|
regex = new RegExp(regexMatch[1]);
|
|
|
|
} else {
|
|
|
|
regex = new RegExp(regexMatch[1], regexMatch[2]);
|
|
|
|
}
|
|
|
|
|
2023-01-19 04:37:19 -08:00
|
|
|
return !(value1 || '').toString().match(regex);
|
2022-03-12 09:52:15 -08:00
|
|
|
},
|
2019-06-23 03:35:23 -07:00
|
|
|
};
|
|
|
|
|
2021-02-27 03:54:45 -08:00
|
|
|
// Converts the input data of a dateTime into a number for easy compare
|
2021-04-16 09:33:36 -07:00
|
|
|
const convertDateTime = (value: NodeParameterValue): number => {
|
2021-02-27 03:54:45 -08:00
|
|
|
let returnValue: number | undefined = undefined;
|
|
|
|
if (typeof value === 'string') {
|
|
|
|
returnValue = new Date(value).getTime();
|
|
|
|
} else if (typeof value === 'number') {
|
|
|
|
returnValue = value;
|
2022-08-17 08:50:24 -07:00
|
|
|
}
|
|
|
|
if (moment.isMoment(value)) {
|
2021-10-26 09:32:54 -07:00
|
|
|
returnValue = value.unix();
|
2022-08-17 08:50:24 -07:00
|
|
|
}
|
|
|
|
if ((value as unknown as object) instanceof Date) {
|
2021-02-27 03:54:45 -08:00
|
|
|
returnValue = (value as unknown as Date).getTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (returnValue === undefined || isNaN(returnValue)) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`The value "${value}" is not a valid DateTime.`,
|
|
|
|
);
|
2021-02-27 03:54:45 -08:00
|
|
|
}
|
|
|
|
|
2021-02-27 04:06:13 -08:00
|
|
|
return returnValue;
|
2021-04-16 09:33:36 -07:00
|
|
|
};
|
2021-02-27 03:54:45 -08:00
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
// The different dataTypes to check the values in
|
2022-08-17 08:50:24 -07:00
|
|
|
const dataTypes = ['boolean', 'dateTime', 'number', 'string'];
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2023-03-03 09:49:19 -08:00
|
|
|
// Iterate over all items to check which ones should be output as via output "true" and
|
2019-06-23 03:35:23 -07:00
|
|
|
// which ones via output "false"
|
|
|
|
let dataType: string;
|
|
|
|
let compareOperationResult: boolean;
|
2021-02-27 03:54:45 -08:00
|
|
|
let value1: NodeParameterValue, value2: NodeParameterValue;
|
2022-08-17 08:50:24 -07:00
|
|
|
itemLoop: for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
|
2019-06-23 03:35:23 -07:00
|
|
|
item = items[itemIndex];
|
|
|
|
|
|
|
|
let compareData: INodeParameters;
|
|
|
|
|
|
|
|
combineOperation = this.getNodeParameter('combineOperation', itemIndex) as string;
|
|
|
|
|
|
|
|
// Check all the values of the different dataTypes
|
|
|
|
for (dataType of dataTypes) {
|
|
|
|
// Check all the values of the current dataType
|
2022-08-17 08:50:24 -07:00
|
|
|
for (compareData of this.getNodeParameter(
|
|
|
|
`conditions.${dataType}`,
|
|
|
|
itemIndex,
|
|
|
|
[],
|
|
|
|
) as INodeParameters[]) {
|
2019-06-23 03:35:23 -07:00
|
|
|
// Check if the values passes
|
2021-02-27 03:54:45 -08:00
|
|
|
|
|
|
|
value1 = compareData.value1 as NodeParameterValue;
|
|
|
|
value2 = compareData.value2 as NodeParameterValue;
|
|
|
|
|
|
|
|
if (dataType === 'dateTime') {
|
|
|
|
value1 = convertDateTime(value1);
|
|
|
|
value2 = convertDateTime(value2);
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
compareOperationResult = compareOperationFunctions[compareData.operation as string](
|
|
|
|
value1,
|
|
|
|
value2,
|
|
|
|
);
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (compareOperationResult && combineOperation === 'any') {
|
2019-06-23 03:35:23 -07:00
|
|
|
// If it passes and the operation is "any" we do not have to check any
|
|
|
|
// other ones as it should pass anyway. So go on with the next item.
|
|
|
|
returnDataTrue.push(item);
|
|
|
|
continue itemLoop;
|
2022-12-02 12:54:28 -08:00
|
|
|
} else if (!compareOperationResult && combineOperation === 'all') {
|
2019-06-23 03:35:23 -07:00
|
|
|
// If it fails and the operation is "all" we do not have to check any
|
|
|
|
// other ones as it should be not pass anyway. So go on with the next item.
|
|
|
|
returnDataFalse.push(item);
|
|
|
|
continue itemLoop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (combineOperation === 'all') {
|
|
|
|
// If the operation is "all" it means the item did match all conditions
|
|
|
|
// so it passes.
|
|
|
|
returnDataTrue.push(item);
|
|
|
|
} else {
|
|
|
|
// If the operation is "any" it means the the item did not match any condition.
|
|
|
|
returnDataFalse.push(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [returnDataTrue, returnDataFalse];
|
|
|
|
}
|
|
|
|
}
|