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 9d03c7079a
commit d941a989ca
2 changed files with 24 additions and 30 deletions

View file

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

View file

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