From 6cb9aefb0b3e4d17382042371a20e63f23641581 Mon Sep 17 00:00:00 2001 From: Florian Bachmann Date: Wed, 20 Jul 2022 13:34:52 +0200 Subject: [PATCH] feat(Notion Node): Allow to ignore Notion URL properties if empty (#3564) * Allows to ignore Notion URL properties if empty * Fixes linting * Fixes another linting error that was not caught locally * Reorders options alphabetically --- .../nodes/Notion/DatabasePageDescription.ts | 26 +++++++++++++++++++ .../nodes/Notion/GenericFunctions.ts | 16 ++++++++---- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/packages/nodes-base/nodes/Notion/DatabasePageDescription.ts b/packages/nodes-base/nodes/Notion/DatabasePageDescription.ts index dc8a4bffa3..6848f1087e 100644 --- a/packages/nodes-base/nodes/Notion/DatabasePageDescription.ts +++ b/packages/nodes-base/nodes/Notion/DatabasePageDescription.ts @@ -319,6 +319,19 @@ export const databasePageFields = [ default: '', description: 'Email address', }, + { + displayName: 'Ignore If Empty', + name: 'ignoreIfEmpty', + type: 'boolean', + displayOptions: { + show: { + type: [ + 'url', + ], + }, + }, + default: false, + }, { displayName: 'URL', name: 'urlValue', @@ -732,6 +745,19 @@ export const databasePageFields = [ }, default: '', }, + { + displayName: 'Ignore If Empty', + name: 'ignoreIfEmpty', + type: 'boolean', + displayOptions: { + show: { + type: [ + 'url', + ], + }, + }, + default: false, + }, { displayName: 'URL', name: 'urlValue', diff --git a/packages/nodes-base/nodes/Notion/GenericFunctions.ts b/packages/nodes-base/nodes/Notion/GenericFunctions.ts index 4d4f8a82c6..a6ab4c7894 100644 --- a/packages/nodes-base/nodes/Notion/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Notion/GenericFunctions.ts @@ -239,7 +239,9 @@ export function formatBlocks(blocks: IDataObject[]) { // tslint:disable-next-line: no-any function getPropertyKeyValue(value: any, type: string, timezone: string, version = 1) { + const ignoreIfEmpty = (v: T, cb: (v: T) => any) => !v && value.ignoreIfEmpty ? undefined : cb(v); // tslint:disable-line: no-any let result = {}; + switch (type) { case 'rich_text': if (value.richText === false) { @@ -255,7 +257,7 @@ function getPropertyKeyValue(value: any, type: string, timezone: string, version result = { type: 'number', number: value.numberValue }; break; case 'url': - result = { type: 'url', url: value.urlValue }; + result = ignoreIfEmpty(value.urlValue, url => ({ type: 'url', url })); break; case 'checkbox': result = { type: 'checkbox', checkbox: value.checkboxValue }; @@ -361,9 +363,13 @@ function getNameAndType(key: string) { } export function mapProperties(properties: IDataObject[], timezone: string, version = 1) { - return properties.reduce((obj, value) => Object.assign(obj, { - [`${(value.key as string).split('|')[0]}`]: getPropertyKeyValue(value, (value.key as string).split('|')[1], timezone, version), - }), {}); + return properties + .filter((property): property is Record => typeof property.key === 'string') // tslint:disable-line: no-any + .map(property => [`${property.key.split('|')[0]}`, getPropertyKeyValue(property, property.key.split('|')[1], timezone, version)] as const) + .filter(([, value]) => value) + .reduce((obj, [key, value]) => Object.assign(obj, { + [key]: value, + }), {}); } export function mapSorting(data: [{ key: string, type: string, direction: string, timestamp: boolean }]) { @@ -425,7 +431,7 @@ function simplifyProperty(property: any) { result = property.plain_text; } else if (['rich_text', 'title'].includes(property.type)) { if (Array.isArray(property[type]) && property[type].length !== 0) { - // tslint:disable-next-line: no-any + // tslint:disable-next-line: no-any result = property[type].map((text: any) => simplifyProperty(text) as string).join(''); } else { result = '';