2023-12-05 02:17:08 -08:00
|
|
|
import { ApplicationError, type IDataObject, type NodeApiError } from 'n8n-workflow';
|
2024-03-07 08:08:01 -08:00
|
|
|
import set from 'lodash/set';
|
2024-03-26 06:22:57 -07:00
|
|
|
import type { UpdateRecord } from './interfaces';
|
2023-07-17 09:42:30 -07:00
|
|
|
|
|
|
|
export function removeIgnored(data: IDataObject, ignore: string | string[]) {
|
|
|
|
if (ignore) {
|
|
|
|
let ignoreFields: string[] = [];
|
|
|
|
|
|
|
|
if (typeof ignore === 'string') {
|
|
|
|
ignoreFields = ignore.split(',').map((field) => field.trim());
|
|
|
|
} else {
|
|
|
|
ignoreFields = ignore;
|
|
|
|
}
|
|
|
|
|
|
|
|
const newData: IDataObject = {};
|
|
|
|
|
|
|
|
for (const field of Object.keys(data)) {
|
|
|
|
if (!ignoreFields.includes(field)) {
|
|
|
|
newData[field] = data[field];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newData;
|
|
|
|
} else {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function findMatches(
|
|
|
|
data: UpdateRecord[],
|
|
|
|
keys: string[],
|
|
|
|
fields: IDataObject,
|
|
|
|
updateAll?: boolean,
|
|
|
|
) {
|
|
|
|
if (updateAll) {
|
|
|
|
const matches = data.filter((record) => {
|
|
|
|
for (const key of keys) {
|
|
|
|
if (record.fields[key] !== fields[key]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!matches?.length) {
|
2023-12-05 02:17:08 -08:00
|
|
|
throw new ApplicationError('No records match provided keys', { level: 'warning' });
|
2023-07-17 09:42:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return matches;
|
|
|
|
} else {
|
|
|
|
const match = data.find((record) => {
|
|
|
|
for (const key of keys) {
|
|
|
|
if (record.fields[key] !== fields[key]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!match) {
|
2023-12-05 02:17:08 -08:00
|
|
|
throw new ApplicationError('Record matching provided keys was not found', {
|
|
|
|
level: 'warning',
|
|
|
|
});
|
2023-07-17 09:42:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return [match];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-07 08:08:01 -08:00
|
|
|
export function processAirtableError(error: NodeApiError, id?: string, itemIndex?: number) {
|
2023-07-17 09:42:30 -07:00
|
|
|
if (error.description === 'NOT_FOUND' && id) {
|
|
|
|
error.description = `${id} is not a valid Record ID`;
|
|
|
|
}
|
|
|
|
if (error.description?.includes('You must provide an array of up to 10 record objects') && id) {
|
|
|
|
error.description = `${id} is not a valid Record ID`;
|
|
|
|
}
|
2024-03-07 08:08:01 -08:00
|
|
|
|
|
|
|
if (itemIndex !== undefined) {
|
|
|
|
set(error, 'context.itemIndex', itemIndex);
|
|
|
|
}
|
|
|
|
|
2023-07-17 09:42:30 -07:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const flattenOutput = (record: IDataObject) => {
|
|
|
|
const { fields, ...rest } = record;
|
|
|
|
return {
|
|
|
|
...rest,
|
|
|
|
...(fields as IDataObject),
|
|
|
|
};
|
|
|
|
};
|