mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-12 05:17:28 -08:00
feat(Cal Trigger Node): Update to support v2 webhooks (#5331)
This commit is contained in:
parent
354edf6886
commit
2889e53b37
|
@ -17,7 +17,7 @@ export class CalTrigger implements INodeType {
|
||||||
name: 'calTrigger',
|
name: 'calTrigger',
|
||||||
icon: 'file:cal.svg',
|
icon: 'file:cal.svg',
|
||||||
group: ['trigger'],
|
group: ['trigger'],
|
||||||
version: 1,
|
version: [1, 2],
|
||||||
subtitle: '=Events: {{$parameter["events"].join(", ")}}',
|
subtitle: '=Events: {{$parameter["events"].join(", ")}}',
|
||||||
description: 'Handle Cal events via webhooks',
|
description: 'Handle Cal events via webhooks',
|
||||||
defaults: {
|
defaults: {
|
||||||
|
@ -60,10 +60,59 @@ export class CalTrigger implements INodeType {
|
||||||
value: 'BOOKING_RESCHEDULED',
|
value: 'BOOKING_RESCHEDULED',
|
||||||
description: 'Receive notifications when a Cal event is rescheduled',
|
description: 'Receive notifications when a Cal event is rescheduled',
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: 'Meeting Ended',
|
||||||
|
value: 'MEETING_ENDED',
|
||||||
|
description: 'Receive notifications when a Cal event or meeting has ended',
|
||||||
|
},
|
||||||
],
|
],
|
||||||
default: [],
|
default: [],
|
||||||
required: true,
|
required: true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
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,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
displayName: 'Options',
|
displayName: 'Options',
|
||||||
name: 'options',
|
name: 'options',
|
||||||
|
@ -125,13 +174,17 @@ export class CalTrigger implements INodeType {
|
||||||
webhookMethods = {
|
webhookMethods = {
|
||||||
default: {
|
default: {
|
||||||
async checkExists(this: IHookFunctions): Promise<boolean> {
|
async checkExists(this: IHookFunctions): Promise<boolean> {
|
||||||
|
const version = this.getNodeParameter('version', 0) as number;
|
||||||
const webhookUrl = this.getNodeWebhookUrl('default');
|
const webhookUrl = this.getNodeWebhookUrl('default');
|
||||||
const webhookData = this.getWorkflowStaticData('node');
|
const webhookData = this.getWorkflowStaticData('node');
|
||||||
const events = this.getNodeParameter('events') as string;
|
const events = this.getNodeParameter('events') as string;
|
||||||
|
|
||||||
// Check all the webhooks which exist already if it is identical to the
|
// Check all the webhooks which exist already if it is identical to the
|
||||||
// one that is supposed to get created.
|
// one that is supposed to get created.
|
||||||
const data = await calApiRequest.call(this, 'GET', '/hooks', {});
|
const data =
|
||||||
|
version === 2
|
||||||
|
? await calApiRequest.call(this, 'GET', '/webhooks', {})
|
||||||
|
: await calApiRequest.call(this, 'GET', '/hooks', {});
|
||||||
|
|
||||||
for (const webhook of data.webhooks) {
|
for (const webhook of data.webhooks) {
|
||||||
if (webhook.subscriberUrl === webhookUrl) {
|
if (webhook.subscriberUrl === webhookUrl) {
|
||||||
|
@ -148,6 +201,7 @@ export class CalTrigger implements INodeType {
|
||||||
return false;
|
return false;
|
||||||
},
|
},
|
||||||
async create(this: IHookFunctions): Promise<boolean> {
|
async create(this: IHookFunctions): Promise<boolean> {
|
||||||
|
const version = this.getNodeParameter('version', 0) as number;
|
||||||
const webhookData = this.getWorkflowStaticData('node');
|
const webhookData = this.getWorkflowStaticData('node');
|
||||||
const subscriberUrl = this.getNodeWebhookUrl('default');
|
const subscriberUrl = this.getNodeWebhookUrl('default');
|
||||||
const eventTriggers = this.getNodeParameter('events') as string;
|
const eventTriggers = this.getNodeParameter('events') as string;
|
||||||
|
@ -161,7 +215,10 @@ export class CalTrigger implements INodeType {
|
||||||
...(options as object),
|
...(options as object),
|
||||||
};
|
};
|
||||||
|
|
||||||
const responseData = await calApiRequest.call(this, 'POST', '/hooks', body);
|
const responseData =
|
||||||
|
version === 2
|
||||||
|
? await calApiRequest.call(this, 'POST', '/webhooks', body)
|
||||||
|
: await calApiRequest.call(this, 'POST', '/hooks', body);
|
||||||
|
|
||||||
if (responseData.webhook.id === undefined) {
|
if (responseData.webhook.id === undefined) {
|
||||||
// Required data is missing so was not successful
|
// Required data is missing so was not successful
|
||||||
|
@ -172,9 +229,13 @@ export class CalTrigger implements INodeType {
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
async delete(this: IHookFunctions): Promise<boolean> {
|
async delete(this: IHookFunctions): Promise<boolean> {
|
||||||
|
const version = this.getNodeParameter('version', 0) as number;
|
||||||
const webhookData = this.getWorkflowStaticData('node');
|
const webhookData = this.getWorkflowStaticData('node');
|
||||||
if (webhookData.webhookId !== undefined) {
|
if (webhookData.webhookId !== undefined) {
|
||||||
const endpoint = `/hooks/${webhookData.webhookId}`;
|
const endpoint =
|
||||||
|
version === 2
|
||||||
|
? `/webhooks/${webhookData.webhookId}`
|
||||||
|
: `/hooks/${webhookData.webhookId}`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await calApiRequest.call(this, 'DELETE', endpoint);
|
await calApiRequest.call(this, 'DELETE', endpoint);
|
||||||
|
|
Loading…
Reference in a new issue