n8n/packages/nodes-base/nodes/ICalendar/ICalendar.node.ts

364 lines
8.9 KiB
TypeScript
Raw Normal View History

import {
IExecuteFunctions,
} from 'n8n-core';
import {
IDataObject,
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import {
promisify,
} from 'util';
import moment from 'moment-timezone';
import * as ics from 'ics';
const createEvent = promisify(ics.createEvent);
export class ICalendar implements INodeType {
description: INodeTypeDescription = {
displayName: 'iCalendar',
name: 'iCal',
icon: 'fa:calendar',
group: ['input'],
version: 1,
subtitle: '={{$parameter["operation"]}}',
description: 'Create iCalendar file',
defaults: {
name: 'iCalendar',
color: '#408000',
},
inputs: ['main'],
outputs: ['main'],
credentials: [],
properties: [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
options: [
{
name: 'Create Event File',
value: 'createEventFile',
},
],
default: 'createEventFile',
},
{
displayName: 'Event Title',
name: 'title',
type: 'string',
default: '',
},
{
displayName: 'Start',
name: 'start',
type: 'dateTime',
default: '',
required: true,
refactor: Apply `eslint-plugin-n8n-nodes-base` autofixable rules (#3174) * :zap: Initial setup * :shirt: Update `.eslintignore` * :shirt: Autofix node-param-default-missing (#3173) * :fire: Remove duplicate key * :shirt: Add exceptions * :package: Update package-lock.json * :shirt: Apply `node-class-description-inputs-wrong-trigger-node` (#3176) * :shirt: Apply `node-class-description-inputs-wrong-regular-node` (#3177) * :shirt: Apply `node-class-description-outputs-wrong` (#3178) * :shirt: Apply `node-execute-block-double-assertion-for-items` (#3179) * :shirt: Apply `node-param-default-wrong-for-collection` (#3180) * :shirt: Apply node-param-default-wrong-for-boolean (#3181) * Autofixed default missing * Autofixed booleans, worked well * :zap: Fix params * :rewind: Undo exempted autofixes * :package: Update package-lock.json * :shirt: Apply node-class-description-missing-subtitle (#3182) * :zap: Fix missing comma * :shirt: Apply `node-param-default-wrong-for-fixed-collection` (#3184) * :shirt: Add exception for `node-class-description-missing-subtitle` * :shirt: Apply `node-param-default-wrong-for-multi-options` (#3185) * :shirt: Apply `node-param-collection-type-unsorted-items` (#3186) * Missing coma * :shirt: Apply `node-param-default-wrong-for-simplify` (#3187) * :shirt: Apply `node-param-description-comma-separated-hyphen` (#3190) * :shirt: Apply `node-param-description-empty-string` (#3189) * :shirt: Apply `node-param-description-excess-inner-whitespace` (#3191) * Rule looks good * Add whitespace rule in eslint config * :zao: fix * :shirt: Apply `node-param-description-identical-to-display-name` (#3193) * :shirt: Apply `node-param-description-missing-for-ignore-ssl-issues` (#3195) * :rewind: Revert ":zao: fix" This reverts commit ef8a76f3dfedffd1bdccf3178af8a8d90cf5a55c. * :shirt: Apply `node-param-description-missing-for-simplify` (#3196) * :shirt: Apply `node-param-description-missing-final-period` (#3194) * Rule working as intended * Add rule to eslint * :shirt: Apply node-param-description-missing-for-return-all (#3197) * :zap: 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>
2022-04-22 09:29:51 -07:00
description: 'Date and time at which the event begins. (For all-day events, the time will be ignored.).',
},
{
displayName: 'End',
name: 'end',
type: 'dateTime',
default: '',
required: true,
refactor: Apply `eslint-plugin-n8n-nodes-base` autofixable rules (#3174) * :zap: Initial setup * :shirt: Update `.eslintignore` * :shirt: Autofix node-param-default-missing (#3173) * :fire: Remove duplicate key * :shirt: Add exceptions * :package: Update package-lock.json * :shirt: Apply `node-class-description-inputs-wrong-trigger-node` (#3176) * :shirt: Apply `node-class-description-inputs-wrong-regular-node` (#3177) * :shirt: Apply `node-class-description-outputs-wrong` (#3178) * :shirt: Apply `node-execute-block-double-assertion-for-items` (#3179) * :shirt: Apply `node-param-default-wrong-for-collection` (#3180) * :shirt: Apply node-param-default-wrong-for-boolean (#3181) * Autofixed default missing * Autofixed booleans, worked well * :zap: Fix params * :rewind: Undo exempted autofixes * :package: Update package-lock.json * :shirt: Apply node-class-description-missing-subtitle (#3182) * :zap: Fix missing comma * :shirt: Apply `node-param-default-wrong-for-fixed-collection` (#3184) * :shirt: Add exception for `node-class-description-missing-subtitle` * :shirt: Apply `node-param-default-wrong-for-multi-options` (#3185) * :shirt: Apply `node-param-collection-type-unsorted-items` (#3186) * Missing coma * :shirt: Apply `node-param-default-wrong-for-simplify` (#3187) * :shirt: Apply `node-param-description-comma-separated-hyphen` (#3190) * :shirt: Apply `node-param-description-empty-string` (#3189) * :shirt: Apply `node-param-description-excess-inner-whitespace` (#3191) * Rule looks good * Add whitespace rule in eslint config * :zao: fix * :shirt: Apply `node-param-description-identical-to-display-name` (#3193) * :shirt: Apply `node-param-description-missing-for-ignore-ssl-issues` (#3195) * :rewind: Revert ":zao: fix" This reverts commit ef8a76f3dfedffd1bdccf3178af8a8d90cf5a55c. * :shirt: Apply `node-param-description-missing-for-simplify` (#3196) * :shirt: Apply `node-param-description-missing-final-period` (#3194) * Rule working as intended * Add rule to eslint * :shirt: Apply node-param-description-missing-for-return-all (#3197) * :zap: 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>
2022-04-22 09:29:51 -07:00
description: 'Date and time at which the event ends. (For all-day events, the time will be ignored.).',
},
{
displayName: 'All Day',
name: 'allDay',
type: 'boolean',
default: false,
description: 'Whether the event lasts all day or not',
},
{
displayName: 'Binary Property',
name: 'binaryPropertyName',
type: 'string',
default: 'data',
required: true,
description: 'The field that your iCalendar file will be available under in the output',
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
operation: [
'createEventFile',
],
},
},
options: [
{
displayName: 'Attendees',
name: 'attendeesUi',
type: 'fixedCollection',
typeOptions: {
multipleValues: true,
},
placeholder: 'Add Attendee',
default: {},
options: [
{
displayName: 'Attendees',
name: 'attendeeValues',
values: [
{
displayName: 'Name',
name: 'name',
type: 'string',
required: true,
default: '',
},
{
displayName: 'Email',
name: 'email',
type: 'string',
required: true,
default: '',
},
{
displayName: 'RSVP',
name: 'rsvp',
type: 'boolean',
default: false,
description: 'Whether the attendee has to confirm attendance or not',
},
],
},
],
},
{
displayName: 'Busy Status',
name: 'busyStatus',
type: 'options',
options: [
{
name: 'Busy',
value: 'BUSY',
},
{
name: 'Tentative',
value: 'TENTATIVE',
},
],
default: '',
description: 'Used to specify busy status for Microsoft applications, like Outlook',
},
{
displayName: 'Calendar Name',
name: 'calName',
type: 'string',
default: '',
description: 'Specifies the calendar (not event) name. Used by Apple iCal and Microsoft Outlook (<a href="https://docs.microsoft.com/en-us/openspecs/exchange_server_protocols/ms-oxcical/1da58449-b97e-46bd-b018-a1ce576f3e6d">spec</a>).',
},
{
displayName: 'Description',
name: 'description',
type: 'string',
default: '',
},
{
displayName: 'File Name',
name: 'fileName',
type: 'string',
default: '',
description: 'The name of the file to be generated. Default value is event.ics.',
},
{
displayName: 'Geolocation',
name: 'geolocationUi',
type: 'fixedCollection',
typeOptions: {
multipleValues: false,
},
placeholder: 'Add Geolocation',
default: {},
options: [
{
displayName: 'Geolocation',
name: 'geolocationValues',
values: [
{
displayName: 'Latitude',
name: 'lat',
type: 'string',
default: '',
},
{
displayName: 'Longitude',
name: 'lon',
type: 'string',
default: '',
},
],
},
],
},
{
displayName: 'Location',
name: 'location',
type: 'string',
default: '',
description: 'The intended venue',
},
{
displayName: 'Recurrence Rule',
name: 'recurrenceRule',
type: 'string',
default: '',
refactor: Apply `eslint-plugin-n8n-nodes-base` autofixable rules (#3174) * :zap: Initial setup * :shirt: Update `.eslintignore` * :shirt: Autofix node-param-default-missing (#3173) * :fire: Remove duplicate key * :shirt: Add exceptions * :package: Update package-lock.json * :shirt: Apply `node-class-description-inputs-wrong-trigger-node` (#3176) * :shirt: Apply `node-class-description-inputs-wrong-regular-node` (#3177) * :shirt: Apply `node-class-description-outputs-wrong` (#3178) * :shirt: Apply `node-execute-block-double-assertion-for-items` (#3179) * :shirt: Apply `node-param-default-wrong-for-collection` (#3180) * :shirt: Apply node-param-default-wrong-for-boolean (#3181) * Autofixed default missing * Autofixed booleans, worked well * :zap: Fix params * :rewind: Undo exempted autofixes * :package: Update package-lock.json * :shirt: Apply node-class-description-missing-subtitle (#3182) * :zap: Fix missing comma * :shirt: Apply `node-param-default-wrong-for-fixed-collection` (#3184) * :shirt: Add exception for `node-class-description-missing-subtitle` * :shirt: Apply `node-param-default-wrong-for-multi-options` (#3185) * :shirt: Apply `node-param-collection-type-unsorted-items` (#3186) * Missing coma * :shirt: Apply `node-param-default-wrong-for-simplify` (#3187) * :shirt: Apply `node-param-description-comma-separated-hyphen` (#3190) * :shirt: Apply `node-param-description-empty-string` (#3189) * :shirt: Apply `node-param-description-excess-inner-whitespace` (#3191) * Rule looks good * Add whitespace rule in eslint config * :zao: fix * :shirt: Apply `node-param-description-identical-to-display-name` (#3193) * :shirt: Apply `node-param-description-missing-for-ignore-ssl-issues` (#3195) * :rewind: Revert ":zao: fix" This reverts commit ef8a76f3dfedffd1bdccf3178af8a8d90cf5a55c. * :shirt: Apply `node-param-description-missing-for-simplify` (#3196) * :shirt: Apply `node-param-description-missing-final-period` (#3194) * Rule working as intended * Add rule to eslint * :shirt: Apply node-param-description-missing-for-return-all (#3197) * :zap: 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>
2022-04-22 09:29:51 -07:00
description: 'A rule to define the repeat pattern of the event (RRULE). (<a href="https://icalendar.org/rrule-tool.html">Rule generator</a>).',
},
{
displayName: 'Organizer',
name: 'organizerUi',
type: 'fixedCollection',
typeOptions: {
multipleValues: false,
},
placeholder: 'Add Organizer',
default: {},
options: [
{
displayName: 'Organizer',
name: 'organizerValues',
values: [
{
displayName: 'Name',
name: 'name',
type: 'string',
default: '',
required: true,
},
{
displayName: 'Email',
name: 'email',
type: 'string',
default: '',
required: true,
},
],
},
],
},
{
displayName: 'Sequence',
name: 'sequence',
type: 'number',
default: 0,
description: 'When sending an update for an event (with the same uid), defines the revision sequence number',
},
{
displayName: 'Status',
name: 'status',
type: 'options',
options: [
{
name: 'Confirmed',
value: 'CONFIRMED',
},
{
name: 'Cancelled',
value: 'CANCELLED',
},
{
name: 'Tentative',
value: 'TENTATIVE',
},
],
default: 'CONFIRMED',
},
{
displayName: 'UID',
name: 'uid',
type: 'string',
default: '',
description: 'Universally unique ID for the event (will be auto-generated if not specified here). Should be globally unique.',
},
{
displayName: 'URL',
name: 'url',
type: 'string',
default: '',
description: 'URL associated with event',
},
],
},
],
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
refactor: Apply `eslint-plugin-n8n-nodes-base` autofixable rules (#3174) * :zap: Initial setup * :shirt: Update `.eslintignore` * :shirt: Autofix node-param-default-missing (#3173) * :fire: Remove duplicate key * :shirt: Add exceptions * :package: Update package-lock.json * :shirt: Apply `node-class-description-inputs-wrong-trigger-node` (#3176) * :shirt: Apply `node-class-description-inputs-wrong-regular-node` (#3177) * :shirt: Apply `node-class-description-outputs-wrong` (#3178) * :shirt: Apply `node-execute-block-double-assertion-for-items` (#3179) * :shirt: Apply `node-param-default-wrong-for-collection` (#3180) * :shirt: Apply node-param-default-wrong-for-boolean (#3181) * Autofixed default missing * Autofixed booleans, worked well * :zap: Fix params * :rewind: Undo exempted autofixes * :package: Update package-lock.json * :shirt: Apply node-class-description-missing-subtitle (#3182) * :zap: Fix missing comma * :shirt: Apply `node-param-default-wrong-for-fixed-collection` (#3184) * :shirt: Add exception for `node-class-description-missing-subtitle` * :shirt: Apply `node-param-default-wrong-for-multi-options` (#3185) * :shirt: Apply `node-param-collection-type-unsorted-items` (#3186) * Missing coma * :shirt: Apply `node-param-default-wrong-for-simplify` (#3187) * :shirt: Apply `node-param-description-comma-separated-hyphen` (#3190) * :shirt: Apply `node-param-description-empty-string` (#3189) * :shirt: Apply `node-param-description-excess-inner-whitespace` (#3191) * Rule looks good * Add whitespace rule in eslint config * :zao: fix * :shirt: Apply `node-param-description-identical-to-display-name` (#3193) * :shirt: Apply `node-param-description-missing-for-ignore-ssl-issues` (#3195) * :rewind: Revert ":zao: fix" This reverts commit ef8a76f3dfedffd1bdccf3178af8a8d90cf5a55c. * :shirt: Apply `node-param-description-missing-for-simplify` (#3196) * :shirt: Apply `node-param-description-missing-final-period` (#3194) * Rule working as intended * Add rule to eslint * :shirt: Apply node-param-description-missing-for-return-all (#3197) * :zap: 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>
2022-04-22 09:29:51 -07:00
const length = items.length;
const returnData: INodeExecutionData[] = [];
const operation = this.getNodeParameter('operation', 0) as string;
if (operation === 'createEventFile') {
for (let i = 0; i < length; i++) {
const title = this.getNodeParameter('title', i) as string;
const allDay = this.getNodeParameter('allDay', i) as boolean;
const start = this.getNodeParameter('start', i) as string;
let end = this.getNodeParameter('end', i) as string;
end = (allDay) ? moment(end).utc().add(1, 'day').format() as string : end;
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i) as string;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
let fileName = 'event.ics';
2022-01-08 04:14:18 -08:00
const eventStart = moment(start).toArray().splice(0, (allDay) ? 3 : 6) as ics.DateArray;
2022-01-08 04:11:58 -08:00
eventStart[1]++;
2022-01-08 04:14:18 -08:00
const eventEnd = moment(end).toArray().splice(0, (allDay) ? 3 : 6) as ics.DateArray;
2022-01-08 04:11:58 -08:00
eventEnd[1]++;
if (additionalFields.fileName) {
fileName = additionalFields.fileName as string;
}
const data: ics.EventAttributes = {
title,
2022-01-08 04:11:58 -08:00
start: eventStart,
end: eventEnd,
startInputType: 'utc',
endInputType: 'utc',
};
if (additionalFields.geolocationUi) {
data.geo = (additionalFields.geolocationUi as IDataObject).geolocationValues as ics.GeoCoordinates;
delete additionalFields.geolocationUi;
}
if (additionalFields.organizerUi) {
data.organizer = (additionalFields.organizerUi as IDataObject).organizerValues as ics.Person;
delete additionalFields.organizerUi;
}
if (additionalFields.attendeesUi) {
data.attendees = (additionalFields.attendeesUi as IDataObject).attendeeValues as ics.Attendee[];
delete additionalFields.attendeesUi;
}
Object.assign(data, additionalFields);
const buffer = Buffer.from(await createEvent(data) as string);
const binaryData = await this.helpers.prepareBinaryData(buffer, fileName, 'text/calendar');
returnData.push(
{
json: {},
binary: {
[binaryPropertyName]: binaryData,
},
},
);
}
}
return [returnData];
}
}