From d7abc3010463ad21a9c162430485ebbb29d378b1 Mon Sep 17 00:00:00 2001 From: Michael Kret <88898367+michael-radency@users.noreply.github.com> Date: Thu, 11 Apr 2024 12:23:44 +0300 Subject: [PATCH] feat(Summarize Node): Option to continue when field to summarize can't be found in any items (#9118) --- .../Transform/Summarize/Summarize.node.ts | 21 ++++++++++++++++++- .../nodes/Transform/Summarize/utils.ts | 1 + 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/nodes-base/nodes/Transform/Summarize/Summarize.node.ts b/packages/nodes-base/nodes/Transform/Summarize/Summarize.node.ts index 1eadc629bb757..60e2a4599d5a8 100644 --- a/packages/nodes-base/nodes/Transform/Summarize/Summarize.node.ts +++ b/packages/nodes-base/nodes/Transform/Summarize/Summarize.node.ts @@ -14,6 +14,7 @@ import { fieldValueGetter, splitData, } from './utils'; +import { generatePairedItemData } from '../../../utils/utilities'; export class Summarize implements INodeType { description: INodeTypeDescription = { @@ -239,6 +240,14 @@ export class Summarize implements INodeType { placeholder: 'Add Option', default: {}, options: [ + { + displayName: 'Continue if Field Not Found', + name: 'continueIfFieldNotFound', + type: 'boolean', + default: false, + description: + "Whether to continue if field to summarize can't be found in any items and return single empty item, owerwise an error would be thrown", + }, { displayName: 'Disable Dot Notation', name: 'disableDotNotation', @@ -304,7 +313,17 @@ export class Summarize implements INodeType { const nodeVersion = this.getNode().typeVersion; if (nodeVersion < 2.1) { - checkIfFieldExists.call(this, newItems, fieldsToSummarize, getValue); + try { + checkIfFieldExists.call(this, newItems, fieldsToSummarize, getValue); + } catch (error) { + if (options.continueIfFieldNotFound) { + const itemData = generatePairedItemData(items.length); + + return [[{ json: {}, pairedItem: itemData }]]; + } else { + throw error; + } + } } const aggregationResult = splitData( diff --git a/packages/nodes-base/nodes/Transform/Summarize/utils.ts b/packages/nodes-base/nodes/Transform/Summarize/utils.ts index 89fdb166008c0..0094d4d8efc9a 100644 --- a/packages/nodes-base/nodes/Transform/Summarize/utils.ts +++ b/packages/nodes-base/nodes/Transform/Summarize/utils.ts @@ -40,6 +40,7 @@ const AggregationDisplayNames = { export const NUMERICAL_AGGREGATIONS = ['average', 'sum']; export type SummarizeOptions = { + continueIfFieldNotFound: boolean; disableDotNotation?: boolean; outputFormat?: 'separateItems' | 'singleItem'; skipEmptySplitFields?: boolean;