adds versioning and how to handle error based on version

This commit is contained in:
Ria 2024-11-25 11:16:14 +05:30
parent 3a1ea55d80
commit ccc3f50585
2 changed files with 24 additions and 30 deletions

View file

@ -18,7 +18,6 @@ import {
fieldValueGetter, fieldValueGetter,
splitData, splitData,
} from './utils'; } from './utils';
// import type { IPairedItemData } from '../../../../workflow/src/Interfaces'; // [ria] this is an interface but cannot import it as such!
export class Summarize implements INodeType { export class Summarize implements INodeType {
description: INodeTypeDescription = { description: INodeTypeDescription = {
@ -27,7 +26,7 @@ export class Summarize implements INodeType {
icon: 'file:summarize.svg', icon: 'file:summarize.svg',
group: ['transform'], group: ['transform'],
subtitle: '', subtitle: '',
version: 1, version: [1, 1.1],
description: 'Sum, count, max, etc. across items', description: 'Sum, count, max, etc. across items',
defaults: { defaults: {
name: 'Summarize', name: 'Summarize',
@ -244,7 +243,6 @@ export class Summarize implements INodeType {
placeholder: 'Add option', placeholder: 'Add option',
default: {}, default: {},
options: [ options: [
// [ria] potentially delete this option??
{ {
displayName: 'Continue if Field Not Found', displayName: 'Continue if Field Not Found',
name: 'continueIfFieldNotFound', name: 'continueIfFieldNotFound',
@ -317,23 +315,18 @@ export class Summarize implements INodeType {
const nodeVersion = this.getNode().typeVersion; const nodeVersion = this.getNode().typeVersion;
if (nodeVersion < 2.1) { try {
const fieldNotFound: string | undefined = checkIfFieldExists.call( checkIfFieldExists.call(this, newItems, fieldsToSummarize, getValue);
this, } catch (error) {
newItems, if (nodeVersion > 1 || options.continueIfFieldNotFound) {
fieldsToSummarize,
getValue,
);
if (options.continueIfFieldNotFound || fieldNotFound) {
// const itemData: IPairedItemData[] = generatePairedItemData(items.length); // [ria] had to delete type because i was getting compilation errors
const itemData = generatePairedItemData(items.length); const itemData = generatePairedItemData(items.length);
const fieldNotFoundHint: NodeExecutionHint = { const fieldNotFoundHint: NodeExecutionHint = {
message: `The field '${fieldNotFound}' does not exist in any items.`, message: error.message,
location: 'outputPane', location: 'outputPane',
}; };
return new NodeExecutionOutput([[{ json: {}, pairedItem: itemData }]], [fieldNotFoundHint]); return new NodeExecutionOutput([[{ json: {}, pairedItem: itemData }]], [fieldNotFoundHint]);
} else { } else {
// throw error; // [ria] show hints instead throw error;
} }
} }

View file

@ -1,5 +1,10 @@
import get from 'lodash/get'; import get from 'lodash/get';
import { type IDataObject, type GenericValue, type IExecuteFunctions } from 'n8n-workflow'; import {
type IDataObject,
type GenericValue,
type IExecuteFunctions,
NodeOperationError,
} from 'n8n-workflow';
type AggregationType = type AggregationType =
| 'append' | 'append'
@ -89,7 +94,6 @@ export const fieldValueGetter = (disableDotNotation?: boolean) => {
}; };
export function checkIfFieldExists( export function checkIfFieldExists(
// add tests that check that no error is thrown and instead warning is issued
this: IExecuteFunctions, this: IExecuteFunctions,
items: IDataObject[], items: IDataObject[],
aggregations: Aggregations, aggregations: Aggregations,
@ -101,13 +105,10 @@ export function checkIfFieldExists(
} }
const exist = items.some((item) => getValue(item, aggregation.field) !== undefined); const exist = items.some((item) => getValue(item, aggregation.field) !== undefined);
if (!exist) { if (!exist) {
return aggregation.field; throw new NodeOperationError(
// throw new NodeOperationError( this.getNode(),
// turn this into warning instead of error will return early `The field '${aggregation.field}' does not exist in any items`,
// return aggregation.field -> field name can be used in the hint description! but if it returns... does it end the iteration!? );
// this.getNode(),
// `The field '${aggregation.field}' does not exist in any items`,
// );
} }
} }
} }
@ -230,20 +231,20 @@ export function splitData(
const [firstSplitKey, ...restSplitKeys] = splitKeys; const [firstSplitKey, ...restSplitKeys] = splitKeys;
const groupedData = data.reduce((acc, item) => { const groupedData = data.reduce((acc, item) => {
let keyValuee = getValue(item, firstSplitKey) as string; let keyValue = getValue(item, firstSplitKey) as string;
if (typeof keyValuee === 'object') { if (typeof keyValue === 'object') {
keyValuee = JSON.stringify(keyValuee); keyValue = JSON.stringify(keyValue);
} }
if (options.skipEmptySplitFields && typeof keyValuee !== 'number' && !keyValuee) { if (options.skipEmptySplitFields && typeof keyValue !== 'number' && !keyValue) {
return acc; return acc;
} }
if (acc[keyValuee] === undefined) { if (acc[keyValue] === undefined) {
acc[keyValuee] = [item]; acc[keyValue] = [item];
} else { } else {
(acc[keyValuee] as IDataObject[]).push(item); (acc[keyValue] as IDataObject[]).push(item);
} }
return acc; return acc;
}, {} as IDataObject); }, {} as IDataObject);