Improvements to Google Calendar Availability

This commit is contained in:
Jan Oberhauser 2020-10-30 09:32:35 +01:00
parent 2202224c94
commit 2c18fd401d
3 changed files with 115 additions and 175 deletions

View file

@ -2,7 +2,7 @@ import {
INodeProperties, INodeProperties,
} from 'n8n-workflow'; } from 'n8n-workflow';
export const freeBusyOperations = [ export const calendarOperations = [
{ {
displayName: 'Operation', displayName: 'Operation',
name: 'operation', name: 'operation',
@ -10,26 +10,29 @@ export const freeBusyOperations = [
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'freeBusy', 'calendar',
], ],
}, },
}, },
options: [ options: [
{ {
name: 'Get', name: 'Availability',
value: 'get', value: 'availability',
description: 'Returns free/busy information for a calendar', description: 'If a time-slot is available in a calendar',
}, },
], ],
default: 'get', default: 'availability',
description: 'The operation to perform.', description: 'The operation to perform.',
}, },
] as INodeProperties[]; ] as INodeProperties[];
export const freeBusyFields = [ export const calendarFields = [
/* -------------------------------------------------------------------------- */
/* calendar:availability */
/* -------------------------------------------------------------------------- */
{ {
displayName: 'Calendar ID', displayName: 'Calendar ID',
name: 'calendarId', name: 'calendar',
type: 'options', type: 'options',
typeOptions: { typeOptions: {
loadOptionsMethod: 'getCalendars', loadOptionsMethod: 'getCalendars',
@ -37,28 +40,25 @@ export const freeBusyFields = [
required: true, required: true,
displayOptions: { displayOptions: {
show: { show: {
operation: [
'get',
],
resource: [ resource: [
'freeBusy', 'calendar',
], ],
}, },
}, },
default: '', default: '',
}, },
{ {
displayName: 'Time Min.', displayName: 'Start Time',
name: 'timeMin', name: 'timeMin',
type: 'dateTime', type: 'dateTime',
required: true, required: true,
displayOptions: { displayOptions: {
show: { show: {
operation: [ operation: [
'get', 'availability',
], ],
resource: [ resource: [
'freeBusy', 'calendar',
], ],
}, },
}, },
@ -66,17 +66,17 @@ export const freeBusyFields = [
description: 'Start of the interval', description: 'Start of the interval',
}, },
{ {
displayName: 'Time Max.', displayName: 'End Time',
name: 'timeMax', name: 'timeMax',
type: 'dateTime', type: 'dateTime',
required: true, required: true,
displayOptions: { displayOptions: {
show: { show: {
operation: [ operation: [
'get', 'availability',
], ],
resource: [ resource: [
'freeBusy', 'calendar',
], ],
}, },
}, },
@ -84,39 +84,46 @@ export const freeBusyFields = [
description: 'End of the interval', description: 'End of the interval',
}, },
{ {
displayName: 'Simple', displayName: 'Options',
name: 'simple', name: 'options',
type: 'boolean',
displayOptions: {
show: {
resource: [
'freeBusy',
],
operation: [
'get',
],
},
},
default: true,
description: 'When set to true a simplify version of the response will be used else the raw data.',
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection', type: 'collection',
placeholder: 'Add Field', placeholder: 'Add Options',
displayOptions: { displayOptions: {
show: { show: {
operation: [ operation: [
'get', 'availability',
], ],
resource: [ resource: [
'freeBusy', 'calendar',
], ],
}, },
}, },
default: {}, default: {},
options: [ options: [
{
displayName: 'Output Format',
name: 'outputFormat',
type: 'options',
options: [
{
name: 'Availability',
value: 'availability',
description: 'Returns if there are any events in the given time or not.',
},
{
name: 'Booked Slots',
value: 'bookedSlots',
description: 'Returns the booked slots.',
},
{
name: 'RAW',
value: 'raw',
description: 'Returns the RAW data from the API.',
},
],
default: 'availability',
description: 'The format to return the data in.',
},
{ {
displayName: 'Timezone', displayName: 'Timezone',
name: 'timezone', name: 'timezone',
@ -129,4 +136,6 @@ export const freeBusyFields = [
}, },
], ],
}, },
] as INodeProperties[]; ] as INodeProperties[];

View file

@ -48,7 +48,7 @@ export const eventOperations = [
export const eventFields = [ export const eventFields = [
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* event:create */ /* event:ALL */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
{ {
displayName: 'Calendar ID', displayName: 'Calendar ID',
@ -60,9 +60,6 @@ export const eventFields = [
required: true, required: true,
displayOptions: { displayOptions: {
show: { show: {
operation: [
'create',
],
resource: [ resource: [
'event', 'event',
], ],
@ -70,6 +67,10 @@ export const eventFields = [
}, },
default: '', default: '',
}, },
/* -------------------------------------------------------------------------- */
/* event:create */
/* -------------------------------------------------------------------------- */
{ {
displayName: 'Start', displayName: 'Start',
name: 'start', name: 'start',
@ -449,29 +450,10 @@ export const eventFields = [
], ],
description: `If the event doesn't use the default reminders, this lists the reminders specific to the event`, description: `If the event doesn't use the default reminders, this lists the reminders specific to the event`,
}, },
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* event:delete */ /* event:delete */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
{
displayName: 'Calendar ID',
name: 'calendar',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getCalendars',
},
required: true,
displayOptions: {
show: {
operation: [
'delete',
],
resource: [
'event',
],
},
},
default: '',
},
{ {
displayName: 'Event ID', displayName: 'Event ID',
name: 'eventId', name: 'eventId',
@ -535,26 +517,6 @@ export const eventFields = [
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* event:get */ /* event:get */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
{
displayName: 'Calendar ID',
name: 'calendar',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getCalendars',
},
required: true,
displayOptions: {
show: {
operation: [
'get',
],
resource: [
'event',
],
},
},
default: '',
},
{ {
displayName: 'Event ID', displayName: 'Event ID',
name: 'eventId', name: 'eventId',
@ -609,29 +571,10 @@ export const eventFields = [
}, },
], ],
}, },
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* event:getAll */ /* event:getAll */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
{
displayName: 'Calendar ID',
name: 'calendar',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getCalendars',
},
required: true,
displayOptions: {
show: {
operation: [
'getAll',
],
resource: [
'event',
],
},
},
default: '',
},
{ {
displayName: 'Return All', displayName: 'Return All',
name: 'returnAll', name: 'returnAll',
@ -754,14 +697,14 @@ export const eventFields = [
events and instances of recurring events, but not the underlying recurring events themselves.`, events and instances of recurring events, but not the underlying recurring events themselves.`,
}, },
{ {
displayName: 'Time Max', displayName: 'End Time',
name: 'timeMax', name: 'timeMax',
type: 'dateTime', type: 'dateTime',
default: '', default: '',
description: `Upper bound (exclusive) for an event's start time to filter by`, description: `Upper bound (exclusive) for an event's start time to filter by`,
}, },
{ {
displayName: 'Time Min', displayName: 'Start Time',
name: 'timeMin', name: 'timeMin',
type: 'dateTime', type: 'dateTime',
default: '', default: '',
@ -787,29 +730,10 @@ export const eventFields = [
}, },
], ],
}, },
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* event:update */ /* event:update */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
{
displayName: 'Calendar ID',
name: 'calendar',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getCalendars',
},
required: true,
displayOptions: {
show: {
operation: [
'update',
],
resource: [
'event',
],
},
},
default: '',
},
{ {
displayName: 'Event ID', displayName: 'Event ID',
name: 'eventId', name: 'eventId',

View file

@ -22,9 +22,9 @@ import {
} from './EventDescription'; } from './EventDescription';
import { import {
freeBusyFields, calendarFields,
freeBusyOperations, calendarOperations,
} from './freeBusyDescription'; } from './CalendarDescription';
import { import {
IEvent, IEvent,
@ -62,21 +62,21 @@ export class GoogleCalendar implements INodeType {
type: 'options', type: 'options',
options: [ options: [
{ {
name: 'Event', name: 'Calendar',
value: 'event', value: 'calendar',
}, },
{ {
name: 'Freebusy', name: 'Event',
value: 'freeBusy', value: 'event',
}, },
], ],
default: 'event', default: 'event',
description: 'The resource to operate on.', description: 'The resource to operate on.',
}, },
...calendarOperations,
...calendarFields,
...eventOperations, ...eventOperations,
...eventFields, ...eventFields,
...freeBusyOperations,
...freeBusyFields,
], ],
}; };
@ -178,6 +178,53 @@ export class GoogleCalendar implements INodeType {
const resource = this.getNodeParameter('resource', 0) as string; const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string; const operation = this.getNodeParameter('operation', 0) as string;
for (let i = 0; i < length; i++) { 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') { if (resource === 'event') {
//https://developers.google.com/calendar/v3/reference/events/insert //https://developers.google.com/calendar/v3/reference/events/insert
if (operation === 'create') { if (operation === 'create') {
@ -554,47 +601,7 @@ export class GoogleCalendar implements INodeType {
); );
} }
} }
if (resource === 'freeBusy') {
//https://developers.google.com/calendar/v3/reference/freebusy/query
if (operation === 'get') {
const timezone = this.getTimezone();
const calendarId = this.getNodeParameter('calendarId', i) as string;
const timeMin = this.getNodeParameter('timeMin', i) as string;
const timeMax = this.getNodeParameter('timeMax', i) as string;
const simple = this.getNodeParameter('simple', i) as boolean;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
const body: IDataObject = {
timeMin: moment.tz(timeMin, timezone).utc().format(),
timeMax: moment.tz(timeMax, timezone).utc().format(),
items: [
{
id: calendarId,
},
],
timeZone: additionalFields.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 (simple) {
responseData = responseData.calendars[calendarId].busy;
}
}
}
if (Array.isArray(responseData)) { if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]); returnData.push.apply(returnData, responseData as IDataObject[]);
} else if (responseData !== undefined) { } else if (responseData !== undefined) {