2024-08-29 06:55:53 -07:00
|
|
|
import {
|
|
|
|
type IDataObject,
|
|
|
|
type IHookFunctions,
|
|
|
|
type IWebhookFunctions,
|
|
|
|
type ILoadOptionsFunctions,
|
|
|
|
type INodePropertyOptions,
|
|
|
|
type INodeType,
|
|
|
|
type INodeTypeDescription,
|
|
|
|
type IWebhookResponseData,
|
|
|
|
NodeConnectionType,
|
2022-06-05 10:30:54 -07:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { calApiRequest, sortOptionParameters } from './GenericFunctions';
|
2022-06-05 10:30:54 -07:00
|
|
|
|
|
|
|
export class CalTrigger implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
2024-07-15 07:16:41 -07:00
|
|
|
displayName: 'Cal.com Trigger',
|
2022-08-01 13:47:55 -07:00
|
|
|
name: 'calTrigger',
|
2024-06-06 04:34:30 -07:00
|
|
|
icon: { light: 'file:cal.svg', dark: 'file:cal.dark.svg' },
|
2022-08-01 13:47:55 -07:00
|
|
|
group: ['trigger'],
|
2023-03-08 07:58:15 -08:00
|
|
|
version: [1, 2],
|
2022-08-01 13:47:55 -07:00
|
|
|
subtitle: '=Events: {{$parameter["events"].join(", ")}}',
|
2024-07-15 07:16:41 -07:00
|
|
|
description: 'Handle Cal.com events via webhooks',
|
2022-08-01 13:47:55 -07:00
|
|
|
defaults: {
|
2024-07-15 07:16:41 -07:00
|
|
|
name: 'Cal.com Trigger',
|
2022-08-01 13:47:55 -07:00
|
|
|
},
|
|
|
|
inputs: [],
|
2024-08-29 06:55:53 -07:00
|
|
|
outputs: [NodeConnectionType.Main],
|
2022-08-01 13:47:55 -07:00
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'calApi',
|
|
|
|
required: true,
|
2022-06-05 10:30:54 -07:00
|
|
|
},
|
2022-08-01 13:47:55 -07:00
|
|
|
],
|
|
|
|
webhooks: [
|
|
|
|
{
|
|
|
|
name: 'default',
|
|
|
|
httpMethod: 'POST',
|
|
|
|
responseMode: 'onReceived',
|
|
|
|
path: 'webhook',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Events',
|
|
|
|
name: 'events',
|
|
|
|
type: 'multiOptions',
|
|
|
|
options: [
|
2022-06-05 10:30:54 -07:00
|
|
|
{
|
2022-08-01 13:47:55 -07:00
|
|
|
name: 'Booking Cancelled',
|
|
|
|
value: 'BOOKING_CANCELLED',
|
|
|
|
description: 'Receive notifications when a Cal event is canceled',
|
2022-06-05 10:30:54 -07:00
|
|
|
},
|
2022-08-01 13:47:55 -07:00
|
|
|
{
|
|
|
|
name: 'Booking Created',
|
|
|
|
value: 'BOOKING_CREATED',
|
|
|
|
description: 'Receive notifications when a new Cal event is created',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Booking Rescheduled',
|
|
|
|
value: 'BOOKING_RESCHEDULED',
|
|
|
|
description: 'Receive notifications when a Cal event is rescheduled',
|
|
|
|
},
|
2023-03-08 07:58:15 -08:00
|
|
|
{
|
|
|
|
name: 'Meeting Ended',
|
|
|
|
value: 'MEETING_ENDED',
|
|
|
|
description: 'Receive notifications when a Cal event or meeting has ended',
|
|
|
|
},
|
2022-08-01 13:47:55 -07:00
|
|
|
],
|
|
|
|
default: [],
|
|
|
|
required: true,
|
|
|
|
},
|
2023-03-08 07:58:15 -08:00
|
|
|
{
|
|
|
|
displayName: 'API Version',
|
|
|
|
name: 'version',
|
|
|
|
type: 'options',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
'@version': [1],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
isNodeSetting: true,
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Before v2.0',
|
|
|
|
value: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'v2.0 Onwards',
|
|
|
|
value: 2,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'API Version',
|
|
|
|
name: 'version',
|
|
|
|
type: 'options',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
'@version': [2],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
isNodeSetting: true,
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Before v2.0',
|
|
|
|
value: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'v2.0 Onwards',
|
|
|
|
value: 2,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 2,
|
|
|
|
},
|
2022-08-01 13:47:55 -07:00
|
|
|
{
|
|
|
|
displayName: 'Options',
|
|
|
|
name: 'options',
|
|
|
|
type: 'collection',
|
|
|
|
placeholder: 'Add Field',
|
|
|
|
default: {},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
displayName: 'App ID',
|
|
|
|
name: 'appId',
|
|
|
|
type: 'string',
|
|
|
|
description: 'The ID of the App to monitor',
|
|
|
|
default: '',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'EventType Name or ID',
|
|
|
|
name: 'eventTypeId',
|
|
|
|
type: 'options',
|
|
|
|
typeOptions: {
|
|
|
|
loadOptionsMethod: 'getEventTypes',
|
2022-06-05 10:30:54 -07:00
|
|
|
},
|
2022-08-01 13:47:55 -07:00
|
|
|
description:
|
|
|
|
'The EventType to monitor. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
|
|
|
|
default: '',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Payload Template',
|
|
|
|
name: 'payloadTemplate',
|
|
|
|
type: 'string',
|
|
|
|
description: 'Template to customize the webhook payload',
|
|
|
|
default: '',
|
|
|
|
typeOptions: {
|
|
|
|
rows: 4,
|
2022-06-05 10:44:04 -07:00
|
|
|
},
|
2022-08-01 13:47:55 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
2022-06-05 10:30:54 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
|
|
|
async getEventTypes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const data = await calApiRequest.call(this, 'GET', '/event-types', {});
|
|
|
|
|
|
|
|
for (const item of data.event_types) {
|
|
|
|
returnData.push({
|
|
|
|
name: item.title,
|
|
|
|
value: item.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return sortOptionParameters(returnData);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
webhookMethods = {
|
|
|
|
default: {
|
|
|
|
async checkExists(this: IHookFunctions): Promise<boolean> {
|
2023-03-08 07:58:15 -08:00
|
|
|
const version = this.getNodeParameter('version', 0) as number;
|
2022-06-05 10:30:54 -07:00
|
|
|
const webhookUrl = this.getNodeWebhookUrl('default');
|
|
|
|
const webhookData = this.getWorkflowStaticData('node');
|
|
|
|
const events = this.getNodeParameter('events') as string;
|
|
|
|
|
|
|
|
// Check all the webhooks which exist already if it is identical to the
|
|
|
|
// one that is supposed to get created.
|
2023-03-08 07:58:15 -08:00
|
|
|
const data =
|
|
|
|
version === 2
|
|
|
|
? await calApiRequest.call(this, 'GET', '/webhooks', {})
|
|
|
|
: await calApiRequest.call(this, 'GET', '/hooks', {});
|
2022-06-05 10:30:54 -07:00
|
|
|
|
|
|
|
for (const webhook of data.webhooks) {
|
|
|
|
if (webhook.subscriberUrl === webhookUrl) {
|
|
|
|
for (const event of events) {
|
|
|
|
if (!webhook.eventTriggers.includes(event)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Set webhook-id to be sure that it can be deleted
|
|
|
|
webhookData.webhookId = webhook.id as string;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
async create(this: IHookFunctions): Promise<boolean> {
|
2023-03-08 07:58:15 -08:00
|
|
|
const version = this.getNodeParameter('version', 0) as number;
|
2022-06-05 10:30:54 -07:00
|
|
|
const webhookData = this.getWorkflowStaticData('node');
|
|
|
|
const subscriberUrl = this.getNodeWebhookUrl('default');
|
|
|
|
const eventTriggers = this.getNodeParameter('events') as string;
|
|
|
|
const options = this.getNodeParameter('options');
|
|
|
|
const active = true;
|
|
|
|
|
|
|
|
const body = {
|
|
|
|
subscriberUrl,
|
|
|
|
eventTriggers,
|
|
|
|
active,
|
2022-08-01 13:47:55 -07:00
|
|
|
...(options as object),
|
2022-06-05 10:30:54 -07:00
|
|
|
};
|
|
|
|
|
2023-03-08 07:58:15 -08:00
|
|
|
const responseData =
|
|
|
|
version === 2
|
|
|
|
? await calApiRequest.call(this, 'POST', '/webhooks', body)
|
|
|
|
: await calApiRequest.call(this, 'POST', '/hooks', body);
|
2022-06-05 10:30:54 -07:00
|
|
|
|
|
|
|
if (responseData.webhook.id === undefined) {
|
|
|
|
// Required data is missing so was not successful
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
webhookData.webhookId = responseData.webhook.id as string;
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
async delete(this: IHookFunctions): Promise<boolean> {
|
2023-03-08 07:58:15 -08:00
|
|
|
const version = this.getNodeParameter('version', 0) as number;
|
2022-06-05 10:30:54 -07:00
|
|
|
const webhookData = this.getWorkflowStaticData('node');
|
|
|
|
if (webhookData.webhookId !== undefined) {
|
2023-03-08 07:58:15 -08:00
|
|
|
const endpoint =
|
|
|
|
version === 2
|
|
|
|
? `/webhooks/${webhookData.webhookId}`
|
|
|
|
: `/hooks/${webhookData.webhookId}`;
|
2022-06-05 10:30:54 -07:00
|
|
|
|
|
|
|
try {
|
|
|
|
await calApiRequest.call(this, 'DELETE', endpoint);
|
|
|
|
} catch (error) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove from the static workflow data so that it is clear
|
2023-03-03 09:49:19 -08:00
|
|
|
// that no webhooks are registered anymore
|
2022-06-05 10:30:54 -07:00
|
|
|
delete webhookData.webhookId;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
|
|
|
|
const req = this.getRequestObject();
|
|
|
|
return {
|
2022-08-01 13:47:55 -07:00
|
|
|
workflowData: [
|
|
|
|
this.helpers.returnJsonArray({
|
2023-02-27 19:39:43 -08:00
|
|
|
triggerEvent: req.body.triggerEvent as string,
|
|
|
|
createdAt: req.body.createdAt as string,
|
|
|
|
...(req.body.payload as IDataObject),
|
2022-08-01 13:47:55 -07:00
|
|
|
}),
|
|
|
|
],
|
2022-06-05 10:30:54 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|