n8n/packages/nodes-base/nodes/Google/Calendar/GoogleCalendar.node.ts

612 lines
18 KiB
TypeScript
Raw Normal View History

import {
IExecuteFunctions,
} from 'n8n-core';
import {
IDataObject,
ILoadOptionsFunctions,
INodeExecutionData,
INodePropertyOptions,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import {
googleApiRequest,
googleApiRequestAllItems,
} from './GenericFunctions';
import {
eventFields,
eventOperations,
} from './EventDescription';
import {
calendarFields,
calendarOperations,
} from './CalendarDescription';
import {
IEvent,
} from './EventInterface';
import * as moment from 'moment-timezone';
import * as uuid from 'uuid/v4';
export class GoogleCalendar implements INodeType {
description: INodeTypeDescription = {
displayName: 'Google Calendar',
name: 'googleCalendar',
icon: 'file:googleCalendar.png',
group: ['input'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Consume Google Calendar API.',
defaults: {
name: 'Google Calendar',
color: '#3E87E4',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
2020-05-30 12:09:04 -07:00
name: 'googleCalendarOAuth2Api',
required: true,
2020-10-22 06:46:03 -07:00
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
options: [
{
name: 'Calendar',
value: 'calendar',
},
{
name: 'Event',
value: 'event',
},
],
default: 'event',
2020-10-22 06:46:03 -07:00
description: 'The resource to operate on.',
},
...calendarOperations,
...calendarFields,
...eventOperations,
...eventFields,
],
};
methods = {
loadOptions: {
// 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,
): 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;
},
// 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[]> {
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
);
for (const calendar of calendars) {
const calendarName = calendar.summary;
const calendarId = calendar.id;
returnData.push({
name: calendarName,
2020-10-22 06:46:03 -07:00
value: calendarId,
});
}
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[]> {
const returnData: INodePropertyOptions[] = [];
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
);
for (const key of Object.keys(event)) {
const colorName = `Background: ${event[key].background} - Foreground: ${event[key].foreground}`;
const colorId = key;
returnData.push({
name: `${colorName}`,
2020-10-22 06:46:03 -07:00
value: colorId,
});
}
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[]> {
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,
});
}
return returnData;
2020-10-22 06:46:03 -07:00
},
},
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: IDataObject[] = [];
2020-06-15 15:47:44 -07:00
const length = (items.length as unknown) as number;
const qs: IDataObject = {};
let responseData;
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
for (let i = 0; i < length; i++) {
if (resource === 'calendar') {
//https://developers.google.com/calendar/v3/reference/freebusy/query
if (operation === 'availability') {
const timezone = this.getTimezone();
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';
const body: IDataObject = {
timeMin: moment.tz(timeMin, timezone).utc().format(),
timeMax: moment.tz(timeMax, timezone).utc().format(),
items: [
{
id: calendarId,
},
],
timeZone: options.timezone || timezone,
};
responseData = await googleApiRequest.call(
this,
'POST',
`/calendar/v3/freeBusy`,
body,
{},
);
if (responseData.calendars[calendarId].errors) {
let errors = responseData.calendars[calendarId].errors;
errors = errors.map((e: IDataObject) => e.reason);
throw new Error(
`Google Calendar error response: ${errors.join('|')}`,
);
}
if (outputFormat === 'availability') {
responseData = {
available: !responseData.calendars[calendarId].busy.length,
};
} else if (outputFormat === 'bookedSlots') {
responseData = responseData.calendars[calendarId].busy;
}
}
}
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;
2020-06-15 15:47:44 -07:00
const useDefaultReminders = this.getNodeParameter(
'useDefaultReminders',
2020-10-22 09:00:28 -07:00
i,
2020-06-15 15:47:44 -07:00
) as boolean;
const additionalFields = this.getNodeParameter(
'additionalFields',
2020-10-22 09:00:28 -07:00
i,
2020-06-15 15:47:44 -07:00
) as IDataObject;
if (additionalFields.maxAttendees) {
qs.maxAttendees = additionalFields.maxAttendees as number;
}
if (additionalFields.sendNotifications) {
qs.sendNotifications = additionalFields.sendNotifications as boolean;
}
if (additionalFields.sendUpdates) {
qs.sendUpdates = additionalFields.sendUpdates as string;
}
const body: IEvent = {
start: {
dateTime: start,
2020-10-22 06:46:03 -07:00
timeZone: additionalFields.timeZone || this.getTimezone(),
},
end: {
dateTime: end,
2020-10-22 06:46:03 -07:00
timeZone: additionalFields.timeZone || this.getTimezone(),
},
};
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 })));
});
}
if (additionalFields.color) {
body.colorId = additionalFields.color as string;
}
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;
}
if (!useDefaultReminders) {
2020-06-15 15:47:44 -07:00
const reminders = (this.getNodeParameter(
'remindersUi',
2020-10-22 09:00:28 -07:00
i,
2020-06-15 15:47:44 -07:00
) as IDataObject).remindersValues as IDataObject[];
body.reminders = {
2020-10-22 06:46:03 -07:00
useDefault: false,
};
if (reminders) {
body.reminders.overrides = reminders;
}
}
if (additionalFields.allday) {
body.start = {
2020-06-15 15:47:44 -07:00
date: moment(start)
.utc()
2020-10-22 06:46:03 -07:00
.format('YYYY-MM-DD'),
};
body.end = {
2020-06-15 15:47:44 -07:00
date: moment(end)
.utc()
2020-10-22 06:46:03 -07:00
.format('YYYY-MM-DD'),
};
}
//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 = [];
2020-06-15 15:47:44 -07:00
if (
additionalFields.repeatHowManyTimes &&
additionalFields.repeatUntil
) {
throw new Error(
2020-10-22 09:00:28 -07:00
`You can set either 'Repeat How Many Times' or 'Repeat Until' but not both`,
2020-06-15 15:47:44 -07:00
);
}
if (additionalFields.repeatFrecuency) {
2020-06-15 15:47:44 -07:00
body.recurrence?.push(
2020-10-22 09:00:28 -07:00
`FREQ=${(additionalFields.repeatFrecuency as string).toUpperCase()};`,
2020-06-15 15:47:44 -07:00
);
}
if (additionalFields.repeatHowManyTimes) {
2020-06-15 15:47:44 -07:00
body.recurrence?.push(
2020-10-22 09:00:28 -07:00
`COUNT=${additionalFields.repeatHowManyTimes};`,
2020-06-15 15:47:44 -07:00
);
}
if (additionalFields.repeatUntil) {
2020-06-15 15:47:44 -07:00
body.recurrence?.push(
`UNTIL=${moment(additionalFields.repeatUntil as string)
.utc()
2020-10-22 09:00:28 -07:00
.format('YYYYMMDDTHHmmss')}Z`,
2020-06-15 15:47:44 -07:00
);
}
if (body.recurrence.length !== 0) {
body.recurrence = [`RRULE:${body.recurrence.join('')}`];
}
if (additionalFields.conferenceDataUi) {
const conferenceData = (additionalFields.conferenceDataUi as IDataObject).conferenceDataValues as IDataObject;
if (conferenceData) {
qs.conferenceDataVersion = 1;
body.conferenceData = {
createRequest: {
requestId: uuid(),
conferenceSolution: {
type: conferenceData.conferenceSolution as string,
2020-10-22 23:05:24 -07:00
},
},
};
}
}
2020-06-15 15:47:44 -07:00
responseData = await googleApiRequest.call(
this,
'POST',
`/calendar/v3/calendars/${calendarId}/events`,
body,
2020-10-22 09:00:28 -07:00
qs,
2020-06-15 15:47:44 -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;
}
2020-06-15 15:47:44 -07:00
responseData = await googleApiRequest.call(
this,
'DELETE',
`/calendar/v3/calendars/${calendarId}/events/${eventId}`,
2020-10-22 09:00:28 -07:00
{},
2020-06-15 15:47:44 -07:00
);
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',
`/calendar/v3/calendars/${calendarId}/events/${eventId}`,
{},
2020-10-22 09:00:28 -07:00
qs,
2020-06-15 15:47:44 -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) {
2020-06-15 15:47:44 -07:00
responseData = await googleApiRequestAllItems.call(
this,
'items',
'GET',
`/calendar/v3/calendars/${calendarId}/events`,
{},
2020-10-22 09:00:28 -07:00
qs,
2020-06-15 15:47:44 -07:00
);
} else {
qs.maxResults = this.getNodeParameter('limit', i) as number;
2020-06-15 15:47:44 -07:00
responseData = await googleApiRequest.call(
this,
'GET',
`/calendar/v3/calendars/${calendarId}/events`,
{},
2020-10-22 09:00:28 -07:00
qs,
2020-06-15 15:47:44 -07:00
);
responseData = responseData.items;
}
}
//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;
2020-06-15 15:47:44 -07:00
const useDefaultReminders = this.getNodeParameter(
'useDefaultReminders',
2020-10-22 09:00:28 -07:00
i,
2020-06-15 15:47:44 -07:00
) as boolean;
const updateFields = this.getNodeParameter(
'updateFields',
2020-10-22 09:00:28 -07:00
i,
2020-06-15 15:47:44 -07:00
) as IDataObject;
if (updateFields.maxAttendees) {
qs.maxAttendees = updateFields.maxAttendees as number;
}
if (updateFields.sendNotifications) {
qs.sendNotifications = updateFields.sendNotifications as boolean;
}
if (updateFields.sendUpdates) {
qs.sendUpdates = updateFields.sendUpdates as string;
}
const body: IEvent = {};
if (updateFields.start) {
body.start = {
dateTime: updateFields.start,
2020-10-22 06:46:03 -07:00
timeZone: updateFields.timeZone || this.getTimezone(),
};
}
if (updateFields.end) {
body.end = {
dateTime: updateFields.end,
2020-10-22 06:46:03 -07:00
timeZone: updateFields.timeZone || this.getTimezone(),
};
}
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;
}
if (updateFields.description) {
body.description = updateFields.description as string;
}
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) {
2020-06-15 15:47:44 -07:00
const reminders = (this.getNodeParameter(
'remindersUi',
2020-10-22 09:00:28 -07:00
i,
2020-06-15 15:47:44 -07:00
) as IDataObject).remindersValues as IDataObject[];
body.reminders = {
2020-10-22 06:46:03 -07:00
useDefault: false,
};
if (reminders) {
body.reminders.overrides = reminders;
}
}
2020-06-15 15:47:44 -07:00
if (updateFields.allday && updateFields.start && updateFields.end) {
body.start = {
2020-06-15 15:47:44 -07:00
date: moment(updateFields.start as string)
.utc()
2020-10-22 06:46:03 -07:00
.format('YYYY-MM-DD'),
};
body.end = {
2020-06-15 15:47:44 -07:00
date: moment(updateFields.end as string)
.utc()
2020-10-22 06:46:03 -07:00
.format('YYYY-MM-DD'),
};
}
//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 = [];
2020-06-15 15:47:44 -07:00
if (updateFields.repeatHowManyTimes && updateFields.repeatUntil) {
throw new Error(
2020-10-22 09:00:28 -07:00
`You can set either 'Repeat How Many Times' or 'Repeat Until' but not both`,
2020-06-15 15:47:44 -07:00
);
}
if (updateFields.repeatFrecuency) {
2020-06-15 15:47:44 -07:00
body.recurrence?.push(
2020-10-22 09:00:28 -07:00
`FREQ=${(updateFields.repeatFrecuency as string).toUpperCase()};`,
2020-06-15 15:47:44 -07:00
);
}
if (updateFields.repeatHowManyTimes) {
body.recurrence?.push(`COUNT=${updateFields.repeatHowManyTimes};`);
}
if (updateFields.repeatUntil) {
2020-06-15 15:47:44 -07:00
body.recurrence?.push(
`UNTIL=${moment(updateFields.repeatUntil as string)
.utc()
2020-10-22 09:00:28 -07:00
.format('YYYYMMDDTHHmmss')}Z`,
2020-06-15 15:47:44 -07:00
);
}
if (body.recurrence.length !== 0) {
body.recurrence = [`RRULE:${body.recurrence.join('')}`];
} else {
delete body.recurrence;
}
2020-06-15 15:47:44 -07:00
responseData = await googleApiRequest.call(
this,
'PATCH',
`/calendar/v3/calendars/${calendarId}/events/${eventId}`,
body,
2020-10-22 09:00:28 -07:00
qs,
2020-06-15 15:47:44 -07:00
);
}
}
if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]);
} else if (responseData !== undefined) {
returnData.push(responseData as IDataObject);
}
}
return [this.helpers.returnJsonArray(returnData)];
}
}