2023-01-27 03:22:44 -08:00
|
|
|
import type { IExecuteFunctions } from 'n8n-core';
|
2020-03-13 18:22:36 -07:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2020-03-13 18:22:36 -07:00
|
|
|
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,
|
2023-02-27 19:39:43 -08:00
|
|
|
JsonObject,
|
2020-03-13 18:22:36 -07:00
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError, NodeOperationError } from 'n8n-workflow';
|
2020-03-13 18:22:36 -07:00
|
|
|
|
2022-11-29 08:11:49 -08:00
|
|
|
import {
|
|
|
|
encodeURIComponentOnce,
|
|
|
|
getCalendars,
|
|
|
|
getTimezones,
|
|
|
|
googleApiRequest,
|
|
|
|
googleApiRequestAllItems,
|
|
|
|
} from './GenericFunctions';
|
2020-03-13 18:22:36 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { eventFields, eventOperations } from './EventDescription';
|
2020-03-13 18:22:36 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { calendarFields, calendarOperations } from './CalendarDescription';
|
2020-10-30 01:31:27 -07:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { 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 = {
|
2022-11-29 08:11:49 -08:00
|
|
|
listSearch: {
|
|
|
|
getCalendars,
|
|
|
|
getTimezones,
|
|
|
|
},
|
2020-03-13 18:22:36 -07:00
|
|
|
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
|
2022-08-17 08:50:24 -07:00
|
|
|
async getConferenceSolutations(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2020-10-22 23:00:00 -07:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const calendar = this.getCurrentNodeParameter('calendar') as string;
|
|
|
|
const posibleSolutions: IDataObject = {
|
2022-08-17 08:50:24 -07:00
|
|
|
eventHangout: 'Google Hangout',
|
|
|
|
eventNamedHangout: 'Google Hangout Classic',
|
|
|
|
hangoutsMeet: 'Google Meet',
|
2020-10-22 23:00:00 -07:00
|
|
|
};
|
2022-08-17 08:50:24 -07:00
|
|
|
const {
|
|
|
|
conferenceProperties: { allowedConferenceSolutionTypes },
|
|
|
|
} = await googleApiRequest.call(
|
2020-10-22 23:00:00 -07:00
|
|
|
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 colors to display them to user so that he can
|
|
|
|
// select them easily
|
2022-08-17 08:50:24 -07:00
|
|
|
async getColors(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2020-03-13 18:22:36 -07:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-08-17 08:50:24 -07:00
|
|
|
const { event } = await googleApiRequest.call(this, 'GET', '/calendar/v3/colors');
|
2023-02-27 19:39:43 -08:00
|
|
|
for (const key of Object.keys(event as IDataObject)) {
|
2020-06-20 08:51:11 -07:00
|
|
|
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;
|
|
|
|
},
|
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();
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
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;
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
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') {
|
2022-11-29 08:11:49 -08:00
|
|
|
// we need to decode once because calendar used to be saved encoded
|
|
|
|
const calendarId = decodeURIComponent(
|
|
|
|
this.getNodeParameter('calendar', i, '', { extractValue: true }) as string,
|
|
|
|
);
|
2021-05-01 20:50:15 -07:00
|
|
|
const timeMin = this.getNodeParameter('timeMin', i) as string;
|
|
|
|
const timeMax = this.getNodeParameter('timeMax', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2023-01-19 04:37:19 -08:00
|
|
|
const outputFormat = options.outputFormat || 'availability';
|
2022-11-29 08:11:49 -08:00
|
|
|
const tz = this.getNodeParameter('options.timezone', i, '', {
|
|
|
|
extractValue: true,
|
|
|
|
}) as string;
|
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,
|
|
|
|
},
|
|
|
|
],
|
2022-11-29 08:11:49 -08:00
|
|
|
timeZone: tz || timezone,
|
2021-05-01 20:50:15 -07:00
|
|
|
};
|
2020-10-30 01:32:35 -07:00
|
|
|
|
2021-05-01 20:50:15 -07:00
|
|
|
responseData = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
2022-12-29 03:20:43 -08:00
|
|
|
'/calendar/v3/freeBusy',
|
2021-05-01 20:50:15 -07:00
|
|
|
body,
|
|
|
|
{},
|
|
|
|
);
|
2020-10-30 01:32:35 -07:00
|
|
|
|
2021-05-01 20:50:15 -07:00
|
|
|
if (responseData.calendars[calendarId].errors) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(
|
|
|
|
this.getNode(),
|
|
|
|
responseData.calendars[calendarId] as JsonObject,
|
|
|
|
{
|
|
|
|
itemIndex: i,
|
|
|
|
},
|
|
|
|
);
|
2021-05-01 20:50:15 -07:00
|
|
|
}
|
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,
|
|
|
|
};
|
|
|
|
} 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') {
|
2022-11-29 08:11:49 -08:00
|
|
|
const calendarId = encodeURIComponentOnce(
|
|
|
|
this.getNodeParameter('calendar', i, '', { extractValue: true }) as string,
|
|
|
|
);
|
2021-05-01 20:50:15 -07:00
|
|
|
const start = this.getNodeParameter('start', i) as string;
|
|
|
|
const end = this.getNodeParameter('end', i) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
const useDefaultReminders = this.getNodeParameter('useDefaultReminders', i) as boolean;
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
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 = [];
|
2022-08-17 08:50:24 -07:00
|
|
|
(additionalFields.attendees as string[]).forEach((attendee) => {
|
|
|
|
body.attendees!.push.apply(
|
|
|
|
body.attendees,
|
|
|
|
attendee
|
|
|
|
.split(',')
|
|
|
|
.map((a) => a.trim())
|
|
|
|
.map((email) => ({ email })),
|
|
|
|
);
|
2021-05-01 20:50:15 -07:00
|
|
|
});
|
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) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const reminders = (this.getNodeParameter('remindersUi', i) as IDataObject)
|
|
|
|
.remindersValues as IDataObject[];
|
2021-05-01 20:50:15 -07:00
|
|
|
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-08-17 08:50:24 -07: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-08-17 08:50:24 -07: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 {
|
2022-08-17 08:50:24 -07:00
|
|
|
if (additionalFields.repeatHowManyTimes && additionalFields.repeatUntil) {
|
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
2022-12-29 03:20:43 -08:00
|
|
|
"You can set either 'Repeat How Many Times' or 'Repeat Until' but not both",
|
2022-08-17 08:50:24 -07:00
|
|
|
{ itemIndex: i },
|
2021-05-01 20:50:15 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (additionalFields.repeatFrecuency) {
|
|
|
|
body.recurrence?.push(
|
|
|
|
`FREQ=${(additionalFields.repeatFrecuency as string).toUpperCase()};`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (additionalFields.repeatHowManyTimes) {
|
2022-08-17 08:50:24 -07:00
|
|
|
body.recurrence?.push(`COUNT=${additionalFields.repeatHowManyTimes};`);
|
2021-05-01 20:50:15 -07:00
|
|
|
}
|
|
|
|
if (additionalFields.repeatUntil) {
|
2022-03-13 04:00:12 -07:00
|
|
|
const repeatUntil = moment(additionalFields.repeatUntil as string)
|
|
|
|
.utc()
|
|
|
|
.format('YYYYMMDDTHHmmss');
|
2022-08-17 08:50:24 -07:00
|
|
|
body.recurrence?.push(`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) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const conferenceData = (additionalFields.conferenceDataUi as IDataObject)
|
|
|
|
.conferenceDataValues as IDataObject;
|
2021-05-01 20:50:15 -07:00
|
|
|
if (conferenceData) {
|
|
|
|
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') {
|
2022-11-29 08:11:49 -08:00
|
|
|
const calendarId = encodeURIComponentOnce(
|
|
|
|
this.getNodeParameter('calendar', i, '', { extractValue: true }) as string,
|
|
|
|
);
|
2021-05-01 20:50:15 -07:00
|
|
|
const eventId = this.getNodeParameter('eventId', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2021-05-01 20:50:15 -07:00
|
|
|
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') {
|
2022-11-29 08:11:49 -08:00
|
|
|
const calendarId = encodeURIComponentOnce(
|
|
|
|
this.getNodeParameter('calendar', i, '', { extractValue: true }) as string,
|
|
|
|
);
|
2021-05-01 20:50:15 -07:00
|
|
|
const eventId = this.getNodeParameter('eventId', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2022-11-29 08:11:49 -08:00
|
|
|
const tz = this.getNodeParameter('options.timeZone', i, '', {
|
|
|
|
extractValue: true,
|
|
|
|
}) as string;
|
2021-05-01 20:50:15 -07:00
|
|
|
if (options.maxAttendees) {
|
|
|
|
qs.maxAttendees = options.maxAttendees as number;
|
|
|
|
}
|
2022-11-29 08:11:49 -08:00
|
|
|
if (tz) {
|
|
|
|
qs.timeZone = tz;
|
2021-05-01 20:50:15 -07:00
|
|
|
}
|
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') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2022-11-29 08:11:49 -08:00
|
|
|
const calendarId = encodeURIComponentOnce(
|
|
|
|
this.getNodeParameter('calendar', i, '', { extractValue: true }) as string,
|
|
|
|
);
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2022-11-29 08:11:49 -08:00
|
|
|
const tz = this.getNodeParameter('options.timeZone', i, '', {
|
|
|
|
extractValue: true,
|
|
|
|
}) as string;
|
2021-05-01 20:50:15 -07:00
|
|
|
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;
|
|
|
|
}
|
2022-11-29 08:11:49 -08:00
|
|
|
if (tz) {
|
|
|
|
qs.timeZone = tz;
|
2021-05-01 20:50:15 -07:00
|
|
|
}
|
|
|
|
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 {
|
2022-11-18 06:26:22 -08:00
|
|
|
qs.maxResults = this.getNodeParameter('limit', i);
|
2021-05-01 20:50:15 -07:00
|
|
|
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') {
|
2022-11-29 08:11:49 -08:00
|
|
|
const calendarId = encodeURIComponentOnce(
|
|
|
|
this.getNodeParameter('calendar', i, '', { extractValue: true }) as string,
|
|
|
|
);
|
2021-05-01 20:50:15 -07:00
|
|
|
const eventId = this.getNodeParameter('eventId', i) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
const useDefaultReminders = this.getNodeParameter('useDefaultReminders', i) as boolean;
|
2022-11-18 07:29:44 -08:00
|
|
|
const updateFields = this.getNodeParameter('updateFields', i);
|
2022-12-02 12:54:28 -08:00
|
|
|
const updateTimezone = updateFields.timezone as string;
|
2022-03-13 01:40:02 -08:00
|
|
|
|
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-12-02 12:54:28 -08:00
|
|
|
dateTime: moment.tz(updateFields.start, updateTimezone).utc().format(),
|
|
|
|
timeZone: updateTimezone,
|
2021-05-01 20:50:15 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
if (updateFields.end) {
|
|
|
|
body.end = {
|
2022-12-02 12:54:28 -08:00
|
|
|
dateTime: moment.tz(updateFields.end, updateTimezone).utc().format(),
|
|
|
|
timeZone: updateTimezone,
|
2021-05-01 20:50:15 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
if (updateFields.attendees) {
|
|
|
|
body.attendees = [];
|
2022-08-17 08:50:24 -07:00
|
|
|
(updateFields.attendees as string[]).forEach((attendee) => {
|
|
|
|
body.attendees!.push.apply(
|
|
|
|
body.attendees,
|
|
|
|
attendee
|
|
|
|
.split(',')
|
|
|
|
.map((a) => a.trim())
|
|
|
|
.map((email) => ({ email })),
|
|
|
|
);
|
2021-05-01 20:50:15 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
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) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const reminders = (this.getNodeParameter('remindersUi', i) as IDataObject)
|
|
|
|
.remindersValues as IDataObject[];
|
2021-05-01 20:50:15 -07:00
|
|
|
body.reminders = {
|
|
|
|
useDefault: false,
|
|
|
|
};
|
|
|
|
if (reminders) {
|
|
|
|
body.reminders.overrides = reminders;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (updateFields.allday && updateFields.start && updateFields.end) {
|
|
|
|
body.start = {
|
2022-12-02 12:54:28 -08:00
|
|
|
date: updateTimezone
|
|
|
|
? moment.tz(updateFields.start, updateTimezone).utc(true).format('YYYY-MM-DD')
|
2022-08-17 08:50:24 -07:00
|
|
|
: moment.tz(updateFields.start, moment.tz.guess()).utc(true).format('YYYY-MM-DD'),
|
2021-05-01 20:50:15 -07:00
|
|
|
};
|
|
|
|
body.end = {
|
2022-12-02 12:54:28 -08:00
|
|
|
date: updateTimezone
|
|
|
|
? moment.tz(updateFields.end, updateTimezone).utc(true).format('YYYY-MM-DD')
|
2022-08-17 08:50:24 -07:00
|
|
|
: 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) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
2022-12-29 03:20:43 -08:00
|
|
|
"You can set either 'Repeat How Many Times' or 'Repeat Until' but not both",
|
2022-08-17 08:50:24 -07:00
|
|
|
{ itemIndex: i },
|
2021-05-01 20:50:15 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
body.recurrence?.push(`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
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-05-01 20:50:15 -07:00
|
|
|
} catch (error) {
|
2022-12-02 12:54:28 -08:00
|
|
|
if (!this.continueOnFail()) {
|
2021-05-01 20:50:15 -07:00
|
|
|
throw error;
|
|
|
|
} else {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionErrorData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionErrorData);
|
2021-05-01 20:50:15 -07:00
|
|
|
continue;
|
2020-03-13 18:22:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
return this.prepareOutputData(returnData);
|
2020-03-13 18:22:36 -07:00
|
|
|
}
|
|
|
|
}
|