fix(Notion Trigger (Beta) Node): Fix Notion trigger polling strategy

This commit is contained in:
Ugo Bataillard
2022-11-09 19:26:13 +09:00
committed by GitHub
parent bae3098e4e
commit 3b3405089d
7 changed files with 97 additions and 36 deletions

View File

@@ -51,11 +51,11 @@ export class NotionTrigger implements INodeType {
},
],
required: true,
default: '',
default: 'pageAddedToDatabase',
},
{
displayName:
"In Notion, make sure you share your database with your integration. Otherwise it won't be accessible, or listed here.",
'In Notion, make sure you <a href="https://www.notion.so/help/add-and-manage-connections-with-the-api#add-connections-to-pages" target="_blank">share your database with your integration</a> . Otherwise it won\'t be accessible, or listed here.',
name: 'notionNotice',
type: 'notice',
default: '',
@@ -127,16 +127,24 @@ export class NotionTrigger implements INodeType {
const event = this.getNodeParameter('event') as string;
const simple = this.getNodeParameter('simple') as boolean;
const now = moment().utc().format();
const lastTimeChecked = webhookData.lastTimeChecked
? moment(webhookData.lastTimeChecked as string)
: moment().set({ second: 0, millisecond: 0 }); // Notion timestamp accuracy is only down to the minute
const startDate = (webhookData.lastTimeChecked as string) || now;
// update lastTimeChecked to now
webhookData.lastTimeChecked = moment().set({ second: 0, millisecond: 0 });
const endDate = now;
webhookData.lastTimeChecked = endDate;
// because Notion timestamp accuracy is only down to the minute some duplicates can be fetch
const possibleDuplicates = (webhookData.possibleDuplicates as string[]) ?? [];
const sortProperty = event === 'pageAddedToDatabase' ? 'created_time' : 'last_edited_time';
const option: IDataObject = {
headers: {
'Notion-Version': '2022-02-22',
},
};
const body: IDataObject = {
page_size: 1,
sorts: [
@@ -145,6 +153,14 @@ export class NotionTrigger implements INodeType {
direction: 'descending',
},
],
...(this.getMode() !== 'manual' && {
filter: {
timestamp: sortProperty,
[sortProperty]: {
on_or_after: lastTimeChecked.utc().format(),
},
},
}),
};
let records: IDataObject[] = [];
@@ -157,6 +173,9 @@ export class NotionTrigger implements INodeType {
'POST',
`/databases/${databaseId}/query`,
body,
{},
'',
option,
);
if (this.getMode() === 'manual') {
@@ -169,7 +188,7 @@ export class NotionTrigger implements INodeType {
}
// if something changed after the last check
if (Object.keys(data[0]).length !== 0 && webhookData.lastRecordProccesed !== data[0].id) {
if (Array.isArray(data) && data.length && Object.keys(data[0]).length !== 0) {
do {
body.page_size = 10;
const { results, has_more, next_cursor } = await notionApiRequest.call(
@@ -177,29 +196,46 @@ export class NotionTrigger implements INodeType {
'POST',
`/databases/${databaseId}/query`,
body,
{},
'',
option,
);
records.push.apply(records, results);
records.push(...results);
hasMore = has_more;
if (next_cursor !== null) {
body['start_cursor'] = next_cursor;
}
// Only stop when we reach records strictly before last recorded time to be sure we catch records from the same minute
} while (
!moment(records[records.length - 1][sortProperty] as string).isSameOrBefore(startDate) &&
!moment(records[records.length - 1][sortProperty] as string).isBefore(lastTimeChecked) &&
hasMore === true
);
if (this.getMode() !== 'manual') {
records = records.filter((record: IDataObject) =>
moment(record[sortProperty] as string).isBetween(startDate, endDate),
);
// Filter out already processed left over records:
// with a time strictly before the last record processed
// or from the same minute not present in the list of processed records
records = records.filter(
(record: IDataObject) => !possibleDuplicates.includes(record.id as string),
);
// Save the time of the most recent record processed
if (records[0]) {
const latestTimestamp = moment(records[0][sortProperty] as string);
// Save record ids with the same timestamp as the latest processed records
webhookData.possibleDuplicates = records
.filter((record: IDataObject) =>
moment(record[sortProperty] as string).isSame(latestTimestamp),
)
.map((record: IDataObject) => record.id);
} else {
webhookData.possibleDuplicates = undefined;
}
if (simple === true) {
records = simplifyObjects(records, false, 1);
}
webhookData.lastRecordProccesed = data[0].id;
if (Array.isArray(records) && records.length) {
return [this.helpers.returnJsonArray(records)];
}