feat(Google Calendar Node): Next occurrence property in recurring events (#8444)

This commit is contained in:
Michael Kret
2024-01-26 12:36:57 +00:00
committed by GitHub
parent 1db35c18e9
commit bf11c7c1bd
4 changed files with 77 additions and 8 deletions

View File

@@ -12,12 +12,12 @@ import type {
import { NodeApiError } from 'n8n-workflow';
import moment from 'moment-timezone';
import { RRule } from 'rrule';
export async function googleApiRequest(
this: IExecuteFunctions | ILoadOptionsFunctions | IPollFunctions,
method: string,
resource: string,
body: any = {},
qs: IDataObject = {},
uri?: string,
@@ -128,3 +128,54 @@ export async function getTimezones(
);
return { results };
}
type RecurentEvent = {
start: {
dateTime: string;
timeZone: string;
};
end: {
dateTime: string;
timeZone: string;
};
recurrence: string[];
nextOccurrence?: {
start: {
dateTime: string;
timeZone: string;
};
end: {
dateTime: string;
timeZone: string;
};
};
};
export function addNextOccurrence(items: RecurentEvent[]) {
for (const item of items) {
if (item.recurrence) {
const rrule = RRule.fromString(item.recurrence[0]);
const until = rrule.options?.until;
const now = new Date();
if (until && until < now) {
continue;
}
const nextOccurrence = rrule.after(new Date());
item.nextOccurrence = {
start: {
dateTime: moment(nextOccurrence).format(),
timeZone: item.start.timeZone,
},
end: {
dateTime: moment(nextOccurrence)
.add(moment(item.end.dateTime).diff(moment(item.start.dateTime)))
.format(),
timeZone: item.end.timeZone,
},
};
}
}
return items;
}

View File

@@ -13,6 +13,7 @@ import { NodeApiError, NodeOperationError } from 'n8n-workflow';
import moment from 'moment-timezone';
import { v4 as uuid } from 'uuid';
import {
addNextOccurrence,
encodeURIComponentOnce,
getCalendars,
getTimezones,
@@ -376,6 +377,10 @@ export class GoogleCalendar implements INodeType {
{},
qs,
);
if (responseData) {
responseData = addNextOccurrence([responseData]);
}
}
//https://developers.google.com/calendar/v3/reference/events/list
if (operation === 'getAll') {
@@ -440,6 +445,10 @@ export class GoogleCalendar implements INodeType {
);
responseData = responseData.items;
}
if (responseData) {
responseData = addNextOccurrence(responseData);
}
}
//https://developers.google.com/calendar/v3/reference/events/patch
if (operation === 'update') {

View File

@@ -880,6 +880,7 @@
"redis": "4.6.12",
"rfc2047": "4.0.1",
"rhea": "1.0.24",
"rrule": "^2.8.1",
"rss-parser": "3.12.0",
"semver": "7.5.4",
"showdown": "2.1.0",