From 9346a037b9fada5428b2ce0b7d241456fc3894d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C4=B1dvan=20Akca?= Date: Mon, 24 Jul 2023 14:45:39 +0300 Subject: [PATCH] feat(notion): add create page action --- .../apps/notion/actions/create-page/index.ts | 104 ++++++++++++++++++ .../backend/src/apps/notion/actions/index.ts | 3 +- .../src/apps/notion/dynamic-data/index.ts | 3 +- .../dynamic-data/list-parent-pages/index.ts | 70 ++++++++++++ packages/docs/pages/apps/notion/actions.md | 2 + 5 files changed, 180 insertions(+), 2 deletions(-) create mode 100644 packages/backend/src/apps/notion/actions/create-page/index.ts create mode 100644 packages/backend/src/apps/notion/dynamic-data/list-parent-pages/index.ts diff --git a/packages/backend/src/apps/notion/actions/create-page/index.ts b/packages/backend/src/apps/notion/actions/create-page/index.ts new file mode 100644 index 00000000..541ce8a9 --- /dev/null +++ b/packages/backend/src/apps/notion/actions/create-page/index.ts @@ -0,0 +1,104 @@ +import { IJSONArray, IJSONObject } from '@automatisch/types'; +import defineAction from '../../../../helpers/define-action'; + +type TBody = { + parent: IJSONObject; + properties: IJSONObject; + children: IJSONArray; +}; + +export default defineAction({ + name: 'Create page', + key: 'createPage', + description: 'Creates a page inside a parent page', + arguments: [ + { + label: 'Parent page', + key: 'parentPageId', + type: 'dropdown' as const, + required: true, + source: { + type: 'query', + name: 'getDynamicData', + arguments: [ + { + name: 'key', + value: 'listParentPages', + }, + ], + }, + }, + { + label: 'Title', + key: 'title', + type: 'string' as const, + required: false, + description: + 'This field has a 2000 character limit. Any characters beyond 2000 will not be included.', + variables: true, + }, + { + label: 'Content', + key: 'content', + type: 'string' as const, + required: false, + description: + 'The text to add to the page body. The max length for this field is 2000 characters. Any characters beyond 2000 will not be included.', + variables: true, + }, + ], + + async run($) { + const parentPageId = $.step.parameters.parentPageId as string; + const title = $.step.parameters.title as string; + const truncatedTitle = title.slice(0, 2000); + const content = $.step.parameters.content as string; + const truncatedContent = content.slice(0, 2000); + + const body: TBody = { + parent: { + page_id: parentPageId, + }, + properties: {}, + children: [], + }; + + if (title) { + body.properties.title = { + type: 'title', + title: [ + { + text: { + content: truncatedTitle, + }, + }, + ], + }; + } + + if (content) { + body.children = [ + { + object: 'block', + type: 'paragraph', + paragraph: { + rich_text: [ + { + type: 'text', + text: { + content: truncatedContent, + }, + }, + ], + }, + }, + ]; + } + + const { data } = await $.http.post('/v1/pages', body); + + $.setActionItem({ + raw: data, + }); + }, +}); diff --git a/packages/backend/src/apps/notion/actions/index.ts b/packages/backend/src/apps/notion/actions/index.ts index 751f0258..7d929160 100644 --- a/packages/backend/src/apps/notion/actions/index.ts +++ b/packages/backend/src/apps/notion/actions/index.ts @@ -1,3 +1,4 @@ import createDatabaseItem from './create-database-item'; +import createPage from './create-page'; -export default [createDatabaseItem]; +export default [createDatabaseItem, createPage]; diff --git a/packages/backend/src/apps/notion/dynamic-data/index.ts b/packages/backend/src/apps/notion/dynamic-data/index.ts index 70002de4..d9d69c68 100644 --- a/packages/backend/src/apps/notion/dynamic-data/index.ts +++ b/packages/backend/src/apps/notion/dynamic-data/index.ts @@ -1,3 +1,4 @@ import listDatabases from './list-databases'; +import listParentPages from './list-parent-pages'; -export default [listDatabases]; +export default [listDatabases, listParentPages]; diff --git a/packages/backend/src/apps/notion/dynamic-data/list-parent-pages/index.ts b/packages/backend/src/apps/notion/dynamic-data/list-parent-pages/index.ts new file mode 100644 index 00000000..f752f587 --- /dev/null +++ b/packages/backend/src/apps/notion/dynamic-data/list-parent-pages/index.ts @@ -0,0 +1,70 @@ +import { IGlobalVariable, IJSONObject } from '@automatisch/types'; + +type Page = { + id: string; + properties: { + title: { + title: [ + { + plain_text: string; + } + ]; + }; + }; + parent: { + workspace: boolean; + }; +}; + +type ResponseData = { + results: Page[]; + next_cursor?: string; +}; + +type Payload = { + filter: { + value: 'page'; + property: 'object'; + }; + start_cursor?: string; +}; + +export default { + name: 'List parent pages', + key: 'listParentPages', + + async run($: IGlobalVariable) { + const parentPages: { + data: IJSONObject[]; + error: IJSONObject | null; + } = { + data: [], + error: null, + }; + const payload: Payload = { + filter: { + value: 'page', + property: 'object', + }, + }; + + do { + const response = await $.http.post('/v1/search', payload); + + payload.start_cursor = response.data.next_cursor; + + const topLevelPages = response.data.results.filter( + (page) => page.parent.workspace + ); + + for (const pages of topLevelPages) { + parentPages.data.push({ + value: pages.id as string, + name: pages.properties.title.title[0].plain_text as string, + }); + } + } while (payload.start_cursor); + + return parentPages; + }, +}; diff --git a/packages/docs/pages/apps/notion/actions.md b/packages/docs/pages/apps/notion/actions.md index 02a221fe..312c5d41 100644 --- a/packages/docs/pages/apps/notion/actions.md +++ b/packages/docs/pages/apps/notion/actions.md @@ -3,6 +3,8 @@ favicon: /favicons/notion.svg items: - name: Create database item desc: Creates an item in a database. + - name: Create page + desc: Creates a page inside a parent page. ---