feat(Notion Node): Continue on fail support (#8788)

This commit is contained in:
Michael Kret 2024-03-05 15:45:36 +02:00 committed by GitHub
parent df064af645
commit 4850f6a9cc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 851 additions and 660 deletions

View file

@ -7,9 +7,9 @@ import type {
} from 'n8n-workflow';
import moment from 'moment-timezone';
import { notionApiRequest, simplifyObjects } from './GenericFunctions';
import { notionApiRequest, simplifyObjects } from './shared/GenericFunctions';
import { getDatabases } from './SearchFunctions';
import { listSearch } from './shared/methods';
export class NotionTrigger implements INodeType {
description: INodeTypeDescription = {
@ -142,9 +142,7 @@ export class NotionTrigger implements INodeType {
};
methods = {
listSearch: {
getDatabases,
},
listSearch,
};
async poll(this: IPollFunctions): Promise<INodeExecutionData[][] | null> {

View file

@ -20,7 +20,7 @@ import { camelCase, capitalCase, snakeCase } from 'change-case';
import moment from 'moment-timezone';
import { validate as uuidValidate } from 'uuid';
import { filters } from './Filters';
import { filters } from './descriptions/Filters';
function uuidValidateWithoutDashes(this: IExecuteFunctions, value: string) {
if (uuidValidate(value)) return true;
@ -151,7 +151,7 @@ export async function notionApiRequestGetBlockChildrens(
return responseData;
}
export function getBlockTypes() {
export function getBlockTypesOptions() {
return [
{
name: 'Paragraph',

View file

@ -1,6 +1,6 @@
import type { INodeProperties } from 'n8n-workflow';
import { getConditions, getSearchFilters } from './GenericFunctions';
import { getConditions, getSearchFilters } from '../GenericFunctions';
import { blocks, text } from './Blocks';

View file

@ -0,0 +1 @@
export * as listSearch from './listSearch';

View file

@ -4,7 +4,7 @@ import type {
INodeListSearchItems,
INodeListSearchResult,
} from 'n8n-workflow';
import { notionApiRequestAllItems } from './GenericFunctions';
import { notionApiRequestAllItems } from '../GenericFunctions';
export async function getDatabases(
this: ILoadOptionsFunctions,

View file

@ -1,4 +1,4 @@
import { formatBlocks } from '../GenericFunctions';
import { formatBlocks } from '../shared/GenericFunctions';
describe('Test NotionV2, formatBlocks', () => {
it('should format to_do block', () => {

View file

@ -10,23 +10,23 @@ import type {
} from 'n8n-workflow';
import moment from 'moment-timezone';
import type { SortData } from '../GenericFunctions';
import type { SortData } from '../shared/GenericFunctions';
import {
extractDatabaseId,
extractDatabaseMentionRLC,
extractPageId,
formatBlocks,
formatTitle,
getBlockTypes,
getBlockTypesOptions,
mapFilters,
mapProperties,
mapSorting,
notionApiRequest,
notionApiRequestAllItems,
simplifyObjects,
} from '../GenericFunctions';
} from '../shared/GenericFunctions';
import { getDatabases } from '../SearchFunctions';
import { listSearch } from '../shared/methods';
import { versionDescription } from './VersionDescription';
export class NotionV1 implements INodeType {
@ -40,9 +40,7 @@ export class NotionV1 implements INodeType {
}
methods = {
listSearch: {
getDatabases,
},
listSearch,
loadOptions: {
async getDatabaseProperties(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
@ -104,7 +102,7 @@ export class NotionV1 implements INodeType {
return returnData;
},
async getBlockTypes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
return getBlockTypes();
return getBlockTypesOptions();
},
async getPropertySelectValues(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const [name, type] = (this.getCurrentNodeParameter('&key') as string).split('|');

View file

@ -1,14 +1,17 @@
/* eslint-disable n8n-nodes-base/node-filename-against-convention */
import type { INodeTypeDescription } from 'n8n-workflow';
import { databaseFields, databaseOperations } from '../DatabaseDescription';
import { databaseFields, databaseOperations } from '../shared/descriptions/DatabaseDescription';
import { userFields, userOperations } from '../UserDescription';
import { userFields, userOperations } from '../shared/descriptions/UserDescription';
import { pageFields, pageOperations } from '../PageDescription';
import { pageFields, pageOperations } from '../shared/descriptions/PageDescription';
import { blockFields, blockOperations } from '../BlockDescription';
import { blockFields, blockOperations } from '../shared/descriptions/BlockDescription';
import { databasePageFields, databasePageOperations } from '../DatabasePageDescription';
import {
databasePageFields,
databasePageOperations,
} from '../shared/descriptions/DatabasePageDescription';
export const versionDescription: INodeTypeDescription = {
displayName: 'Notion',

View file

@ -1,17 +1,14 @@
import type {
IExecuteFunctions,
IDataObject,
ILoadOptionsFunctions,
INodeExecutionData,
INodePropertyOptions,
INodeType,
INodeTypeBaseDescription,
INodeTypeDescription,
} from 'n8n-workflow';
import { jsonParse, NodeApiError } from 'n8n-workflow';
import moment from 'moment-timezone';
import type { SortData, FileRecord } from '../GenericFunctions';
import type { SortData, FileRecord } from '../shared/GenericFunctions';
import {
downloadFiles,
extractDatabaseId,
@ -19,7 +16,6 @@ import {
extractPageId,
formatBlocks,
formatTitle,
getBlockTypes,
mapFilters,
mapProperties,
mapSorting,
@ -29,9 +25,10 @@ import {
simplifyBlocksOutput,
simplifyObjects,
validateJSON,
} from '../GenericFunctions';
} from '../shared/GenericFunctions';
import { getDatabases } from '../SearchFunctions';
import { listSearch } from '../shared/methods';
import { loadOptions } from './methods';
import { versionDescription } from './VersionDescription';
export class NotionV2 implements INodeType {
@ -44,211 +41,33 @@ export class NotionV2 implements INodeType {
};
}
methods = {
listSearch: {
getDatabases,
},
loadOptions: {
async getDatabaseProperties(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const databaseId = this.getCurrentNodeParameter('databaseId', {
extractValue: true,
}) as string;
const { properties } = await notionApiRequest.call(this, 'GET', `/databases/${databaseId}`);
for (const key of Object.keys(properties as IDataObject)) {
//remove parameters that cannot be set from the API.
if (
![
'created_time',
'last_edited_time',
'created_by',
'last_edited_by',
'formula',
'rollup',
].includes(properties[key].type as string)
) {
returnData.push({
name: `${key}`,
value: `${key}|${properties[key].type}`,
});
}
}
returnData.sort((a, b) => {
if (a.name.toLocaleLowerCase() < b.name.toLocaleLowerCase()) {
return -1;
}
if (a.name.toLocaleLowerCase() > b.name.toLocaleLowerCase()) {
return 1;
}
return 0;
});
return returnData;
},
async getFilterProperties(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const databaseId = this.getCurrentNodeParameter('databaseId', {
extractValue: true,
}) as string;
const { properties } = await notionApiRequest.call(this, 'GET', `/databases/${databaseId}`);
for (const key of Object.keys(properties as IDataObject)) {
returnData.push({
name: `${key}`,
value: `${key}|${properties[key].type}`,
});
}
returnData.sort((a, b) => {
if (a.name.toLocaleLowerCase() < b.name.toLocaleLowerCase()) {
return -1;
}
if (a.name.toLocaleLowerCase() > b.name.toLocaleLowerCase()) {
return 1;
}
return 0;
});
return returnData;
},
async getBlockTypes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
return getBlockTypes();
},
async getPropertySelectValues(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const [name, type] = (this.getCurrentNodeParameter('&key') as string).split('|');
const databaseId = this.getCurrentNodeParameter('databaseId', {
extractValue: true,
}) as string;
const resource = this.getCurrentNodeParameter('resource') as string;
const operation = this.getCurrentNodeParameter('operation') as string;
const { properties } = await notionApiRequest.call(this, 'GET', `/databases/${databaseId}`);
if (resource === 'databasePage') {
if (['multi_select', 'select', 'status'].includes(type) && operation === 'getAll') {
return properties[name][type].options.map((option: IDataObject) => ({
name: option.name,
value: option.name,
}));
} else if (
['multi_select', 'select', 'status'].includes(type) &&
['create', 'update'].includes(operation)
) {
return properties[name][type].options.map((option: IDataObject) => ({
name: option.name,
value: option.name,
}));
}
}
return properties[name][type].options.map((option: IDataObject) => ({
name: option.name,
value: option.id,
}));
},
async getUsers(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const users = await notionApiRequestAllItems.call(this, 'results', 'GET', '/users');
for (const user of users) {
if (user.type === 'person') {
returnData.push({
name: user.name,
value: user.id,
});
}
}
return returnData;
},
async getDatabaseIdFromPage(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const pageId = extractPageId(
this.getCurrentNodeParameter('pageId', { extractValue: true }) as string,
);
const {
parent: { database_id: databaseId },
} = await notionApiRequest.call(this, 'GET', `/pages/${pageId}`);
const { properties } = await notionApiRequest.call(this, 'GET', `/databases/${databaseId}`);
for (const key of Object.keys(properties as IDataObject)) {
//remove parameters that cannot be set from the API.
if (
![
'created_time',
'last_edited_time',
'created_by',
'last_edited_by',
'formula',
'rollup',
].includes(properties[key].type as string)
) {
returnData.push({
name: `${key}`,
value: `${key}|${properties[key].type}`,
});
}
}
returnData.sort((a, b) => {
if (a.name.toLocaleLowerCase() < b.name.toLocaleLowerCase()) {
return -1;
}
if (a.name.toLocaleLowerCase() > b.name.toLocaleLowerCase()) {
return 1;
}
return 0;
});
return returnData;
},
async getDatabaseOptionsFromPage(
this: ILoadOptionsFunctions,
): Promise<INodePropertyOptions[]> {
const pageId = extractPageId(
this.getCurrentNodeParameter('pageId', { extractValue: true }) as string,
);
const [name, type] = (this.getCurrentNodeParameter('&key') as string).split('|');
const {
parent: { database_id: databaseId },
} = await notionApiRequest.call(this, 'GET', `/pages/${pageId}`);
const { properties } = await notionApiRequest.call(this, 'GET', `/databases/${databaseId}`);
return properties[name][type].options.map((option: IDataObject) => ({
name: option.name,
value: option.name,
}));
},
// Get all the timezones to display them to user so that they can
// select them easily
async getTimezones(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
for (const timezone of moment.tz.names()) {
const timezoneName = timezone;
const timezoneId = timezone;
returnData.push({
name: timezoneName,
value: timezoneId,
});
}
returnData.unshift({
name: 'Default',
value: 'default',
description: 'Timezone set in n8n',
});
return returnData;
},
},
};
methods = { listSearch, loadOptions };
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: INodeExecutionData[] = [];
const length = items.length;
let responseData;
const qs: IDataObject = {};
const timezone = this.getTimezone();
const resource = this.getNodeParameter('resource', 0);
const operation = this.getNodeParameter('operation', 0);
const itemsLength = items.length;
const timezone = this.getTimezone();
const qs: IDataObject = {};
let returnData: INodeExecutionData[] = [];
let responseData;
let download = false;
if (resource === 'block') {
if (operation === 'append') {
for (let i = 0; i < length; i++) {
for (let i = 0; i < itemsLength; i++) {
try {
const blockId = extractPageId(
this.getNodeParameter('blockId', i, '', { extractValue: true }) as string,
);
const blockValues = this.getNodeParameter('blockUi.blockValues', i, []) as IDataObject[];
const blockValues = this.getNodeParameter(
'blockUi.blockValues',
i,
[],
) as IDataObject[];
extractDatabaseMentionRLC(blockValues);
const body: IDataObject = {
children: formatBlocks(blockValues),
@ -264,12 +83,23 @@ export class NotionV2 implements INodeType {
this.helpers.returnJsonArray(block as IDataObject),
{ itemData: { item: i } },
);
returnData.push(...executionData);
returnData = returnData.concat(executionData);
} catch (error) {
if (this.continueOnFail()) {
returnData.push({
json: { error: error.message },
pairedItem: { item: i },
});
} else {
throw error;
}
}
}
}
if (operation === 'getAll') {
for (let i = 0; i < length; i++) {
for (let i = 0; i < itemsLength; i++) {
try {
const blockId = extractPageId(
this.getNodeParameter('blockId', i, '', { extractValue: true }) as string,
);
@ -301,7 +131,12 @@ export class NotionV2 implements INodeType {
const results = responseData.results;
if (fetchNestedBlocks) {
responseData = await notionApiRequestGetBlockChildrens.call(this, results, [], limit);
responseData = await notionApiRequestGetBlockChildrens.call(
this,
results,
[],
limit,
);
} else {
responseData = results;
}
@ -327,7 +162,17 @@ export class NotionV2 implements INodeType {
this.helpers.returnJsonArray(responseData as IDataObject),
{ itemData: { item: i } },
);
returnData.push(...executionData);
returnData = returnData.concat(executionData);
} catch (error) {
if (this.continueOnFail()) {
returnData.push({
json: { error: error.message },
pairedItem: { item: i },
});
} else {
throw error;
}
}
}
}
}
@ -335,7 +180,8 @@ export class NotionV2 implements INodeType {
if (resource === 'database') {
if (operation === 'get') {
const simple = this.getNodeParameter('simple', 0) as boolean;
for (let i = 0; i < length; i++) {
for (let i = 0; i < itemsLength; i++) {
try {
const databaseId = extractDatabaseId(
this.getNodeParameter('databaseId', i, '', { extractValue: true }) as string,
);
@ -348,13 +194,24 @@ export class NotionV2 implements INodeType {
this.helpers.returnJsonArray(responseData as IDataObject),
{ itemData: { item: i } },
);
returnData.push(...executionData);
returnData = returnData.concat(executionData);
} catch (error) {
if (this.continueOnFail()) {
returnData.push({
json: { error: error.message },
pairedItem: { item: i },
});
} else {
throw error;
}
}
}
}
if (operation === 'getAll') {
const simple = this.getNodeParameter('simple', 0) as boolean;
for (let i = 0; i < length; i++) {
for (let i = 0; i < itemsLength; i++) {
try {
const body: IDataObject = {
filter: { property: 'object', value: 'database' },
};
@ -380,12 +237,23 @@ export class NotionV2 implements INodeType {
this.helpers.returnJsonArray(responseData as IDataObject),
{ itemData: { item: i } },
);
returnData.push(...executionData);
returnData = returnData.concat(executionData);
} catch (error) {
if (this.continueOnFail()) {
returnData.push({
json: { error: error.message },
pairedItem: { item: i },
});
} else {
throw error;
}
}
}
}
if (operation === 'search') {
for (let i = 0; i < length; i++) {
for (let i = 0; i < itemsLength; i++) {
try {
const text = this.getNodeParameter('text', i) as string;
const options = this.getNodeParameter('options', i);
const returnAll = this.getNodeParameter('returnAll', i);
@ -432,7 +300,17 @@ export class NotionV2 implements INodeType {
this.helpers.returnJsonArray(responseData as IDataObject),
{ itemData: { item: i } },
);
returnData.push(...executionData);
returnData = returnData.concat(executionData);
} catch (error) {
if (this.continueOnFail()) {
returnData.push({
json: { error: error.message },
pairedItem: { item: i },
});
} else {
throw error;
}
}
}
}
}
@ -449,7 +327,8 @@ export class NotionV2 implements INodeType {
titleKey = key;
}
}
for (let i = 0; i < length; i++) {
for (let i = 0; i < itemsLength; i++) {
try {
const title = this.getNodeParameter('title', i) as string;
const simple = this.getNodeParameter('simple', i) as boolean;
@ -482,7 +361,11 @@ export class NotionV2 implements INodeType {
mapProperties.call(this, propertiesValues, timezone, 2) as IDataObject,
);
}
const blockValues = this.getNodeParameter('blockUi.blockValues', i, []) as IDataObject[];
const blockValues = this.getNodeParameter(
'blockUi.blockValues',
i,
[],
) as IDataObject[];
extractDatabaseMentionRLC(blockValues);
body.children = formatBlocks(blockValues);
@ -504,12 +387,23 @@ export class NotionV2 implements INodeType {
this.helpers.returnJsonArray(responseData as IDataObject),
{ itemData: { item: i } },
);
returnData.push(...executionData);
returnData = returnData.concat(executionData);
} catch (error) {
if (this.continueOnFail()) {
returnData.push({
json: { error: error.message },
pairedItem: { item: i },
});
} else {
throw error;
}
}
}
}
if (operation === 'get') {
for (let i = 0; i < length; i++) {
for (let i = 0; i < itemsLength; i++) {
try {
const pageId = extractPageId(
this.getNodeParameter('pageId', i, '', { extractValue: true }) as string,
);
@ -523,12 +417,23 @@ export class NotionV2 implements INodeType {
this.helpers.returnJsonArray(responseData as IDataObject),
{ itemData: { item: i } },
);
returnData.push(...executionData);
returnData = returnData.concat(executionData);
} catch (error) {
if (this.continueOnFail()) {
returnData.push({
json: { error: error.message },
pairedItem: { item: i },
});
} else {
throw error;
}
}
}
}
if (operation === 'getAll') {
for (let i = 0; i < length; i++) {
for (let i = 0; i < itemsLength; i++) {
try {
download = this.getNodeParameter('options.downloadFiles', 0, false) as boolean;
const simple = this.getNodeParameter('simple', 0) as boolean;
const databaseId = this.getNodeParameter('databaseId', i, '', {
@ -603,12 +508,23 @@ export class NotionV2 implements INodeType {
this.helpers.returnJsonArray(responseData as IDataObject),
{ itemData: { item: i } },
);
returnData.push(...executionData);
returnData = returnData.concat(executionData);
} catch (error) {
if (this.continueOnFail()) {
returnData.push({
json: { error: error.message },
pairedItem: { item: i },
});
} else {
throw error;
}
}
}
}
if (operation === 'update') {
for (let i = 0; i < length; i++) {
for (let i = 0; i < itemsLength; i++) {
try {
const pageId = extractPageId(
this.getNodeParameter('pageId', i, '', { extractValue: true }) as string,
);
@ -644,14 +560,25 @@ export class NotionV2 implements INodeType {
this.helpers.returnJsonArray(responseData as IDataObject),
{ itemData: { item: i } },
);
returnData.push(...executionData);
returnData = returnData.concat(executionData);
} catch (error) {
if (this.continueOnFail()) {
returnData.push({
json: { error: error.message },
pairedItem: { item: i },
});
} else {
throw error;
}
}
}
}
}
if (resource === 'user') {
if (operation === 'get') {
for (let i = 0; i < length; i++) {
for (let i = 0; i < itemsLength; i++) {
try {
const userId = this.getNodeParameter('userId', i) as string;
responseData = await notionApiRequest.call(this, 'GET', `/users/${userId}`);
@ -659,11 +586,22 @@ export class NotionV2 implements INodeType {
this.helpers.returnJsonArray(responseData as IDataObject),
{ itemData: { item: i } },
);
returnData.push(...executionData);
returnData = returnData.concat(executionData);
} catch (error) {
if (this.continueOnFail()) {
returnData.push({
json: { error: error.message },
pairedItem: { item: i },
});
} else {
throw error;
}
}
}
}
if (operation === 'getAll') {
for (let i = 0; i < length; i++) {
for (let i = 0; i < itemsLength; i++) {
try {
const returnAll = this.getNodeParameter('returnAll', i);
if (returnAll) {
responseData = await notionApiRequestAllItems.call(this, 'results', 'GET', '/users');
@ -677,14 +615,25 @@ export class NotionV2 implements INodeType {
this.helpers.returnJsonArray(responseData as IDataObject),
{ itemData: { item: i } },
);
returnData.push(...executionData);
returnData = returnData.concat(executionData);
} catch (error) {
if (this.continueOnFail()) {
returnData.push({
json: { error: error.message },
pairedItem: { item: i },
});
} else {
throw error;
}
}
}
}
}
if (resource === 'page') {
if (operation === 'archive') {
for (let i = 0; i < length; i++) {
for (let i = 0; i < itemsLength; i++) {
try {
const pageId = extractPageId(
this.getNodeParameter('pageId', i, '', { extractValue: true }) as string,
);
@ -700,12 +649,23 @@ export class NotionV2 implements INodeType {
this.helpers.returnJsonArray(responseData as IDataObject),
{ itemData: { item: i } },
);
returnData.push(...executionData);
returnData = returnData.concat(executionData);
} catch (error) {
if (this.continueOnFail()) {
returnData.push({
json: { error: error.message },
pairedItem: { item: i },
});
} else {
throw error;
}
}
}
}
if (operation === 'create') {
for (let i = 0; i < length; i++) {
for (let i = 0; i < itemsLength; i++) {
try {
const simple = this.getNodeParameter('simple', i) as boolean;
const body: { [key: string]: any } = {
parent: {},
@ -715,7 +675,11 @@ export class NotionV2 implements INodeType {
this.getNodeParameter('pageId', i, '', { extractValue: true }) as string,
);
body.properties = formatTitle(this.getNodeParameter('title', i) as string);
const blockValues = this.getNodeParameter('blockUi.blockValues', i, []) as IDataObject[];
const blockValues = this.getNodeParameter(
'blockUi.blockValues',
i,
[],
) as IDataObject[];
extractDatabaseMentionRLC(blockValues);
body.children = formatBlocks(blockValues);
@ -737,12 +701,23 @@ export class NotionV2 implements INodeType {
this.helpers.returnJsonArray(responseData as IDataObject),
{ itemData: { item: i } },
);
returnData.push(...executionData);
returnData = returnData.concat(executionData);
} catch (error) {
if (this.continueOnFail()) {
returnData.push({
json: { error: error.message },
pairedItem: { item: i },
});
} else {
throw error;
}
}
}
}
if (operation === 'search') {
for (let i = 0; i < length; i++) {
for (let i = 0; i < itemsLength; i++) {
try {
const text = this.getNodeParameter('text', i) as string;
const options = this.getNodeParameter('options', i);
const returnAll = this.getNodeParameter('returnAll', i);
@ -788,7 +763,17 @@ export class NotionV2 implements INodeType {
this.helpers.returnJsonArray(responseData as IDataObject),
{ itemData: { item: i } },
);
returnData.push(...executionData);
returnData = returnData.concat(executionData);
} catch (error) {
if (this.continueOnFail()) {
returnData.push({
json: { error: error.message },
pairedItem: { item: i },
});
} else {
throw error;
}
}
}
}
}

View file

@ -1,14 +1,17 @@
/* eslint-disable n8n-nodes-base/node-filename-against-convention */
import type { INodeTypeDescription } from 'n8n-workflow';
import { databaseFields, databaseOperations } from '../DatabaseDescription';
import { databaseFields, databaseOperations } from '../shared/descriptions/DatabaseDescription';
import { userFields, userOperations } from '../UserDescription';
import { userFields, userOperations } from '../shared/descriptions/UserDescription';
import { pageFields, pageOperations } from '../PageDescription';
import { pageFields, pageOperations } from '../shared/descriptions/PageDescription';
import { blockFields, blockOperations } from '../BlockDescription';
import { blockFields, blockOperations } from '../shared/descriptions/BlockDescription';
import { databasePageFields, databasePageOperations } from '../DatabasePageDescription';
import {
databasePageFields,
databasePageOperations,
} from '../shared/descriptions/DatabasePageDescription';
export const versionDescription: INodeTypeDescription = {
displayName: 'Notion',

View file

@ -0,0 +1 @@
export * as loadOptions from './loadOptions';

View file

@ -0,0 +1,202 @@
import type { IDataObject, ILoadOptionsFunctions, INodePropertyOptions } from 'n8n-workflow';
import moment from 'moment-timezone';
import {
extractPageId,
getBlockTypesOptions,
notionApiRequest,
notionApiRequestAllItems,
} from '../../shared/GenericFunctions';
export async function getDatabaseProperties(
this: ILoadOptionsFunctions,
): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const databaseId = this.getCurrentNodeParameter('databaseId', {
extractValue: true,
}) as string;
const { properties } = await notionApiRequest.call(this, 'GET', `/databases/${databaseId}`);
for (const key of Object.keys(properties as IDataObject)) {
//remove parameters that cannot be set from the API.
if (
![
'created_time',
'last_edited_time',
'created_by',
'last_edited_by',
'formula',
'rollup',
].includes(properties[key].type as string)
) {
returnData.push({
name: `${key}`,
value: `${key}|${properties[key].type}`,
});
}
}
returnData.sort((a, b) => {
if (a.name.toLocaleLowerCase() < b.name.toLocaleLowerCase()) {
return -1;
}
if (a.name.toLocaleLowerCase() > b.name.toLocaleLowerCase()) {
return 1;
}
return 0;
});
return returnData;
}
export async function getFilterProperties(
this: ILoadOptionsFunctions,
): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const databaseId = this.getCurrentNodeParameter('databaseId', {
extractValue: true,
}) as string;
const { properties } = await notionApiRequest.call(this, 'GET', `/databases/${databaseId}`);
for (const key of Object.keys(properties as IDataObject)) {
returnData.push({
name: `${key}`,
value: `${key}|${properties[key].type}`,
});
}
returnData.sort((a, b) => {
if (a.name.toLocaleLowerCase() < b.name.toLocaleLowerCase()) {
return -1;
}
if (a.name.toLocaleLowerCase() > b.name.toLocaleLowerCase()) {
return 1;
}
return 0;
});
return returnData;
}
export async function getBlockTypes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
return getBlockTypesOptions();
}
export async function getPropertySelectValues(
this: ILoadOptionsFunctions,
): Promise<INodePropertyOptions[]> {
const [name, type] = (this.getCurrentNodeParameter('&key') as string).split('|');
const databaseId = this.getCurrentNodeParameter('databaseId', {
extractValue: true,
}) as string;
const resource = this.getCurrentNodeParameter('resource') as string;
const operation = this.getCurrentNodeParameter('operation') as string;
const { properties } = await notionApiRequest.call(this, 'GET', `/databases/${databaseId}`);
if (resource === 'databasePage') {
if (['multi_select', 'select', 'status'].includes(type) && operation === 'getAll') {
return properties[name][type].options.map((option: IDataObject) => ({
name: option.name,
value: option.name,
}));
} else if (
['multi_select', 'select', 'status'].includes(type) &&
['create', 'update'].includes(operation)
) {
return properties[name][type].options.map((option: IDataObject) => ({
name: option.name,
value: option.name,
}));
}
}
return properties[name][type].options.map((option: IDataObject) => ({
name: option.name,
value: option.id,
}));
}
export async function getUsers(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const users = await notionApiRequestAllItems.call(this, 'results', 'GET', '/users');
for (const user of users) {
if (user.type === 'person') {
returnData.push({
name: user.name,
value: user.id,
});
}
}
return returnData;
}
export async function getDatabaseIdFromPage(
this: ILoadOptionsFunctions,
): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const pageId = extractPageId(
this.getCurrentNodeParameter('pageId', { extractValue: true }) as string,
);
const {
parent: { database_id: databaseId },
} = await notionApiRequest.call(this, 'GET', `/pages/${pageId}`);
const { properties } = await notionApiRequest.call(this, 'GET', `/databases/${databaseId}`);
for (const key of Object.keys(properties as IDataObject)) {
//remove parameters that cannot be set from the API.
if (
![
'created_time',
'last_edited_time',
'created_by',
'last_edited_by',
'formula',
'rollup',
].includes(properties[key].type as string)
) {
returnData.push({
name: `${key}`,
value: `${key}|${properties[key].type}`,
});
}
}
returnData.sort((a, b) => {
if (a.name.toLocaleLowerCase() < b.name.toLocaleLowerCase()) {
return -1;
}
if (a.name.toLocaleLowerCase() > b.name.toLocaleLowerCase()) {
return 1;
}
return 0;
});
return returnData;
}
export async function getDatabaseOptionsFromPage(
this: ILoadOptionsFunctions,
): Promise<INodePropertyOptions[]> {
const pageId = extractPageId(
this.getCurrentNodeParameter('pageId', { extractValue: true }) as string,
);
const [name, type] = (this.getCurrentNodeParameter('&key') as string).split('|');
const {
parent: { database_id: databaseId },
} = await notionApiRequest.call(this, 'GET', `/pages/${pageId}`);
const { properties } = await notionApiRequest.call(this, 'GET', `/databases/${databaseId}`);
return properties[name][type].options.map((option: IDataObject) => ({
name: option.name,
value: option.name,
}));
}
// Get all the timezones to display them to user so that they can
// select them easily
export async function getTimezones(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
for (const timezone of moment.tz.names()) {
const timezoneName = timezone;
const timezoneId = timezone;
returnData.push({
name: timezoneName,
value: timezoneId,
});
}
returnData.unshift({
name: 'Default',
value: 'default',
description: 'Timezone set in n8n',
});
return returnData;
}