n8n-vikunja-nodes/nodes/Vikunja/VikunjaTrigger.node.ts

158 lines
4.3 KiB
TypeScript
Raw Permalink Normal View History

2023-10-24 16:24:53 +00:00
import type {
IHookFunctions,
IWebhookFunctions,
INodeType,
INodeTypeDescription,
IWebhookResponseData,
2024-06-06 09:38:12 +00:00
} from 'n8n-workflow';
import { apiRequest } from './helper';
import { availableWebhookEvents } from './properties/Webhook';
2023-10-24 16:24:53 +00:00
export class VikunjaTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'Vikunja Trigger',
name: 'vikunjaTrigger',
icon: 'file:vikunja.svg',
group: ['trigger'],
version: 1,
description: 'Starts the workflow when Vikunja events occur',
defaults: {
name: 'Vikunja Trigger',
},
inputs: [],
outputs: ['main'],
credentials: [
{
name: 'vikunjaApi',
required: true,
},
],
webhooks: [
{
name: 'default',
httpMethod: 'POST',
responseMode: 'onReceived',
path: 'webhook',
},
],
properties: [
{
displayName: 'Project ID',
name: 'project',
type: 'number',
required: true,
default: 0,
description: 'The project you want to operate on. Choose from the list, or specify an ID.',
},
{
displayName: 'Events',
name: 'events',
type: 'multiOptions',
default: [],
description: 'The webhook events which should fire this webhook target',
options: availableWebhookEvents,
},
],
2024-06-06 09:38:12 +00:00
};
2023-10-24 16:24:53 +00:00
webhookMethods = {
default: {
async checkExists(this: IHookFunctions): Promise<boolean> {
// Check all the webhooks which exist already if it is identical to the
// one that is supposed to get created.
2024-06-06 09:38:12 +00:00
const projectId = this.getNodeParameter('project') as number;
const endpoint = `/projects/${projectId}/webhooks`;
2023-10-24 16:24:53 +00:00
2024-06-06 09:38:12 +00:00
const responseData = await apiRequest.call(this, 'GET', endpoint);
2023-10-24 16:24:53 +00:00
2024-06-06 09:38:12 +00:00
const webhookUrl = this.getNodeWebhookUrl('default');
2023-10-24 16:24:53 +00:00
for (const webhook of responseData) {
if (webhook.target_url === webhookUrl) {
// Set webhook-id to be sure that it can be deleted
2024-06-06 09:38:12 +00:00
const webhookData = this.getWorkflowStaticData('node');
webhookData.webhookId = webhook.id as number;
return true;
2023-10-24 16:24:53 +00:00
}
}
2024-06-06 09:38:12 +00:00
return false;
2023-10-24 16:24:53 +00:00
},
async create(this: IHookFunctions): Promise<boolean> {
2024-06-06 09:38:12 +00:00
const webhookUrl = this.getNodeWebhookUrl('default');
const projectId = this.getNodeParameter('project') as number;
const endpoint = `/projects/${projectId}/webhooks`;
2023-10-24 16:24:53 +00:00
const body = {
target_url: webhookUrl,
events: this.getNodeParameter('events') as string[],
2024-06-06 09:38:12 +00:00
};
2023-10-24 16:24:53 +00:00
2024-06-06 09:38:12 +00:00
const responseData = await apiRequest.call(this, 'PUT', endpoint, body);
2023-10-24 16:24:53 +00:00
if (responseData.id === undefined) {
// Required data is missing so was not successful
2024-06-06 09:38:12 +00:00
return false;
2023-10-24 16:24:53 +00:00
}
2024-06-06 09:38:12 +00:00
const webhookData = this.getWorkflowStaticData('node');
webhookData.webhookId = responseData.id as string;
2023-10-24 16:24:53 +00:00
2024-06-06 09:38:12 +00:00
return true;
2023-10-24 16:24:53 +00:00
},
async delete(this: IHookFunctions): Promise<boolean> {
2024-06-06 09:38:12 +00:00
const webhookData = this.getWorkflowStaticData('node');
2023-10-24 16:24:53 +00:00
if (webhookData.webhookId !== undefined) {
2024-06-06 09:38:12 +00:00
const projectId = this.getNodeParameter('project') as number;
const endpoint = `/projects/${projectId}/webhooks/${webhookData.webhookId}`;
2023-10-24 16:24:53 +00:00
try {
2024-06-06 09:38:12 +00:00
await apiRequest.call(this, 'DELETE', endpoint);
2023-10-24 16:24:53 +00:00
} catch (error) {
2024-06-06 09:38:12 +00:00
return false;
2023-10-24 16:24:53 +00:00
}
// Remove from the static workflow data so that it is clear
// that no webhooks are registered anymore
2024-06-06 09:38:12 +00:00
delete webhookData.webhookId;
2023-10-24 16:24:53 +00:00
}
2024-06-06 09:38:12 +00:00
return true;
2023-10-24 16:24:53 +00:00
},
},
2024-06-06 09:38:12 +00:00
};
2023-10-24 16:24:53 +00:00
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
2024-06-06 09:38:12 +00:00
const webhookName = this.getWebhookName();
2023-10-24 16:24:53 +00:00
if (webhookName === 'setup') {
// Is a create webhook confirmation request
2024-06-06 09:38:12 +00:00
const res = this.getResponseObject();
res.status(200).end();
2023-10-24 16:24:53 +00:00
return {
noWebhookResponse: true,
2024-06-06 09:38:12 +00:00
};
2023-10-24 16:24:53 +00:00
}
2024-06-06 09:38:12 +00:00
const bodyData = this.getBodyData();
2023-10-24 16:24:53 +00:00
// TODO: Check why that does not work as expected even though it gets done as described
//const credentials = await this.getCredentials('vikunjaApi');
// // Check if the request is valid
// const headerData = this.getHeaderData() as IDataObject;
// const webhookUrl = this.getNodeWebhookUrl('default');
// const checkContent = JSON.stringify(bodyData) + webhookUrl;
// const computedSignature = createHmac('sha1', credentials.oauthSecret as string).update(checkContent).digest('base64');
// if (headerData['x-vikunja-webhook'] !== computedSignature) {
// // Signature is not valid so ignore call
// return {};
// }
return {
workflowData: [this.helpers.returnJsonArray(bodyData)],
2024-06-06 09:38:12 +00:00
};
2023-10-24 16:24:53 +00:00
}
}