mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-13 16:14:07 -08:00
Merge b0c2c595d4
into 0fdb79a270
This commit is contained in:
commit
ab545ef00b
|
@ -553,6 +553,18 @@ export const eventFields: INodeProperties[] = [
|
|||
description:
|
||||
'The maximum number of attendees to include in the response. If there are more than the specified number of attendees, only the participant is returned.',
|
||||
},
|
||||
{
|
||||
displayName: 'Return Next Instance of Recurring Event',
|
||||
name: 'returnNextInstance',
|
||||
type: 'boolean',
|
||||
default: false,
|
||||
description: 'Whether to return next instances of recurring event, instead of event itself',
|
||||
displayOptions: {
|
||||
show: {
|
||||
'@version': [{ _cnd: { gte: 1.3 } }],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
displayName: 'Timezone',
|
||||
name: 'timeZone',
|
||||
|
@ -629,6 +641,34 @@ export const eventFields: INodeProperties[] = [
|
|||
default: 50,
|
||||
description: 'Max number of results to return',
|
||||
},
|
||||
{
|
||||
displayName: 'After',
|
||||
name: 'timeMin',
|
||||
type: 'dateTime',
|
||||
default: '',
|
||||
description: 'At least some part of the event must be after this time',
|
||||
displayOptions: {
|
||||
show: {
|
||||
'@version': [{ _cnd: { gte: 1.3 } }],
|
||||
operation: ['getAll'],
|
||||
resource: ['event'],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
displayName: 'Before',
|
||||
name: 'timeMax',
|
||||
type: 'dateTime',
|
||||
default: '',
|
||||
description: 'At least some part of the event must be before this time',
|
||||
displayOptions: {
|
||||
show: {
|
||||
'@version': [{ _cnd: { gte: 1.3 } }],
|
||||
operation: ['getAll'],
|
||||
resource: ['event'],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
displayName: 'Options',
|
||||
name: 'options',
|
||||
|
@ -648,6 +688,11 @@ export const eventFields: INodeProperties[] = [
|
|||
type: 'dateTime',
|
||||
default: '',
|
||||
description: 'At least some part of the event must be after this time',
|
||||
displayOptions: {
|
||||
hide: {
|
||||
'@version': [{ _cnd: { gte: 1.3 } }],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
displayName: 'Before',
|
||||
|
@ -655,6 +700,24 @@ export const eventFields: INodeProperties[] = [
|
|||
type: 'dateTime',
|
||||
default: '',
|
||||
description: 'At least some part of the event must be before this time',
|
||||
displayOptions: {
|
||||
hide: {
|
||||
'@version': [{ _cnd: { gte: 1.3 } }],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
displayName: 'Expand Events',
|
||||
name: 'singleEvents',
|
||||
type: 'boolean',
|
||||
default: false,
|
||||
description:
|
||||
'Whether to expand recurring events into instances and only return single one-off events and instances of recurring events, but not the underlying recurring events themselves',
|
||||
displayOptions: {
|
||||
hide: {
|
||||
'@version': [{ _cnd: { gte: 1.3 } }],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
displayName: 'Fields',
|
||||
|
@ -708,6 +771,34 @@ export const eventFields: INodeProperties[] = [
|
|||
description:
|
||||
'Free text search terms to find events that match these terms in any field, except for extended properties',
|
||||
},
|
||||
{
|
||||
displayName: 'Recurring Event Handling',
|
||||
name: 'recurringEventHandling',
|
||||
type: 'options',
|
||||
default: 'expand',
|
||||
options: [
|
||||
{
|
||||
name: 'All Occurrences',
|
||||
value: 'expand',
|
||||
description: 'Return all instances of recurring event for specified time range',
|
||||
},
|
||||
{
|
||||
name: 'First Occurrence',
|
||||
value: 'first',
|
||||
description: 'Return event with specified recurrence rule',
|
||||
},
|
||||
{
|
||||
name: 'Next Occurrence',
|
||||
value: 'next',
|
||||
description: 'Return next instance of recurring event',
|
||||
},
|
||||
],
|
||||
displayOptions: {
|
||||
show: {
|
||||
'@version': [{ _cnd: { gte: 1.3 } }],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
displayName: 'Show Deleted',
|
||||
name: 'showDeleted',
|
||||
|
@ -723,14 +814,6 @@ export const eventFields: INodeProperties[] = [
|
|||
default: false,
|
||||
description: 'Whether to include hidden invitations in the result',
|
||||
},
|
||||
{
|
||||
displayName: 'Single Events',
|
||||
name: 'singleEvents',
|
||||
type: 'boolean',
|
||||
default: false,
|
||||
description:
|
||||
'Whether to expand recurring events into instances and only return single one-off events and instances of recurring events, but not the underlying recurring events themselves',
|
||||
},
|
||||
{
|
||||
displayName: 'Timezone',
|
||||
name: 'timeZone',
|
||||
|
@ -797,6 +880,30 @@ export const eventFields: INodeProperties[] = [
|
|||
},
|
||||
default: '',
|
||||
},
|
||||
{
|
||||
displayName: 'Modify',
|
||||
name: 'modifyTarget',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'Reccuring Event Instance',
|
||||
value: 'instance',
|
||||
},
|
||||
{
|
||||
name: 'Reccuring Event',
|
||||
value: 'event',
|
||||
},
|
||||
],
|
||||
default: 'instance',
|
||||
displayOptions: {
|
||||
show: {
|
||||
'@version': [{ _cnd: { gte: 1.3 } }],
|
||||
resource: ['event'],
|
||||
operation: ['update'],
|
||||
eventId: [{ _cnd: { includes: '_' } }],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
displayName: 'Use Default Reminders',
|
||||
name: 'useDefaultReminders',
|
||||
|
|
|
@ -3,13 +3,14 @@ import type {
|
|||
IExecuteFunctions,
|
||||
IHttpRequestMethods,
|
||||
ILoadOptionsFunctions,
|
||||
INode,
|
||||
INodeListSearchItems,
|
||||
INodeListSearchResult,
|
||||
IPollFunctions,
|
||||
IRequestOptions,
|
||||
JsonObject,
|
||||
} from 'n8n-workflow';
|
||||
import { NodeApiError } from 'n8n-workflow';
|
||||
import { NodeApiError, NodeOperationError, sleep } from 'n8n-workflow';
|
||||
|
||||
import moment from 'moment-timezone';
|
||||
import { RRule } from 'rrule';
|
||||
|
@ -52,7 +53,6 @@ export async function googleApiRequestAllItems(
|
|||
propertyName: string,
|
||||
method: IHttpRequestMethods,
|
||||
endpoint: string,
|
||||
|
||||
body: any = {},
|
||||
query: IDataObject = {},
|
||||
): Promise<any> {
|
||||
|
@ -129,24 +129,26 @@ export async function getTimezones(
|
|||
return { results };
|
||||
}
|
||||
|
||||
type RecurentEvent = {
|
||||
export type RecurentEvent = {
|
||||
start: {
|
||||
dateTime: string;
|
||||
timeZone: string;
|
||||
date?: string;
|
||||
dateTime?: string;
|
||||
timeZone?: string;
|
||||
};
|
||||
end: {
|
||||
dateTime: string;
|
||||
timeZone: string;
|
||||
date?: string;
|
||||
dateTime?: string;
|
||||
timeZone?: string;
|
||||
};
|
||||
recurrence: string[];
|
||||
nextOccurrence?: {
|
||||
start: {
|
||||
dateTime: string;
|
||||
timeZone: string;
|
||||
timeZone?: string;
|
||||
};
|
||||
end: {
|
||||
dateTime: string;
|
||||
timeZone: string;
|
||||
timeZone?: string;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
@ -157,30 +159,45 @@ export function addNextOccurrence(items: RecurentEvent[]) {
|
|||
let eventRecurrence;
|
||||
try {
|
||||
eventRecurrence = item.recurrence.find((r) => r.toUpperCase().startsWith('RRULE'));
|
||||
|
||||
if (!eventRecurrence) continue;
|
||||
|
||||
const rrule = RRule.fromString(eventRecurrence);
|
||||
const start = moment(item.start.dateTime || item.end.date).utc();
|
||||
const end = moment(item.end.dateTime || item.end.date).utc();
|
||||
|
||||
const rruleWithStartDate = `DTSTART:${start.format(
|
||||
'YYYYMMDDTHHmmss',
|
||||
)}Z\n${eventRecurrence}`;
|
||||
|
||||
const rrule = RRule.fromString(rruleWithStartDate);
|
||||
|
||||
const until = rrule.options?.until;
|
||||
|
||||
const now = new Date();
|
||||
if (until && until < now) {
|
||||
const now = moment().utc();
|
||||
|
||||
if (until && moment(until).isBefore(now)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const nextOccurrence = rrule.after(new Date());
|
||||
const nextDate = rrule.after(now.toDate(), false);
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
if (nextDate) {
|
||||
const nextStart = moment(nextDate);
|
||||
|
||||
const duration = moment.duration(moment(end).diff(moment(start)));
|
||||
const nextEnd = moment(nextStart).add(duration);
|
||||
|
||||
item.nextOccurrence = {
|
||||
start: {
|
||||
dateTime: nextStart.format(),
|
||||
timeZone: item.start.timeZone,
|
||||
},
|
||||
end: {
|
||||
dateTime: nextEnd.format(),
|
||||
timeZone: item.end.timeZone,
|
||||
},
|
||||
};
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(`Error adding next occurrence ${eventRecurrence}`);
|
||||
}
|
||||
|
@ -195,3 +212,60 @@ export function addTimezoneToDate(date: string, timezone: string) {
|
|||
if (hasTimezone(date)) return date;
|
||||
return moment.tz(date, timezone).utc().format();
|
||||
}
|
||||
|
||||
async function requestWithRetries(
|
||||
node: INode,
|
||||
requestFn: () => Promise<any>,
|
||||
retryCount: number = 0,
|
||||
maxRetries: number = 10,
|
||||
itemIndex: number = 0,
|
||||
): Promise<any> {
|
||||
try {
|
||||
return await requestFn();
|
||||
} catch (error) {
|
||||
if (!(error instanceof NodeApiError)) {
|
||||
throw new NodeOperationError(node, error.message, { itemIndex });
|
||||
}
|
||||
if (retryCount >= maxRetries) throw error;
|
||||
|
||||
if (error.httpCode === '403' || error.httpCode === '429') {
|
||||
const delay = 1000 * Math.pow(2, retryCount);
|
||||
|
||||
console.log(`Rate limit hit. Retrying in ${delay}ms... (Attempt ${retryCount + 1})`);
|
||||
|
||||
await sleep(delay);
|
||||
return await requestWithRetries(node, requestFn, retryCount + 1, maxRetries, itemIndex);
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function googleApiRequestWithRetries({
|
||||
context,
|
||||
method,
|
||||
resource,
|
||||
body = {},
|
||||
qs = {},
|
||||
uri,
|
||||
headers = {},
|
||||
itemIndex = 0,
|
||||
}: {
|
||||
context: IExecuteFunctions | ILoadOptionsFunctions | IPollFunctions;
|
||||
method: IHttpRequestMethods;
|
||||
resource: string;
|
||||
body?: any;
|
||||
qs?: IDataObject;
|
||||
uri?: string;
|
||||
headers?: IDataObject;
|
||||
itemIndex?: number;
|
||||
}) {
|
||||
const requestFn = async (): Promise<any> => {
|
||||
return await googleApiRequest.call(context, method, resource, body, qs, uri, headers);
|
||||
};
|
||||
|
||||
const retryCount = 0;
|
||||
const maxRetries = 10;
|
||||
|
||||
return await requestWithRetries(context.getNode(), requestFn, retryCount, maxRetries, itemIndex);
|
||||
}
|
||||
|
|
|
@ -20,6 +20,8 @@ import {
|
|||
getTimezones,
|
||||
googleApiRequest,
|
||||
googleApiRequestAllItems,
|
||||
googleApiRequestWithRetries,
|
||||
type RecurentEvent,
|
||||
} from './GenericFunctions';
|
||||
|
||||
import { eventFields, eventOperations } from './EventDescription';
|
||||
|
@ -34,7 +36,7 @@ export class GoogleCalendar implements INodeType {
|
|||
name: 'googleCalendar',
|
||||
icon: 'file:googleCalendar.svg',
|
||||
group: ['input'],
|
||||
version: [1, 1.1, 1.2],
|
||||
version: [1, 1.1, 1.2, 1.3],
|
||||
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
||||
description: 'Consume Google Calendar API',
|
||||
defaults: {
|
||||
|
@ -382,16 +384,33 @@ export class GoogleCalendar implements INodeType {
|
|||
if (tz) {
|
||||
qs.timeZone = tz;
|
||||
}
|
||||
responseData = await googleApiRequest.call(
|
||||
responseData = (await googleApiRequest.call(
|
||||
this,
|
||||
'GET',
|
||||
`/calendar/v3/calendars/${calendarId}/events/${eventId}`,
|
||||
{},
|
||||
qs,
|
||||
);
|
||||
)) as IDataObject;
|
||||
|
||||
if (responseData) {
|
||||
responseData = addNextOccurrence([responseData]);
|
||||
if (nodeVersion >= 1.3 && options.returnNextInstance && responseData.recurrence) {
|
||||
const eventInstances =
|
||||
((
|
||||
(await googleApiRequest.call(
|
||||
this,
|
||||
'GET',
|
||||
`/calendar/v3/calendars/${calendarId}/events/${responseData.id}/instances`,
|
||||
{},
|
||||
{
|
||||
timeMin: new Date().toISOString(),
|
||||
maxResults: 1,
|
||||
},
|
||||
)) as IDataObject
|
||||
).items as IDataObject[]) || [];
|
||||
responseData = eventInstances[0] ? [eventInstances[0]] : [responseData];
|
||||
} else {
|
||||
responseData = addNextOccurrence([responseData as RecurentEvent]);
|
||||
}
|
||||
}
|
||||
}
|
||||
//https://developers.google.com/calendar/v3/reference/events/list
|
||||
|
@ -404,6 +423,22 @@ export class GoogleCalendar implements INodeType {
|
|||
const tz = this.getNodeParameter('options.timeZone', i, '', {
|
||||
extractValue: true,
|
||||
}) as string;
|
||||
|
||||
if (nodeVersion >= 1.3) {
|
||||
const timeMin = this.getNodeParameter('timeMin', i) as string;
|
||||
const timeMax = this.getNodeParameter('timeMax', i) as string;
|
||||
if (timeMin) {
|
||||
qs.timeMin = addTimezoneToDate(timeMin as string, tz || timezone);
|
||||
}
|
||||
if (timeMax) {
|
||||
qs.timeMax = addTimezoneToDate(timeMax as string, tz || timezone);
|
||||
}
|
||||
|
||||
if (!options.recurringEventHandling || options.recurringEventHandling === 'expand') {
|
||||
qs.singleEvents = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (options.iCalUID) {
|
||||
qs.iCalUID = options.iCalUID as string;
|
||||
}
|
||||
|
@ -463,7 +498,34 @@ export class GoogleCalendar implements INodeType {
|
|||
}
|
||||
|
||||
if (responseData) {
|
||||
responseData = addNextOccurrence(responseData);
|
||||
if (nodeVersion >= 1.3 && options.recurringEventHandling === 'next') {
|
||||
const updatedEvents: IDataObject[] = [];
|
||||
|
||||
for (const event of responseData) {
|
||||
if (event.recurrence) {
|
||||
const eventInstances =
|
||||
((
|
||||
(await googleApiRequestWithRetries({
|
||||
context: this,
|
||||
method: 'GET',
|
||||
resource: `/calendar/v3/calendars/${calendarId}/events/${event.id}/instances`,
|
||||
qs: {
|
||||
timeMin: new Date().toISOString(),
|
||||
maxResults: 1,
|
||||
},
|
||||
itemIndex: i,
|
||||
})) as IDataObject
|
||||
).items as IDataObject[]) || [];
|
||||
updatedEvents.push(eventInstances[0] || event);
|
||||
continue;
|
||||
}
|
||||
|
||||
updatedEvents.push(event);
|
||||
}
|
||||
responseData = updatedEvents;
|
||||
} else {
|
||||
responseData = addNextOccurrence(responseData);
|
||||
}
|
||||
}
|
||||
}
|
||||
//https://developers.google.com/calendar/v3/reference/events/patch
|
||||
|
@ -471,7 +533,22 @@ export class GoogleCalendar implements INodeType {
|
|||
const calendarId = encodeURIComponentOnce(
|
||||
this.getNodeParameter('calendar', i, '', { extractValue: true }) as string,
|
||||
);
|
||||
const eventId = this.getNodeParameter('eventId', i) as string;
|
||||
let eventId = this.getNodeParameter('eventId', i) as string;
|
||||
|
||||
if (nodeVersion >= 1.3) {
|
||||
const modifyTarget = this.getNodeParameter('modifyTarget', i, 'instance') as string;
|
||||
if (modifyTarget === 'event') {
|
||||
const instance = (await googleApiRequest.call(
|
||||
this,
|
||||
'GET',
|
||||
`/calendar/v3/calendars/${calendarId}/events/${eventId}`,
|
||||
{},
|
||||
qs,
|
||||
)) as IDataObject;
|
||||
eventId = instance.recurringEventId as string;
|
||||
}
|
||||
}
|
||||
|
||||
const useDefaultReminders = this.getNodeParameter('useDefaultReminders', i) as boolean;
|
||||
const updateFields = this.getNodeParameter('updateFields', i);
|
||||
let updateTimezone = updateFields.timezone as string;
|
||||
|
|
|
@ -0,0 +1,88 @@
|
|||
import moment from 'moment-timezone';
|
||||
import type { RecurentEvent } from '../GenericFunctions';
|
||||
import { addNextOccurrence } from '../GenericFunctions';
|
||||
|
||||
const mockNow = '2024-09-06T16:30:00+03:00';
|
||||
jest.spyOn(global.Date, 'now').mockImplementation(() => moment(mockNow).valueOf());
|
||||
|
||||
describe('addNextOccurrence', () => {
|
||||
it('should not modify event if no recurrence exists', () => {
|
||||
const event: RecurentEvent[] = [
|
||||
{
|
||||
start: {
|
||||
dateTime: '2024-09-01T08:00:00Z',
|
||||
timeZone: 'UTC',
|
||||
},
|
||||
end: {
|
||||
dateTime: '2024-09-01T09:00:00Z',
|
||||
timeZone: 'UTC',
|
||||
},
|
||||
recurrence: [],
|
||||
},
|
||||
];
|
||||
|
||||
const result = addNextOccurrence(event);
|
||||
|
||||
expect(result[0].nextOccurrence).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should handle event with no RRULE correctly', () => {
|
||||
const event: RecurentEvent[] = [
|
||||
{
|
||||
start: {
|
||||
dateTime: '2024-09-01T08:00:00Z',
|
||||
timeZone: 'UTC',
|
||||
},
|
||||
end: {
|
||||
dateTime: '2024-09-01T09:00:00Z',
|
||||
timeZone: 'UTC',
|
||||
},
|
||||
recurrence: ['FREQ=WEEKLY;COUNT=2'],
|
||||
},
|
||||
];
|
||||
|
||||
const result = addNextOccurrence(event);
|
||||
|
||||
expect(result[0].nextOccurrence).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should ignore recurrence if until date is in the past', () => {
|
||||
const event: RecurentEvent[] = [
|
||||
{
|
||||
start: {
|
||||
dateTime: '2024-08-01T08:00:00Z',
|
||||
timeZone: 'UTC',
|
||||
},
|
||||
end: {
|
||||
dateTime: '2024-08-01T09:00:00Z',
|
||||
timeZone: 'UTC',
|
||||
},
|
||||
recurrence: ['RRULE:FREQ=DAILY;UNTIL=20240805T000000Z'],
|
||||
},
|
||||
];
|
||||
|
||||
const result = addNextOccurrence(event);
|
||||
|
||||
expect(result[0].nextOccurrence).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should handle errors gracefully without breaking and return unchenged event', () => {
|
||||
const event: RecurentEvent[] = [
|
||||
{
|
||||
start: {
|
||||
dateTime: '2024-09-06T17:30:00+03:00',
|
||||
timeZone: 'Europe/Berlin',
|
||||
},
|
||||
end: {
|
||||
dateTime: '2024-09-06T18:00:00+03:00',
|
||||
timeZone: 'Europe/Berlin',
|
||||
},
|
||||
recurrence: ['xxxxx'],
|
||||
},
|
||||
];
|
||||
|
||||
const result = addNextOccurrence(event);
|
||||
|
||||
expect(result).toEqual(event);
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue