2021-12-04 02:11:22 -08:00
|
|
|
|
import {
|
|
|
|
|
IDataObject,
|
|
|
|
|
INodeExecutionData,
|
|
|
|
|
INodeType,
|
|
|
|
|
INodeTypeDescription,
|
|
|
|
|
IPollFunctions,
|
|
|
|
|
NodeApiError,
|
|
|
|
|
NodeOperationError,
|
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
2022-11-29 08:11:49 -08:00
|
|
|
|
import { getCalendars, googleApiRequest, googleApiRequestAllItems } from './GenericFunctions';
|
2021-12-04 02:11:22 -08:00
|
|
|
|
|
2022-04-08 14:32:08 -07:00
|
|
|
|
import moment from 'moment';
|
2021-12-04 02:11:22 -08:00
|
|
|
|
|
|
|
|
|
export class GoogleCalendarTrigger implements INodeType {
|
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
|
displayName: 'Google Calendar Trigger',
|
|
|
|
|
name: 'googleCalendarTrigger',
|
|
|
|
|
icon: 'file:googleCalendar.svg',
|
|
|
|
|
group: ['trigger'],
|
|
|
|
|
version: 1,
|
|
|
|
|
subtitle: '={{$parameter["triggerOn"]}}',
|
|
|
|
|
description: 'Starts the workflow when Google Calendar events occur',
|
|
|
|
|
defaults: {
|
|
|
|
|
name: 'Google Calendar Trigger',
|
|
|
|
|
},
|
|
|
|
|
inputs: [],
|
|
|
|
|
outputs: ['main'],
|
|
|
|
|
credentials: [
|
|
|
|
|
{
|
|
|
|
|
name: 'googleCalendarOAuth2Api',
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
polling: true,
|
|
|
|
|
properties: [
|
|
|
|
|
{
|
2022-11-29 08:11:49 -08:00
|
|
|
|
displayName: 'Calendar',
|
2021-12-04 02:11:22 -08:00
|
|
|
|
name: 'calendarId',
|
2022-11-29 08:11:49 -08:00
|
|
|
|
type: 'resourceLocator',
|
|
|
|
|
default: { mode: 'list', value: '' },
|
2021-12-04 02:11:22 -08:00
|
|
|
|
required: true,
|
2022-11-29 08:11:49 -08:00
|
|
|
|
description: 'Google Calendar to operate on',
|
|
|
|
|
modes: [
|
|
|
|
|
{
|
|
|
|
|
displayName: 'Calendar',
|
|
|
|
|
name: 'list',
|
|
|
|
|
type: 'list',
|
|
|
|
|
placeholder: 'Select a Calendar...',
|
|
|
|
|
typeOptions: {
|
|
|
|
|
searchListMethod: 'getCalendars',
|
|
|
|
|
searchable: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
displayName: 'ID',
|
|
|
|
|
name: 'id',
|
|
|
|
|
type: 'string',
|
|
|
|
|
validation: [
|
|
|
|
|
{
|
|
|
|
|
type: 'regex',
|
|
|
|
|
properties: {
|
|
|
|
|
// calendar ids are emails. W3C email regex with optional trailing whitespace.
|
|
|
|
|
regex:
|
|
|
|
|
'(^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*(?:[ \t]+)*$)',
|
|
|
|
|
errorMessage: 'Not a valid Google Calendar ID',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
extractValue: {
|
|
|
|
|
type: 'regex',
|
|
|
|
|
regex: '(^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*)',
|
|
|
|
|
},
|
|
|
|
|
placeholder: 'name@google.com',
|
|
|
|
|
},
|
|
|
|
|
],
|
2021-12-04 02:11:22 -08:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
displayName: 'Trigger On',
|
|
|
|
|
name: 'triggerOn',
|
|
|
|
|
type: 'options',
|
|
|
|
|
required: true,
|
|
|
|
|
default: '',
|
|
|
|
|
options: [
|
|
|
|
|
{
|
|
|
|
|
name: 'Event Created',
|
|
|
|
|
value: 'eventCreated',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'Event Ended',
|
|
|
|
|
value: 'eventEnded',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'Event Started',
|
|
|
|
|
value: 'eventStarted',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'Event Updated',
|
|
|
|
|
value: 'eventUpdated',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
displayName: 'Options',
|
|
|
|
|
name: 'options',
|
|
|
|
|
type: 'collection',
|
|
|
|
|
placeholder: 'Add Option',
|
|
|
|
|
default: {},
|
|
|
|
|
options: [
|
|
|
|
|
{
|
|
|
|
|
displayName: 'Match Term',
|
|
|
|
|
name: 'matchTerm',
|
|
|
|
|
type: 'string',
|
|
|
|
|
default: '',
|
2022-08-17 08:50:24 -07:00
|
|
|
|
description:
|
|
|
|
|
'Free text search terms to filter events that match these terms in any field, except for extended properties',
|
2021-12-04 02:11:22 -08:00
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
methods = {
|
2022-11-29 08:11:49 -08:00
|
|
|
|
listSearch: {
|
|
|
|
|
getCalendars,
|
2021-12-04 02:11:22 -08:00
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
async poll(this: IPollFunctions): Promise<INodeExecutionData[][] | null> {
|
|
|
|
|
const poolTimes = this.getNodeParameter('pollTimes.item', []) as IDataObject[];
|
|
|
|
|
const triggerOn = this.getNodeParameter('triggerOn', '') as string;
|
2022-11-29 08:11:49 -08:00
|
|
|
|
const calendarId = this.getNodeParameter('calendarId', '', { extractValue: true }) as string;
|
2021-12-04 02:11:22 -08:00
|
|
|
|
const webhookData = this.getWorkflowStaticData('node');
|
|
|
|
|
const matchTerm = this.getNodeParameter('options.matchTerm', '') as string;
|
|
|
|
|
|
|
|
|
|
if (poolTimes.length === 0) {
|
2022-08-17 08:50:24 -07:00
|
|
|
|
throw new NodeOperationError(this.getNode(), 'Please set a poll time');
|
2021-12-04 02:11:22 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (triggerOn === '') {
|
2022-08-17 08:50:24 -07:00
|
|
|
|
throw new NodeOperationError(this.getNode(), 'Please select an event');
|
2021-12-04 02:11:22 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (calendarId === '') {
|
2022-08-17 08:50:24 -07:00
|
|
|
|
throw new NodeOperationError(this.getNode(), 'Please select a calendar');
|
2021-12-04 02:11:22 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const now = moment().utc().format();
|
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
|
const startDate = (webhookData.lastTimeChecked as string) || now;
|
2021-12-04 02:11:22 -08:00
|
|
|
|
|
|
|
|
|
const endDate = now;
|
|
|
|
|
|
|
|
|
|
const qs: IDataObject = {
|
|
|
|
|
showDeleted: false,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (matchTerm !== '') {
|
|
|
|
|
qs.q = matchTerm;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let events;
|
|
|
|
|
|
|
|
|
|
if (triggerOn === 'eventCreated' || triggerOn === 'eventUpdated') {
|
|
|
|
|
Object.assign(qs, {
|
|
|
|
|
updatedMin: startDate,
|
|
|
|
|
orderBy: 'updated',
|
|
|
|
|
});
|
|
|
|
|
} else if (triggerOn === 'eventStarted' || triggerOn === 'eventEnded') {
|
|
|
|
|
Object.assign(qs, {
|
|
|
|
|
singleEvents: true,
|
|
|
|
|
timeMin: moment(startDate).startOf('second').utc().format(),
|
|
|
|
|
timeMax: moment(endDate).endOf('second').utc().format(),
|
|
|
|
|
orderBy: 'startTime',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.getMode() === 'manual') {
|
|
|
|
|
delete qs.updatedMin;
|
|
|
|
|
delete qs.timeMin;
|
|
|
|
|
delete qs.timeMax;
|
|
|
|
|
|
|
|
|
|
qs.maxResults = 1;
|
2022-08-17 08:50:24 -07:00
|
|
|
|
events = await googleApiRequest.call(
|
|
|
|
|
this,
|
|
|
|
|
'GET',
|
|
|
|
|
`/calendar/v3/calendars/${calendarId}/events`,
|
|
|
|
|
{},
|
|
|
|
|
qs,
|
|
|
|
|
);
|
2021-12-04 02:11:22 -08:00
|
|
|
|
events = events.items;
|
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
|
events = await googleApiRequestAllItems.call(
|
|
|
|
|
this,
|
|
|
|
|
'items',
|
|
|
|
|
'GET',
|
|
|
|
|
`/calendar/v3/calendars/${calendarId}/events`,
|
|
|
|
|
{},
|
|
|
|
|
qs,
|
|
|
|
|
);
|
2021-12-04 02:11:22 -08:00
|
|
|
|
if (triggerOn === 'eventCreated') {
|
2022-08-17 08:50:24 -07:00
|
|
|
|
events = events.filter((event: { created: string }) =>
|
|
|
|
|
moment(event.created).isBetween(startDate, endDate),
|
|
|
|
|
);
|
2021-12-04 02:11:22 -08:00
|
|
|
|
} else if (triggerOn === 'eventUpdated') {
|
2022-08-17 08:50:24 -07:00
|
|
|
|
events = events.filter(
|
|
|
|
|
(event: { created: string; updated: string }) =>
|
|
|
|
|
!moment(moment(event.created).format('YYYY-MM-DDTHH:mm:ss')).isSame(
|
|
|
|
|
moment(event.updated).format('YYYY-MM-DDTHH:mm:ss'),
|
|
|
|
|
),
|
|
|
|
|
);
|
2021-12-04 02:11:22 -08:00
|
|
|
|
} else if (triggerOn === 'eventStarted') {
|
2022-08-17 08:50:24 -07:00
|
|
|
|
events = events.filter((event: { start: { dateTime: string } }) =>
|
|
|
|
|
moment(event.start.dateTime).isBetween(startDate, endDate, null, '[]'),
|
|
|
|
|
);
|
2021-12-04 02:11:22 -08:00
|
|
|
|
} else if (triggerOn === 'eventEnded') {
|
2022-08-17 08:50:24 -07:00
|
|
|
|
events = events.filter((event: { end: { dateTime: string } }) =>
|
|
|
|
|
moment(event.end.dateTime).isBetween(startDate, endDate, null, '[]'),
|
|
|
|
|
);
|
2021-12-04 02:11:22 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
webhookData.lastTimeChecked = endDate;
|
|
|
|
|
|
|
|
|
|
if (Array.isArray(events) && events.length) {
|
|
|
|
|
return [this.helpers.returnJsonArray(events)];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.getMode() === 'manual') {
|
2022-08-17 08:50:24 -07:00
|
|
|
|
throw new NodeApiError(this.getNode(), {
|
|
|
|
|
message: 'No data with the current filter could be found',
|
|
|
|
|
});
|
2021-12-04 02:11:22 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2022-04-08 14:32:08 -07:00
|
|
|
|
}
|