mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-25 04:34:06 -08:00
⚡ Add Google Calendar availability (#1105)
* ⚡ Add Feebusy resource to the Google Calendar node * ⚡ Improvements Co-authored-by: Harshil <ghagrawal17@gmail.com>
This commit is contained in:
parent
b806ee3a57
commit
2202224c94
|
@ -51,7 +51,7 @@ export const eventFields = [
|
|||
/* event:create */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
{
|
||||
displayName: 'Calendar',
|
||||
displayName: 'Calendar ID',
|
||||
name: 'calendar',
|
||||
type: 'options',
|
||||
typeOptions: {
|
||||
|
@ -453,7 +453,7 @@ export const eventFields = [
|
|||
/* event:delete */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
{
|
||||
displayName: 'Calendar',
|
||||
displayName: 'Calendar ID',
|
||||
name: 'calendar',
|
||||
type: 'options',
|
||||
typeOptions: {
|
||||
|
@ -536,7 +536,7 @@ export const eventFields = [
|
|||
/* event:get */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
{
|
||||
displayName: 'Calendar',
|
||||
displayName: 'Calendar ID',
|
||||
name: 'calendar',
|
||||
type: 'options',
|
||||
typeOptions: {
|
||||
|
@ -613,7 +613,7 @@ export const eventFields = [
|
|||
/* event:getAll */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
{
|
||||
displayName: 'Calendar',
|
||||
displayName: 'Calendar ID',
|
||||
name: 'calendar',
|
||||
type: 'options',
|
||||
typeOptions: {
|
||||
|
@ -791,7 +791,7 @@ export const eventFields = [
|
|||
/* event:update */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
{
|
||||
displayName: 'Calendar',
|
||||
displayName: 'Calendar ID',
|
||||
name: 'calendar',
|
||||
type: 'options',
|
||||
typeOptions: {
|
||||
|
|
132
packages/nodes-base/nodes/Google/Calendar/FreebusyDescription.ts
Normal file
132
packages/nodes-base/nodes/Google/Calendar/FreebusyDescription.ts
Normal file
|
@ -0,0 +1,132 @@
|
|||
import {
|
||||
INodeProperties,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
export const freeBusyOperations = [
|
||||
{
|
||||
displayName: 'Operation',
|
||||
name: 'operation',
|
||||
type: 'options',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'freeBusy',
|
||||
],
|
||||
},
|
||||
},
|
||||
options: [
|
||||
{
|
||||
name: 'Get',
|
||||
value: 'get',
|
||||
description: 'Returns free/busy information for a calendar',
|
||||
},
|
||||
],
|
||||
default: 'get',
|
||||
description: 'The operation to perform.',
|
||||
},
|
||||
] as INodeProperties[];
|
||||
|
||||
export const freeBusyFields = [
|
||||
{
|
||||
displayName: 'Calendar ID',
|
||||
name: 'calendarId',
|
||||
type: 'options',
|
||||
typeOptions: {
|
||||
loadOptionsMethod: 'getCalendars',
|
||||
},
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: [
|
||||
'get',
|
||||
],
|
||||
resource: [
|
||||
'freeBusy',
|
||||
],
|
||||
},
|
||||
},
|
||||
default: '',
|
||||
},
|
||||
{
|
||||
displayName: 'Time Min.',
|
||||
name: 'timeMin',
|
||||
type: 'dateTime',
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: [
|
||||
'get',
|
||||
],
|
||||
resource: [
|
||||
'freeBusy',
|
||||
],
|
||||
},
|
||||
},
|
||||
default: '',
|
||||
description: 'Start of the interval',
|
||||
},
|
||||
{
|
||||
displayName: 'Time Max.',
|
||||
name: 'timeMax',
|
||||
type: 'dateTime',
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: [
|
||||
'get',
|
||||
],
|
||||
resource: [
|
||||
'freeBusy',
|
||||
],
|
||||
},
|
||||
},
|
||||
default: '',
|
||||
description: 'End of the interval',
|
||||
},
|
||||
{
|
||||
displayName: 'Simple',
|
||||
name: 'simple',
|
||||
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',
|
||||
placeholder: 'Add Field',
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: [
|
||||
'get',
|
||||
],
|
||||
resource: [
|
||||
'freeBusy',
|
||||
],
|
||||
},
|
||||
},
|
||||
default: {},
|
||||
options: [
|
||||
{
|
||||
displayName: 'Timezone',
|
||||
name: 'timezone',
|
||||
type: 'options',
|
||||
typeOptions: {
|
||||
loadOptionsMethod: 'getTimezones',
|
||||
},
|
||||
default: '',
|
||||
description: 'Time zone used in the response. By default n8n timezone is used.',
|
||||
},
|
||||
],
|
||||
},
|
||||
] as INodeProperties[];
|
|
@ -21,6 +21,11 @@ import {
|
|||
eventOperations,
|
||||
} from './EventDescription';
|
||||
|
||||
import {
|
||||
freeBusyFields,
|
||||
freeBusyOperations,
|
||||
} from './freeBusyDescription';
|
||||
|
||||
import {
|
||||
IEvent,
|
||||
} from './EventInterface';
|
||||
|
@ -60,12 +65,18 @@ export class GoogleCalendar implements INodeType {
|
|||
name: 'Event',
|
||||
value: 'event',
|
||||
},
|
||||
{
|
||||
name: 'Freebusy',
|
||||
value: 'freeBusy',
|
||||
},
|
||||
],
|
||||
default: 'event',
|
||||
description: 'The resource to operate on.',
|
||||
},
|
||||
...eventOperations,
|
||||
...eventFields,
|
||||
...freeBusyOperations,
|
||||
...freeBusyFields,
|
||||
],
|
||||
};
|
||||
|
||||
|
@ -543,6 +554,47 @@ 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)) {
|
||||
returnData.push.apply(returnData, responseData as IDataObject[]);
|
||||
} else if (responseData !== undefined) {
|
||||
|
|
Loading…
Reference in a new issue