* ⚡ Initial setup * 👕 Update `.eslintignore` * 👕 Autofix node-param-default-missing (#3173) * 🔥 Remove duplicate key * 👕 Add exceptions * 📦 Update package-lock.json * 👕 Apply `node-class-description-inputs-wrong-trigger-node` (#3176) * 👕 Apply `node-class-description-inputs-wrong-regular-node` (#3177) * 👕 Apply `node-class-description-outputs-wrong` (#3178) * 👕 Apply `node-execute-block-double-assertion-for-items` (#3179) * 👕 Apply `node-param-default-wrong-for-collection` (#3180) * 👕 Apply node-param-default-wrong-for-boolean (#3181) * Autofixed default missing * Autofixed booleans, worked well * ⚡ Fix params * ⏪ Undo exempted autofixes * 📦 Update package-lock.json * 👕 Apply node-class-description-missing-subtitle (#3182) * ⚡ Fix missing comma * 👕 Apply `node-param-default-wrong-for-fixed-collection` (#3184) * 👕 Add exception for `node-class-description-missing-subtitle` * 👕 Apply `node-param-default-wrong-for-multi-options` (#3185) * 👕 Apply `node-param-collection-type-unsorted-items` (#3186) * Missing coma * 👕 Apply `node-param-default-wrong-for-simplify` (#3187) * 👕 Apply `node-param-description-comma-separated-hyphen` (#3190) * 👕 Apply `node-param-description-empty-string` (#3189) * 👕 Apply `node-param-description-excess-inner-whitespace` (#3191) * Rule looks good * Add whitespace rule in eslint config * :zao: fix * 👕 Apply `node-param-description-identical-to-display-name` (#3193) * 👕 Apply `node-param-description-missing-for-ignore-ssl-issues` (#3195) * ⏪ Revert ":zao: fix" This reverts commit ef8a76f3dfedffd1bdccf3178af8a8d90cf5a55c. * 👕 Apply `node-param-description-missing-for-simplify` (#3196) * 👕 Apply `node-param-description-missing-final-period` (#3194) * Rule working as intended * Add rule to eslint * 👕 Apply node-param-description-missing-for-return-all (#3197) * ⚡ Restore `lintfix` command Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com> Co-authored-by: Omar Ajoue <krynble@gmail.com> Co-authored-by: agobrech <ael.gobrecht@gmail.com> Co-authored-by: Michael Kret <michael.k@radency.com>
214 lines
5.4 KiB
TypeScript
214 lines
5.4 KiB
TypeScript
|
|
import {
|
|
IDataObject,
|
|
ILoadOptionsFunctions,
|
|
INodeExecutionData,
|
|
INodePropertyOptions,
|
|
INodeType,
|
|
INodeTypeDescription,
|
|
IPollFunctions,
|
|
NodeApiError,
|
|
NodeOperationError,
|
|
} from 'n8n-workflow';
|
|
|
|
import {
|
|
googleApiRequest,
|
|
googleApiRequestAllItems,
|
|
} from './GenericFunctions';
|
|
|
|
import moment from 'moment';
|
|
|
|
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: [
|
|
{
|
|
displayName: 'Calendar Name/ID',
|
|
name: 'calendarId',
|
|
type: 'options',
|
|
required: true,
|
|
typeOptions: {
|
|
loadOptionsMethod: 'getCalendars',
|
|
},
|
|
default: '',
|
|
},
|
|
{
|
|
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: '',
|
|
description: 'Free text search terms to filter events that match these terms in any field, except for extended properties',
|
|
},
|
|
],
|
|
},
|
|
],
|
|
};
|
|
|
|
methods = {
|
|
loadOptions: {
|
|
// Get all the calendars to display them to user so that he can
|
|
// select them easily
|
|
async getCalendars(
|
|
this: ILoadOptionsFunctions,
|
|
): Promise<INodePropertyOptions[]> {
|
|
const returnData: INodePropertyOptions[] = [];
|
|
const calendars = await googleApiRequestAllItems.call(
|
|
this,
|
|
'items',
|
|
'GET',
|
|
'/calendar/v3/users/me/calendarList',
|
|
);
|
|
for (const calendar of calendars) {
|
|
returnData.push({
|
|
name: calendar.summary,
|
|
value: calendar.id,
|
|
});
|
|
}
|
|
return returnData;
|
|
},
|
|
},
|
|
};
|
|
|
|
async poll(this: IPollFunctions): Promise<INodeExecutionData[][] | null> {
|
|
const poolTimes = this.getNodeParameter('pollTimes.item', []) as IDataObject[];
|
|
const triggerOn = this.getNodeParameter('triggerOn', '') as string;
|
|
const calendarId = this.getNodeParameter('calendarId') as string;
|
|
const webhookData = this.getWorkflowStaticData('node');
|
|
const matchTerm = this.getNodeParameter('options.matchTerm', '') as string;
|
|
|
|
if (poolTimes.length === 0) {
|
|
throw new NodeOperationError(
|
|
this.getNode(),
|
|
'Please set a poll time',
|
|
);
|
|
}
|
|
|
|
if (triggerOn === '') {
|
|
throw new NodeOperationError(
|
|
this.getNode(),
|
|
'Please select an event',
|
|
);
|
|
}
|
|
|
|
if (calendarId === '') {
|
|
throw new NodeOperationError(
|
|
this.getNode(),
|
|
'Please select a calendar',
|
|
);
|
|
}
|
|
|
|
const now = moment().utc().format();
|
|
|
|
const startDate = webhookData.lastTimeChecked as string || now;
|
|
|
|
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;
|
|
events = await googleApiRequest.call(this, 'GET', `/calendar/v3/calendars/${calendarId}/events`, {}, qs);
|
|
events = events.items;
|
|
} else {
|
|
events = await googleApiRequestAllItems.call(this, 'items', 'GET', `/calendar/v3/calendars/${calendarId}/events`, {}, qs);
|
|
if (triggerOn === 'eventCreated') {
|
|
events = events.filter((event: { created: string }) => moment(event.created).isBetween(startDate, endDate));
|
|
} else if (triggerOn === 'eventUpdated') {
|
|
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')));
|
|
} else if (triggerOn === 'eventStarted') {
|
|
events = events.filter((event: { start: { dateTime: string } }) => moment(event.start.dateTime).isBetween(startDate, endDate, null, '[]'));
|
|
} else if (triggerOn === 'eventEnded') {
|
|
events = events.filter((event: { end: { dateTime: string } }) => moment(event.end.dateTime).isBetween(startDate, endDate, null, '[]'));
|
|
}
|
|
}
|
|
|
|
webhookData.lastTimeChecked = endDate;
|
|
|
|
if (Array.isArray(events) && events.length) {
|
|
return [this.helpers.returnJsonArray(events)];
|
|
}
|
|
|
|
if (this.getMode() === 'manual') {
|
|
throw new NodeApiError(this.getNode(), { message: 'No data with the current filter could be found' });
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|