Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(notion): add updated database items trigger #1456

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/backend/src/apps/notion/triggers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import newDatabaseItems from './new-database-items';
import updatedDatabaseItems from './updated-database-items';

export default [newDatabaseItems];
export default [newDatabaseItems, updatedDatabaseItems];
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import defineTrigger from '../../../../helpers/define-trigger';
import updatedDatabaseItems from './updated-database-items';

export default defineTrigger({
name: 'Updated database items',
key: 'updatedDatabaseItems',
pollInterval: 15,
description:
'Triggers when there is an update to an item in a chosen database',
arguments: [
{
label: 'Database',
key: 'databaseId',
type: 'dropdown' as const,
required: false,
variables: false,
source: {
type: 'query',
name: 'getDynamicData',
arguments: [
{
name: 'key',
value: 'listDatabases',
},
],
},
},
],

async run($) {
await updatedDatabaseItems($);
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { IGlobalVariable } from '@automatisch/types';

type DatabaseItem = {
id: string;
last_edited_time: string;
};

type ResponseData = {
results: DatabaseItem[];
next_cursor?: string;
};

type Payload = {
sorts: [
{
timestamp: 'created_time' | 'last_edited_time';
direction: 'ascending' | 'descending';
}
];
start_cursor?: string;
};

const updatedDatabaseItems = async ($: IGlobalVariable) => {
const payload: Payload = {
sorts: [
{
timestamp: 'last_edited_time',
direction: 'descending',
},
],
};

const databaseId = $.step.parameters.databaseId as string;
const path = `/v1/databases/${databaseId}/query`;
do {
const response = await $.http.post<ResponseData>(path, payload);

payload.start_cursor = response.data.next_cursor;

for (const databaseItem of response.data.results) {
$.pushTriggerItem({
raw: databaseItem,
meta: {
internalId: `${databaseItem.id}-${databaseItem.last_edited_time}`,
},
});
}
} while (payload.start_cursor);
};

export default updatedDatabaseItems;
2 changes: 2 additions & 0 deletions packages/docs/pages/apps/notion/triggers.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ favicon: /favicons/notion.svg
items:
- name: New database items
desc: Triggers when a new database item is created.
- name: Updated database items
desc: Triggers when there is an update to an item in a chosen database.
---

<script setup>
Expand Down
Loading