Skip to content

Commit

Permalink
feat(Notion Node): Allow to ignore Notion URL properties if empty (#3564
Browse files Browse the repository at this point in the history
)

* Allows to ignore Notion URL properties if empty

* Fixes linting

* Fixes another linting error that was not caught locally

* Reorders options alphabetically
  • Loading branch information
Florian Bachmann committed Jul 20, 2022
1 parent 577c73e commit 6cb9aef
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
26 changes: 26 additions & 0 deletions packages/nodes-base/nodes/Notion/DatabasePageDescription.ts
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
16 changes: 11 additions & 5 deletions packages/nodes-base/nodes/Notion/GenericFunctions.ts
Expand Up @@ -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 = <T>(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) {
Expand All @@ -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 };
Expand Down Expand Up @@ -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<string, { key: string; [k: string]: any }> => 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 }]) {
Expand Down Expand Up @@ -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 = '';
Expand Down

0 comments on commit 6cb9aef

Please sign in to comment.