|
| 1 | +import type { |
| 2 | + IDataObject, |
| 3 | + IHookFunctions, |
| 4 | + INodeType, |
| 5 | + INodeTypeDescription, |
| 6 | + IWebhookFunctions, |
| 7 | + IWebhookResponseData, |
| 8 | +} from 'n8n-workflow'; |
| 9 | +import { NodeConnectionTypes } from 'n8n-workflow'; |
| 10 | + |
| 11 | +import { taskadeApiRequest } from './GenericFunctions'; |
| 12 | + |
| 13 | +export class TaskadeTrigger implements INodeType { |
| 14 | + description: INodeTypeDescription = { |
| 15 | + displayName: 'Taskade Trigger', |
| 16 | + name: 'taskadeTrigger', |
| 17 | + icon: 'file:taskade.svg', |
| 18 | + group: ['trigger'], |
| 19 | + version: 1, |
| 20 | + subtitle: '={{$parameter["event"]}}', |
| 21 | + description: 'Starts a workflow when something happens in Taskade', |
| 22 | + defaults: { name: 'Taskade Trigger' }, |
| 23 | + inputs: [], |
| 24 | + outputs: [NodeConnectionTypes.Main], |
| 25 | + credentials: [{ name: 'taskadeApi', required: true }], |
| 26 | + webhooks: [ |
| 27 | + { |
| 28 | + name: 'default', |
| 29 | + httpMethod: 'POST', |
| 30 | + responseMode: 'onReceived', |
| 31 | + path: 'webhook', |
| 32 | + }, |
| 33 | + ], |
| 34 | + properties: [ |
| 35 | + { |
| 36 | + displayName: 'Event', |
| 37 | + name: 'event', |
| 38 | + type: 'options', |
| 39 | + options: [ |
| 40 | + { name: 'Comment Created', value: 'comment.created' }, |
| 41 | + { name: 'Member Joined Project', value: 'project.joined' }, |
| 42 | + { name: 'Project Assigned', value: 'project.assigned' }, |
| 43 | + { name: 'Project Created', value: 'project.created' }, |
| 44 | + { name: 'Task Assigned', value: 'task.assigned' }, |
| 45 | + { name: 'Task Due', value: 'task.due' }, |
| 46 | + ], |
| 47 | + default: 'task.due', |
| 48 | + required: true, |
| 49 | + description: 'The Taskade event that starts the workflow', |
| 50 | + }, |
| 51 | + ], |
| 52 | + }; |
| 53 | + |
| 54 | + webhookMethods = { |
| 55 | + default: { |
| 56 | + async checkExists(this: IHookFunctions): Promise<boolean> { |
| 57 | + return this.getWorkflowStaticData('node').hookId != null; |
| 58 | + }, |
| 59 | + async create(this: IHookFunctions): Promise<boolean> { |
| 60 | + const response = await taskadeApiRequest.call( |
| 61 | + this as never, |
| 62 | + 'POST', |
| 63 | + '/subscribeWebhook', |
| 64 | + { |
| 65 | + targetUrl: this.getNodeWebhookUrl('default') as string, |
| 66 | + triggerType: this.getNodeParameter('event') as string, |
| 67 | + }, |
| 68 | + 'v2', |
| 69 | + ); |
| 70 | + const hookId = (response as IDataObject).hookId; |
| 71 | + if (hookId == null) { |
| 72 | + return false; |
| 73 | + } |
| 74 | + this.getWorkflowStaticData('node').hookId = hookId; |
| 75 | + return true; |
| 76 | + }, |
| 77 | + async delete(this: IHookFunctions): Promise<boolean> { |
| 78 | + const staticData = this.getWorkflowStaticData('node'); |
| 79 | + if (staticData.hookId == null) { |
| 80 | + return true; |
| 81 | + } |
| 82 | + try { |
| 83 | + await taskadeApiRequest.call( |
| 84 | + this as never, |
| 85 | + 'POST', |
| 86 | + '/unsubscribeWebhook', |
| 87 | + { hookId: staticData.hookId as string }, |
| 88 | + 'v2', |
| 89 | + ); |
| 90 | + } catch { |
| 91 | + // Hook may already be gone server-side; always clear local state so |
| 92 | + // a future activation re-subscribes instead of silently never firing. |
| 93 | + } |
| 94 | + delete staticData.hookId; |
| 95 | + return true; |
| 96 | + }, |
| 97 | + }, |
| 98 | + }; |
| 99 | + |
| 100 | + async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> { |
| 101 | + const body = this.getBodyData(); |
| 102 | + return { |
| 103 | + workflowData: [this.helpers.returnJsonArray(body as IDataObject)], |
| 104 | + }; |
| 105 | + } |
| 106 | +} |
0 commit comments