2020-06-17 14:46:34 -07:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
} from 'n8n-core';
|
2020-03-13 18:22:36 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodeExecutionData,
|
2020-06-17 14:46:34 -07:00
|
|
|
INodePropertyOptions,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
2022-03-13 01:40:02 -08:00
|
|
|
JsonObject,
|
2021-04-16 09:33:36 -07:00
|
|
|
NodeApiError,
|
|
|
|
NodeOperationError,
|
2020-03-13 18:22:36 -07:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2020-06-17 14:46:34 -07:00
|
|
|
import {
|
|
|
|
googleApiRequest,
|
|
|
|
googleApiRequestAllItems,
|
|
|
|
} from './GenericFunctions';
|
2020-03-13 18:22:36 -07:00
|
|
|
|
2020-06-17 14:46:34 -07:00
|
|
|
import {
|
|
|
|
eventFields,
|
2020-10-01 05:01:39 -07:00
|
|
|
eventOperations,
|
2020-06-17 14:46:34 -07:00
|
|
|
} from './EventDescription';
|
2020-03-13 18:22:36 -07:00
|
|
|
|
2020-10-30 01:31:27 -07:00
|
|
|
import {
|
2020-10-30 01:32:35 -07:00
|
|
|
calendarFields,
|
|
|
|
calendarOperations,
|
|
|
|
} from './CalendarDescription';
|
2020-10-30 01:31:27 -07:00
|
|
|
|
2020-06-17 14:46:34 -07:00
|
|
|
import {
|
|
|
|
IEvent,
|
|
|
|
} from './EventInterface';
|
2020-03-13 18:22:36 -07:00
|
|
|
|
2022-04-08 14:32:08 -07:00
|
|
|
import moment from 'moment-timezone';
|
2020-03-13 18:22:36 -07:00
|
|
|
|
2021-06-13 10:17:39 -07:00
|
|
|
import { v4 as uuid } from 'uuid';
|
2020-10-22 23:00:00 -07:00
|
|
|
|
2020-03-13 18:22:36 -07:00
|
|
|
export class GoogleCalendar implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Google Calendar',
|
|
|
|
name: 'googleCalendar',
|
2021-02-21 13:04:00 -08:00
|
|
|
icon: 'file:googleCalendar.svg',
|
2020-03-13 18:22:36 -07:00
|
|
|
group: ['input'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
2021-07-03 05:40:16 -07:00
|
|
|
description: 'Consume Google Calendar API',
|
2020-03-13 18:22:36 -07:00
|
|
|
defaults: {
|
|
|
|
name: 'Google Calendar',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
2020-05-30 12:09:04 -07:00
|
|
|
name: 'googleCalendarOAuth2Api',
|
2020-06-17 14:46:34 -07:00
|
|
|
required: true,
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2020-03-13 18:22:36 -07:00
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-03-13 01:40:02 -08:00
|
|
|
noDataExpression: true,
|
2020-03-13 18:22:36 -07:00
|
|
|
options: [
|
|
|
|
{
|
2020-10-30 01:32:35 -07:00
|
|
|
name: 'Calendar',
|
|
|
|
value: 'calendar',
|
2020-06-17 14:46:34 -07:00
|
|
|
},
|
2020-10-30 01:31:27 -07:00
|
|
|
{
|
2020-10-30 01:32:35 -07:00
|
|
|
name: 'Event',
|
|
|
|
value: 'event',
|
2020-10-30 01:31:27 -07:00
|
|
|
},
|
2020-03-13 18:22:36 -07:00
|
|
|
],
|
|
|
|
default: 'event',
|
|
|
|
},
|
2020-10-30 01:32:35 -07:00
|
|
|
...calendarOperations,
|
|
|
|
...calendarFields,
|
2020-03-13 18:22:36 -07:00
|
|
|
...eventOperations,
|
2020-06-17 14:46:34 -07:00
|
|
|
...eventFields,
|
|
|
|
],
|
2020-03-13 18:22:36 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
2020-10-22 23:00:00 -07:00
|
|
|
// Get all the calendars to display them to user so that he can
|
|
|
|
// select them easily
|
|
|
|
async getConferenceSolutations(
|
2020-10-22 23:05:24 -07:00
|
|
|
this: ILoadOptionsFunctions,
|
2020-10-22 23:00:00 -07:00
|
|
|
): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const calendar = this.getCurrentNodeParameter('calendar') as string;
|
|
|
|
const posibleSolutions: IDataObject = {
|
|
|
|
'eventHangout': 'Google Hangout',
|
|
|
|
'eventNamedHangout': 'Google Hangout Classic',
|
|
|
|
'hangoutsMeet': 'Google Meet',
|
|
|
|
};
|
|
|
|
const { conferenceProperties: { allowedConferenceSolutionTypes } } = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/calendar/v3/users/me/calendarList/${calendar}`,
|
|
|
|
);
|
|
|
|
for (const solution of allowedConferenceSolutionTypes) {
|
|
|
|
returnData.push({
|
|
|
|
name: posibleSolutions[solution] as string,
|
|
|
|
value: solution,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
2020-03-13 18:22:36 -07:00
|
|
|
// Get all the calendars to display them to user so that he can
|
|
|
|
// select them easily
|
2020-06-15 15:47:44 -07:00
|
|
|
async getCalendars(
|
2020-10-22 09:00:28 -07:00
|
|
|
this: ILoadOptionsFunctions,
|
2020-06-15 15:47:44 -07:00
|
|
|
): Promise<INodePropertyOptions[]> {
|
2020-03-13 18:22:36 -07:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2020-06-15 15:47:44 -07:00
|
|
|
const calendars = await googleApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'items',
|
|
|
|
'GET',
|
2020-10-22 09:00:28 -07:00
|
|
|
'/calendar/v3/users/me/calendarList',
|
2020-06-15 15:47:44 -07:00
|
|
|
);
|
2020-03-13 18:22:36 -07:00
|
|
|
for (const calendar of calendars) {
|
|
|
|
const calendarName = calendar.summary;
|
2022-05-14 01:44:19 -07:00
|
|
|
const calendarId = encodeURIComponent(calendar.id);
|
2020-03-13 18:22:36 -07:00
|
|
|
returnData.push({
|
|
|
|
name: calendarName,
|
2020-10-22 06:46:03 -07:00
|
|
|
value: calendarId,
|
2020-03-13 18:22:36 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
// Get all the colors to display them to user so that he can
|
|
|
|
// select them easily
|
2020-06-15 15:47:44 -07:00
|
|
|
async getColors(
|
2020-10-22 09:00:28 -07:00
|
|
|
this: ILoadOptionsFunctions,
|
2020-06-15 15:47:44 -07:00
|
|
|
): Promise<INodePropertyOptions[]> {
|
2020-03-13 18:22:36 -07:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2020-06-20 08:51:11 -07:00
|
|
|
const { event } = await googleApiRequest.call(
|
2020-06-15 15:47:44 -07:00
|
|
|
this,
|
|
|
|
'GET',
|
2020-10-22 09:00:28 -07:00
|
|
|
'/calendar/v3/colors',
|
2020-06-15 15:47:44 -07:00
|
|
|
);
|
2020-06-20 08:51:11 -07:00
|
|
|
for (const key of Object.keys(event)) {
|
|
|
|
const colorName = `Background: ${event[key].background} - Foreground: ${event[key].foreground}`;
|
2020-03-13 18:22:36 -07:00
|
|
|
const colorId = key;
|
|
|
|
returnData.push({
|
2020-06-20 08:51:11 -07:00
|
|
|
name: `${colorName}`,
|
2020-10-22 06:46:03 -07:00
|
|
|
value: colorId,
|
2020-03-13 18:22:36 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
// Get all the timezones to display them to user so that he can
|
|
|
|
// select them easily
|
2020-06-15 15:47:44 -07:00
|
|
|
async getTimezones(
|
2020-10-22 09:00:28 -07:00
|
|
|
this: ILoadOptionsFunctions,
|
2020-06-15 15:47:44 -07:00
|
|
|
): Promise<INodePropertyOptions[]> {
|
2020-03-13 18:22:36 -07:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
for (const timezone of moment.tz.names()) {
|
|
|
|
const timezoneName = timezone;
|
|
|
|
const timezoneId = timezone;
|
|
|
|
returnData.push({
|
|
|
|
name: timezoneName,
|
2020-10-22 06:46:03 -07:00
|
|
|
value: timezoneId,
|
2020-03-13 18:22:36 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
|
|
|
},
|
2020-03-13 18:22:36 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
|
|
|
const returnData: IDataObject[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2020-03-13 18:22:36 -07:00
|
|
|
const qs: IDataObject = {};
|
|
|
|
let responseData;
|
|
|
|
const resource = this.getNodeParameter('resource', 0) as string;
|
|
|
|
const operation = this.getNodeParameter('operation', 0) as string;
|
2022-03-13 04:00:12 -07:00
|
|
|
const timezone = this.getTimezone();
|
2020-03-13 18:22:36 -07:00
|
|
|
for (let i = 0; i < length; i++) {
|
2021-05-01 20:50:15 -07:00
|
|
|
try {
|
|
|
|
if (resource === 'calendar') {
|
|
|
|
//https://developers.google.com/calendar/v3/reference/freebusy/query
|
|
|
|
if (operation === 'availability') {
|
|
|
|
const calendarId = this.getNodeParameter('calendar', i) as string;
|
|
|
|
const timeMin = this.getNodeParameter('timeMin', i) as string;
|
|
|
|
const timeMax = this.getNodeParameter('timeMax', i) as string;
|
|
|
|
const options = this.getNodeParameter('options', i) as IDataObject;
|
|
|
|
const outputFormat = options.outputFormat || 'availability';
|
2020-10-30 01:32:35 -07:00
|
|
|
|
2021-05-01 20:50:15 -07:00
|
|
|
const body: IDataObject = {
|
2022-03-13 04:00:12 -07:00
|
|
|
timeMin: moment(timeMin).utc().format(),
|
|
|
|
timeMax: moment(timeMax).utc().format(),
|
2021-05-01 20:50:15 -07:00
|
|
|
items: [
|
|
|
|
{
|
|
|
|
id: calendarId,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
timeZone: options.timezone || timezone,
|
|
|
|
};
|
2020-10-30 01:32:35 -07:00
|
|
|
|
2021-05-01 20:50:15 -07:00
|
|
|
responseData = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
`/calendar/v3/freeBusy`,
|
|
|
|
body,
|
|
|
|
{},
|
|
|
|
);
|
2020-10-30 01:32:35 -07:00
|
|
|
|
2021-05-01 20:50:15 -07:00
|
|
|
if (responseData.calendars[calendarId].errors) {
|
|
|
|
throw new NodeApiError(this.getNode(), responseData.calendars[calendarId]);
|
|
|
|
}
|
2020-10-30 01:32:35 -07:00
|
|
|
|
2021-05-01 20:50:15 -07:00
|
|
|
if (outputFormat === 'availability') {
|
|
|
|
responseData = {
|
|
|
|
available: !responseData.calendars[calendarId].busy.length,
|
|
|
|
};
|
2020-10-30 01:32:35 -07:00
|
|
|
|
2021-05-01 20:50:15 -07:00
|
|
|
} else if (outputFormat === 'bookedSlots') {
|
|
|
|
responseData = responseData.calendars[calendarId].busy;
|
|
|
|
}
|
2020-10-30 01:32:35 -07:00
|
|
|
}
|
|
|
|
}
|
2021-05-01 20:50:15 -07:00
|
|
|
if (resource === 'event') {
|
|
|
|
//https://developers.google.com/calendar/v3/reference/events/insert
|
|
|
|
if (operation === 'create') {
|
|
|
|
const calendarId = this.getNodeParameter('calendar', i) as string;
|
|
|
|
const start = this.getNodeParameter('start', i) as string;
|
|
|
|
const end = this.getNodeParameter('end', i) as string;
|
|
|
|
const useDefaultReminders = this.getNodeParameter(
|
|
|
|
'useDefaultReminders',
|
2020-10-22 09:00:28 -07:00
|
|
|
i,
|
2021-05-01 20:50:15 -07:00
|
|
|
) as boolean;
|
|
|
|
const additionalFields = this.getNodeParameter(
|
|
|
|
'additionalFields',
|
|
|
|
i,
|
|
|
|
) as IDataObject;
|
2022-03-13 01:40:02 -08:00
|
|
|
|
2021-05-01 20:50:15 -07:00
|
|
|
if (additionalFields.maxAttendees) {
|
|
|
|
qs.maxAttendees = additionalFields.maxAttendees as number;
|
2020-03-13 18:22:36 -07:00
|
|
|
}
|
2021-05-01 20:50:15 -07:00
|
|
|
if (additionalFields.sendNotifications) {
|
|
|
|
qs.sendNotifications = additionalFields.sendNotifications as boolean;
|
|
|
|
}
|
|
|
|
if (additionalFields.sendUpdates) {
|
|
|
|
qs.sendUpdates = additionalFields.sendUpdates as string;
|
|
|
|
}
|
|
|
|
const body: IEvent = {
|
|
|
|
start: {
|
2022-03-13 04:00:12 -07:00
|
|
|
dateTime: moment.tz(start, timezone).utc().format(),
|
|
|
|
timeZone: timezone,
|
2021-05-01 20:50:15 -07:00
|
|
|
},
|
|
|
|
end: {
|
2022-03-13 04:00:12 -07:00
|
|
|
dateTime: moment.tz(end, timezone).utc().format(),
|
|
|
|
timeZone: timezone,
|
2021-05-01 20:50:15 -07:00
|
|
|
},
|
2020-03-13 18:22:36 -07:00
|
|
|
};
|
2021-05-01 20:50:15 -07:00
|
|
|
if (additionalFields.attendees) {
|
|
|
|
body.attendees = [];
|
|
|
|
(additionalFields.attendees as string[]).forEach(attendee => {
|
|
|
|
body.attendees!.push.apply(body.attendees, attendee.split(',').map(a => a.trim()).map(email => ({ email })));
|
|
|
|
});
|
2021-02-21 13:04:00 -08:00
|
|
|
}
|
2021-05-01 20:50:15 -07:00
|
|
|
if (additionalFields.color) {
|
|
|
|
body.colorId = additionalFields.color as string;
|
2021-02-21 13:04:00 -08:00
|
|
|
}
|
2021-05-01 20:50:15 -07:00
|
|
|
if (additionalFields.description) {
|
|
|
|
body.description = additionalFields.description as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.guestsCanInviteOthers) {
|
|
|
|
body.guestsCanInviteOthers = additionalFields.guestsCanInviteOthers as boolean;
|
|
|
|
}
|
|
|
|
if (additionalFields.guestsCanModify) {
|
|
|
|
body.guestsCanModify = additionalFields.guestsCanModify as boolean;
|
|
|
|
}
|
|
|
|
if (additionalFields.guestsCanSeeOtherGuests) {
|
|
|
|
body.guestsCanSeeOtherGuests = additionalFields.guestsCanSeeOtherGuests as boolean;
|
|
|
|
}
|
|
|
|
if (additionalFields.id) {
|
|
|
|
body.id = additionalFields.id as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.location) {
|
|
|
|
body.location = additionalFields.location as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.summary) {
|
|
|
|
body.summary = additionalFields.summary as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.showMeAs) {
|
|
|
|
body.transparency = additionalFields.showMeAs as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.visibility) {
|
|
|
|
body.visibility = additionalFields.visibility as string;
|
2021-02-21 13:04:00 -08:00
|
|
|
}
|
2021-05-01 20:50:15 -07:00
|
|
|
if (!useDefaultReminders) {
|
|
|
|
const reminders = (this.getNodeParameter(
|
|
|
|
'remindersUi',
|
|
|
|
i,
|
|
|
|
) as IDataObject).remindersValues as IDataObject[];
|
|
|
|
body.reminders = {
|
|
|
|
useDefault: false,
|
|
|
|
};
|
|
|
|
if (reminders) {
|
|
|
|
body.reminders.overrides = reminders;
|
|
|
|
}
|
|
|
|
}
|
2022-03-13 01:40:02 -08:00
|
|
|
|
2021-05-01 20:50:15 -07:00
|
|
|
if (additionalFields.allday) {
|
|
|
|
body.start = {
|
2022-03-13 01:40:02 -08:00
|
|
|
date: timezone ?
|
|
|
|
moment.tz(start, timezone).utc(true).format('YYYY-MM-DD') :
|
|
|
|
moment.tz(start, moment.tz.guess()).utc(true).format('YYYY-MM-DD'),
|
2021-05-01 20:50:15 -07:00
|
|
|
};
|
|
|
|
body.end = {
|
2022-03-13 01:40:02 -08:00
|
|
|
date: timezone ?
|
|
|
|
moment.tz(end, timezone).utc(true).format('YYYY-MM-DD') :
|
|
|
|
moment.tz(end, moment.tz.guess()).utc(true).format('YYYY-MM-DD'),
|
2021-05-01 20:50:15 -07:00
|
|
|
};
|
2021-02-21 13:04:00 -08:00
|
|
|
}
|
2021-05-01 20:50:15 -07:00
|
|
|
//exampel: RRULE:FREQ=WEEKLY;INTERVAL=2;COUNT=10;UNTIL=20110701T170000Z
|
|
|
|
//https://icalendar.org/iCalendar-RFC-5545/3-8-5-3-recurrence-rule.html
|
|
|
|
body.recurrence = [];
|
|
|
|
if (additionalFields.rrule) {
|
|
|
|
body.recurrence = [`RRULE:${additionalFields.rrule}`];
|
|
|
|
} else {
|
|
|
|
if (
|
|
|
|
additionalFields.repeatHowManyTimes &&
|
|
|
|
additionalFields.repeatUntil
|
|
|
|
) {
|
|
|
|
throw new NodeOperationError(this.getNode(),
|
|
|
|
`You can set either 'Repeat How Many Times' or 'Repeat Until' but not both`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (additionalFields.repeatFrecuency) {
|
|
|
|
body.recurrence?.push(
|
|
|
|
`FREQ=${(additionalFields.repeatFrecuency as string).toUpperCase()};`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (additionalFields.repeatHowManyTimes) {
|
|
|
|
body.recurrence?.push(
|
|
|
|
`COUNT=${additionalFields.repeatHowManyTimes};`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (additionalFields.repeatUntil) {
|
2022-03-13 04:00:12 -07:00
|
|
|
const repeatUntil = moment(additionalFields.repeatUntil as string)
|
|
|
|
.utc()
|
|
|
|
.format('YYYYMMDDTHHmmss');
|
2021-05-01 20:50:15 -07:00
|
|
|
body.recurrence?.push(
|
2022-03-13 01:40:02 -08:00
|
|
|
`UNTIL=${repeatUntil}Z`,
|
2021-05-01 20:50:15 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (body.recurrence.length !== 0) {
|
|
|
|
body.recurrence = [`RRULE:${body.recurrence.join('')}`];
|
|
|
|
}
|
2021-02-21 13:04:00 -08:00
|
|
|
}
|
2020-10-22 23:00:00 -07:00
|
|
|
|
2021-05-01 20:50:15 -07:00
|
|
|
if (additionalFields.conferenceDataUi) {
|
|
|
|
const conferenceData = (additionalFields.conferenceDataUi as IDataObject).conferenceDataValues as IDataObject;
|
|
|
|
if (conferenceData) {
|
2020-10-22 23:00:00 -07:00
|
|
|
|
2021-05-01 20:50:15 -07:00
|
|
|
qs.conferenceDataVersion = 1;
|
|
|
|
body.conferenceData = {
|
|
|
|
createRequest: {
|
|
|
|
requestId: uuid(),
|
|
|
|
conferenceSolution: {
|
|
|
|
type: conferenceData.conferenceSolution as string,
|
|
|
|
},
|
2020-10-22 23:05:24 -07:00
|
|
|
},
|
2021-05-01 20:50:15 -07:00
|
|
|
};
|
|
|
|
}
|
2020-10-22 23:00:00 -07:00
|
|
|
}
|
2021-05-01 20:50:15 -07:00
|
|
|
responseData = await googleApiRequest.call(
|
2020-06-15 15:47:44 -07:00
|
|
|
this,
|
2021-05-01 20:50:15 -07:00
|
|
|
'POST',
|
2020-06-15 15:47:44 -07:00
|
|
|
`/calendar/v3/calendars/${calendarId}/events`,
|
2021-05-01 20:50:15 -07:00
|
|
|
body,
|
2020-10-22 09:00:28 -07:00
|
|
|
qs,
|
2020-06-15 15:47:44 -07:00
|
|
|
);
|
2021-05-01 20:50:15 -07:00
|
|
|
}
|
|
|
|
//https://developers.google.com/calendar/v3/reference/events/delete
|
|
|
|
if (operation === 'delete') {
|
|
|
|
const calendarId = this.getNodeParameter('calendar', i) as string;
|
|
|
|
const eventId = this.getNodeParameter('eventId', i) as string;
|
|
|
|
const options = this.getNodeParameter('options', i) as IDataObject;
|
|
|
|
if (options.sendUpdates) {
|
|
|
|
qs.sendUpdates = options.sendUpdates as number;
|
|
|
|
}
|
|
|
|
responseData = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'DELETE',
|
|
|
|
`/calendar/v3/calendars/${calendarId}/events/${eventId}`,
|
|
|
|
{},
|
|
|
|
);
|
|
|
|
responseData = { success: true };
|
|
|
|
}
|
|
|
|
//https://developers.google.com/calendar/v3/reference/events/get
|
|
|
|
if (operation === 'get') {
|
|
|
|
const calendarId = this.getNodeParameter('calendar', i) as string;
|
|
|
|
const eventId = this.getNodeParameter('eventId', i) as string;
|
|
|
|
const options = this.getNodeParameter('options', i) as IDataObject;
|
|
|
|
if (options.maxAttendees) {
|
|
|
|
qs.maxAttendees = options.maxAttendees as number;
|
|
|
|
}
|
|
|
|
if (options.timeZone) {
|
|
|
|
qs.timeZone = options.timeZone as string;
|
|
|
|
}
|
2020-06-15 15:47:44 -07:00
|
|
|
responseData = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
2021-05-01 20:50:15 -07:00
|
|
|
`/calendar/v3/calendars/${calendarId}/events/${eventId}`,
|
2020-06-15 15:47:44 -07:00
|
|
|
{},
|
2020-10-22 09:00:28 -07:00
|
|
|
qs,
|
2020-06-15 15:47:44 -07:00
|
|
|
);
|
2020-03-13 18:22:36 -07:00
|
|
|
}
|
2021-05-01 20:50:15 -07:00
|
|
|
//https://developers.google.com/calendar/v3/reference/events/list
|
|
|
|
if (operation === 'getAll') {
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
|
|
const calendarId = this.getNodeParameter('calendar', i) as string;
|
|
|
|
const options = this.getNodeParameter('options', i) as IDataObject;
|
|
|
|
if (options.iCalUID) {
|
|
|
|
qs.iCalUID = options.iCalUID as string;
|
|
|
|
}
|
|
|
|
if (options.maxAttendees) {
|
|
|
|
qs.maxAttendees = options.maxAttendees as number;
|
|
|
|
}
|
|
|
|
if (options.orderBy) {
|
|
|
|
qs.orderBy = options.orderBy as number;
|
|
|
|
}
|
|
|
|
if (options.query) {
|
|
|
|
qs.q = options.query as number;
|
|
|
|
}
|
|
|
|
if (options.showDeleted) {
|
|
|
|
qs.showDeleted = options.showDeleted as boolean;
|
|
|
|
}
|
|
|
|
if (options.showHiddenInvitations) {
|
|
|
|
qs.showHiddenInvitations = options.showHiddenInvitations as boolean;
|
|
|
|
}
|
|
|
|
if (options.singleEvents) {
|
|
|
|
qs.singleEvents = options.singleEvents as boolean;
|
|
|
|
}
|
|
|
|
if (options.timeMax) {
|
|
|
|
qs.timeMax = options.timeMax as string;
|
|
|
|
}
|
|
|
|
if (options.timeMin) {
|
|
|
|
qs.timeMin = options.timeMin as string;
|
|
|
|
}
|
|
|
|
if (options.timeZone) {
|
|
|
|
qs.timeZone = options.timeZone as string;
|
|
|
|
}
|
|
|
|
if (options.updatedMin) {
|
|
|
|
qs.updatedMin = options.updatedMin as string;
|
|
|
|
}
|
|
|
|
if (returnAll) {
|
|
|
|
responseData = await googleApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'items',
|
|
|
|
'GET',
|
|
|
|
`/calendar/v3/calendars/${calendarId}/events`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
qs.maxResults = this.getNodeParameter('limit', i) as number;
|
|
|
|
responseData = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/calendar/v3/calendars/${calendarId}/events`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
responseData = responseData.items;
|
|
|
|
}
|
2020-03-13 18:22:36 -07:00
|
|
|
}
|
2021-05-01 20:50:15 -07:00
|
|
|
//https://developers.google.com/calendar/v3/reference/events/patch
|
|
|
|
if (operation === 'update') {
|
|
|
|
const calendarId = this.getNodeParameter('calendar', i) as string;
|
|
|
|
const eventId = this.getNodeParameter('eventId', i) as string;
|
|
|
|
const useDefaultReminders = this.getNodeParameter(
|
|
|
|
'useDefaultReminders',
|
2020-10-22 09:00:28 -07:00
|
|
|
i,
|
2021-05-01 20:50:15 -07:00
|
|
|
) as boolean;
|
|
|
|
const updateFields = this.getNodeParameter(
|
|
|
|
'updateFields',
|
|
|
|
i,
|
|
|
|
) as IDataObject;
|
2022-03-13 01:40:02 -08:00
|
|
|
const timezone = (updateFields.timezone as string);
|
|
|
|
|
2021-05-01 20:50:15 -07:00
|
|
|
if (updateFields.maxAttendees) {
|
|
|
|
qs.maxAttendees = updateFields.maxAttendees as number;
|
2020-03-13 18:22:36 -07:00
|
|
|
}
|
2021-05-01 20:50:15 -07:00
|
|
|
if (updateFields.sendNotifications) {
|
|
|
|
qs.sendNotifications = updateFields.sendNotifications as boolean;
|
2021-02-21 13:04:00 -08:00
|
|
|
}
|
2021-05-01 20:50:15 -07:00
|
|
|
if (updateFields.sendUpdates) {
|
|
|
|
qs.sendUpdates = updateFields.sendUpdates as string;
|
|
|
|
}
|
|
|
|
const body: IEvent = {};
|
|
|
|
if (updateFields.start) {
|
|
|
|
body.start = {
|
2022-03-13 04:00:12 -07:00
|
|
|
dateTime: moment.tz(updateFields.start, timezone).utc().format(),
|
|
|
|
timeZone: timezone,
|
2021-05-01 20:50:15 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
if (updateFields.end) {
|
|
|
|
body.end = {
|
2022-03-13 04:00:12 -07:00
|
|
|
dateTime: moment.tz(updateFields.end, timezone).utc().format(),
|
|
|
|
timeZone: timezone,
|
2021-05-01 20:50:15 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
if (updateFields.attendees) {
|
|
|
|
body.attendees = [];
|
|
|
|
(updateFields.attendees as string[]).forEach(attendee => {
|
|
|
|
body.attendees!.push.apply(body.attendees, attendee.split(',').map(a => a.trim()).map(email => ({ email })));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (updateFields.color) {
|
|
|
|
body.colorId = updateFields.color as string;
|
2021-02-21 13:04:00 -08:00
|
|
|
}
|
2021-05-01 20:50:15 -07:00
|
|
|
if (updateFields.description) {
|
|
|
|
body.description = updateFields.description as string;
|
2021-02-21 13:04:00 -08:00
|
|
|
}
|
2021-05-01 20:50:15 -07:00
|
|
|
if (updateFields.guestsCanInviteOthers) {
|
|
|
|
body.guestsCanInviteOthers = updateFields.guestsCanInviteOthers as boolean;
|
|
|
|
}
|
|
|
|
if (updateFields.guestsCanModify) {
|
|
|
|
body.guestsCanModify = updateFields.guestsCanModify as boolean;
|
|
|
|
}
|
|
|
|
if (updateFields.guestsCanSeeOtherGuests) {
|
|
|
|
body.guestsCanSeeOtherGuests = updateFields.guestsCanSeeOtherGuests as boolean;
|
|
|
|
}
|
|
|
|
if (updateFields.id) {
|
|
|
|
body.id = updateFields.id as string;
|
|
|
|
}
|
|
|
|
if (updateFields.location) {
|
|
|
|
body.location = updateFields.location as string;
|
|
|
|
}
|
|
|
|
if (updateFields.summary) {
|
|
|
|
body.summary = updateFields.summary as string;
|
|
|
|
}
|
|
|
|
if (updateFields.showMeAs) {
|
|
|
|
body.transparency = updateFields.showMeAs as string;
|
|
|
|
}
|
|
|
|
if (updateFields.visibility) {
|
|
|
|
body.visibility = updateFields.visibility as string;
|
|
|
|
}
|
|
|
|
if (!useDefaultReminders) {
|
|
|
|
const reminders = (this.getNodeParameter(
|
|
|
|
'remindersUi',
|
|
|
|
i,
|
|
|
|
) as IDataObject).remindersValues as IDataObject[];
|
|
|
|
body.reminders = {
|
|
|
|
useDefault: false,
|
|
|
|
};
|
|
|
|
if (reminders) {
|
|
|
|
body.reminders.overrides = reminders;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (updateFields.allday && updateFields.start && updateFields.end) {
|
|
|
|
body.start = {
|
2022-03-13 01:40:02 -08:00
|
|
|
date: timezone ?
|
|
|
|
moment.tz(updateFields.start, timezone).utc(true).format('YYYY-MM-DD') :
|
|
|
|
moment.tz(updateFields.start, moment.tz.guess()).utc(true).format('YYYY-MM-DD'),
|
2021-05-01 20:50:15 -07:00
|
|
|
};
|
|
|
|
body.end = {
|
2022-03-13 01:40:02 -08:00
|
|
|
date: timezone ?
|
|
|
|
moment.tz(updateFields.end, timezone).utc(true).format('YYYY-MM-DD') :
|
|
|
|
moment.tz(updateFields.end, moment.tz.guess()).utc(true).format('YYYY-MM-DD'),
|
2021-05-01 20:50:15 -07:00
|
|
|
};
|
2021-02-21 13:04:00 -08:00
|
|
|
}
|
2021-05-01 20:50:15 -07:00
|
|
|
//exampel: RRULE:FREQ=WEEKLY;INTERVAL=2;COUNT=10;UNTIL=20110701T170000Z
|
|
|
|
//https://icalendar.org/iCalendar-RFC-5545/3-8-5-3-recurrence-rule.html
|
|
|
|
body.recurrence = [];
|
|
|
|
if (updateFields.rrule) {
|
|
|
|
body.recurrence = [`RRULE:${updateFields.rrule}`];
|
2021-02-21 13:04:00 -08:00
|
|
|
} else {
|
2021-05-01 20:50:15 -07:00
|
|
|
if (updateFields.repeatHowManyTimes && updateFields.repeatUntil) {
|
|
|
|
throw new NodeOperationError(this.getNode(),
|
|
|
|
`You can set either 'Repeat How Many Times' or 'Repeat Until' but not both`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (updateFields.repeatFrecuency) {
|
|
|
|
body.recurrence?.push(
|
|
|
|
`FREQ=${(updateFields.repeatFrecuency as string).toUpperCase()};`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (updateFields.repeatHowManyTimes) {
|
|
|
|
body.recurrence?.push(`COUNT=${updateFields.repeatHowManyTimes};`);
|
|
|
|
}
|
|
|
|
if (updateFields.repeatUntil) {
|
2022-03-13 04:00:12 -07:00
|
|
|
const repeatUntil = moment(updateFields.repeatUntil as string)
|
|
|
|
.utc()
|
|
|
|
.format('YYYYMMDDTHHmmss');
|
2022-03-13 01:40:02 -08:00
|
|
|
|
2021-05-01 20:50:15 -07:00
|
|
|
body.recurrence?.push(
|
2022-03-13 01:40:02 -08:00
|
|
|
`UNTIL=${repeatUntil}Z`,
|
2021-05-01 20:50:15 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (body.recurrence.length !== 0) {
|
|
|
|
body.recurrence = [`RRULE:${body.recurrence.join('')}`];
|
|
|
|
} else {
|
|
|
|
delete body.recurrence;
|
|
|
|
}
|
2021-02-21 13:04:00 -08:00
|
|
|
}
|
2021-05-01 20:50:15 -07:00
|
|
|
responseData = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'PATCH',
|
|
|
|
`/calendar/v3/calendars/${calendarId}/events/${eventId}`,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
);
|
2020-03-13 18:22:36 -07:00
|
|
|
}
|
2021-05-01 20:50:15 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
|
|
|
|
if (Array.isArray(responseData)) {
|
|
|
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
|
|
|
} else if (responseData !== undefined) {
|
|
|
|
returnData.push(responseData as IDataObject);
|
|
|
|
}
|
2021-05-01 20:50:15 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail() !== true) {
|
|
|
|
throw error;
|
|
|
|
} else {
|
|
|
|
// Return the actual reason as error
|
|
|
|
returnData.push(
|
|
|
|
{
|
2022-03-13 01:40:02 -08:00
|
|
|
error: (error as JsonObject).message,
|
2021-05-01 20:50:15 -07:00
|
|
|
},
|
2020-06-15 15:47:44 -07:00
|
|
|
);
|
2021-05-01 20:50:15 -07:00
|
|
|
continue;
|
2020-03-13 18:22:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
|
|
}
|
|
|
|
}
|